1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.annotation;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.service.util.AppPathService;
38
39 import org.scannotation.AnnotationDB;
40
41 import java.io.File;
42 import java.io.FilenameFilter;
43 import java.io.IOException;
44
45 import java.lang.annotation.Annotation;
46
47 import java.net.MalformedURLException;
48 import java.net.URL;
49
50 import java.util.Date;
51 import java.util.HashSet;
52 import java.util.Map;
53 import java.util.Set;
54
55
56
57
58
59
60 public class ScannotationDB implements IAnnotationDB
61 {
62 private static final String CONSTANT_WEB_INF_LIB = "/WEB-INF/lib/";
63 private static final String CONSTANT_WEB_INF_CLASS = "/WEB-INF/classes/";
64
65
66
67
68 private static final String CONSTANT_DEFAULT_FILENAME_FILTER = "(plugin-.*\\.jar)|(module-.*\\.jar)|(lutece-core-.*\\.jar)|(library-.*\\.jar)";
69 private final AnnotationDB _db;
70 private String _strFileFilter;
71
72
73
74
75 public ScannotationDB( )
76 {
77 _db = new AnnotationDB( );
78 }
79
80
81
82
83
84 public String getFileFilter( )
85 {
86 return _strFileFilter;
87 }
88
89
90
91
92
93 public void setFileFilter( String strFileFilter )
94 {
95 _strFileFilter = strFileFilter;
96 }
97
98
99
100
101
102 @Override
103 public void init( )
104 {
105 AppLogService.info( "ScannotationDB Scanning classpath..." );
106
107 if ( getFileFilter( ) == null )
108 {
109 setFileFilter( CONSTANT_DEFAULT_FILENAME_FILTER );
110 AppLogService.info( "Using default filename filter" );
111 }
112 else
113 {
114 AppLogService.info( "Using " + getFileFilter( ) + " as filename filter" );
115 }
116
117 Date start = new Date( );
118 File libDirectory = new File( AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_LIB );
119
120 String[] allJars = libDirectory.list( new FilenameFilter( )
121 {
122
123
124
125 @Override
126 public boolean accept( File dir, String name )
127 {
128 return name.matches( _strFileFilter );
129 }
130 } );
131
132 for ( String strJar : allJars )
133 {
134 try
135 {
136 if ( AppLogService.isDebugEnabled( ) )
137 {
138 AppLogService.debug( "Scanning " + strJar );
139 }
140
141 _db.scanArchives( new URL( "file:///" + AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_LIB +
142 strJar ) );
143 }
144 catch ( MalformedURLException e )
145 {
146 AppLogService.error( e.getMessage( ), e );
147 }
148 catch ( IOException e )
149 {
150 AppLogService.error( e.getMessage( ), e );
151 }
152 }
153
154 AppLogService.info( "ScannotationDB WEB-INF/lib scanned" );
155
156 try
157 {
158 _db.scanArchives( new URL( "file:///" + AppPathService.getWebAppPath( ) + CONSTANT_WEB_INF_CLASS ) );
159 }
160 catch ( MalformedURLException e )
161 {
162 AppLogService.error( e.getMessage( ), e );
163 }
164 catch ( IOException e )
165 {
166 AppLogService.error( e.getMessage( ), e );
167 }
168
169 AppLogService.info( "ScannotationDB Classpath scanned in " + ( new Date( ).getTime( ) - start.getTime( ) ) +
170 "ms" );
171 }
172
173
174
175
176 @Override
177 public Set<String> getClassesName( Class<?extends Annotation> annotationType )
178 {
179 return getClassesName( annotationType.getName( ) );
180 }
181
182
183
184
185 @Override
186 public Set<String> getClassesName( String strAnnotationType )
187 {
188 Map<String, Set<String>> index = _db.getAnnotationIndex( );
189
190 Set<String> setClasses = index.get( strAnnotationType );
191
192 if ( setClasses == null )
193 {
194 setClasses = new HashSet<String>( );
195 }
196
197 return setClasses;
198 }
199 }