KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > plugin > PluginFinder


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.plugin;
17
18 import java.io.File JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.logging.Logger JavaDoc;
22
23 import org.columba.core.config.DefaultConfigDirectory;
24
25
26 /**
27  * Find all plugins in the users config-directory and Columba's
28  * program folder.
29  *
30  * @author fdietz
31  */

32 public final class PluginFinder {
33
34     private static final Logger JavaDoc LOG = Logger.getLogger("org.columba.core.plugin");
35
36     /**
37      * Constructor for PluginFinder.
38      */

39     private PluginFinder() {
40         super();
41     }
42
43     /**
44      * Get list of all possible plugin folders.
45      *
46      * @return array of plugin folders
47      */

48     public static File JavaDoc[] searchPlugins() {
49         File JavaDoc[] programList = null;
50         File JavaDoc[] configList = null;
51
52         File JavaDoc programFolder = new File JavaDoc("plugins");
53
54         if (programFolder.exists()) {
55             programList = programFolder.listFiles();
56         } else {
57             LOG.fine("Folder \"" + programFolder.getPath() + "\" doesn't exist.");
58         }
59
60         File JavaDoc configFolder = new File JavaDoc(DefaultConfigDirectory.getInstance().getCurrentPath(),
61                 "plugins");
62
63         if (configFolder.exists()) {
64             configList = configFolder.listFiles();
65         } else {
66             LOG.fine("Folder \"" + configFolder.getPath() + "\" doesn't exist.");
67         }
68
69         if ((programList != null) && (configList != null)) {
70             File JavaDoc[] result = new File JavaDoc[programList.length + configList.length];
71             System.arraycopy(programList, 0, result, 0, programList.length);
72             System.arraycopy(configList, 0, result, programList.length,
73                 configList.length);
74
75             // remove directories which don't contain a plugin
76
return filterDirectories(result);
77         } else if (programList != null) {
78             return programList;
79         } else if (configList != null) {
80             return configList;
81         }
82
83         return null;
84     }
85
86     /**
87      * Filter out directories which are valid. Remove all
88      * other files.
89      *
90      * @param files array of plugin directories
91      * @return array of valid plugin directories
92      */

93     public static File JavaDoc[] filterDirectories(File JavaDoc[] files) {
94         List JavaDoc list = new ArrayList JavaDoc();
95         for (int i = 0; i < files.length; i++) {
96             if (checkDirectory(files[i])) {
97                 list.add(files[i]);
98             }
99         }
100         return (File JavaDoc[]) list.toArray(new File JavaDoc[0]);
101     }
102
103
104     /**
105      * Check if directory is valid plugin directory.
106      * <p>
107      * A directory is valid if it contains a plugin.xml file
108      * containing the plugin's meta information.
109      *
110      * @param file plugin directory to check
111      * @return true, if directory contains plugin. False, otherwise.
112      */

113     public static boolean checkDirectory(File JavaDoc file) {
114         if (file.isDirectory()) {
115             File JavaDoc plugin = new File JavaDoc(file, "plugin.xml");
116             return plugin.exists();
117         }
118
119         return false;
120     }
121 }
122
Popular Tags