1 22 package org.jboss.ejb3.embedded; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.JarURLConnection ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.net.URLConnection ; 31 import java.net.MalformedURLException ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.HashSet ; 35 import java.util.Hashtable ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 import java.util.Set ; 40 import java.util.zip.ZipFile ; 41 import javax.management.MBeanServer ; 42 import javax.management.MBeanServerFactory ; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingEnumeration ; 45 46 import org.jboss.dependency.spi.ControllerContext; 47 import org.jboss.ejb3.DeploymentUnit; 48 import org.jboss.ejb3.interceptor.InterceptorInfoRepository; 49 import org.jboss.kernel.Kernel; 50 import org.jboss.logging.Logger; 51 import org.jboss.virtual.VFS; 52 import org.jboss.virtual.VirtualFile; 53 import org.jboss.virtual.VirtualFileFilter; 54 import org.jboss.virtual.VisitorAttributes; 55 import org.jboss.virtual.plugins.context.jar.JarUtils; 56 import org.jboss.virtual.plugins.vfs.helpers.FilterVirtualFileVisitor; 57 import org.jboss.virtual.plugins.vfs.helpers.SuffixesExcludeFilter; 58 59 68 public class EJB3StandaloneDeployer 69 { 70 private static class DeployerUnit implements DeploymentUnit 71 { 72 private URL url; 73 private ClassLoader resourceLoader; 74 private ClassLoader loader; 75 private Map defaultProps; 76 private Hashtable jndiProperties; 77 private InterceptorInfoRepository interceptorInfoRepository = new InterceptorInfoRepository(); 78 private VirtualFile virtualFile; 79 80 public DeployerUnit(ClassLoader loader, URL url, Map defaultProps, Hashtable jndiProperties) 81 { 82 this.loader = loader; 83 this.url = url; 84 URL [] urls = {url}; 85 URL [] empty = {}; 86 URLClassLoader parent = new URLClassLoader (empty) 87 { 88 @Override 89 public URL getResource(String name) 90 { 91 return null; 92 } 93 }; 94 resourceLoader = new URLClassLoader (urls, parent); 95 this.defaultProps = defaultProps; 96 this.jndiProperties = jndiProperties; 97 try 98 { 99 VFS vfs = VFS.getVFS(url); 100 virtualFile = vfs.getRoot(); 101 } 102 catch (IOException e) 103 { 104 throw new RuntimeException (); 105 } 106 } 107 108 public URL getRelativeURL(String jar) 109 { 110 URL url = null; 111 try 112 { 113 url = new URL (jar); 114 } 115 catch (MalformedURLException e) 116 { 117 try 118 { 119 if (jar.startsWith("..")) 120 { 121 if (getUrl() == null) 122 throw new RuntimeException ("relative <jar-file> not allowed when standalone deployment unit is used"); 123 String base = getUrl().toString(); 124 jar = jar.replaceAll("\\.\\./", "+"); 125 int idx = jar.lastIndexOf('+'); 126 jar = jar.substring(idx + 1); 127 for (int i = 0; i < idx + 1; i++) 128 { 129 int slash = base.lastIndexOf('/'); 130 base = base.substring(0, slash + 1); 131 } 132 url = new URL (base + jar.substring(idx)); 133 } 134 else 135 { 136 File fp = new File (jar); 137 url = fp.toURL(); 138 } 139 } 140 catch (MalformedURLException e1) 141 { 142 throw new RuntimeException ("Unable to find relative url: " + jar, e1); 143 } 144 } 145 return url; 146 } 147 148 149 public List <VirtualFile> getResources(VirtualFileFilter filter) 150 { 151 VisitorAttributes va = new VisitorAttributes(); 152 va.setLeavesOnly(true); 153 SuffixesExcludeFilter noJars = new SuffixesExcludeFilter(JarUtils.getSuffixes()); 154 va.setRecurseFilter(noJars); 155 FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, va); 156 try 157 { 158 virtualFile.visit(visitor); 159 } 160 catch (IOException e) 161 { 162 throw new RuntimeException (e); 163 } 164 return visitor.getMatched(); 165 166 } 167 168 public Hashtable getJndiProperties() 169 { 170 return jndiProperties; 171 } 172 173 public URL getPersistenceXml() 174 { 175 return getResourceLoader().getResource("META-INF/persistence.xml"); 176 } 177 178 public URL getEjbJarXml() 179 { 180 return getResourceLoader().getResource("META-INF/ejb-jar.xml"); 181 } 182 183 public URL getJbossXml() 184 { 185 return getResourceLoader().getResource("META-INF/jboss.xml"); 186 } 187 188 public List <Class > getClasses() 189 { 190 return null; 191 } 192 193 public ClassLoader getClassLoader() 194 { 195 return loader; 196 } 197 198 public ClassLoader getResourceLoader() 199 { 200 return resourceLoader; 201 } 202 203 public String getShortName() 204 { 205 String url = getUrl().toString(); 206 if (url.endsWith("/")) url = url.substring(0, url.length() - 1); 207 208 int dotIdx = url.lastIndexOf('.'); 209 int slashIdx = url.lastIndexOf('/'); 210 String name = null; 211 if (slashIdx > dotIdx) 212 { 213 name = url.substring(url.lastIndexOf('/') + 1); 214 } 215 else 216 { 217 name = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.')); 218 } 219 return name; 220 } 221 222 public URL getUrl() 223 { 224 return url; 225 } 226 227 public String getDefaultEntityManagerName() 228 { 229 return getShortName(); 230 } 231 232 public Map getDefaultPersistenceProperties() 233 { 234 return defaultProps; 235 } 236 237 public InterceptorInfoRepository getInterceptorInfoRepository() 238 { 239 return interceptorInfoRepository; 240 } 241 } 242 243 protected static final Logger log = Logger.getLogger(EJB3StandaloneDeployer.class); 244 245 protected Set <URL > archives = new HashSet <URL >(); 246 protected Set <URL > deployDirs = new HashSet <URL >(); 247 protected Set <String > archivesByResource = new HashSet <String >(); 248 protected Set <String > deployDirsByResource = new HashSet <String >(); 249 protected ClassLoader classLoader; 250 private Map defaultPersistenceProperties; 251 private Hashtable jndiProperties; 252 253 private List <EJB3StandaloneDeployment> deployments = new ArrayList <EJB3StandaloneDeployment>(); 254 255 private Kernel kernel; 256 private MBeanServer mbeanServer; 257 258 259 public EJB3StandaloneDeployer() 260 { 261 classLoader = Thread.currentThread().getContextClassLoader(); 262 } 263 264 public Kernel getKernel() 265 { 266 return kernel; 267 } 268 269 public void setKernel(Kernel kernel) 270 { 271 this.kernel = kernel; 272 } 273 274 279 public MBeanServer getMbeanServer() 280 { 281 return mbeanServer; 282 } 283 284 public void setMbeanServer(MBeanServer mbeanServer) 285 { 286 this.mbeanServer = mbeanServer; 287 } 288 289 294 public Set <URL > getArchives() 295 { 296 return archives; 297 } 298 299 public void setArchives(Set archives) 300 { 301 new Exception ().printStackTrace(); 302 this.archives = archives; 303 } 304 305 306 313 public Set <URL > getDeployDirs() 314 { 315 return deployDirs; 316 } 317 318 public void setDeployDirs(Set deployDirs) 319 { 320 this.deployDirs = deployDirs; 321 } 322 323 329 public Set <String > getArchivesByResource() 330 { 331 return archivesByResource; 332 } 333 334 public void setArchivesByResource(Set archivesByResource) 335 { 336 this.archivesByResource = archivesByResource; 337 } 338 339 346 public Set <String > getDeployDirsByResource() 347 { 348 return deployDirsByResource; 349 } 350 351 public void setDeployDirsByResource(Set deployDirsByResource) 352 { 353 this.deployDirsByResource = deployDirsByResource; 354 } 355 356 public ClassLoader getClassLoader() 357 { 358 return classLoader; 359 } 360 361 362 367 public void setClassLoader(ClassLoader classLoader) 368 { 369 this.classLoader = classLoader; 370 } 371 372 public Map getDefaultPersistenceProperties() 373 { 374 return defaultPersistenceProperties; 375 } 376 377 378 384 public void setDefaultPersistenceProperties(Map defaultPersistenceProperties) 385 { 386 this.defaultPersistenceProperties = defaultPersistenceProperties; 387 } 388 389 public Hashtable getJndiProperties() 390 { 391 return jndiProperties; 392 } 393 394 public void setJndiProperties(Hashtable jndiProperties) 395 { 396 this.jndiProperties = jndiProperties; 397 } 398 399 404 public List <EJB3StandaloneDeployment> getDeployments() 405 { 406 return deployments; 407 } 408 409 public void setDeployments(List <EJB3StandaloneDeployment> deployments) 410 { 411 this.deployments = deployments; 412 } 413 414 public static URL getContainingUrlFromResource(URL url, String resource) throws Exception 415 { 416 if (url.getProtocol().equals("jar")) 417 { 418 URL jarURL = url; 419 URLConnection urlConn = jarURL.openConnection(); 420 JarURLConnection jarConn = (JarURLConnection ) urlConn; 421 String parentArchiveName = jarConn.getJarFile().getName(); 423 File fp = new File (parentArchiveName); 424 return fp.toURL(); 425 } 426 427 String base = url.toString(); 429 int idx = base.lastIndexOf(resource); 430 base = base.substring(0, idx); 431 return new URL (base); 432 } 433 434 public static URL getDeployDirFromResource(URL url, String resource) throws Exception 435 { 436 if (url.getProtocol().equals("jar")) 437 { 438 URL jarURL = url; 439 URLConnection urlConn = jarURL.openConnection(); 440 JarURLConnection jarConn = (JarURLConnection ) urlConn; 441 String parentArchiveName = jarConn.getJarFile().getName(); 443 File fp = new File (parentArchiveName); 444 return fp.getParentFile().toURL(); 445 } 446 447 String base = url.toString(); 449 int idx = base.lastIndexOf(resource); 450 base = base.substring(0, idx); 451 File fp = new File (base); 452 return fp.getParentFile().toURL(); 453 } 454 455 public void create() throws Exception 456 { 457 try 458 { 459 for (String resource : deployDirsByResource) 460 { 461 Enumeration <URL > urls = classLoader.getResources(resource); 462 while (urls.hasMoreElements()) 463 { 464 URL url = urls.nextElement(); 465 URL deployUrl = getDeployDirFromResource(url, resource); 466 deployDirs.add(deployUrl); 467 } 468 } 469 470 for (URL url : deployDirs) 471 { 472 File dir = new File (url.toURI()); 473 for (File fp : dir.listFiles()) 474 { 475 if (fp.isDirectory()) 476 { 477 archives.add(fp.toURL()); 478 continue; 479 } 480 try 481 { 482 ZipFile zip = new ZipFile (fp); 483 zip.entries(); 484 zip.close(); 485 archives.add(fp.toURL()); 486 } 487 catch (IOException e) 488 { 489 } 490 } 491 } 492 493 for (String resource : archivesByResource) 494 { 495 Enumeration <URL > urls = classLoader.getResources(resource); 496 while (urls.hasMoreElements()) 497 { 498 URL url = urls.nextElement(); 499 URL archiveUrl = getContainingUrlFromResource(url, resource); 500 archives.add(archiveUrl); 501 } 502 } 503 504 if (defaultPersistenceProperties == null) 505 { 506 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties"); 507 if (is == null) throw new RuntimeException ("cannot find default.persistence.properties"); 508 Properties defaults = new Properties (); 509 defaults.load(is); 510 defaultPersistenceProperties = defaults; 511 } 512 513 for (URL archive : archives) 514 { 515 DeployerUnit du = new DeployerUnit(classLoader, archive, defaultPersistenceProperties, jndiProperties); 516 EJB3StandaloneDeployment deployment = new EJB3StandaloneDeployment(du, kernel, mbeanServer); 517 deployments.add(deployment); 518 deployment.create(); 519 } 520 } 521 catch (Exception e) 522 { 523 e.printStackTrace(); 524 throw e; 525 } 526 } 527 528 private void lookup(String name) 529 { 530 System.out.println("lookup " + name); 531 try { 532 InitialContext jndiContext = new InitialContext (); 533 NamingEnumeration names = jndiContext.list(name); 534 if (names != null){ 535 while (names.hasMore()){ 536 System.out.println(" " + names.next()); 537 } 538 } 539 } catch (Exception e){ 540 } 541 } 542 543 public void start() throws Exception 544 { 545 try 546 { 547 loadMbeanServer(); 548 549 for (EJB3StandaloneDeployment deployment : deployments) 550 { 551 if (deployment.getMbeanServer() == null) 552 { 553 deployment.setMbeanServer(mbeanServer); 554 } 555 556 deployment.start(); 557 lookup(""); 558 } 559 } 560 catch (Exception e) 561 { 562 e.printStackTrace(); 563 throw e; 564 } 565 } 566 567 private void loadMbeanServer() 568 { 569 if (mbeanServer == null) 570 { 571 ControllerContext context = kernel.getController().getInstalledContext("MBeanServer"); 572 573 if (context != null) 574 mbeanServer = (MBeanServer ) context.getTarget(); 575 else 576 { 577 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 578 if (servers.size() == 0) 579 mbeanServer = MBeanServerFactory.createMBeanServer(); 580 else 581 mbeanServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 582 } 583 } 584 } 585 586 public void stop() throws Exception 587 { 588 for (EJB3StandaloneDeployment deployment : deployments) 589 { 590 deployment.stop(); 591 } 592 } 593 594 public void destroy() throws Exception 595 { 596 for (EJB3StandaloneDeployment deployment : deployments) 597 { 598 deployment.destroy(); 599 } 600 } 601 } 602 | Popular Tags |