KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > autoload > InstalledDirectoryScanner


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: InstalledDirectoryScanner.java,v 1.2 2006/04/07 10:24:27 ofabre Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.petals.jbi.management.autoload;
26
27 import java.io.File JavaDoc;
28 import java.io.FilenameFilter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.TimerTask JavaDoc;
33
34 /**
35  * Periodicly check if new Install or Deploy elements are presents in the
36  * autoload directories.
37  *
38  * @author ofabre
39  *
40  */

41 public class InstalledDirectoryScanner extends TimerTask JavaDoc {
42
43     /**
44      * filter for autoloading. Return true if the file is an archive : JAR or
45      * ZIP.
46      *
47      * @author alouis
48      *
49      */

50     private class ArchiveFileFilter implements FilenameFilter JavaDoc {
51
52         /**
53          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
54          */

55         public boolean accept(File JavaDoc dir, String JavaDoc name) {
56
57             String JavaDoc lcName = name.toLowerCase();
58
59             if (lcName.endsWith("jar") || lcName.endsWith("zip")) {
60                 return true;
61             }
62             return false;
63         }
64
65     }
66
67     ArchiveFileFilter archiveFilter;
68
69     AutoLoaderImpl autoLoader;
70
71     File JavaDoc installedDir;
72
73     List JavaDoc<File JavaDoc> previousFileList = new ArrayList JavaDoc<File JavaDoc>();
74
75     public InstalledDirectoryScanner(AutoLoaderImpl autoLoader,
76             File JavaDoc installedDir) {
77         this.installedDir = installedDir;
78
79         this.autoLoader = autoLoader;
80
81         archiveFilter = new ArchiveFileFilter();
82     }
83
84     public void run() {
85         
86         List JavaDoc<File JavaDoc> newFileList = null;
87         File JavaDoc[] files = installedDir.listFiles(archiveFilter);
88         if(files != null){
89             newFileList = Arrays.asList(files);
90         }else{
91             newFileList = new ArrayList JavaDoc<File JavaDoc>();
92         }
93         List JavaDoc<File JavaDoc> filesToUninstall = new ArrayList JavaDoc<File JavaDoc>();
94
95         for (File JavaDoc file : previousFileList) {
96             if (!newFileList.contains(file)) {
97                 filesToUninstall.add(file);
98             }
99         }
100
101         if (filesToUninstall != null && !filesToUninstall.isEmpty()) {
102             autoLoader.uninstall(filesToUninstall);
103         }
104
105         previousFileList.clear();
106         previousFileList.addAll(newFileList);
107     }
108 }
109
Popular Tags