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 java.util.logging.Level.FINE;
22  import static java.util.logging.Level.INFO;
23  import static java.util.logging.Logger.getLogger;
24  import static org.apache.onami.autobind.install.BindingStage.IGNORE;
25  
26  import java.lang.annotation.Annotation;
27  import java.util.Map;
28  import java.util.logging.Logger;
29  
30  import javax.inject.Singleton;
31  
32  import org.apache.onami.autobind.annotations.GuiceModule;
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.ModuleBindingJob;
36  import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
37  
38  import com.google.inject.Module;
39  
40  @Singleton
41  public class ModuleBindingFeature
42      extends BindingScannerFeature
43  {
44  
45      private final Logger _logger = getLogger( getClass().getName() );
46  
47      @Override
48      public BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations )
49      {
50          if ( annotations.containsKey( GuiceModule.class.getName() ) )
51          {
52              GuiceModule module = (GuiceModule) annotations.get( GuiceModule.class.getName() );
53              return module.stage();
54          }
55          return IGNORE;
56      }
57  
58      @Override
59      public void process( final Class<Object> annotatedClass, Map<String, Annotation> annotations )
60      {
61          BindingJob job = new ModuleBindingJob( annotatedClass.getName() );
62          if ( !tracer.contains( job ) )
63          {
64              if ( _logger.isLoggable( INFO ) )
65              {
66                  _logger.info( "Installing Module: " + annotatedClass.getName() );
67              }
68              synchronized ( _binder )
69              {
70                  _binder.install( (Module) injector.getInstance( annotatedClass ) );
71              }
72          }
73          else
74          {
75              String message = format( "Ignoring BindingJob \"%s\", because it was already bound.", job );
76  
77              if ( _logger.isLoggable( FINE ) )
78              {
79                  _logger.log( FINE, message, new Exception( message ) );
80              }
81              else if ( _logger.isLoggable( INFO ) )
82              {
83                  _logger.log( INFO, message );
84              }
85          }
86      }
87  
88  }