001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.plugin;
017:
018: import java.io.File;
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.logging.Logger;
022:
023: import org.columba.core.config.DefaultConfigDirectory;
024:
025: /**
026: * Find all plugins in the users config-directory and Columba's
027: * program folder.
028: *
029: * @author fdietz
030: */
031: public final class PluginFinder {
032:
033: private static final Logger LOG = Logger
034: .getLogger("org.columba.core.plugin");
035:
036: /**
037: * Constructor for PluginFinder.
038: */
039: private PluginFinder() {
040: super ();
041: }
042:
043: /**
044: * Get list of all possible plugin folders.
045: *
046: * @return array of plugin folders
047: */
048: public static File[] searchPlugins() {
049: File[] programList = null;
050: File[] configList = null;
051:
052: File programFolder = new File("plugins");
053:
054: if (programFolder.exists()) {
055: programList = programFolder.listFiles();
056: } else {
057: LOG.fine("Folder \"" + programFolder.getPath()
058: + "\" doesn't exist.");
059: }
060:
061: File configFolder = new File(DefaultConfigDirectory
062: .getInstance().getCurrentPath(), "plugins");
063:
064: if (configFolder.exists()) {
065: configList = configFolder.listFiles();
066: } else {
067: LOG.fine("Folder \"" + configFolder.getPath()
068: + "\" doesn't exist.");
069: }
070:
071: if ((programList != null) && (configList != null)) {
072: File[] result = new File[programList.length
073: + configList.length];
074: System.arraycopy(programList, 0, result, 0,
075: programList.length);
076: System.arraycopy(configList, 0, result, programList.length,
077: configList.length);
078:
079: // remove directories which don't contain a plugin
080: return filterDirectories(result);
081: } else if (programList != null) {
082: return programList;
083: } else if (configList != null) {
084: return configList;
085: }
086:
087: return null;
088: }
089:
090: /**
091: * Filter out directories which are valid. Remove all
092: * other files.
093: *
094: * @param files array of plugin directories
095: * @return array of valid plugin directories
096: */
097: public static File[] filterDirectories(File[] files) {
098: List list = new ArrayList();
099: for (int i = 0; i < files.length; i++) {
100: if (checkDirectory(files[i])) {
101: list.add(files[i]);
102: }
103: }
104: return (File[]) list.toArray(new File[0]);
105: }
106:
107: /**
108: * Check if directory is valid plugin directory.
109: * <p>
110: * A directory is valid if it contains a plugin.xml file
111: * containing the plugin's meta information.
112: *
113: * @param file plugin directory to check
114: * @return true, if directory contains plugin. False, otherwise.
115: */
116: public static boolean checkDirectory(File file) {
117: if (file.isDirectory()) {
118: File plugin = new File(file, "plugin.xml");
119: return plugin.exists();
120: }
121:
122: return false;
123: }
124: }
|