浏览代码

Actually added the plugins this time.

git-svn-id: http://svn.dmdirc.com/trunk@71 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Gregory Holmes 17 年前
父节点
当前提交
f474feb674

+ 12
- 4
src/dmdirc/Config.java 查看文件

@@ -93,7 +93,9 @@ public class Config {
93 93
      * @param option the name of the option
94 94
      */
95 95
     public static boolean hasOption(String domain, String option) {
96
-	assert(properties != null);
96
+	if (properties == null) {
97
+	    initialise();
98
+	}
97 99
 	
98 100
 	return (properties.getProperty(domain+"."+option) != null);
99 101
     }
@@ -105,7 +107,9 @@ public class Config {
105 107
      * @param option the name of the option
106 108
      */
107 109
     public static String getOption(String domain, String option) {
108
-	assert(properties != null);
110
+	if (properties == null) {
111
+	    initialise();
112
+	}
109 113
 	
110 114
 	return properties.getProperty(domain+"."+option);
111 115
     }
@@ -117,7 +121,9 @@ public class Config {
117 121
      * @param value value of the option
118 122
      */
119 123
     public static void setOption(String domain, String option, String value) {
120
-	assert(properties != null);
124
+	if (properties == null) {
125
+	    initialise();
126
+	}
121 127
 	
122 128
 	properties.setProperty(domain+"."+option, value);
123 129
     }
@@ -159,7 +165,9 @@ public class Config {
159 165
      * Saves the config file to disc
160 166
      */
161 167
     public static void save() {
162
-	assert(properties != null);
168
+	if (properties == null) {
169
+	    initialise();
170
+	}
163 171
 	try {
164 172
 	    properties.storeToXML(new FileOutputStream(new File(getConfigFile())), null);
165 173
 	} catch (FileNotFoundException ex) {

+ 0
- 2
src/dmdirc/Main.java 查看文件

@@ -56,8 +56,6 @@ public class Main {
56 56
             ex.printStackTrace();
57 57
         }
58 58
         
59
-        Config.initialise();
60
-        
61 59
         MainFrame frame = MainFrame.getMainFrame();
62 60
     }
63 61
     

+ 52
- 0
src/dmdirc/plugins/AbstractPlugin.java 查看文件

@@ -0,0 +1,52 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package dmdirc.plugins;
24
+
25
+public abstract class AbstractPlugin {
26
+    
27
+    private boolean running = false;
28
+    
29
+    private PluginManager pm = null;
30
+
31
+    public AbstractPlugin(PluginManager pm) {
32
+	this.pm = pm;
33
+    }
34
+    
35
+    void start() {
36
+	running = true;
37
+	while (running) {
38
+	    //Do stuff
39
+	}
40
+	pm.removePlugin(this);
41
+    }
42
+
43
+    void onLoad() {
44
+    }
45
+    
46
+    void onUnload() {
47
+    }
48
+    
49
+    void stopPlugin() {
50
+	running = false;
51
+    }    
52
+}

+ 123
- 0
src/dmdirc/plugins/PluginClassLoader.java 查看文件

@@ -0,0 +1,123 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package dmdirc.plugins;
24
+
25
+import java.io.DataInputStream;
26
+import java.io.File;
27
+import java.io.FileInputStream;
28
+import java.io.IOException;
29
+
30
+
31
+/**
32
+ * A custom ClassLoader to load and unload plugins.
33
+ */
34
+class PluginClassLoader extends ClassLoader {
35
+    private String baseDir = null;
36
+    
37
+    /**
38
+     * Constructs new PluginClassLoader
39
+     *
40
+     * @param baseDir plugin base dir
41
+     */
42
+    public PluginClassLoader(String baseDir) {
43
+	this.baseDir = baseDir;
44
+    }
45
+    
46
+    /**
47
+     * Loads a plugin from disk.
48
+     *
49
+     * @param name plugin class name to load
50
+     * @return plugin class
51
+     */
52
+    public Class loadClass(String name) throws ClassNotFoundException {
53
+	Class loadedClass = findLoadedClass(name);
54
+	if ( loadedClass == null ) {
55
+	    try {
56
+		loadedClass = findSystemClass(name);
57
+	    } catch ( Exception e ) {
58
+		// do nothing
59
+	    }
60
+	    
61
+	    if ( loadedClass == null ) {
62
+		String fileName = baseDir + File.separator +
63
+			name.replace('.', File.separatorChar) + ".class";
64
+		byte[] data = null;
65
+		
66
+		System.out.println("Trying to load: " + fileName);
67
+		
68
+		try {
69
+		    data = loadClassData(fileName);
70
+		} catch( IOException e ) {
71
+		    System.out.println(e);
72
+		    throw new ClassNotFoundException(e.getMessage());
73
+		}
74
+		
75
+		loadedClass = defineClass(name,data, 0, data.length);
76
+		
77
+		if ( loadedClass == null ) {
78
+		    System.out.println("loadedClass == null");
79
+		    throw new ClassNotFoundException("Could not load " + name);
80
+		} else {
81
+		    resolveClass(loadedClass);
82
+		}
83
+	    } else {
84
+		//Found loaded system class
85
+	    }
86
+	} else {
87
+	    //Found loaded class
88
+	}
89
+	
90
+	return loadedClass;
91
+    }
92
+    
93
+    /**
94
+     * Loads binary class data from disk.
95
+     *
96
+     * @param fileName file name
97
+     * @return bytecodes
98
+     */
99
+    private byte[] loadClassData(String fileName) throws IOException {
100
+	File file = new File(fileName);
101
+	byte buffer[] = new byte[(int)file.length()];
102
+	
103
+	FileInputStream in = new FileInputStream(file);
104
+	DataInputStream dataIn = new DataInputStream(in);
105
+	
106
+	dataIn.readFully(buffer);
107
+	dataIn.close();
108
+	
109
+	return buffer;
110
+    }
111
+}
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+

+ 88
- 0
src/dmdirc/plugins/PluginManager.java 查看文件

@@ -0,0 +1,88 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package dmdirc.plugins;
24
+
25
+import dmdirc.parser.IRCParser;
26
+import java.io.File;
27
+import java.io.FileInputStream;
28
+import java.io.FileOutputStream;
29
+import java.io.IOException;
30
+import java.util.Enumeration;
31
+import java.util.Hashtable;
32
+import java.util.Properties;
33
+
34
+public class PluginManager {
35
+    
36
+    private Hashtable<String, AbstractPlugin> loadedPlugins = null;
37
+    
38
+    public PluginManager(IRCParser parser) {
39
+	loadedPlugins = new Hashtable<String, AbstractPlugin>();
40
+    }
41
+    
42
+    public void forwardCallback() {
43
+    }
44
+    
45
+    public boolean addPlugin(String pluginClassName, AbstractPlugin plugin) {
46
+	if( !loadedPlugins.containsKey(pluginClassName) ) {
47
+	    loadedPlugins.put(pluginClassName,plugin);
48
+	    
49
+	    plugin.onLoad();
50
+	} else {
51
+	    return false;
52
+	}
53
+	plugin.start();
54
+	return true;
55
+    }
56
+    
57
+    protected synchronized boolean removePlugin(AbstractPlugin plugin) {
58
+	if(loadedPlugins.contains(plugin) ) {
59
+	    
60
+	    ClassLoader loader = plugin.getClass().getClassLoader();
61
+	    loader = null;
62
+	    plugin = null;
63
+	    
64
+	    System.gc();
65
+	    
66
+	    loadedPlugins.remove(plugin);
67
+	    return true;
68
+	}
69
+	
70
+	return false;
71
+    }
72
+    
73
+    public void stopPlugin(String pluginClassName) {
74
+	AbstractPlugin plugin = null;
75
+	
76
+	if( loadedPlugins.containsKey(pluginClassName) ) {
77
+	    plugin = (AbstractPlugin)loadedPlugins.get(pluginClassName);
78
+	    plugin.onUnload();
79
+	    plugin.stopPlugin();
80
+	}
81
+    }
82
+    public void stopPlugin(AbstractPlugin plugin) {
83
+	if( loadedPlugins.contains(plugin) ) {
84
+	    plugin.onUnload();
85
+	    plugin.stopPlugin();
86
+	}
87
+    }
88
+}

正在加载...
取消
保存