1 9 package org.jboss.portal.server.deployment.jboss; 10 11 import org.jboss.deployment.SubDeployerSupport; 12 import org.jboss.deployment.DeploymentInfo; 13 import org.jboss.deployment.DeploymentException; 14 import org.jboss.portal.server.ServerManager; 15 import org.jboss.portal.server.deployment.PortalWebApp; 16 import org.jboss.portal.common.util.XML; 17 import org.jboss.portal.common.util.Tools; 18 import org.jboss.mx.loading.RepositoryClassLoader; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Document ; 21 import org.xml.sax.SAXException ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.util.regex.Pattern ; 30 import java.util.regex.Matcher ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 34 38 public class ServerDeployer extends SubDeployerSupport implements ServerDeployerMBean 39 { 40 41 42 private ServerManager serverManager; 43 44 45 private Element factoryConfig; 46 47 48 private List factoryEntries; 49 50 public ServerDeployer() 51 { 52 factoryEntries = new ArrayList (); 53 } 54 55 58 public boolean accepts(DeploymentInfo di) 59 { 60 return false; 61 } 62 63 public ServerManager getServetManager() 64 { 65 return serverManager; 66 } 67 68 public void setServerManager(ServerManager serverManager) 69 { 70 this.serverManager = serverManager; 71 } 72 73 public ServerDeployer getDeployer() 74 { 75 return this; 76 } 77 78 public Element getFactoryConfig() 79 { 80 return factoryConfig; 81 } 82 83 public void setFactoryConfig(Element factoryConfig) 84 { 85 this.factoryConfig = factoryConfig; 86 } 87 88 protected void createService() throws Exception 89 { 90 List factoryElts = XML.getChildren(factoryConfig, "factory"); 91 for (int i = 0;i < factoryElts.size();i++) 92 { 93 Element factoryElt = (Element )factoryElts.get(i); 94 Element urlPatternElt = (Element )XML.getUniqueChild(factoryElt, "url-pattern", true); 95 Element factoryClassElt = (Element )XML.getUniqueChild(factoryElt, "factory-class", true); 96 String urlPatternAsString = XML.asString(urlPatternElt); 97 String factoryClassAsString = XML.asString(factoryClassElt); 98 Pattern urlPattern = Pattern.compile(urlPatternAsString); 99 Class factoryClass = Thread.currentThread().getContextClassLoader().loadClass(factoryClassAsString); 100 DeploymentFactory factory = (DeploymentFactory)factoryClass.newInstance(); 101 FactoryEntry entry = new FactoryEntry(urlPattern, factory); 102 factoryEntries.add(entry); 103 } 104 super.createService(); 105 } 106 107 protected void destroyService() throws Exception 108 { 109 super.destroyService(); 110 factoryEntries.clear(); 111 } 112 113 protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException 116 { 117 if (di.isDirectory) 119 { 120 String s = di.url.getFile(); 121 File dir = new File (s); 122 File [] children = dir.listFiles(); 123 for (int i = 0; i < children.length; i++) 124 { 125 try 126 { 127 File child = children[i]; 128 URL url = child.toURL(); 129 DeploymentFactory factory = findFactory(url); 130 if (factory != null) 131 { 132 Deployment blah = (Deployment)di.metaData; 133 Deployment sdi = factory.newInstance(blah.getServerManager(), url, blah.pwa, blah.appName); 134 DeploymentInfo sub = new DeploymentInfo(url, di, server); 135 sub.metaData = sdi; 136 sub.deployer = this; 137 } 138 } 139 catch (MalformedURLException e) 140 { 141 log.error("Unexpected exception when converting file to URL", e); 142 } 143 } 144 } 145 } 146 147 public DeploymentFactory findFactory(URL url) 148 { 149 String urlAsFile = url.getFile(); 150 151 for (int j = 0; j < factoryEntries.size(); j++) 153 { 154 FactoryEntry entry = (FactoryEntry)factoryEntries.get(j); 155 Matcher matcher = entry.urlPattern.matcher(urlAsFile); 156 if (matcher.matches()) 157 { 158 return entry.factory; 159 } 160 } 161 162 return null; 164 } 165 166 public void create(DeploymentInfo di) throws DeploymentException 167 { 168 Deployment deployment = (Deployment)di.metaData; 169 if (deployment != null) 170 { 171 deployment.create(); 172 } 173 super.create(di); 174 } 175 176 public void start(DeploymentInfo di) throws DeploymentException 177 { 178 Deployment deployment = (Deployment)di.metaData; 179 if (deployment != null) 180 { 181 deployment.start(); 182 } 183 super.start(di); 184 } 185 186 public void stop(DeploymentInfo di) throws DeploymentException 187 { 188 Deployment deployment = (Deployment)di.metaData; 189 if (deployment != null) 190 { 191 deployment.stop(); 192 } 193 super.stop(di); 194 } 195 196 public void destroy(DeploymentInfo di) throws DeploymentException 197 { 198 Deployment deployment = (Deployment)di.metaData; 199 if (deployment != null) 200 { 201 deployment.destroy(); 202 } 203 super.destroy(di); 204 } 205 206 public void deploy(PortalWebApp pwa) throws DeploymentException 207 { 208 String appName = pwa.getContextPath(); 210 211 InputStream in = null; 213 try 214 { 215 in = pwa.getServletContext().getResourceAsStream("/WEB-INF/jboss-app.xml"); 216 if (in != null) 217 { 218 Document doc = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(in); 219 Element jbossAppElt = doc.getDocumentElement(); 220 Element appNameElt = XML.getUniqueChild(jbossAppElt, "app-name", false); 221 if (appNameElt != null) 222 { 223 appName = XML.asString(appNameElt); 224 log.debug("Detected explicit app name = " + appName + " for application under path " + pwa.getContextPath()); 225 } 226 } 227 } 228 catch (IOException e) 229 { 230 log.debug("Cannot read jboss-app.xml", e); 231 } 232 catch (ParserConfigurationException e) 233 { 234 log.debug("Cannot read jboss-app.xml", e); 235 } 236 catch (SAXException e) 237 { 238 log.debug("Cannot read jboss-app.xml", e); 239 } 240 finally 241 { 242 Tools.safeClose(in); 243 } 244 245 RepositoryClassLoader rcl = Deployment.findRepositoryClassLoader(pwa.getClassLoader()); 247 URL url = Deployment.findWEBINFURL(pwa.getURL()); 248 Deployment pwadi = new Deployment(serverManager, url, pwa, appName); 249 250 DeploymentInfo di = new DeploymentInfo(url, null, server); 252 di.ucl = rcl; 253 di.metaData = pwadi; 254 di.deployer = this; 255 mainDeployer.deploy(di); 256 } 257 258 public void undeploy(PortalWebApp pwa) throws DeploymentException 259 { 260 try 261 { 262 URL url = Deployment.findWEBINFURL(pwa.getURL()); 263 mainDeployer.undeploy(url); 264 } 265 catch (Exception e) 266 { 267 log.error("Error when undeploying portal web app", e); 268 } 269 } 270 271 274 private static class FactoryEntry 275 { 276 private Pattern urlPattern; 277 private DeploymentFactory factory; 278 public FactoryEntry(Pattern urlPattern, DeploymentFactory factory) 279 { 280 this.urlPattern = urlPattern; 281 this.factory = factory; 282 } 283 } 284 285 } 286 | Popular Tags |