1 22 package org.jboss.ejb3; 23 24 import java.io.DataInputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.InputStreamReader ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Properties ; 36 import javax.management.ObjectName ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.naming.RefAddr ; 41 import javax.naming.Reference ; 42 import javax.naming.StringRefAddr ; 43 import org.jboss.deployment.DeploymentException; 44 import org.jboss.deployment.DeploymentInfo; 45 import org.jboss.deployment.SubDeployer; 46 import org.jboss.deployment.SubDeployerSupport; 47 import org.jboss.metadata.MetaData; 48 import org.jboss.metadata.XmlFileLoader; 49 import org.jboss.mx.loading.LoaderRepositoryFactory; 50 import org.jboss.mx.util.MBeanProxyExt; 51 import org.jboss.mx.util.ObjectNameConverter; 52 import org.jboss.system.ServiceControllerMBean; 53 import org.jboss.util.file.ArchiveBrowser; 54 import org.jboss.util.file.ClassFileFilter; 55 import org.w3c.dom.Element ; 56 57 import javassist.bytecode.AnnotationsAttribute; 58 import javassist.bytecode.ClassFile; 59 60 66 public class EJB3Deployer extends SubDeployerSupport 67 implements SubDeployer, EJB3DeployerMBean 68 { 69 private ServiceControllerMBean serviceController; 70 71 72 private HashMap deployments = new HashMap (); 73 74 75 private SubDeployer thisProxy; 76 77 private Properties DefaultProperties; 78 79 private HashSet ignoredJarsSet; 80 private HashMap <DeploymentInfo, String > jmxNames = new HashMap (); 81 82 87 public EJB3Deployer() 88 { 89 setSuffixes(new String []{".jar", ".ejb3", ".par"}); 90 setRelativeOrder(400); } 92 93 public static boolean hasFile(DeploymentInfo di, String filePath) 94 { 95 String urlStr = di.url.getFile(); 96 try 97 { 98 URL dd = di.localCl.findResource(filePath); 99 if (dd != null) 100 { 101 102 if (di.localUrl != null) 106 { 107 urlStr = di.localUrl.toString(); 108 } 109 110 String ddStr = dd.toString(); 111 if (ddStr.indexOf(urlStr) >= 0) 112 { 113 return true; 114 } 115 } 116 } 117 catch (Exception ignore) 118 { 119 } 120 return false; 121 } 122 123 public static boolean hasPersistenceXml(DeploymentInfo di) 124 { 125 return hasFile(di, "META-INF/persistence.xml"); 126 } 127 128 public static boolean has30EjbJarXml(DeploymentInfo di) 129 { 130 if (!hasFile(di, "META-INF/ejb-jar.xml")) return false; 131 InputStream ddStream = di.localCl.getResourceAsStream("META-INF/ejb-jar.xml"); 132 133 return has30EjbJarXml(ddStream); 134 } 135 136 public static boolean has30EjbJarXml(InputStream ddStream) 137 { 138 try 139 { 140 byte[] stringToFind = "version=\"3.0\"".getBytes(); 142 InputStreamReader reader = new InputStreamReader (ddStream); 143 try 144 { 145 int idx = 0; 146 int len = stringToFind.length; 147 while (reader.ready()) 148 { 149 int read = reader.read(); 150 if (read == stringToFind[idx]) 151 { 152 idx++; 153 if (idx == len) 154 { 155 return true; 156 } 157 } 158 else 159 { 160 idx = 0; 161 } 162 } 163 164 } 165 finally 166 { 167 try 168 { 169 reader.close(); 170 ddStream.close(); 171 } 172 catch (IOException ignored) 173 { 174 } 175 } 176 } 177 catch (Exception ignore) 178 { 179 } 180 return false; 181 } 182 183 protected boolean hasOnlyJbossXml(DeploymentInfo di) 184 { 185 if (!hasFile(di, "META-INF/ejb-jar.xml") 186 && hasFile(di, "META-INF/jboss.xml")) 187 { 188 return true; 189 } 190 return false; 191 } 192 193 public boolean hasEjbAnnotation(DeploymentInfo di) 194 { 195 Iterator it = ArchiveBrowser.getBrowser(di.url, new ClassFileFilter()); 196 try 197 { 198 while (it.hasNext()) 199 { 200 InputStream stream = (InputStream ) it.next(); 201 DataInputStream dstream = new DataInputStream (stream); 202 ClassFile cf = null; 203 try 204 { 205 cf = new ClassFile(dstream); 206 AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag); 207 if (visible != null) 208 { 209 if (EJB3Util.isStateless(visible)) return true; 210 if (EJB3Util.isStatefulSession(visible)) return true; 211 if (EJB3Util.isMessageDriven(visible)) return true; 212 if (EJB3Util.isConsumer(visible)) return true; 213 if (EJB3Util.isService(visible)) return true; 214 } 215 } 216 finally 217 { 218 dstream.close(); 219 stream.close(); 220 } 221 } 222 } 223 catch (IOException e) 224 { 225 throw new RuntimeException (e); 226 } 227 return false; 228 } 229 230 236 public boolean accepts(DeploymentInfo di) 237 { 238 String urlStr = di.url.getFile(); 239 if (urlStr.endsWith(".ejb3") || urlStr.endsWith(".ejb3/") || urlStr.endsWith(".par") || urlStr.endsWith(".par/")) 240 { 241 return true; 242 } 243 if (!urlStr.endsWith(".jar") && !urlStr.endsWith(".jar/")) 245 { 246 return false; 247 } 248 249 if (ignoredJarsSet.contains(di.shortName)) 250 { 251 return false; 252 } 253 254 if (hasPersistenceXml(di)) return true; 255 if (hasOnlyJbossXml(di)) return true; 256 if (has30EjbJarXml(di)) return true; 257 if (hasEjbAnnotation(di)) return true; 258 259 260 return false; 261 } 262 263 public Properties getDefaultProperties() 264 { 265 return DefaultProperties; 266 } 267 268 public void setJarsIgnoredForScanning(JarsIgnoredForScanningMBean mbean) 269 { 270 ignoredJarsSet = mbean.getIgnoredJarsSet(); 271 } 272 273 278 protected void createService() throws Exception 279 { 280 URL propsUrl = this.getClass().getClassLoader().getResource("META-INF/persistence.properties"); 281 DefaultProperties = new Properties (); 282 DefaultProperties.load(propsUrl.openStream()); 283 log.debug("Default persistence.properties: " + DefaultProperties); 284 287 String bcprovider = DefaultProperties.getProperty("hibernate.bytecode.provider", "javassist"); 288 System.setProperty("hibernate.bytecode.provider", bcprovider); 289 super.createService(); 290 } 291 292 295 protected void startService() throws Exception 296 { 297 serviceController = (ServiceControllerMBean) 298 MBeanProxyExt.create(ServiceControllerMBean.class, 299 ServiceControllerMBean.OBJECT_NAME, server); 300 301 thisProxy = (SubDeployer) 304 MBeanProxyExt.create(SubDeployer.class, super.getServiceName(), super.getServer()); 305 306 mainDeployer.addDeployer(thisProxy); 308 309 InitialContext iniCtx = new InitialContext (); 311 initializeJavaComp(iniCtx); 312 } 313 314 public static void initializeJavaComp(InitialContext iniCtx) 315 throws NamingException 316 { 317 RefAddr refAddr = new StringRefAddr ("nns", "ENC-EJB3"); 318 Reference envRef = new Reference ("javax.naming.Context", refAddr, ThreadLocalENCFactory.class.getName(), null); 319 Context ctx = (Context ) iniCtx.lookup("java:"); 320 ctx.rebind("comp.ejb3", envRef); 321 } 322 323 327 protected void stopService() throws Exception 328 { 329 for (Iterator modules = deployments.values().iterator(); 330 modules.hasNext();) 331 { 332 DeploymentInfo di = (DeploymentInfo) modules.next(); 333 stop(di); 334 } for (Iterator modules = new ArrayList (deployments.values()).iterator(); 336 modules.hasNext();) 337 { 338 DeploymentInfo di = (DeploymentInfo) modules.next(); 339 destroy(di); 340 } 341 deployments.clear(); 342 343 mainDeployer.removeDeployer(thisProxy); 345 346 serviceController = null; 347 } 348 349 public void init(DeploymentInfo di) throws DeploymentException 350 { 351 try 352 { 353 if( di.url.getProtocol().equalsIgnoreCase("file") ) 354 { 355 File file = new File (di.url.getFile()); 356 357 if( !file.isDirectory() ) 358 { 359 di.watch = di.url; 361 } 362 else 363 { 364 di.watch = new URL (di.url, "META-INF/ejb-jar.xml"); 366 } 367 } 368 else 369 { 370 di.watch = di.url; 372 } 373 374 XmlFileLoader xfl = new XmlFileLoader(); 375 InputStream in = di.localCl.getResourceAsStream("META-INF/jboss.xml"); 376 if( in != null ) 377 { 378 try 379 { 380 Element jboss = xfl.getDocument(in, "META-INF/jboss.xml").getDocumentElement(); 381 Element loader = MetaData.getOptionalChild(jboss, "loader-repository"); 383 if( loader != null ) 384 { 385 LoaderRepositoryFactory.LoaderRepositoryConfig config = 386 LoaderRepositoryFactory.parseRepositoryConfig(loader); 387 di.setRepositoryInfo(config); 388 } 389 390 Element jmxNameElement = MetaData.getOptionalChild(jboss, "jmx-name"); 391 if (jmxNameElement != null) 392 { 393 jmxNames.put(di, jmxNameElement.getChildNodes().item(0).getNodeValue()); 394 } 395 } 396 finally 397 { 398 in.close(); 399 } 400 } 401 } 402 catch (Exception e) 403 { 404 if (e instanceof DeploymentException) 405 { 406 throw (DeploymentException) e; 407 } 408 throw new DeploymentException( "failed to initialize", e ); 409 } 410 411 super.init(di); 413 } 414 415 public synchronized void create(DeploymentInfo di) throws DeploymentException 416 { 417 log.debug("create, " + di.shortName); 418 419 try 420 { 421 URL loaderURL = (di.localUrl != null ? di.localUrl : di.url); 423 di.annotationsCl = new URLClassLoader (new URL []{loaderURL}, di.ucl); 424 425 Ejb3Module ejbModule = new Ejb3Module(di); 426 String name = jmxNames.get(di); 427 if (name == null) 428 name = Ejb3Module.BASE_EJB3_JMX_NAME + ",module=" + di.shortName; 429 ObjectName ejbModuleName = ObjectNameConverter.convert(name); 431 if (server.isRegistered(ejbModuleName) == true) 433 { 434 log.debug("The EJBModule name: " + ejbModuleName 435 + "is already registered, adding uid=" + System.identityHashCode(ejbModule)); 436 name = name + ",uid=" + System.identityHashCode(ejbModule); 437 ejbModuleName = ObjectNameConverter.convert(name); 438 } 439 server.registerMBean(ejbModule, ejbModuleName); 440 di.deployedObject = ejbModuleName; 441 log.debug("Deploying: " + di.url); 442 serviceController.create(di.deployedObject); 444 } 445 catch (Exception e) 446 { 447 throw new DeploymentException("Error during create of EjbModule: " 448 + di.url, e); 449 } 450 super.create(di); 451 } 452 453 public synchronized void start(DeploymentInfo di) 454 throws DeploymentException 455 { 456 try 457 { 458 log.debug("start application, deploymentInfo: " + di + 460 ", short name: " + di.shortName + 461 ", parent short name: " + 462 (di.parent == null ? "null" : di.parent.shortName)); 463 serviceController.start(di.deployedObject); 464 log.info("Deployed: " + di.url); deployments.put(di.url, di); 467 } 468 catch (Exception e) 469 { 470 stop(di); 471 destroy(di); 472 throw new DeploymentException("Could not deploy " + di.url, e); 473 } 474 super.start(di); 475 } 476 477 public void stop(DeploymentInfo di) 478 throws DeploymentException 479 { 480 log.debug("init, " + di.shortName); 481 try 482 { 483 serviceController.stop(di.deployedObject); 484 } 485 catch (Exception e) 486 { 487 throw new DeploymentException("problem stopping ejb module: " + 488 di.url, e); 489 } 490 491 super.stop(di); 492 } 493 494 public void destroy(DeploymentInfo di) 495 throws DeploymentException 496 { 497 deployments.remove(di.url); 499 try 500 { 501 serviceController.destroy(di.deployedObject); 502 serviceController.remove(di.deployedObject); 503 } 504 catch (Exception e) 505 { 506 throw new DeploymentException("problem destroying ejb module: " + 507 di.url, e); 508 } 509 510 jmxNames.remove(di); 511 512 super.destroy(di); 513 } 514 } 515 | Popular Tags |