001: /* CVS ID: $Id: PluginHandler.java,v 1.1.1.1 2002/10/02 18:42:51 wastl Exp $ */
002: package net.wastl.webmail.server;
003:
004: import net.wastl.webmail.config.*;
005: import net.wastl.webmail.misc.*;
006: import net.wastl.webmail.exceptions.WebMailException;
007: import java.io.*;
008: import java.util.*;
009:
010: /**
011: * PluginHandler.java
012: *
013: * Handle WebMail Plugins
014: *
015: * Created: Tue Aug 31 15:28:45 1999
016: *
017: * Copyright (C) 1999-2000 Sebastian Schaffert
018: *
019: * This program is free software; you can redistribute it and/or
020: * modify it under the terms of the GNU General Public License
021: * as published by the Free Software Foundation; either version 2
022: * of the License, or (at your option) any later version.
023: *
024: * This program is distributed in the hope that it will be useful,
025: * but WITHOUT ANY WARRANTY; without even the implied warranty of
026: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
027: * GNU General Public License for more details.
028: *
029: * You should have received a copy of the GNU General Public License
030: * along with this program; if not, write to the Free Software
031: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
032: */
033: /**
034: *
035: *
036: *
037: * @author Sebastian Schaffert
038: * @version
039: */
040:
041: public class PluginHandler {
042:
043: WebMailServer parent;
044: String plugin_list;
045: Vector plugins;
046:
047: public PluginHandler(WebMailServer parent) throws WebMailException {
048: this .parent = parent;
049: this .plugin_list = parent.getProperty("webmail.plugins");
050: if (plugin_list == null) {
051: throw new WebMailException(
052: "Error: No Plugins defined (Property webmail.plugins).");
053: }
054: plugins = new Vector();
055: registerPlugins();
056: }
057:
058: /**
059: * Initialize and register WebMail Plugins.
060: */
061: public void registerPlugins() {
062: parent.getStorage().log(Storage.LOG_INFO,
063: "Initializing WebMail Plugins ...");
064: // System.setProperty("java.class.path",System.getProperty("java.class.path")+System.getProperty("path.separator")+pluginpath);
065:
066: StringTokenizer tok = new StringTokenizer(plugin_list, ":;, ");
067:
068: Class plugin_class = null;
069: try {
070: plugin_class = Class
071: .forName("net.wastl.webmail.server.Plugin");
072: } catch (ClassNotFoundException ex) {
073: parent.getStorage().log(Storage.LOG_CRIT,
074: "===> Could not find interface 'Plugin'!!");
075: System.exit(1);
076: }
077:
078: PluginDependencyTree pt = new PluginDependencyTree("");
079: Queue q = new Queue();
080:
081: int count = 0;
082:
083: while (tok.hasMoreTokens()) {
084: String name = (String) tok.nextToken();
085: try {
086: Class c = Class.forName(name);
087: if (plugin_class.isAssignableFrom(c)) {
088: Plugin p = (Plugin) c.newInstance();
089: q.queue(p);
090: plugins.addElement(p);
091: //System.err.print(p.getName()+" ");
092: //System.err.flush();
093: count++;
094: }
095: } catch (Exception ex) {
096: parent.getStorage().log(Storage.LOG_ERR,
097: "could not register plugin \"" + name + "\"!");
098: ex.printStackTrace();
099: }
100: }
101:
102: parent.getStorage().log(Storage.LOG_INFO,
103: count + " plugins loaded correctly.");
104:
105: count = 0;
106: while (!q.isEmpty()) {
107: Plugin p = (Plugin) q.next();
108: if (!pt.addPlugin(p)) {
109: q.queue(p);
110: }
111: }
112: pt.register(parent);
113: parent.getStorage().log(Storage.LOG_INFO,
114: count + " plugins initialized.");
115: };
116:
117: public Enumeration getPlugins() {
118: return plugins.elements();
119: }
120:
121: /**
122: * A filter to find WebMail Plugins.
123: */
124: class FFilter implements FilenameFilter {
125: FFilter() {
126: }
127:
128: public boolean accept(File f, String s) {
129: if (s.endsWith(".class")) {
130: return true;
131: } else {
132: return false;
133: }
134: }
135: }
136:
137: } // PluginHandler
|