1 9 package org.jboss.portal.server.impl; 10 11 import java.io.InputStream ; 12 import java.lang.reflect.Constructor ; 13 import java.util.Collections ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.NodeList ; 51 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 52 53 60 public class ServerManagerImpl extends Service implements ServerManager 61 { 62 63 64 private String containerName; 65 66 67 private BootStrapContext bootStrap; 68 69 70 private PortalServer portalServer; 71 72 private ThemeServer themeServer; 73 74 private LayoutServer layoutServer; 75 76 77 private ObjectFactory objectFactory; 78 79 80 private InvocationFactory invocationFactory; 81 82 83 private Kernel kernel; 84 85 86 private Configuration configuration; 87 88 89 private String invocationFactoryClassName; 90 91 92 private Map deployed; 93 94 public ServerManagerImpl() 95 { 96 this.kernel = new Kernel(); 97 this.deployed = new ConcurrentHashMap(); 98 } 99 100 103 public static void loadInterceptorSetMetaData(InterceptorListMetaData interceptorListMD, Element interceptorsElt) 104 { 105 NodeList interceptorElts = interceptorsElt.getElementsByTagName("interceptor"); 106 for (int i = 0;i < interceptorElts.getLength();i++) 107 { 108 Element interceptorElt = (Element )interceptorElts.item(i); 109 Element interceptorClassElt = XML.getUniqueChild( 110 interceptorElt, 111 "interceptor-class", 112 true); 113 String className = XML.asString(interceptorClassElt); 114 InterceptorMetaData interceptorMD = new InterceptorMetaData(className); 115 NodeList interceptorParamElts = interceptorElt.getElementsByTagName("interceptor-param"); 116 for (int j = 0;j < interceptorParamElts.getLength();j++) 117 { 118 Element interceptorParamElt = (Element )interceptorParamElts.item(j); 119 String paramName = XML.asString(XML.getUniqueChild( 120 interceptorParamElt, 121 "param-name", 122 true)); 123 String 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 134 { 135 Map componentConfigurationsMD = null; 136 137 InputStream in = null; 138 InterceptorListMetaData ilm = null; 139 try 140 { 141 in = configuration.load("/jboss-container.xml"); 142 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 143 Document doc = builder.parse(in); 144 145 Element rootElt = doc.getDocumentElement(); 146 Element containerConfigurationElt = XML.getUniqueChild(rootElt, "container-configuration", true); 147 148 Element 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 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 160 Class invocationFactoryClass = classLoader.loadClass(invocationFactoryClassName); 161 Constructor ctor = invocationFactoryClass.getConstructor(new Class []{ServerManager.class}); 162 invocationFactory = (InvocationFactory)ctor.newInstance(new Object []{this}); 163 164 PortalServerMetaData portalObjectContainerMD = new PortalServerMetaData(containerName); 167 portalObjectContainerMD.setInterceptorList(ilm); 168 portalObjectContainerMD.setComponentConfigurations(componentConfigurationsMD); 169 170 portalServer = (PortalServer)objectFactory.create(portalObjectContainerMD); 172 portalServer.setManager(this); 173 174 kernel.register(portalObjectContainerMD.getID(), portalServer); 176 177 installPlugins(portalServer, new HashSet ()); 179 180 kernel.start(portalObjectContainerMD.getID()); 182 183 log.info("Started " + PortalConstants.VERSION); 184 } 185 186 protected void stopService() throws Exception 187 { 188 ServerObjectID containerID = portalServer.getID(); 190 kernel.destroy(containerID); 191 192 uninstallPlugins(portalServer); 194 195 kernel.unregister(containerID); 197 } 198 199 202 public String getInvocationFactoryClassName() 203 { 204 return invocationFactoryClassName; 205 } 206 207 210 public void setInvocationFactoryClassName(String invocationFactoryClassName) 211 { 212 this.invocationFactoryClassName = invocationFactoryClassName; 213 } 214 215 218 public String getContainerName() 219 { 220 return containerName; 221 } 222 223 226 public InvocationFactory getInvocationContext() 227 { 228 return invocationFactory; 229 } 230 231 234 public void setInvocationContext(InvocationFactory invocationFactory) 235 { 236 this.invocationFactory = invocationFactory; 237 } 238 239 242 public void setContainerName(String containerName) 243 { 244 this.containerName = containerName; 245 } 246 247 250 public ObjectFactory getObjectFactory() 251 { 252 return objectFactory; 253 } 254 255 258 public void setObjectFactory(ObjectFactory objectFactory) 259 { 260 this.objectFactory = objectFactory; 261 } 262 263 266 public BootStrapContext getBootStrap() 267 { 268 return bootStrap; 269 } 270 271 275 public void setBootStrap(BootStrapContext bootStrap) 276 { 277 this.bootStrap = bootStrap; 278 } 279 280 283 public Configuration getConfiguration() 284 { 285 return configuration; 286 } 287 288 291 public void setConfiguration(Configuration configuration) 292 { 293 this.configuration = configuration; 294 } 295 296 299 public Kernel getKernel() 300 { 301 return kernel; 302 } 303 304 307 public PortalServer getPortalServer() 308 { 309 return portalServer; 310 } 311 312 315 public ThemeServer getThemeServer() 316 { 317 return themeServer; 318 } 319 320 323 public void setThemeServer(ThemeServer server) 324 { 325 themeServer = server; 326 } 327 328 331 public LayoutServer getLayoutServer() 332 { 333 return layoutServer; 334 } 335 336 339 public void setLayoutServer(LayoutServer server) 340 { 341 layoutServer = server; 342 } 343 344 private void installPlugins(ServerObject object, Collection installed) throws Exception 345 { 346 ServerObjectMetaData metaData = object.getMetaData(); 347 log.debug("Installing plugins for " + metaData.getID()); 348 for (Iterator i = metaData.getPluginKeySet().iterator();i.hasNext();) 349 { 350 String key = (String )i.next(); 351 PluginMetaData pluginMD = metaData.getPlugin(key); 352 log.debug("Plugin found " + key); 353 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 i = metaData.getPluginKeySet().iterator();i.hasNext();) 369 { 370 String key = (String )i.next(); 371 PluginID id = new PluginID(object.getID(), key); 372 try 373 { 374 kernel.unregister(id); 375 } 376 catch (Exception e) 377 { 378 log.error("Unexpected, Error when removing plugin", e); 379 } 380 } 381 } 382 383 389 public void deploy(ServerObjectMetaData metaData) throws Exception 390 { 391 ServerObject object = (ServerObject)objectFactory.create(metaData); 393 394 ServerObjectID id = metaData.getID(); 396 Set depends = metaData.getDepends(); 397 kernel.register(id, object, depends); 398 399 List installed = new ArrayList (); 402 installed.add(id); 403 deployed.put(id, installed); 404 405 installPlugins(object, installed); 407 408 kernel.start(id); 410 } 411 412 418 public void undeploy(ServerObjectID id) 419 { 420 List installed = (List )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 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 e) 442 { 443 log.error("Error when unregistering " + id, e); 444 } 445 } 446 } 447 } 448 | Popular Tags |