Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2015 Google, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package dagger.producers.monitoring.internal;
     17 
     18 import dagger.producers.monitoring.ProductionComponentMonitor;
     19 import java.util.Set;
     20 import java.util.logging.Level;
     21 import java.util.logging.Logger;
     22 import javax.inject.Provider;
     23 
     24 /**
     25  * A class that provides a {@link ProductionComponentMonitor} for use in production components.
     26  *
     27  * <p>This caches the underlying the monitor, since we want a single instance for each component.
     28  */
     29 public final class MonitorCache {
     30   private static final Logger logger = Logger.getLogger(MonitorCache.class.getName());
     31 
     32   private volatile ProductionComponentMonitor monitor;
     33 
     34   /**
     35    * Returns the underlying monitor. This will only actually compute the monitor the first time it
     36    * is called; subsequent calls will simply return the cached value, so the arguments to this
     37    * method are ignored. It is expected (though not checked) that this method is called with
     38    * equivalent arguments each time (like a {@link dagger.Provides @Provides} method would).
     39    */
     40   public ProductionComponentMonitor monitor(
     41       Provider<?> componentProvider,
     42       Provider<Set<ProductionComponentMonitor.Factory>> monitorFactorySetProvider) {
     43     ProductionComponentMonitor result = monitor;
     44     if (result == null) {
     45       synchronized (this) {
     46         result = monitor;
     47         if (result == null) {
     48           try {
     49             ProductionComponentMonitor.Factory factory =
     50                 Monitors.delegatingProductionComponentMonitorFactory(
     51                     monitorFactorySetProvider.get());
     52             result = monitor = factory.create(componentProvider.get());
     53           } catch (RuntimeException e) {
     54             logger.log(Level.SEVERE, "RuntimeException while constructing monitor factories.", e);
     55             result = monitor = Monitors.noOpProductionComponentMonitor();
     56           }
     57         }
     58       }
     59     }
     60     return result;
     61   }
     62 }
     63