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 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
52
53
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
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
69
70 public ScannotationDB( )
71 {
72 _db = new AnnotationDB( );
73 }
74
75
76
77
78
79
80 public String getFileFilter( )
81 {
82 return _strFileFilter;
83 }
84
85
86
87
88
89
90
91 public void setFileFilter( String strFileFilter )
92 {
93 _strFileFilter = strFileFilter;
94 }
95
96
97
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
152
153 @Override
154 public Set<String> getClassesName( Class<? extends Annotation> annotationType )
155 {
156 return getClassesName( annotationType.getName( ) );
157 }
158
159
160
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 }