KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > impl > ServerManagerImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.impl;
10
11 import java.io.InputStream JavaDoc;
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24
25 import org.jboss.portal.common.plugin.PluginMetaData;
26 import org.jboss.portal.common.util.Tools;
27 import org.jboss.portal.common.util.XML;
28 import org.jboss.portal.server.ObjectFactory;
29 import org.jboss.portal.server.PortalServer;
30 import org.jboss.portal.server.ServerManager;
31 import org.jboss.portal.server.ServerObject;
32 import org.jboss.portal.server.ServerObjectID;
33 import org.jboss.portal.server.ThemeServer;
34 import org.jboss.portal.server.LayoutServer;
35 import org.jboss.portal.server.PortalConstants;
36 import org.jboss.portal.server.config.Configuration;
37 import org.jboss.portal.server.invocation.InvocationFactory;
38 import org.jboss.portal.server.kernel.Kernel;
39 import org.jboss.portal.server.kernel.ServiceID;
40 import org.jboss.portal.server.metadata.InterceptorListMetaData;
41 import org.jboss.portal.server.metadata.InterceptorMetaData;
42 import org.jboss.portal.server.metadata.PortalServerMetaData;
43 import org.jboss.portal.server.metadata.ServerObjectMetaData;
44 import org.jboss.portal.server.plugins.PluginID;
45 import org.jboss.portal.server.plugins.PluginService;
46 import org.jboss.portal.server.servlet.BootStrapContext;
47 import org.jboss.portal.server.util.Service;
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
52
53 /**
54  * @jmx.mbean
55  * @jboss.xmbean
56  *
57  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
58  * @version $Revision: 1.10 $
59  */

60 public class ServerManagerImpl extends Service implements ServerManager
61 {
62
63    /** The container name. */
64    private String JavaDoc containerName;
65
66    /** The web container very usefull objects. */
67    private BootStrapContext bootStrap;
68
69    /** The portal object container managed. */
70    private PortalServer portalServer;
71
72    private ThemeServer themeServer;
73
74    private LayoutServer layoutServer;
75
76    /** Factory used to create portal objects. */
77    private ObjectFactory objectFactory;
78
79    /** Invocation context. */
80    private InvocationFactory invocationFactory;
81
82    /** . */
83    private Kernel kernel;
84
85    /** . */
86    private Configuration configuration;
87
88    /** . */
89    private String JavaDoc invocationFactoryClassName;
90
91    /** . */
92    private Map JavaDoc deployed;
93
94    public ServerManagerImpl()
95    {
96       this.kernel = new Kernel();
97       this.deployed = new ConcurrentHashMap();
98    }
99
100    /**
101     * Load the interceptor configuration.
102     */

103    public static void loadInterceptorSetMetaData(InterceptorListMetaData interceptorListMD, Element JavaDoc interceptorsElt)
104    {
105       NodeList JavaDoc interceptorElts = interceptorsElt.getElementsByTagName("interceptor");
106       for (int i = 0;i < interceptorElts.getLength();i++)
107       {
108          Element JavaDoc interceptorElt = (Element JavaDoc)interceptorElts.item(i);
109          Element JavaDoc interceptorClassElt = XML.getUniqueChild(
110                interceptorElt,
111                "interceptor-class",
112                true);
113          String JavaDoc className = XML.asString(interceptorClassElt);
114          InterceptorMetaData interceptorMD = new InterceptorMetaData(className);
115          NodeList JavaDoc interceptorParamElts = interceptorElt.getElementsByTagName("interceptor-param");
116          for (int j = 0;j < interceptorParamElts.getLength();j++)
117          {
118             Element JavaDoc interceptorParamElt = (Element JavaDoc)interceptorParamElts.item(j);
119             String JavaDoc paramName = XML.asString(XML.getUniqueChild(
120                   interceptorParamElt,
121                   "param-name",
122                   true));
123             String JavaDoc paramValue = XML.asString(XML.getUniqueChild(
124                   interceptorParamElt,
125                   "param-value",
126                   true));
127             interceptorMD.setParamValue(paramName, paramValue);
128          }
129          interceptorListMD.add(interceptorMD);
130       }
131    }
132
133    protected void startService() throws Exception JavaDoc
134    {
135       Map JavaDoc componentConfigurationsMD = null;
136
137       InputStream JavaDoc in = null;
138       InterceptorListMetaData ilm = null;
139       try
140       {
141          in = configuration.load("/jboss-container.xml");
142          DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
143          Document JavaDoc doc = builder.parse(in);
144
145          Element JavaDoc rootElt = doc.getDocumentElement();
146          Element JavaDoc containerConfigurationElt = XML.getUniqueChild(rootElt, "container-configuration", true);
147
148          // Load the interceptor configuration
149
Element JavaDoc interceptorsElt = XML.getUniqueChild(containerConfigurationElt, "interceptors", true);
150          ilm = new InterceptorListMetaData();
151          loadInterceptorSetMetaData(ilm, interceptorsElt);
152       }
153       finally
154       {
155          Tools.safeClose(in);
156       }
157
158       //
159
ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
160       Class JavaDoc invocationFactoryClass = classLoader.loadClass(invocationFactoryClassName);
161       Constructor JavaDoc ctor = invocationFactoryClass.getConstructor(new Class JavaDoc[]{ServerManager.class});
162       invocationFactory = (InvocationFactory)ctor.newInstance(new Object JavaDoc[]{this});
163
164       // Create the container metadata
165
// Get that from a config file
166
PortalServerMetaData portalObjectContainerMD = new PortalServerMetaData(containerName);
167       portalObjectContainerMD.setInterceptorList(ilm);
168       portalObjectContainerMD.setComponentConfigurations(componentConfigurationsMD);
169
170       // Create the containe object itself
171
portalServer = (PortalServer)objectFactory.create(portalObjectContainerMD);
172       portalServer.setManager(this);
173
174       // Register and start
175
kernel.register(portalObjectContainerMD.getID(), portalServer);
176
177       //
178
installPlugins(portalServer, new HashSet JavaDoc());
179
180       // Cascade start
181
kernel.start(portalObjectContainerMD.getID());
182
183       log.info("Started " + PortalConstants.VERSION);
184    }
185
186    protected void stopService() throws Exception JavaDoc
187    {
188       // Cascade destroy
189
ServerObjectID containerID = portalServer.getID();
190       kernel.destroy(containerID);
191
192       //
193
uninstallPlugins(portalServer);
194
195       // Unregister
196
kernel.unregister(containerID);
197    }
198
199    /**
200     * @jmx.managed-attribute
201     */

202    public String JavaDoc getInvocationFactoryClassName()
203    {
204       return invocationFactoryClassName;
205    }
206
207    /**
208     * @jmx.managed-attribute
209     */

210    public void setInvocationFactoryClassName(String JavaDoc invocationFactoryClassName)
211    {
212       this.invocationFactoryClassName = invocationFactoryClassName;
213    }
214
215    /**
216     * @jmx.managed-attribute
217     */

218    public String JavaDoc getContainerName()
219    {
220       return containerName;
221    }
222
223    /**
224     * @jmx.managed-attribute
225     */

226    public InvocationFactory getInvocationContext()
227    {
228       return invocationFactory;
229    }
230
231    /**
232     * @jmx.managed-attribute
233     */

234    public void setInvocationContext(InvocationFactory invocationFactory)
235    {
236       this.invocationFactory = invocationFactory;
237    }
238
239    /**
240     * @jmx.managed-attribute
241     */

242    public void setContainerName(String JavaDoc containerName)
243    {
244       this.containerName = containerName;
245    }
246
247    /**
248     * @jmx.managed-attribute
249     */

250    public ObjectFactory getObjectFactory()
251    {
252       return objectFactory;
253    }
254
255    /**
256     * @jmx.managed-attribute
257     */

258    public void setObjectFactory(ObjectFactory objectFactory)
259    {
260       this.objectFactory = objectFactory;
261    }
262
263    /**
264     * @jmx.managed-attribute
265     */

266    public BootStrapContext getBootStrap()
267    {
268       return bootStrap;
269    }
270
271    /**
272     * @jmx.managed-attribute
273     * access="write-only"
274     */

275    public void setBootStrap(BootStrapContext bootStrap)
276    {
277       this.bootStrap = bootStrap;
278    }
279
280    /**
281     * @jmx.managed-attribute
282     */

283    public Configuration getConfiguration()
284    {
285       return configuration;
286    }
287
288    /**
289     * @jmx.managed-attribute
290     */

291    public void setConfiguration(Configuration configuration)
292    {
293       this.configuration = configuration;
294    }
295
296    /**
297     * @jmx.managed-attribute
298     */

299    public Kernel getKernel()
300    {
301       return kernel;
302    }
303
304    /**
305     * @jmx.managed-attribute
306     */

307    public PortalServer getPortalServer()
308    {
309       return portalServer;
310    }
311
312    /**
313     * @jmx.managed-attribute
314     */

315    public ThemeServer getThemeServer()
316    {
317       return themeServer;
318    }
319
320    /**
321     * @jmx.managed-attribute
322     */

323    public void setThemeServer(ThemeServer server)
324    {
325       themeServer = server;
326    }
327
328    /**
329     * @jmx.managed-attribute
330     */

331    public LayoutServer getLayoutServer()
332    {
333       return layoutServer;
334    }
335
336    /**
337     * @jmx.managed-attribute
338     */

339    public void setLayoutServer(LayoutServer server)
340    {
341       layoutServer = server;
342    }
343
344    private void installPlugins(ServerObject object, Collection JavaDoc installed) throws Exception JavaDoc
345    {
346       ServerObjectMetaData metaData = object.getMetaData();
347       log.debug("Installing plugins for " + metaData.getID());
348       for (Iterator JavaDoc i = metaData.getPluginKeySet().iterator();i.hasNext();)
349       {
350          String JavaDoc key = (String JavaDoc)i.next();
351          PluginMetaData pluginMD = metaData.getPlugin(key);
352          log.debug("Plugin found " + key);
353          // Force to be a plugin service, but should use a wrapper if not a direct service
354
// May lead to class cast exceptions
355
PluginService plugin = (PluginService)objectFactory.create(pluginMD);
356          log.debug("Instantiated plugin " + key + " with class " + plugin.getClass().getName());
357          plugin.setMetaData(pluginMD);
358          ServerObjectID parentID = object.getID();
359          PluginID id = new PluginID(parentID, key);
360          kernel.register(id, plugin, new ServiceID[]{parentID});
361          installed.add(id);
362       }
363    }
364
365    private void uninstallPlugins(ServerObject object)
366    {
367       ServerObjectMetaData metaData = object.getMetaData();
368       for (Iterator JavaDoc i = metaData.getPluginKeySet().iterator();i.hasNext();)
369       {
370          String JavaDoc key = (String JavaDoc)i.next();
371          PluginID id = new PluginID(object.getID(), key);
372          try
373          {
374             kernel.unregister(id);
375          }
376          catch (Exception JavaDoc e)
377          {
378             log.error("Unexpected, Error when removing plugin", e);
379          }
380       }
381    }
382
383    /**
384     * @jmx.managed-operation
385     * @jmx.managed-parameter
386     * name="metaData"
387     * type="org.jboss.portal.server.metadata.ServerObjectMetaData"
388     */

389    public void deploy(ServerObjectMetaData metaData) throws Exception JavaDoc
390    {
391       // Create object
392
ServerObject object = (ServerObject)objectFactory.create(metaData);
393
394       // Register
395
ServerObjectID id = metaData.getID();
396       Set JavaDoc depends = metaData.getDepends();
397       kernel.register(id, object, depends);
398
399       // Create the installed list after we have been capable to register the main service so we are
400
// sure that the deployed map was not containing this service before and we don't overwrite an existing deployment
401
List JavaDoc installed = new ArrayList JavaDoc();
402       installed.add(id);
403       deployed.put(id, installed);
404
405       // Create plugins
406
installPlugins(object, installed);
407
408       // Cascade start
409
kernel.start(id);
410    }
411
412    /**
413     * @jmx.managed-operation
414     * @jmx.managed-parameter
415     * name="id"
416     * type="org.jboss.portal.server.ServerObjectID"
417     */

418    public void undeploy(ServerObjectID id)
419    {
420       List JavaDoc installed = (List JavaDoc)deployed.remove(id);
421       Collections.reverse(installed);
422       for (int i = 0; i < installed.size(); i++)
423       {
424          try
425          {
426             ServiceID serviceID = (ServiceID)installed.get(i);
427             kernel.destroy(serviceID);
428          }
429          catch (Exception JavaDoc e)
430          {
431             log.error("Error when stopping/destroying " + id, e);
432          }
433       }
434       for (int i = 0; i < installed.size(); i++)
435       {
436          try
437          {
438             ServiceID serviceID = (ServiceID)installed.get(i);
439             kernel.unregister(serviceID);
440          }
441          catch (Exception JavaDoc e)
442          {
443             log.error("Error when unregistering " + id, e);
444          }
445       }
446    }
447 }
448
Popular Tags