KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > deployment > SimpleConfigurator


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.deployment;
10
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import org.jboss.portal.server.Application;
16 import org.jboss.portal.server.Component;
17 import org.jboss.portal.server.Portal;
18 import org.jboss.portal.server.ServerManager;
19 import org.jboss.portal.server.ServerObjectID;
20 import org.jboss.portal.server.kernel.KernelEvent;
21 import org.jboss.portal.server.kernel.KernelEventListener;
22 import org.jboss.portal.server.kernel.state.State;
23 import org.jboss.portal.server.kernel.state.StateChangeEvent;
24 import org.jboss.portal.server.metadata.InstanceMetaData;
25 import org.jboss.portal.server.metadata.WindowMetaData;
26 import org.jboss.portal.server.util.Service;
27
28 /**
29  * @jmx.mbean
30  * @jboss.xmbean
31  *
32  * This configurator create an instance/window of each component in each portal.
33  *
34  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
35  * @version $Revision: 1.3 $
36  */

37 public class SimpleConfigurator
38    extends Service
39    implements KernelEventListener
40 {
41
42    private ServerManager manager;
43    private Set JavaDoc portals;
44    private Set JavaDoc components;
45
46    public SimpleConfigurator()
47    {
48       portals = new HashSet JavaDoc();
49       components = new HashSet JavaDoc();
50    }
51
52    /**
53     * @jmx.managed-attribute
54     */

55    public ServerManager getServerManager()
56    {
57       return manager;
58    }
59
60    /**
61     * @jmx.managed-attribute
62     */

63    public void setServerManager(ServerManager manager)
64    {
65       this.manager = manager;
66    }
67
68    public void startService() throws Exception JavaDoc
69    {
70       manager.getKernel().addListener(this);
71    }
72
73    public void stopService()
74    {
75       manager.getKernel().removeListener(this);
76    }
77
78    public void handleEvent(KernelEvent event)
79    {
80       if (event instanceof StateChangeEvent)
81       {
82          StateChangeEvent revent = (StateChangeEvent)event;
83          org.jboss.portal.server.kernel.Service service = revent.getImplementation().getService();
84          if (revent.getOldState() == State.REGISTERED &&
85              revent.getNewState() == State.STOPPED)
86          {
87             if (service instanceof Portal)
88             {
89                Portal portal = (Portal)service;
90                for (Iterator JavaDoc i = components.iterator();i.hasNext();)
91                {
92                   Component component = (Component)i.next();
93                   createWindow(portal, component);
94                }
95                portals.add(portal);
96             }
97             else if (service instanceof Component)
98             {
99                Component component = (Component)service;
100                createInstance(component);
101                for (Iterator JavaDoc i = portals.iterator();i.hasNext();)
102                {
103                   Portal portal = (Portal)i.next();
104                   createWindow(portal, component);
105                }
106                components.add(component);
107             }
108          }
109          else if (revent.getOldState() == State.STOPPED &&
110                   revent.getNewState() == State.REGISTERED)
111          {
112             if (service instanceof Portal&& portals.remove(service))
113             {
114                Portal portal = (Portal)service;
115                for (Iterator JavaDoc i = components.iterator();i.hasNext();)
116                {
117                   Component component = (Component)i.next();
118                   destroyWindow(portal, component);
119                }
120             }
121             else if (service instanceof Component && components.remove(service))
122             {
123                Component component = (Component)service;
124                for (Iterator JavaDoc i = portals.iterator();i.hasNext();)
125                {
126                   Portal portal = (Portal)i.next();
127                   destroyWindow(portal, component);
128                }
129                destroyInstance(component);
130             }
131          }
132       }
133    }
134
135    private void createWindow(Portal portal, Component component)
136    {
137       try
138       {
139          String JavaDoc windowName = getWindowName(portal, component);
140          String JavaDoc instanceName = getInstanceName(component);
141          ServerObjectID serverID = portal.getID();
142          ServerObjectID componentID = component.getID();
143          ServerObjectID instanceID = ServerObjectID.createInstanceID(componentID, instanceName);
144          WindowMetaData windowMD = new WindowMetaData(windowName, serverID, instanceID);
145          manager.deploy(windowMD);
146       }
147       catch (Exception JavaDoc e)
148       {
149          log.error("Problem when installing window", e);
150       }
151    }
152
153    private void destroyWindow(Portal portal, Component component)
154    {
155       String JavaDoc windowName = getWindowName(portal, component);
156       ServerObjectID windowID = ServerObjectID.createWindowID(portal.getName(), windowName);
157       manager.undeploy(windowID);
158    }
159
160    private void createInstance(Component component)
161    {
162       try
163       {
164          String JavaDoc instanceName = getInstanceName(component);
165          ServerObjectID componentID = component.getID();
166          InstanceMetaData instanceMD = new InstanceMetaData(instanceName, componentID);
167          manager.deploy(instanceMD);
168       }
169       catch (Exception JavaDoc e)
170       {
171          log.error("Problem when installing instance", e);
172       }
173    }
174
175    private void destroyInstance(Component component)
176    {
177       ServerObjectID componentID = component.getID();
178       String JavaDoc instanceName = getInstanceName(component);
179       ServerObjectID instanceID = ServerObjectID.createInstanceID(componentID, instanceName);
180       manager.undeploy(instanceID);
181    }
182
183    /**
184     * Override to provide an alternative naming scheme.
185     */

186    protected String JavaDoc getInstanceName(Component component)
187    {
188       Application application = component.getApplication();
189       String JavaDoc applicationName = application.getName();
190       String JavaDoc componentName = component.getName();
191       return applicationName + "/" + componentName;
192    }
193
194    /**
195     * Override to provide an alternative naming scheme.
196     */

197    protected String JavaDoc getWindowName(Portal portal, Component component)
198    {
199       Application application = component.getApplication();
200       String JavaDoc portalName = portal.getName();
201       String JavaDoc applicationName = application.getName();
202       String JavaDoc componentName = component.getName();
203       return applicationName + "/" + componentName + "/" + portalName;
204    }
205 }
206
Popular Tags