1 9 package org.jboss.portal.server.deployment; 10 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Set ; 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 37 public class SimpleConfigurator 38 extends Service 39 implements KernelEventListener 40 { 41 42 private ServerManager manager; 43 private Set portals; 44 private Set components; 45 46 public SimpleConfigurator() 47 { 48 portals = new HashSet (); 49 components = new HashSet (); 50 } 51 52 55 public ServerManager getServerManager() 56 { 57 return manager; 58 } 59 60 63 public void setServerManager(ServerManager manager) 64 { 65 this.manager = manager; 66 } 67 68 public void startService() throws Exception 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 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 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 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 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 windowName = getWindowName(portal, component); 140 String 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 e) 148 { 149 log.error("Problem when installing window", e); 150 } 151 } 152 153 private void destroyWindow(Portal portal, Component component) 154 { 155 String 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 instanceName = getInstanceName(component); 165 ServerObjectID componentID = component.getID(); 166 InstanceMetaData instanceMD = new InstanceMetaData(instanceName, componentID); 167 manager.deploy(instanceMD); 168 } 169 catch (Exception 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 instanceName = getInstanceName(component); 179 ServerObjectID instanceID = ServerObjectID.createInstanceID(componentID, instanceName); 180 manager.undeploy(instanceID); 181 } 182 183 186 protected String getInstanceName(Component component) 187 { 188 Application application = component.getApplication(); 189 String applicationName = application.getName(); 190 String componentName = component.getName(); 191 return applicationName + "/" + componentName; 192 } 193 194 197 protected String getWindowName(Portal portal, Component component) 198 { 199 Application application = component.getApplication(); 200 String portalName = portal.getName(); 201 String applicationName = application.getName(); 202 String componentName = component.getName(); 203 return applicationName + "/" + componentName + "/" + portalName; 204 } 205 } 206 | Popular Tags |