KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > PortalServer


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;
10
11 import java.beans.PropertyEditorManager JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.servlet.ServletContext JavaDoc;
18
19 import org.jboss.portal.server.invocation.Interceptor;
20 import org.jboss.portal.server.invocation.Invocation;
21 import org.jboss.portal.server.kernel.Kernel;
22 import org.jboss.portal.server.kernel.KernelException;
23 import org.jboss.portal.server.kernel.Service;
24 import org.jboss.portal.server.kernel.ServiceImplementation;
25 import org.jboss.portal.server.kernel.state.State;
26 import org.jboss.portal.server.metadata.PortalServerMetaData;
27 import org.jboss.portal.server.metadata.ServerObjectMetaData;
28 import org.jboss.portal.server.plugins.invocation.InvocationPlugin;
29 import org.jboss.portal.server.util.ServerObjectIDEditor;
30
31 /**
32  * A container for portal objects.
33  *
34  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
35  * @version $Revision: 1.5 $
36  */

37 public class PortalServer extends ServerObject
38 {
39
40    private Map JavaDoc portals;
41    private Map JavaDoc applications;
42    private ServerManager manager;
43    private PortalServerMetaData metaData;
44
45    public PortalServer(PortalServerMetaData metaData)
46    {
47       super(metaData.getName());
48       this.metaData = metaData;
49
50       this.portals = Collections.synchronizedMap(new HashMap JavaDoc());
51       this.applications = Collections.synchronizedMap(new HashMap JavaDoc());
52    }
53
54    public void create(ServerObjectID id)
55    {
56       try
57       {
58          manager.getKernel().create(id);
59       }
60       catch (KernelException e)
61       {
62          e.printStackTrace();
63       }
64    }
65
66    public void start(ServerObjectID id)
67    {
68       try
69       {
70          manager.getKernel().start(id);
71       }
72       catch (KernelException e)
73       {
74          e.printStackTrace();
75       }
76    }
77
78    public void stop(ServerObjectID id)
79    {
80       try
81       {
82          manager.getKernel().stop(id);
83       }
84       catch (KernelException e)
85       {
86          e.printStackTrace();
87       }
88    }
89
90    public void destroy(ServerObjectID id)
91    {
92       try
93       {
94          manager.getKernel().destroy(id);
95       }
96       catch (KernelException e)
97       {
98          e.printStackTrace();
99       }
100    }
101
102    /**
103     * Locate an object
104     */

105    public ServerObject getObject(ServerObjectID id)
106    {
107       return (ServerObject)manager.getKernel().getService(id);
108    }
109
110    public ServerManager getManager()
111    {
112       return manager;
113    }
114
115    public void setManager(ServerManager manager)
116    {
117       this.manager = manager;
118    }
119
120    public PortalServer getServer()
121    {
122       return this;
123    }
124
125    public ServerObjectMetaData getMetaData()
126    {
127       return metaData;
128    }
129
130    public Collection JavaDoc getPortals()
131    {
132       return portals.values();
133    }
134
135    public Portal getPortal(String JavaDoc name)
136    {
137       return (Portal)portals.get(name);
138    }
139
140    public Portal getDefaultPortal()
141    {
142       return getPortal(PortalConstants.DEFAULT_PORTAL_NAME);
143    }
144
145    public Application getApplication(String JavaDoc name)
146    {
147       return (Application)applications.get(name);
148    }
149
150    public Collection JavaDoc getApplications()
151    {
152       return applications.values();
153    }
154
155    public void create() throws Exception JavaDoc
156    {
157       PropertyEditorManager.registerEditor(ServerObjectID.class, ServerObjectIDEditor.class);
158    }
159
160    public void destroy()
161    {
162       PropertyEditorManager.registerEditor(ServerObjectID.class, null);
163    }
164
165    public boolean isStarted(ServerObject object)
166    {
167       if (object != null)
168       {
169          Kernel kernel = manager.getKernel();
170          State state = kernel.getState(object.getID());
171          return state == State.STARTED;
172       }
173       else
174       {
175          return false;
176       }
177    }
178
179    /**
180     * Invoke on this component.
181     */

182    public Object JavaDoc invoke(Invocation invocation)
183    {
184       InvocationPlugin ip = (InvocationPlugin)getPlugin(PortalConstants.PLUGIN_INVOCATION);
185       Interceptor[] stack = ip.getStack();
186       return invocation.invokeNext(stack);
187    }
188
189    public void addDependsOnMe(ServiceImplementation implementation)
190    {
191       Service service = implementation.getService();
192       if (service instanceof Portal)
193       {
194          // Get portal name
195
Portal portal = (Portal)service;
196          String JavaDoc name = portal.getName();
197
198          // Keep the reference
199
portals.put(portal.getName(), portal);
200          log.info("Added portal " + name);
201       }
202       else if (service instanceof Application)
203       {
204          // Get application name
205
Application application = (Application)service;
206          String JavaDoc name = application.getName();
207
208          // Keep the reference
209
applications.put(name, application);
210          log.info("Added application " + name);
211       }
212       else
213       {
214          super.addDependsOnMe(implementation);
215       }
216    }
217
218    public void removeDependsOnMe(ServiceImplementation implementation)
219    {
220       Service service = implementation.getService();
221       if (service instanceof Portal)
222       {
223          // Get portal name
224
Portal portal = (Portal)service;
225          String JavaDoc name = portal.getName();
226
227          // Remove reference
228
portals.remove(name);
229          log.info("Removed portal " + name);
230       }
231       else if (service instanceof Application)
232       {
233          // Get application name
234
Application application = (Application)service;
235          String JavaDoc name = application.getName();
236
237          // Remove reference
238
applications.remove(name);
239          log.info("Removed application " + name);
240       }
241       else
242       {
243          super.removeDependsOnMe(implementation);
244       }
245    }
246 }
247
Popular Tags