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.plugins.updater.service;
35
36 import fr.paris.lutece.plugins.updater.business.resource.DirectoryTreeResource;
37 import fr.paris.lutece.plugins.updater.business.resource.FilePatternResource;
38 import fr.paris.lutece.plugins.updater.business.resource.FileResource;
39 import fr.paris.lutece.plugins.updater.business.resource.FileSystemResource;
40 import fr.paris.lutece.plugins.updater.util.sql.SqlUtils;
41 import fr.paris.lutece.util.filesystem.FileNameComparator;
42
43 import org.apache.commons.io.FileUtils;
44
45 import org.apache.log4j.Logger;
46
47 import java.io.File;
48 import java.io.IOException;
49
50 import java.sql.SQLException;
51
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.List;
55 import java.util.TreeSet;
56
57
58
59
60
61 public final class PluginManagerService
62 {
63 private static final int FILE = 1;
64 private static final int DIRECTORY_TREE = 2;
65 private static final int FILE_PREFIX = 3;
66 private static final String PATH_BACKUP = "/plugins/updater/backup/";
67 private static final String PATH_DEPLOY = "/plugins/updater/deploy/";
68 private static final String FOLDER_WEBAPP = "/webapp";
69 private static final String FOLDER_SQL = "/sql";
70 private static PluginManagerService _singleton = new PluginManagerService( );
71 private static String _strWebAppPath;
72 private static Logger _logger = Logger.getLogger( "lutece.plugins.updater" );
73
74
75
76
77 private PluginManagerService( )
78 {
79 }
80
81
82
83
84
85 public static PluginManagerService getInstance( )
86 {
87 return _singleton;
88 }
89
90
91
92
93
94
95 public String getWebAppPath( )
96 {
97
98
99 return _strWebAppPath;
100 }
101
102
103
104
105
106
107 public void setWebAppPath( String strWebAppPath )
108 {
109 _strWebAppPath = strWebAppPath;
110 }
111
112
113
114
115
116 public void cleanPluginsMarkedForUpdate( int nDeleteMode )
117 {
118 _logger.info( "Searching for plugin to remove before update ..." );
119
120 String strDeployDirectory = getWebAppPath( ) + PATH_DEPLOY;
121 File fDeployDirectory = new File( strDeployDirectory );
122 boolean bFound = false;
123
124 if ( fDeployDirectory.exists( ) )
125 {
126 File[] files = fDeployDirectory.listFiles( );
127
128 if ( files.length > 0 )
129 {
130 for ( int i = 0; i < files.length; i++ )
131 {
132 File fPluginDirectory = files[i];
133 String strPluginName = fPluginDirectory.getName( );
134 _logger.info( "An update has been found for plugin : " + strPluginName +
135 ". The installed plugin will be deleted" );
136 bFound = true;
137 cleanPlugin( strPluginName, nDeleteMode, true );
138 }
139 }
140 }
141
142 if ( !bFound )
143 {
144 _logger.info( "No plugin to removed was found." );
145 }
146 }
147
148
149
150
151 public void installPlugins( )
152 {
153 _logger.info( "Searching for plugin to install ..." );
154
155 String strDeployDirectory = getWebAppPath( ) + PATH_DEPLOY;
156 File fDeployDirectory = new File( strDeployDirectory );
157 boolean bFound = false;
158
159 if ( fDeployDirectory.exists( ) )
160 {
161 File[] files = fDeployDirectory.listFiles( );
162
163 if ( files.length > 0 )
164 {
165 for ( int i = 0; i < files.length; i++ )
166 {
167 File fPluginDirectory = files[i];
168 String strPluginName = fPluginDirectory.getName( );
169
170 try
171 {
172 _logger.info( "Installation data has been found for plugin : " + strPluginName );
173 installPlugin( strPluginName );
174 bFound = true;
175 _logger.info( "Plugin '" + strPluginName + "' installed" );
176 }
177 catch ( UpdaterInstallException ex )
178 {
179 _logger.info( "Plugin '" + strPluginName + "' installation failed : " + ex.getMessage( ) );
180 _logger.info( "Plugin '" + strPluginName + "' installation rolled back" );
181 cleanPlugin( strPluginName, 0, false );
182 }
183 }
184 }
185 }
186
187 if ( !bFound )
188 {
189 _logger.info( "No plugin to install or update was found." );
190 }
191 }
192
193
194
195
196
197
198
199
200 public void cleanPlugin( String strPluginName, int nDeleteMode, boolean bBackup )
201 {
202 if ( bBackup )
203 {
204
205 backupPlugin( strPluginName );
206 }
207
208 try
209 {
210
211 removePlugin( strPluginName, nDeleteMode );
212 }
213 catch ( Exception e )
214 {
215 restorePlugin( strPluginName );
216 }
217 }
218
219
220
221
222
223
224 public void removePlugin( String strPluginName, int nDeleteMode )
225 {
226 _logger.info( "remove plugin : " + strPluginName );
227
228 List<FileSystemResource> listResources = getPluginResources( strPluginName );
229
230 try
231 {
232 for ( FileSystemResource resource : listResources )
233 {
234 resource.delete( nDeleteMode );
235 _logger.debug( "resource removed : " + resource.getFullPath( ) );
236 }
237 }
238 catch ( IOException ex )
239 {
240 _logger.error( "error removing plugin : ", ex );
241 }
242 }
243
244
245
246
247
248 public void backupPlugin( String strPluginName )
249 {
250 _logger.info( "backup plugin : " + strPluginName );
251
252 List<FileSystemResource> listResources = getPluginResources( strPluginName );
253 String strBackupPath = PATH_BACKUP + strPluginName + FOLDER_WEBAPP;
254
255 try
256 {
257 for ( FileSystemResource resource : listResources )
258 {
259 _logger.debug( "resource copied : " + resource.getFullPath( ) );
260 resource.copy( strBackupPath );
261 }
262 }
263 catch ( IOException ex )
264 {
265 _logger.error( "error backup plugin : ", ex );
266 }
267 }
268
269
270
271
272
273 public void restorePlugin( String strPluginName )
274 {
275 _logger.info( "restore plugin : " + strPluginName );
276
277 try
278 {
279 File fileSource = new File( getWebAppPath( ) + PATH_BACKUP + strPluginName + FOLDER_WEBAPP );
280 File fileDest = new File( getWebAppPath( ) );
281
282 FileUtils.copyDirectory( fileSource, fileDest );
283 }
284 catch ( IOException ex )
285 {
286 _logger.error( "error restoring plugin : ", ex );
287 }
288 }
289
290
291
292
293
294
295 public boolean checkRestorable( String strPluginName )
296 {
297 String strBackupDirectory = getWebAppPath( ) + PATH_BACKUP + strPluginName;
298 File fileBackupDirectory = new File( strBackupDirectory );
299
300 return fileBackupDirectory.exists( );
301 }
302
303
304
305
306
307
308 public boolean checkInstallInProgress( String strPluginName )
309 {
310 String strDeployDirectory = getWebAppPath( ) + PATH_DEPLOY + strPluginName;
311 File fileDeployDirectory = new File( strDeployDirectory );
312
313 return fileDeployDirectory.exists( );
314 }
315
316
317
318
319
320 public void cancelInstallInProgress( String strPluginName )
321 {
322
323 String strDeployDirectory = getWebAppPath( ) + PATH_DEPLOY + strPluginName;
324 File fileDeployDirectory = new File( strDeployDirectory );
325
326 if ( fileDeployDirectory.exists( ) )
327 {
328 try
329 {
330 FileUtils.deleteDirectory( fileDeployDirectory );
331 }
332 catch ( IOException ex )
333 {
334 _logger.error( "error installing plugin : ", ex );
335 }
336 }
337 }
338
339
340
341
342
343
344 public String getDeployWebappPath( String strPluginName )
345 {
346 return getWebAppPath( ) + PATH_DEPLOY + strPluginName + FOLDER_WEBAPP;
347 }
348
349
350
351
352
353
354 public String getDeploySqlPath( String strPluginName )
355 {
356 return getWebAppPath( ) + PATH_DEPLOY + strPluginName + FOLDER_SQL;
357 }
358
359
360
361
362
363
364 private void executeScripts( String strScriptsDirectory )
365 throws UpdaterScriptException
366 {
367 File fileScriptsDirectory = new File( strScriptsDirectory );
368
369 if ( fileScriptsDirectory.exists( ) )
370 {
371 try
372 {
373
374 TreeSet<File> set = new TreeSet<File>( new FileNameComparator( ) );
375
376 File[] files = fileScriptsDirectory.listFiles( );
377
378 if ( files != null )
379 {
380 set.addAll(Arrays.asList(files));
381
382 for ( File file : set )
383 {
384 SqlUtils.executeSqlFileScript( file.getAbsolutePath( ), null );
385 }
386 }
387 }
388 catch ( IOException e )
389 {
390 throw new UpdaterScriptException( "file not found", e );
391 }
392 catch ( SQLException e )
393 {
394 throw new UpdaterScriptException( "SQL Error", e );
395 }
396 }
397 }
398
399
400
401
402
403
404 private List<FileSystemResource> getPluginResources( String strPluginName )
405 {
406 List<FileSystemResource> listResources = new ArrayList<FileSystemResource>( );
407 addResource( "/css/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
408 addResource( "/js/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
409 addResource( "/images/admin/skin/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
410 addResource( "/images/local/skin/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
411 addResource( "/jsp/site/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
412 addResource( "/jsp/admin/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
413 addResource( "/WEB-INF/classes/fr/paris/lutece/plugins/" + strPluginName + "/resources", DIRECTORY_TREE,
414 listResources );
415 addResource( "/WEB-INF/conf/plugins/" + strPluginName + ".properties", FILE, listResources );
416 addResource( "/WEB-INF/conf/plugins/" + strPluginName + "_context.xml", FILE, listResources );
417 addResource( "/WEB-INF/plugins/" + strPluginName + ".xml", FILE, listResources );
418
419 addResource( "/WEB-INF/sql/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
420 addResource( "/WEB-INF/templates/admin/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
421 addResource( "/WEB-INF/templates/skin/plugins/" + strPluginName, DIRECTORY_TREE, listResources );
422
423 addResource( "/WEB-INF/lib/", "plugin-" + strPluginName, FILE_PREFIX, listResources );
424 addResource( "/WEB-INF/xsl/normal/", "portlet_" + strPluginName, FILE_PREFIX, listResources );
425
426 return listResources;
427 }
428
429
430
431
432
433
434
435 private void addResource( String strResource, int nType, List<FileSystemResource> listResources )
436 {
437 addResource( strResource, "", nType, listResources );
438 }
439
440
441
442
443
444
445
446
447 private void addResource( String strResourceDirectory, String strResourcePattern, int nType,
448 List<FileSystemResource> listResources )
449 {
450 try
451 {
452 File file = new File( getWebAppPath( ) + "/" + strResourceDirectory );
453
454 if ( file.exists( ) )
455 {
456 FileSystemResource resource = null;
457
458 switch ( nType )
459 {
460 case FILE:
461 resource = new FileResource( getWebAppPath( ), strResourceDirectory );
462 _logger.debug( "plugin resource 'file' found : " + file.getPath( ) );
463
464 break;
465
466 case DIRECTORY_TREE:
467 resource = new DirectoryTreeResource( getWebAppPath( ), strResourceDirectory );
468 _logger.debug( "plugin resource 'directory tree' found : " + file.getPath( ) );
469
470 break;
471
472 case FILE_PREFIX:
473 resource = new FilePatternResource( getWebAppPath( ), strResourceDirectory,
474 strResourcePattern, FilePatternResource.PATTERN_PREFIX );
475 _logger.debug( "plugin resource 'file pattern' found : " + file.getPath( ) );
476
477 break;
478
479 default:
480 break;
481 }
482
483 listResources.add( resource );
484 }
485 else
486 {
487 _logger.debug( "plugin resource not found : " + file.getPath( ) );
488 }
489 }
490 catch ( Exception e )
491 {
492 _logger.error( "PluginManagerService:AddResource error : " + e.getMessage( ), e );
493 }
494 }
495
496
497
498
499
500
501 private void installPlugin( String strPluginName )
502 throws UpdaterInstallException
503 {
504 _logger.info( "install plugin : " + strPluginName );
505
506 try
507 {
508
509 File fileSource = new File( getWebAppPath( ) + PATH_DEPLOY + strPluginName + FOLDER_WEBAPP );
510 File fileDest = new File( getWebAppPath( ) );
511
512 FileUtils.copyDirectory( fileSource, fileDest );
513
514
515 executeScripts( getWebAppPath( ) + PATH_DEPLOY + strPluginName + FOLDER_SQL );
516
517
518 File fileDeploy = new File( getWebAppPath( ) + PATH_DEPLOY + strPluginName );
519 FileUtils.deleteDirectory( fileDeploy );
520 }
521 catch ( IOException ex )
522 {
523 _logger.error( "error installing plugin : ", ex );
524 throw new UpdaterInstallException( "Installation exception", ex );
525 }
526 catch ( UpdaterScriptException ex )
527 {
528 throw new UpdaterInstallException( "Installation exception", ex );
529 }
530 }
531 }