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.portal.service.plugin;
35
36 import fr.paris.lutece.portal.service.database.AppConnectionService;
37 import fr.paris.lutece.portal.service.datastore.DatastoreService;
38 import fr.paris.lutece.portal.service.init.LuteceInitException;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40 import fr.paris.lutece.portal.service.util.AppPathService;
41 import fr.paris.lutece.util.filesystem.FileListFilter;
42
43 import java.io.File;
44 import java.io.FileInputStream;
45 import java.io.FilenameFilter;
46 import java.io.IOException;
47
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Properties;
54 import java.util.TreeSet;
55
56
57
58
59
60 public final class PluginService
61 {
62
63 private static final String PATH_CONF = "path.conf";
64 private static final String CORE_XML = "core.xml";
65 private static final String CORE = "core";
66 private static Plugin _pluginCore;
67 private static final String PATH_PLUGIN = "path.plugins";
68 private static final String FILE_PLUGINS_STATUS = "plugins.dat";
69 private static final String EXTENSION_FILE = "xml";
70 private static final String PROPERTY_IS_INSTALLED = ".installed";
71 private static final String PROPERTY_DB_POOL_NAME = ".pool";
72 private static final String KEY_PLUGINS_STATUS = "core.plugins.status.";
73
74
75 private static Map<String, Plugin> _mapPlugins = new HashMap<String, Plugin>( );
76 private static List<PluginEventListener> _listPluginEventListeners = new ArrayList<PluginEventListener>( );
77
78
79
80
81 private PluginService( )
82 {
83 }
84
85
86
87
88
89 public static void init( ) throws LuteceInitException
90 {
91 loadPluginsStatus( );
92 _mapPlugins.clear( );
93 loadCoreComponents( );
94 loadPlugins( );
95 }
96
97
98
99
100
101
102 public static Collection<Plugin> getPluginList( )
103 {
104 TreeSet<Plugin> setSorted = new TreeSet<Plugin>( _mapPlugins.values( ) );
105
106 return setSorted;
107 }
108
109
110
111
112
113
114
115 public static Plugin getPlugin( String strPluginName )
116 {
117 return _mapPlugins.get( strPluginName );
118 }
119
120
121
122
123
124 private static void loadCoreComponents( ) throws LuteceInitException
125 {
126 File file = new File( AppPathService.getPath( PATH_CONF, CORE_XML ) );
127
128 if ( file.exists( ) )
129 {
130 loadPluginFromFile( file, false );
131 }
132 }
133
134
135
136
137
138 private static void loadPlugins( ) throws LuteceInitException
139 {
140 File dirPlugin = new File( AppPathService.getPath( PATH_PLUGIN ) );
141
142 if ( dirPlugin.exists( ) )
143 {
144 FilenameFilter select = new FileListFilter( "", EXTENSION_FILE );
145 File[] listFile = dirPlugin.listFiles( select );
146
147 for ( File file : listFile )
148 {
149 loadPluginFromFile( file, true );
150 }
151 }
152 }
153
154
155
156
157
158
159
160
161 private static void loadPluginFromFile( File file, boolean bRegisterAsPlugin )
162 throws LuteceInitException
163 {
164 PluginFile pluginFile = new PluginFile( );
165 pluginFile.load( file.getAbsolutePath( ) );
166
167 String strPluginClass = pluginFile.getPluginClass( );
168
169 if ( strPluginClass != null )
170 {
171 try
172 {
173 Plugin plugin = (Plugin) Class.forName( strPluginClass ).newInstance( );
174 plugin.load( pluginFile );
175
176 if ( bRegisterAsPlugin )
177 {
178 plugin.setStatus( getPluginStatus( plugin ) );
179 registerPlugin( plugin );
180 }
181 else
182 {
183 plugin.setStatus( true );
184 registerCore( plugin );
185 }
186
187
188
189 if ( plugin.isDbPoolRequired( ) )
190 {
191 String strPoolName = getPluginPoolName( plugin );
192 plugin.setPoolName( strPoolName );
193 plugin.initConnectionService( strPoolName );
194 }
195
196 plugin.init( );
197
198
199 PluginEvent event = new PluginEvent( plugin, PluginEvent.PLUGIN_INSTALLED );
200 notifyListeners( event );
201 }
202 catch ( Exception e )
203 {
204 throw new LuteceInitException( "Error instantiating plugin defined in file : " +
205 file.getAbsolutePath( ), e );
206 }
207 }
208 else
209 {
210 AppLogService.error( "No plugin class defined in file : " + file.getAbsolutePath( ) );
211 }
212 }
213
214
215
216
217
218
219 private static void registerPlugin( Plugin plugin )
220 {
221 _mapPlugins.put( plugin.getName( ), plugin );
222
223 String strStatusWarning = ( plugin.isInstalled( ) ) ? "" : " *** Warning : current status is 'disabled' ***";
224 AppLogService.info( "New Plugin registered : " + plugin.getName( ) + strStatusWarning );
225 }
226
227
228
229
230
231 private static synchronized void registerCore( Plugin plugin )
232 {
233 _pluginCore = plugin;
234 }
235
236
237
238
239
240
241 public static Plugin getCore( )
242 {
243 return _pluginCore;
244 }
245
246
247
248
249
250
251 public static void updatePluginData( Plugin plugin )
252 {
253 String strKey = getInstalledKey( plugin.getName( ) );
254 String strValue = plugin.isInstalled( ) ? DatastoreService.VALUE_TRUE : DatastoreService.VALUE_FALSE;
255 DatastoreService.setInstanceDataValue( strKey, strValue );
256
257 if ( plugin.isDbPoolRequired( ) )
258 {
259 DatastoreService.setInstanceDataValue( getPoolNameKey( plugin.getName( ) ), plugin.getDbPoolName( ) );
260 }
261 }
262
263
264
265
266
267
268 private static String getInstalledKey( String strPluginName )
269 {
270 return KEY_PLUGINS_STATUS + strPluginName + PROPERTY_IS_INSTALLED;
271 }
272
273
274
275
276
277
278 private static String getPoolNameKey( String strPluginName )
279 {
280 return KEY_PLUGINS_STATUS + strPluginName + PROPERTY_DB_POOL_NAME;
281 }
282
283
284
285
286 private static void loadPluginsStatus( )
287 {
288
289 String strPluginStatusFile = AppPathService.getPath( PATH_PLUGIN, FILE_PLUGINS_STATUS );
290 File file = new File( strPluginStatusFile );
291 Properties props = new Properties( );
292 FileInputStream fis = null;
293
294 try
295 {
296 fis = new FileInputStream( file );
297 props.load( fis );
298 }
299 catch ( Exception e )
300 {
301 AppLogService.error( "Error loading plugin defined in file : " + file.getAbsolutePath( ), e );
302 }
303 finally
304 {
305 if ( fis != null )
306 {
307 try
308 {
309 fis.close( );
310 }
311 catch ( IOException e )
312 {
313 AppLogService.error( e.getMessage( ), e );
314 }
315 }
316 }
317
318
319 for ( String strKey : props.stringPropertyNames( ) )
320 {
321
322 int nPos = strKey.indexOf( PROPERTY_IS_INSTALLED );
323
324 if ( nPos > 0 )
325 {
326 String strPluginName = strKey.substring( 0, nPos );
327 String strDSKey = getInstalledKey( strPluginName );
328
329 if ( !DatastoreService.existsInstanceKey( strDSKey ) )
330 {
331 String strValue = props.getProperty( strKey ).equals( "1" ) ? DatastoreService.VALUE_TRUE
332 : DatastoreService.VALUE_FALSE;
333 DatastoreService.setInstanceDataValue( strDSKey, strValue );
334 }
335 }
336
337
338 nPos = strKey.indexOf( PROPERTY_DB_POOL_NAME );
339
340 if ( nPos > 0 )
341 {
342 String strPluginName = strKey.substring( 0, nPos );
343 String strDSKey = getPoolNameKey( strPluginName );
344
345 if ( !DatastoreService.existsInstanceKey( strDSKey ) )
346 {
347 String strValue = props.getProperty( strKey );
348 DatastoreService.setInstanceDataValue( strDSKey, strValue );
349 }
350 }
351 }
352 }
353
354
355
356
357
358
359
360 private static boolean getPluginStatus( Plugin plugin )
361 {
362 String strValue = DatastoreService.getInstanceDataValue( getInstalledKey( plugin.getName( ) ),
363 DatastoreService.VALUE_FALSE );
364
365 return strValue.equals( DatastoreService.VALUE_TRUE );
366 }
367
368
369
370
371
372
373
374 private static String getPluginPoolName( Plugin plugin )
375 {
376 String strPoolname = DatastoreService.getInstanceDataValue( getPoolNameKey( plugin.getName( ) ),
377 AppConnectionService.NO_POOL_DEFINED );
378
379 if ( strPoolname.equals( AppConnectionService.NO_POOL_DEFINED ) && plugin.isDbPoolRequired( ) &&
380 !plugin.getName( ).equals( CORE ) )
381 {
382 AppLogService.info( " *** WARNING *** - The plugin '" + plugin +
383 "' has no pool defined in db.properties or datastore. Using the default pool '" +
384 AppConnectionService.DEFAULT_POOL_NAME + "' instead." );
385 strPoolname = AppConnectionService.DEFAULT_POOL_NAME;
386 }
387
388 return strPoolname;
389 }
390
391
392
393
394
395
396 public static boolean isPluginEnable( String strPluginName )
397 {
398 if ( strPluginName.equals( CORE ) )
399 {
400 return true;
401 }
402
403 Plugin plugin = getPlugin( strPluginName );
404
405 return ( ( plugin != null ) && ( plugin.isInstalled( ) ) );
406 }
407
408
409
410
411
412 public static void registerPluginEventListener( PluginEventListener listener )
413 {
414 _listPluginEventListeners.add( listener );
415 }
416
417
418
419
420
421 public static void notifyListeners( PluginEvent event )
422 {
423 for ( PluginEventListener listener : _listPluginEventListeners )
424 {
425 listener.processPluginEvent( event );
426 }
427 }
428 }