1 9 10 package org.jboss.portal.server.deployment; 11 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import javax.management.Notification ; 21 import javax.management.NotificationListener ; 22 import javax.management.ObjectName ; 23 24 import org.jboss.deployment.DeploymentInfo; 25 import org.jboss.deployment.SubDeployer; 26 import org.jboss.portal.server.util.Service; 27 import org.jboss.web.WebApplication; 28 29 43 public abstract class WebAppIntercepter 44 extends Service 45 implements NotificationListener 46 { 47 48 49 private ObjectName interceptedDeployer; 50 51 52 private ObjectName currentInterceptedDeployer; 53 54 55 private Map deployments; 56 57 58 private PortalWebAppFactory factory; 59 60 63 public WebAppIntercepter() 64 { 65 deployments = Collections.synchronizedMap(new HashMap ()); 66 } 67 68 73 public void setInterceptedDeployer(ObjectName interceptedDeployer) 74 { 75 this.interceptedDeployer = interceptedDeployer; 76 } 77 78 83 public ObjectName getInterceptedDeployer() 84 { 85 return interceptedDeployer; 86 } 87 88 94 public Collection getDeployedURLs() 95 { 96 return new ArrayList (deployments.keySet()); 97 } 98 99 102 public void handleNotification(Notification notification, Object handback) 103 { 104 String type = notification.getType(); 105 boolean start = SubDeployer.START_NOTIFICATION.equals(type); 106 boolean stop = SubDeployer.STOP_NOTIFICATION.equals(type); 107 108 if (start || stop) 110 { 111 ClassLoader previousLoader = Thread.currentThread().getContextClassLoader(); 113 114 try 115 { 116 ClassLoader nextLoader = server.getClassLoaderFor(getServiceName()); 119 Thread.currentThread().setContextClassLoader(nextLoader); 120 121 DeploymentInfo info = (DeploymentInfo)notification.getUserData(); 123 124 if (start) 125 { 126 Iterator iterator = (Iterator )server.getAttribute(interceptedDeployer, "DeployedApplications"); 128 while (iterator.hasNext()) 129 { 130 WebApplication webApp = (WebApplication)iterator.next(); 131 if (info == webApp.getDeploymentInfo()) 132 { 133 PortalWebApp pwa = factory.create(webApp); 134 URL url = info.url; 135 deployments.put(url, pwa); 136 log.debug("Seen URL " + url + " about to deploy"); 137 deploy(pwa); 138 } 139 } 140 } 141 if (stop) 142 { 143 PortalWebApp pwa = (PortalWebApp)deployments.remove(info.url); 145 146 if (pwa != null) 148 { 149 undeploy(pwa); 150 } 151 } 152 } 153 catch(Exception e) 154 { 155 log.error("Cannot handle the intercepted deployment", e); 156 } 157 finally 158 { 159 Thread.currentThread().setContextClassLoader(previousLoader); 161 } 162 } 163 } 164 165 168 protected void startService() throws Exception 169 { 170 if (interceptedDeployer != null) 171 { 172 factory = new PortalWebAppFactory(server); 174 175 currentInterceptedDeployer = interceptedDeployer; 177 178 log.debug("Start listening notifications from intercepted deployer" + currentInterceptedDeployer); 180 server.addNotificationListener(currentInterceptedDeployer, getServiceName(), null, null); 181 182 Iterator iterator = (Iterator )server.getAttribute(currentInterceptedDeployer, "DeployedApplications"); 184 log.debug("Scan previously deployed web applications"); 185 while (iterator.hasNext()) 186 { 187 WebApplication webApp = (WebApplication)iterator.next(); 188 if (!deployments.containsKey(webApp.getURL())) 189 { 190 PortalWebApp pwa = factory.create(webApp); 191 deployments.put(pwa.getURL(), pwa); 192 log.debug("Seen URL " + pwa.getURL() + " about to deploy"); 193 deploy(pwa); 194 } 195 } 196 197 } 198 else 199 { 200 throw new Exception ("No intercepted deployer name present"); 201 } 202 } 203 204 207 protected void stopService() throws Exception 208 { 209 if (currentInterceptedDeployer != null) 210 { 211 for (Iterator i = deployments.values().iterator();i.hasNext();) 213 { 214 PortalWebApp pwa = (PortalWebApp)i.next(); 215 i.remove(); 216 log.debug("Removing URL " + pwa.getURL()); 217 undeploy(pwa); 218 } 219 220 log.debug("Stop listening notifications from intercepted deployer" + currentInterceptedDeployer); 222 server.removeNotificationListener(currentInterceptedDeployer, getServiceName()); 223 224 factory = null; 226 227 currentInterceptedDeployer = null; 229 } 230 } 231 232 235 protected abstract void deploy(PortalWebApp pwa); 236 237 240 protected abstract void undeploy(PortalWebApp pwa); 241 } 242 | Popular Tags |