• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Dagger Authors.
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 
17 package dagger.producers.monitoring;
18 
19 import dagger.producers.Produces;
20 import dagger.producers.ProductionComponent;
21 
22 /**
23  * A hook for monitoring execution of {@linkplain ProductionComponent production components}. To
24  * install a {@code ProductionComponentMonitor}, contribute to a set binding of
25  * {@code ProductionComponentMonitor.Factory}. The factory will be asked to create one monitor for
26  * the component, and the resulting single instance will be used to create individual monitors for
27  * producers.
28  *
29  * <p>For example: <pre><code>
30  *   {@literal @Module}
31  *   final class MyMonitorModule {
32  *     {@literal @Provides @IntoSet} ProductionComponentMonitor.Factory provideMonitorFactory(
33  *         MyProductionComponentMonitor.Factory monitorFactory) {
34  *       return monitorFactory;
35  *     }
36  *   }
37  *
38  *   {@literal @ProductionComponent(modules = {MyMonitorModule.class, MyProducerModule.class})}
39  *   interface MyComponent {
40  *     {@literal ListenableFuture<SomeType>} someType();
41  *   }
42  * </code></pre>
43  *
44  * <p>If any of these methods throw, then the exception will be logged, and the framework will act
45  * as though a no-op monitor was returned.
46  *
47  * @since 2.1
48  */
49 public abstract class ProductionComponentMonitor {
50   /** Returns a monitor for an individual {@linkplain Produces producer method}. */
producerMonitorFor(ProducerToken token)51   public abstract ProducerMonitor producerMonitorFor(ProducerToken token);
52 
53   private static final ProductionComponentMonitor NO_OP =
54       new ProductionComponentMonitor() {
55         @Override
56         public ProducerMonitor producerMonitorFor(ProducerToken token) {
57           return ProducerMonitor.noOp();
58         }
59       };
60 
61   /** Returns a monitor that does no monitoring. */
noOp()62   public static ProductionComponentMonitor noOp() {
63     return NO_OP;
64   }
65 
66   public abstract static class Factory {
67     /** Creates a component-specific monitor when the component is created. */
create(Object component)68     public abstract ProductionComponentMonitor create(Object component);
69 
70     private static final Factory NO_OP_FACTORY =
71         new Factory() {
72           @Override
73           public ProductionComponentMonitor create(Object component) {
74             return ProductionComponentMonitor.noOp();
75           }
76         };
77 
78     /** Returns a factory that returns no-op monitors. */
noOp()79     public static Factory noOp() {
80       return NO_OP_FACTORY;
81     }
82   }
83 }
84