1 22 package org.jboss.deployment; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.Serializable ; 27 import java.net.URL ; 28 import java.net.URLClassLoader ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Set ; 36 import java.util.jar.JarFile ; 37 import java.util.jar.Manifest ; 38 39 import javax.management.MBeanServer ; 40 import javax.management.ObjectName ; 41 42 import org.jboss.logging.Logger; 43 import org.jboss.mx.loading.LoaderRepositoryFactory; 44 import org.jboss.mx.loading.RepositoryClassLoader; 45 import org.jboss.mx.loading.LoaderRepositoryFactory.LoaderRepositoryConfig; 46 import org.jboss.util.collection.ListSet; 47 import org.jboss.util.file.Files; 48 import org.w3c.dom.Document ; 49 50 66 public class DeploymentInfo implements Serializable 67 { 68 69 private static final long serialVersionUID = 1131841473723490707L; 70 71 private static final Logger log = Logger.getLogger(DeploymentInfo.class); 72 73 75 76 public Date date = new Date (); 77 78 79 public URL url; 80 81 82 public URL localUrl; 83 84 85 public URL watch; 86 87 88 public String shortName; 89 90 91 public long lastDeployed = 0; 92 93 94 public long lastModified = 0; 95 96 97 public String status; 98 99 100 public DeploymentState state = DeploymentState.CONSTRUCTED; 101 102 103 public transient SubDeployer deployer; 104 105 106 public transient RepositoryClassLoader ucl; 107 108 113 public transient URLClassLoader localCl; 114 115 118 public transient URLClassLoader annotationsCl; 119 120 121 public final Collection classpath = new ArrayList (); 122 123 124 public final List <ObjectName > mbeans = new ArrayList <ObjectName >(); 125 126 127 public final Set subDeployments = new ListSet(); 128 129 130 public DeploymentInfo parent = null; 131 132 133 public String webContext; 134 135 139 public transient Manifest manifest; 140 141 146 public Document document; 147 148 149 public URL documentUrl; 150 151 152 public transient Object metaData; 153 154 155 public String alternativeDD; 156 157 158 public transient HashMap context = new HashMap (); 159 160 161 public boolean isXML; 162 163 164 public boolean isScript; 165 166 167 public boolean isDirectory; 168 169 170 public boolean sortedSubDeployments = false ; 171 176 public ObjectName deployedObject; 177 178 179 public LoaderRepositoryConfig repositoryConfig; 180 181 182 private transient MBeanServer server; 183 184 187 public DeploymentInfo(final URL url, final DeploymentInfo parent, final MBeanServer server) 188 throws DeploymentException 189 { 190 this.server = server; 191 this.url = url; 193 194 this.watch = url; 196 197 this.parent = parent; 199 200 if (url.getProtocol().startsWith("file") && new File (url.getFile()).isDirectory()) 202 this.isDirectory = true; 203 204 if (!isDirectory) 206 { 207 try 208 { 209 url.openStream().close(); 210 } 211 catch (Exception e) 212 { 213 throw new DeploymentException("url " + url + " could not be opened, does it exist?"); 214 } 215 } 216 217 if (parent != null) 218 { 219 parent.subDeployments.add(this); 220 repositoryConfig = getTopRepositoryConfig(); 222 } 223 224 shortName = getShortName(url.getPath()); 226 227 isXML = shortName.toLowerCase().endsWith(".xml"); 229 230 isScript = shortName.toLowerCase().endsWith(".bsh"); 232 } 233 234 public MBeanServer getServer() 235 { 236 return server; 237 } 238 239 public void setServer(MBeanServer server) 240 { 241 this.server = server; 242 } 243 244 249 public void createClassLoaders() throws Exception 250 { 251 if( localCl == null ) 253 localCl = new URLClassLoader (new URL [] {localUrl}); 254 255 260 URL origUrl = url; 261 DeploymentInfo current = this; 262 while (current.parent != null) 263 { 264 current = current.parent; 265 } 266 origUrl = current.url; 267 repositoryConfig = current.repositoryConfig; 268 if( parent == null ) 269 { 270 if( repositoryConfig == null ) 271 repositoryConfig = new LoaderRepositoryConfig(); 272 LoaderRepositoryFactory.createLoaderRepository(server, repositoryConfig); 274 log.debug("createLoaderRepository from config: "+repositoryConfig); 275 277 Object [] args = { isXML ? null : localUrl, origUrl, Boolean.TRUE }; 278 String [] sig = { "java.net.URL", "java.net.URL", "boolean" }; 279 ucl = (RepositoryClassLoader) server.invoke(repositoryConfig.repositoryName, 280 "newClassLoader",args, sig); 281 } 282 else 283 { 284 LoaderRepositoryFactory.createLoaderRepository(server, repositoryConfig); 286 ucl = parent.ucl; 288 ucl.addURL(localUrl); 289 } 290 if( classpath.size() > 0 ) 292 { 293 Iterator jars = classpath.iterator(); 294 while( jars.hasNext() ) 295 { 296 URL jar = (URL ) jars.next(); 297 ucl.addURL(jar); 298 } 299 } 300 } 301 302 307 public void setRepositoryInfo(LoaderRepositoryConfig config) 308 throws Exception 309 { 310 if( parent != null ) 311 { 312 log.warn("Only the root deployment can set the loader repository, " 313 + "ignoring config="+config); 314 return; 315 } 316 317 this.repositoryConfig = config; 318 if( ucl != null ) 320 { 321 ucl.unregister(); 322 LoaderRepositoryFactory.createLoaderRepository(server, repositoryConfig); 324 log.debug("createLoaderRepository from config: "+repositoryConfig); 325 327 Object [] args = { isXML ? null : localUrl, url, Boolean.TRUE }; 328 String [] sig = { "java.net.URL", "java.net.URL", "boolean" }; 329 ucl = (RepositoryClassLoader) server.invoke(repositoryConfig.repositoryName, 330 "newClassLoader",args, sig); 331 } 332 } 333 334 339 public void addLibraryJar(URL libJar) 340 { 341 DeploymentInfo current = this; 342 while (current.parent != null) 343 { 344 current = current.parent; 345 } 346 349 if( current.ucl != null ) 350 current.ucl.addURL(libJar); 351 else 352 classpath.add(libJar); 353 } 354 355 357 public LoaderRepositoryConfig getTopRepositoryConfig() 358 { 359 LoaderRepositoryConfig topConfig = repositoryConfig; 360 DeploymentInfo info = this; 361 while( info.parent != null ) 362 { 363 info = info.parent; 364 topConfig = info.repositoryConfig; 365 } 366 return topConfig; 367 } 368 369 373 public Manifest getManifest() 374 { 375 try 376 { 377 if (manifest == null) 378 { 379 File file = new File (localUrl.getFile()); 380 381 if (file.isDirectory()) 382 { 383 FileInputStream fis = new FileInputStream (new File (file, "META-INF/MANIFEST.MF")); 384 manifest = new Manifest (fis); 385 fis.close(); 386 } 387 else if(isXML == false) manifest = new JarFile (file).getManifest(); 389 } 390 391 return manifest; 392 } 393 catch (Exception ignored) { return null;} 395 } 396 397 public void cleanup() 398 { 399 if( parent == null && ucl != null ) 401 ucl.unregister(); 402 ucl = null; 403 404 if ( repositoryConfig != null ) 406 { 407 LoaderRepositoryFactory.destroyLoaderRepository(server, 408 repositoryConfig.repositoryName); 409 } 410 411 subDeployments.clear(); 412 mbeans.clear(); 413 context.clear(); 414 if (localUrl == null || localUrl.equals(url)) 415 { 416 log.debug("Not deleting localUrl, it is null or not a copy: " + localUrl); 417 } 418 else if (Files.delete(localUrl.getFile())) 419 { 420 log.debug("Cleaned Deployment: " + localUrl); 421 } 422 else 423 { 424 log.debug("Could not delete " + localUrl + " restart will delete it"); 425 } 426 427 localCl = null; 429 annotationsCl = null; 430 localUrl = null; 431 repositoryConfig = null; 432 watch = null; 433 parent = null; 434 manifest = null; 435 document = null; 436 metaData = null; 437 server = null; 438 classpath.clear(); 439 state = DeploymentState.DESTROYED; 440 } 443 444 445 public String getCanonicalName() 446 { 447 String name = shortName; 448 if (parent != null) 449 name = parent.getCanonicalName() + "/" + name; 450 return name; 451 } 452 453 private String getShortName(String name) 454 { 455 if (name.endsWith("/")) name = name.substring(0, name.length() - 1); 456 name = name.substring(name.lastIndexOf("/") + 1); 457 return name; 458 } 459 460 public int hashCode() 461 { 462 return url.hashCode(); 463 } 464 465 public boolean equals(Object other) 466 { 467 if (other instanceof DeploymentInfo) 468 { 469 return ((DeploymentInfo) other).url.equals(this.url); 470 } 471 return false; 472 } 473 474 public String toString() 475 { 476 StringBuffer s = new StringBuffer (super.toString()); 477 s.append(" { url=" + url + " }\n"); 478 s.append(" deployer: " + deployer + "\n"); 479 s.append(" status: " + status + "\n"); 480 s.append(" state: " + state + "\n"); 481 s.append(" watch: " + watch + "\n"); 482 s.append(" altDD: " + alternativeDD + "\n"); 483 s.append(" lastDeployed: " + lastDeployed + "\n"); 484 s.append(" lastModified: " + lastModified + "\n"); 485 s.append(" mbeans:\n"); 486 for (Iterator i = mbeans.iterator(); i.hasNext(); ) 487 { 488 ObjectName o = (ObjectName )i.next(); 489 try 490 { 491 String state = (String )server.getAttribute(o, "StateString"); 492 s.append(" " + o + " state: " + state + "\n"); 493 } 494 catch (Exception e) 495 { 496 s.append(" " + o + " (state not available)\n"); 497 } 499 } 501 return s.toString(); 502 } 503 } 504 | Popular Tags |