View Javadoc

1   package org.apache.onami.autobind.scanner.features;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *  http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static com.google.inject.multibindings.Multibinder.newSetBinder;
21  
22  import java.lang.annotation.Annotation;
23  
24  import org.apache.onami.autobind.scanner.ScannerModule;
25  
26  import com.google.inject.binder.ScopedBindingBuilder;
27  import com.google.inject.multibindings.Multibinder;
28  
29  /**
30   * Default Implementation for Annotation Listeners, which should stay informed
31   * abbout found annotated classes. Due the fact, that we need the Binder of the
32   * Child Injector, it will be set at runtime by the {@link ScannerModule}.
33   */
34  public abstract class MultiBindingScannerFeature
35      extends BindingScannerFeature
36  {
37  
38      protected <T, V extends T> void bindInstance( V impl, Class<T> interf, Annotation annotation,
39                                                    Class<? extends Annotation> scope )
40      {
41          Multibinder<T> builder;
42          synchronized ( _binder )
43          {
44              if ( annotation != null )
45              {
46                  builder = newSetBinder( _binder, interf, annotation );
47              }
48              else
49              {
50                  builder = newSetBinder( _binder, interf );
51              }
52  
53              builder.addBinding().toInstance( impl );
54          }
55      }
56  
57      protected void bindConstant( String value, Annotation annotation )
58      {
59          Multibinder<String> builder;
60          synchronized ( _binder )
61          {
62              builder = newSetBinder( _binder, String.class, annotation );
63              builder.addBinding().toInstance( value );
64          }
65      }
66  
67      protected <T, V extends T> void bind( Class<V> impl, Class<T> interf, Annotation annotation,
68                                            Class<? extends Annotation> scope )
69      {
70          Multibinder<T> builder;
71          synchronized ( _binder )
72          {
73              if ( annotation != null )
74              {
75                  builder = newSetBinder( _binder, interf, annotation );
76              }
77              else
78              {
79                  builder = newSetBinder( _binder, interf );
80              }
81  
82              ScopedBindingBuilder scopedBindingBuilder = builder.addBinding().to( impl );
83              if ( scope != null )
84              {
85                  scopedBindingBuilder.in( scope );
86              }
87          }
88      }
89  
90  }