KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > PluginManager


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
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 (at your option) 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 USA
18  */

19 package org.lucane.client;
20
21 import org.lucane.common.*;
22 import org.lucane.common.net.ObjectConnection;
23
24 import java.net.URL JavaDoc;
25 import java.util.*;
26
27
28 /**
29  * This is an important part of the Client.
30  * It can dynamically load plugins.
31  */

32 public class PluginManager
33 {
34     private static PluginManager instance = null;
35     
36     //stores available plugins in a (name, plugin) map
37
private HashMap availablePlugins;
38     
39     //stores running plugins
40
private ArrayList runningPlugins;
41     
42     /**
43      * PluginManager is a singleton
44      *
45      * @return the unique PluginManager instance
46      */

47     public static PluginManager getInstance()
48     {
49         if(instance == null)
50             instance = new PluginManager();
51         
52         return instance;
53     }
54     
55     /**
56      * Creates a PluginManager and initialize its plugins list
57      */

58     private PluginManager()
59     {
60         this.availablePlugins = new HashMap();
61         this.runningPlugins = new ArrayList();
62     }
63     
64     //-- plugin initialization & run
65

66     /**
67      * Get a new plugin instance
68      *
69      * @param name the plugin name
70      * @param friends the connectinfos
71      * @param starter true if the plugin is starter
72      * @return a new plugin instance
73      */

74     public Plugin newPluginInstance(String JavaDoc name, ConnectInfo[] friends, boolean starter)
75     {
76         Plugin base = (Plugin)this.availablePlugins.get(name);
77         Plugin p = base.newInstance(friends);
78         p.setStarter(starter);
79         p.setName(base.getName());
80         p.setLocale(Client.getInstance().getConfig().getLanguage());
81         
82         return p;
83     }
84     
85     /**
86      * Loads a Plugin.
87      * Initialize it with the correct streams and run it in a new Thread
88      *
89      * @param oc the ObjectConnection
90      * @param message the message
91      */

92     public Plugin load(ObjectConnection oc, Message message)
93     {
94         String JavaDoc name = message.getApplication();
95         Logging.getLogger().fine("Trying to load plugin " + name);
96         
97         Plugin p = newPluginInstance(name, new ConnectInfo[0], false);
98         p.setStarter(false);
99         p.load(oc, message.getSender(), (String JavaDoc)message.getData());
100         (new Thread JavaDoc(p, p.getName())).start();
101         Logging.getLogger().info("Plugin " + name + " loaded.");
102         return p;
103     }
104     
105     /**
106      * Runs the requested Plugin in a new Thread
107      *
108      * @param name the Plugin to run
109      * @param friends the connexions associated with the plugin
110      */

111     public Plugin run(String JavaDoc name, ConnectInfo[] friends)
112     {
113         Logging.getLogger().fine("Trying to run plugin " + name);
114         Plugin p = newPluginInstance(name, friends, true);
115         p.setStarter(true);
116         (new Thread JavaDoc(p, p.getName())).start();
117         Logging.getLogger().fine("Plugin " + name + " started.");
118         
119         return p;
120     }
121     
122     //-- available plugins
123

124     /**
125      * Check if a Plugin is available
126      *
127      * @param name the Plugin to check
128      * @return true if the client has this Plugin
129      */

130     public boolean isAvailable(String JavaDoc name)
131     {
132         return this.isAvailable(name, "", false);
133     }
134     
135     /**
136      * Check if a Plugin is available
137      *
138      * @param name the Plugin to check
139      * @param version the version to check
140      * @return true if the Client has this version of the Plugin
141      */

142     public boolean isAvailable(String JavaDoc name, String JavaDoc version)
143     {
144         return isAvailable(name, version, false);
145     }
146     
147     /**
148      * Check if a Plugin is available
149      *
150      * @param name the Plugin to check
151      * @param version the version to check
152      * @param load trus if the PluginManager has to load the Plugin
153      * @return true if the Client has this version of the Plugin
154      */

155     protected boolean isAvailable(String JavaDoc name, String JavaDoc version, boolean load)
156     {
157         Plugin plugin;
158         if(load == true)
159         {
160             try
161             {
162                 LucaneClassLoader loader = LucaneClassLoader.getInstance();
163                 String JavaDoc baseURL = System.getProperty("user.dir") + "/" + Client.APPLICATIONS_DIRECTORY;
164                 String JavaDoc jarUrl = baseURL + name + ".jar";
165                 URL JavaDoc url = new URL JavaDoc("jar:file:///" + jarUrl + "!/");
166                 loader.addUrl(url);
167                 Logging.getLogger().fine("plugin URL: " + url);
168                 
169                 //we have to set our ClassLoader to reload the plugin.
170
String JavaDoc className = JarUtils.getPluginClass(jarUrl);
171                 Logging.getLogger().finer("classname: " + className);
172                 
173                 plugin = (Plugin)Class.forName(className, true, loader).newInstance();
174                 plugin.setName(name);
175                 plugin.setLocale(Client.getInstance().getConfig().getLanguage());
176                 this.availablePlugins.put(plugin.getName(), plugin);
177                 Logging.getLogger().info("Loaded plugin " + plugin.getName() + " v. " +
178                         plugin.getVersion());
179             }
180             catch(Exception JavaDoc e)
181             {
182                 return false;
183             }
184         }
185         
186         
187         plugin = (Plugin)this.availablePlugins.get(name);
188         return plugin != null && (plugin.getVersion().equals(version) || version.length() == 0);
189     }
190     
191     /**
192      * Get all available plugins
193      *
194      * @return an iterator
195      */

196     public Iterator getAvailablePlugins()
197     {
198         return this.availablePlugins.values().iterator();
199     }
200     
201     /**
202      * Get a plugin by its name
203      *
204      * @param name the plugin name
205      * @return the corresponding Plugin
206      */

207     public Plugin getPlugin(String JavaDoc name)
208     {
209         return (Plugin)this.availablePlugins.get(name);
210     }
211     
212     
213     //-- running plugins
214

215     /**
216      * Tells the client that a plugin has been launched.
217      */

218     protected void addRunningPlugin(Plugin p)
219     {
220         Logging.getLogger().fine("registering : " + p.getName());
221         this.runningPlugins.add(p);
222     }
223     
224     /**
225      * Tells a client that a plugin has been closed.
226      */

227     protected void removeRunningPlugin(Plugin p)
228     {
229         Logging.getLogger().fine("unregistering : " + p.getName());
230         this.runningPlugins.remove(p);
231         
232         //if no more plugins are running, or if we quit the startup plugin,
233
//exits the client cleanly
234
if(runningPlugins.isEmpty() || p.getName().equals(Client.getInstance().getStartupPlugin()))
235             Client.getInstance().cleanExit();
236     }
237     
238     /**
239      * Get plugins that are currently running
240      *
241      * @return an iterator containing active plugins
242      */

243     public Iterator getRunningPlugins()
244     {
245         return runningPlugins.iterator();
246     }
247 }
248
Popular Tags