View Javadoc

1   package org.apache.onami.autobind.annotations.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 java.lang.String.format;
21  import static com.google.inject.multibindings.Multibinder.newSetBinder;
22  import static java.util.logging.Level.FINE;
23  import static java.util.logging.Level.INFO;
24  import static org.apache.onami.autobind.install.BindingStage.BINDING;
25  import static org.apache.onami.autobind.install.BindingStage.IGNORE;
26  
27  import java.lang.annotation.Annotation;
28  import java.util.Map;
29  
30  import javax.inject.Singleton;
31  
32  import org.apache.onami.autobind.annotations.Bind;
33  import org.apache.onami.autobind.install.BindingStage;
34  import org.apache.onami.autobind.install.bindjob.BindingJob;
35  import org.apache.onami.autobind.install.bindjob.MultiBindingJob;
36  
37  import com.google.inject.binder.ScopedBindingBuilder;
38  import com.google.inject.multibindings.Multibinder;
39  
40  @Singleton
41  public class MultiBindingFeature
42      extends AutoBindingFeature
43  {
44  
45      @Override
46      public BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations )
47      {
48          if ( annotations.containsKey( Bind.class.getName() ) )
49          {
50              Bind annotation = (Bind) annotations.get( Bind.class.getName() );
51              if ( annotation.multiple() )
52              {
53                  return BINDING;
54              }
55          }
56          return IGNORE;
57      }
58  
59      @Override
60      protected <T, V extends T> void bind( Class<V> implementationClass, Class<T> interf, Annotation annotation,
61                                            Class<? extends Annotation> scope )
62      {
63          BindingJob job = new MultiBindingJob( scope, annotation, implementationClass.getName(), interf.getName() );
64  
65          if ( !tracer.contains( job ) )
66          {
67              Multibinder<T> builder;
68              synchronized ( _binder )
69              {
70                  if ( annotation != null )
71                  {
72                      builder = newSetBinder( _binder, interf, annotation );
73                  }
74                  else
75                  {
76                      builder = newSetBinder( _binder, interf );
77                  }
78  
79                  ScopedBindingBuilder scopedBindingBuilder = builder.addBinding().to( implementationClass );
80                  if ( scope != null )
81                  {
82                      scopedBindingBuilder.in( scope );
83                  }
84              }
85              tracer.add( job );
86          }
87          else
88          {
89              String message = format( "Ignoring Multi-BindingJob \"%s\", because it was already bound.", job );
90  
91              if ( _logger.isLoggable( FINE ) )
92              {
93                  _logger.log( FINE, message, new Exception( message ) );
94              }
95              else if ( _logger.isLoggable( INFO ) )
96              {
97                  _logger.log( INFO, message );
98              }
99          }
100     }
101 
102 }