View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.annotation;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.lang.annotation.Annotation;
39  import java.net.URL;
40  import java.util.Date;
41  import java.util.HashSet;
42  import java.util.Map;
43  import java.util.Set;
44  
45  import org.scannotation.AnnotationDB;
46  
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPathService;
49  
50  /**
51   * Uses {@link AnnotationDB} from scannotation.
52   * 
53   * @see #init()
54   */
55  public class ScannotationDB implements IAnnotationDB
56  {
57      private static final String CONSTANT_WEB_INF_LIB = "/WEB-INF/lib/";
58      private static final String CONSTANT_WEB_INF_CLASS = "/WEB-INF/classes/";
59  
60      /**
61       * Default filename filter matches <strong>ONLY</strong> lutece jars.
62       */
63      private static final String CONSTANT_DEFAULT_FILENAME_FILTER = "(plugin-.*\\.jar)|(module-.*\\.jar)|(lutece-core-.*\\.jar)|(library-.*\\.jar)";
64      private final AnnotationDB _db;
65      private String _strFileFilter;
66  
67      /**
68       * Constructor.
69       */
70      public ScannotationDB( )
71      {
72          _db = new AnnotationDB( );
73      }
74  
75      /**
76       * Gets the filename filter
77       * 
78       * @return the filename filter
79       */
80      public String getFileFilter( )
81      {
82          return _strFileFilter;
83      }
84  
85      /**
86       * Sets the filename filter
87       * 
88       * @param strFileFilter
89       *            the filename filter
90       */
91      public void setFileFilter( String strFileFilter )
92      {
93          _strFileFilter = strFileFilter;
94      }
95  
96      /**
97       * Scans the WEB-INF/classes and WEB-INF/lib using {@link #getFileFilter()} to filter jars.
98       */
99      @Override
100     public void init( )
101     {
102         AppLogService.info( "ScannotationDB Scanning classpath..." );
103 
104         if ( getFileFilter( ) == null )
105         {
106             setFileFilter( CONSTANT_DEFAULT_FILENAME_FILTER );
107             AppLogService.info( "Using default filename filter" );
108         }
109         else
110         {
111             AppLogService.info( "Using {} as filename filter", getFileFilter( ) );
112         }
113 
114         Date start = new Date( );
115         File libDirectory = new File( AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_LIB );
116 
117         String [ ] allJars = libDirectory.list( ( File dir, String name ) -> name.matches( _strFileFilter ) );
118 
119         for ( String strJar : allJars )
120         {
121             try
122             {
123                 if ( AppLogService.isDebugEnabled( ) )
124                 {
125                     AppLogService.debug( "Scanning " + strJar );
126                 }
127 
128                 _db.scanArchives( new URL( "file:///" + AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_LIB + strJar ) );
129             }
130             catch( IOException e )
131             {
132                 AppLogService.error( e.getMessage( ), e );
133             }
134         }
135 
136         AppLogService.info( "ScannotationDB WEB-INF/lib scanned" );
137 
138         try
139         {
140             _db.scanArchives( new URL( "file:///" + AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_CLASS ) );
141         }
142         catch( IOException e )
143         {
144             AppLogService.error( e.getMessage( ), e );
145         }
146 
147         AppLogService.info( "ScannotationDB Classpath scanned in {} ms", ( ) -> ( new Date( ).getTime( ) - start.getTime( ) ) );
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public Set<String> getClassesName( Class<? extends Annotation> annotationType )
155     {
156         return getClassesName( annotationType.getName( ) );
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public Set<String> getClassesName( String strAnnotationType )
164     {
165         Map<String, Set<String>> index = _db.getAnnotationIndex( );
166 
167         Set<String> setClasses = index.get( strAnnotationType );
168 
169         if ( setClasses == null )
170         {
171             setClasses = new HashSet<>( );
172         }
173 
174         return setClasses;
175     }
176 }