KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > application > ApplicationPluginManager


1 package org.objectweb.celtix.application;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import org.objectweb.celtix.common.i18n.Message;
10 import org.objectweb.celtix.common.logging.LogUtils;
11 import org.objectweb.celtix.configuration.Configuration;
12 import org.objectweb.celtix.plugins.PluginException;
13 import org.objectweb.celtix.plugins.PluginManager;
14
15 /**
16  * @author asmyth The ApplicationPluginManager manages objects loaded on demand
17  * in response to a getPlugin request. The loading of one pluggable
18  * object may require explicitly loading dependent objects. Such
19  * dependencies are always specified by plugin name (as otherwise they
20  * can trivially be resolved by the clasloader). Circular dependencies
21  * are not allowed and will cause a PluginException. Plugins may be
22  * unloaded after they have been explicitly unregistered.
23  */

24 public class ApplicationPluginManager implements PluginManager {
25
26     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ApplicationPluginManager.class);
27     
28     private static final MessageFormat JavaDoc PLUGINS_CLASSNAME_FMT =
29         new MessageFormat JavaDoc("plugins:{0}:className");
30
31     private static final MessageFormat JavaDoc PLUGINS_PREREQUISITES_FMT =
32         new MessageFormat JavaDoc("plugins:{0}:prerequisites");
33
34     private final List JavaDoc<PluginInfo> plugins;
35
36     public ApplicationPluginManager() {
37         plugins = new ArrayList JavaDoc<PluginInfo>();
38     }
39     
40     /*
41      * (non-Javadoc)
42      *
43      * @see org.objectweb.celtix.plugins.PluginManager#getPlugin(java.lang.String)
44      */

45     public Object JavaDoc getPlugin(String JavaDoc className) throws PluginException {
46         return getPlugin(null, className, null);
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.objectweb.celtix.plugins.PluginManager#getPluginByName(java.lang.String)
53      */

54     public Object JavaDoc getPluginByName(String JavaDoc pluginName) throws PluginException {
55         return getPluginByName(pluginName, null);
56     }
57     
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.objectweb.celtix.plugins.PluginManager#registerPlugin(org.objectweb.celtix.plugins.Plugin)
62      */

63     public synchronized void registerPlugin(Object JavaDoc plugin) throws PluginException {
64         PluginInfo info = findPluginInfo(plugin);
65         if (info.isRegisteredWith(this)) {
66             throw new PluginException(new Message("ALREADY_REGISTERED_EXC", LOG, info.getClassName()));
67         } else {
68             info.register(this);
69         }
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.objectweb.celtix.plugins.PluginManager#unloadPlugin(java.lang.String)
76      */

77     public synchronized void unloadPlugin(Object JavaDoc plugin) throws PluginException {
78         PluginInfo info = findPluginInfo(plugin);
79         if (info.isRegistered()) {
80             throw new PluginException(new Message("STILL_REGISTERED_EXC", LOG, info.getClassName()));
81         } else {
82             plugins.remove(plugin);
83             info = null;
84         }
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.objectweb.celtix.plugins.PluginManager#unregisterPlugin(java.lang.String)
91      */

92     public synchronized void unregisterPlugin(Object JavaDoc plugin) throws PluginException {
93         PluginInfo info = findPluginInfo(plugin);
94         if (info.isRegisteredWith(this)) {
95             info.unregister(this);
96         } else {
97             throw new PluginException(new Message("NOT_REGISTERED_EXC", LOG, info.getClassName()));
98         }
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.objectweb.celtix.plugins.PluginManager#getPluginClassLoader()
105      */

106     public ClassLoader JavaDoc getPluginClassLoader() {
107         return getClass().getClassLoader();
108     }
109
110     /* (non-Javadoc)
111      * @see org.objectweb.celtix.plugins.PluginManager#getConfiguration()
112      */

113     public Configuration getConfiguration() {
114         return Application.getInstance().getConfiguration();
115     }
116
117     Object JavaDoc getPluginByName(String JavaDoc pluginName, PluginInfo dependent) throws PluginException {
118         String JavaDoc key = PLUGINS_CLASSNAME_FMT.format(pluginName);
119         Configuration configuration = getConfiguration();
120         String JavaDoc pluginClassName = (String JavaDoc)configuration.getObject(key);
121         
122         return getPlugin(pluginName, pluginClassName, dependent);
123     }
124
125     Object JavaDoc getPlugin(String JavaDoc pluginName, String JavaDoc pluginClassName, PluginInfo dependent) throws PluginException {
126         LOG.entering(getClass().getName(), "getPlugin");
127         PluginInfo info = null;
128         PluginStateMachine state = null;
129         synchronized (this) {
130             info = findPluginByClassname(pluginClassName);
131             if (info == null) {
132                 info = new PluginInfo(pluginClassName, this);
133                 plugins.add(info);
134             }
135             state = info.getState();
136             if (PluginStateMachine.PluginState.LOADING == state.getCurrentState()) {
137                 state.waitForState(PluginStateMachine.PluginState.LOADED);
138                 LOG.exiting(getClass().getName(), "getPlugin", "object currently being loaded");
139                 return info.getPlugin();
140             } else if (PluginStateMachine.PluginState.LOADED == state.getCurrentState()) {
141                 LOG.exiting(getClass().getName(), "getPlugin", "object already loaded");
142                 return info.getPlugin();
143             }
144             state.setNextState(PluginStateMachine.PluginState.LOADING);
145         }
146
147         // check for circular dependencies
148

149         if (dependent != null) {
150             info.setRequiredFor(dependent);
151             if (info.isCircularDependency()) {
152                 throw new PluginException(new Message("CIRCULAR_DEPENDENCY_EXC", LOG, pluginClassName));
153             }
154         }
155
156         if (null != pluginName) {
157             Configuration configuration = getConfiguration();
158
159             String JavaDoc key = PLUGINS_PREREQUISITES_FMT.format(pluginName);
160             String JavaDoc[] prerequisites = (String JavaDoc[])configuration.getObject(key);
161
162             if (prerequisites != null) {
163                 for (String JavaDoc p : prerequisites) {
164                     getPluginByName(p, info);
165                 }
166             }
167         }
168         
169         Object JavaDoc plugin = createPlugin(pluginClassName);
170         info.setPlugin(plugin);
171         state.setNextState(PluginStateMachine.PluginState.LOADED);
172         LOG.exiting(getClass().getName(), "getPlugin", "object newly created");
173         return plugin;
174     }
175
176     Object JavaDoc createPlugin(String JavaDoc pluginClassName) throws PluginException {
177
178         ClassLoader JavaDoc cl = getPluginClassLoader();
179         Object JavaDoc plugin = null;
180         try {
181             Class JavaDoc<?> pluginClass = Class.forName(pluginClassName, true, cl);
182             plugin = pluginClass.newInstance();
183         } catch (Exception JavaDoc ex) {
184             LogUtils.log(LOG, Level.SEVERE, "PLUGIN_LOAD_FAILURE_MSG", ex, pluginClassName);
185             throw new PluginException(new Message("LOAD_FAILED_EXC", LOG, pluginClassName), ex);
186         }
187         return plugin;
188     }
189     
190     PluginInfo findPluginByClassname(String JavaDoc className) {
191         for (PluginInfo info : plugins) {
192             if (info.getClassName().equals(className)
193                 && info.getClassLoader() == getPluginClassLoader()) {
194                 return info;
195             }
196         }
197         LOG.info("Could not find plugin info for class " + className);
198         return null;
199     }
200     
201     PluginInfo findPluginInfo(Object JavaDoc plugin) {
202
203         for (PluginInfo info : plugins) {
204             if (plugin == info.getPlugin()) {
205                 return info;
206             }
207         }
208         LOG.info("Could not find plugin info for plugin " + plugin);
209         return null;
210     }
211 }
212
Popular Tags