View Javadoc

1   package org.apache.onami.autobind.scanner;
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.System.getProperty;
21  import static java.util.logging.Level.FINE;
22  import static java.util.logging.Level.INFO;
23  import static java.util.logging.Level.SEVERE;
24  import static java.util.logging.Logger.getLogger;
25  
26  import java.io.IOException;
27  import java.util.Set;
28  import java.util.logging.Logger;
29  
30  import javax.inject.Inject;
31  
32  import org.apache.onami.autobind.annotations.Bind;
33  import org.apache.onami.autobind.annotations.GuiceModule;
34  import org.apache.onami.autobind.install.InstallationContext;
35  import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
36  import org.apache.onami.autobind.scanner.features.ScannerFeature;
37  
38  import com.google.inject.Binder;
39  import com.google.inject.Module;
40  
41  /**
42   * The ScannerModule will be injected with a ClasspathScanner and the needed
43   * Annotation Listeners will be added. The attached Listeners will install all
44   * Modules annotated with {@link GuiceModule} and bind all Beans annotated with
45   * {@link Bind}.
46   */
47  public class ScannerModule
48      implements Module
49  {
50  
51      private static String LINE_SEPARATOR = getProperty( "line.separator" );
52  
53      private final Logger _logger = getLogger( getClass().getName() );
54  
55      @Inject
56      private ClasspathScanner _scanner;
57  
58      @Inject
59      private Set<ScannerFeature> _listeners;
60  
61      @Inject
62      private InstallationContext _context;
63  
64      @Override
65      public void configure( Binder binder )
66      {
67          if ( _logger.isLoggable( INFO ) )
68          {
69              StringBuilder builder = new StringBuilder( _scanner.getClass().getName() )
70                                      .append( " is using following Scanner Features: " )
71                                      .append( LINE_SEPARATOR );
72              for ( ScannerFeature listener : _listeners )
73              {
74                  builder.append( listener.getClass().getName() ).append( LINE_SEPARATOR );
75              }
76              _logger.log( INFO, builder.toString() );
77          }
78          for ( ScannerFeature listener : _listeners )
79          {
80              if ( listener instanceof BindingScannerFeature )
81              {
82                  ( (BindingScannerFeature) listener ).setBinder( binder );
83                  if ( _logger.isLoggable( FINE ) )
84                  {
85                      _logger.fine( "Binding AnnotationListeners " + listener.getClass().getName() );
86                  }
87              }
88          }
89          try
90          {
91              _scanner.scan();
92          }
93          catch ( IOException e )
94          {
95              _logger.log( SEVERE, "Failure while Scanning the Classpath for Classes with Annotations.", e );
96          }
97          try
98          {
99              _context.process();
100         }
101         catch ( Exception e )
102         {
103             _logger.log( SEVERE, "Failure while executing the collected Tasks.", e );
104         }
105     }
106 
107 }