1 7 package org.jboss.ejb3; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.URL ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.LinkedHashMap ; 15 import java.util.List ; 16 import java.util.Properties ; 17 import javax.ejb.Stateful ; 18 import javax.ejb.Stateless ; 19 import javax.persistence.Entity; 20 import org.jboss.aop.AspectManager; 21 import org.jboss.aop.DomainDefinition; 22 import org.jboss.ejb3.entity.EntityManagerFactoryLoader; 23 import org.jboss.ejb3.entity.ManagedEntityManagerFactory; 24 import org.jboss.ejb3.stateful.StatefulContainer; 25 import org.jboss.ejb3.stateless.StatelessContainer; 26 import org.jboss.logging.Logger; 27 import org.jboss.mx.util.MBeanProxy; 28 29 35 public class StandaloneDeploymentUnit 36 { 37 protected static final Logger log = Logger 38 .getLogger(StandaloneDeploymentUnit.class); 39 40 protected LinkedHashMap <String , Container> containers = new LinkedHashMap <String , Container>(); 41 42 protected List <String > entities = new ArrayList <String >(); 43 44 protected List <String > packages = new ArrayList <String >(); 45 46 protected List <InputStream > hbmXmlFiles = new ArrayList <InputStream >(); 47 48 protected ManagedEntityManagerFactory factory; 49 50 protected ClassLoader cl; 51 52 protected Properties defaultHibernateProperties; 53 54 protected InputStream hibernateConfigFile; 55 56 protected Properties hibernateProperties; 57 58 public static final String ENTITY_CLASSNAMES = "ENTITIES_CLASSNAMES"; 59 60 public StandaloneDeploymentUnit(ClassLoader cl, 61 Properties defaultHibernateProperties) 62 { 63 this.cl = cl; 64 this.defaultHibernateProperties = defaultHibernateProperties; 65 } 66 67 public InputStream getHibernateConfigFile() 68 { 69 return hibernateConfigFile; 70 } 71 72 public void setHibernateConfigFile(InputStream hibernateConfigFile) 73 { 74 this.hibernateConfigFile = hibernateConfigFile; 75 } 76 77 public Properties getHibernateProperties() 78 { 79 return hibernateProperties; 80 } 81 82 public void setHibernateProperties(Properties hibernateProperties) 83 { 84 this.hibernateProperties = hibernateProperties; 85 } 86 87 public void addHtmXmlFile(InputStream stream) 88 { 89 hbmXmlFiles.add(stream); 90 } 91 92 public void addEntity(String entityClass) 93 { 94 entities.add(entityClass); 95 } 96 97 public void addPackage(String pkg) 98 { 99 packages.add(pkg); 100 } 101 102 public void addClass(Class clazz) 103 { 104 if (clazz.isAnnotationPresent(Stateless .class)) 105 { 106 Stateless stateless = (Stateless ) clazz.getAnnotation(Stateless .class); 107 String ejbName = stateless.name(); 108 if (ejbName.equals("")) 109 ejbName = clazz.getName(); 110 String containerName = "Stateless Bean"; 111 DomainDefinition def = AspectManager.instance().getContainer( 112 containerName); 113 if (def == null) 114 throw new RuntimeException ("Domain " + containerName + " not found"); 115 StatelessContainer container = new StatelessContainer(clazz 116 .getClassLoader(), clazz.getName(), ejbName, def.getManager()); 117 containers.put(ejbName, container); 118 } else if (clazz.isAnnotationPresent(Stateful .class)) 119 { 120 Stateful stateless = (Stateful ) clazz.getAnnotation(Stateless .class); 121 String ejbName = stateless.name(); 122 if (ejbName.equals("")) 123 ejbName = clazz.getName(); 124 String containerName = "Stateful Bean"; 125 DomainDefinition def = AspectManager.instance().getContainer( 126 containerName); 127 if (def == null) 128 throw new RuntimeException ("Domain " + containerName + " not found"); 129 StatefulContainer container = new StatefulContainer(clazz 130 .getClassLoader(), clazz.getName(), ejbName, def.getManager()); 131 containers.put(ejbName, container); 132 } else if (clazz.isAnnotationPresent(Entity.class)) 133 { 134 entities.add(clazz.getName()); 135 } 136 } 137 138 public void create() throws Exception 139 { 140 Iterator it = containers.keySet().iterator(); 141 142 while (it.hasNext()) 143 { 144 String on = (String ) it.next(); 145 Container con = (Container) containers.get(on); 146 try 147 { 148 con.create(); 149 } catch (Exception e) 150 { 151 throw new RuntimeException (e); 152 } 153 } 154 } 155 156 public void start() throws Exception 157 { 158 Iterator it = containers.keySet().iterator(); 159 factory = createManagedEntityManagerFactory(); 160 while (it.hasNext()) 161 { 162 String on = (String ) it.next(); 163 Container con = (Container) containers.get(on); 164 con.setManagedEntityManagerFactory(factory); 165 try 166 { 167 con.start(); 168 } catch (Exception e) 169 { 170 throw new RuntimeException (e); 171 } 172 } 173 } 174 225 226 protected ManagedEntityManagerFactory createManagedEntityManagerFactory() 227 throws Exception 228 { 229 if (entities.size() == 0) 230 return null; 231 List <Class > classes = new ArrayList <Class >(); 232 for (int i = 0; i < entities.size(); i++) 233 { 234 String entity = (String ) entities.get(i); 235 classes.add(cl.loadClass(entity)); 236 } 237 entities.clear(); 238 if (hibernateConfigFile != null && hibernateProperties != null) 239 throw new RuntimeException ( 240 "You cannot have both a hibernate.properties and hibernate.cfg.xml file in META-INF"); 241 242 if (hibernateConfigFile != null) 243 { 244 throw new RuntimeException ("NOT SUPPORTED"); 245 } 246 247 Properties props = null; 248 if (hibernateProperties != null) 249 { 250 log.info("Found hibernate.properties file in EJB3 jar"); 251 props = hibernateProperties; 252 } else 253 { 254 props = new Properties (); 255 props.putAll(defaultHibernateProperties); 256 log.info("EJB3 jar using default hibernate.properties: " + props); 257 } 258 return new ManagedEntityManagerFactory(EntityManagerFactoryLoader 259 .loadFactory(hbmXmlFiles, packages, classes, props)); 260 } 261 262 public void stop() throws Exception 263 { 264 Iterator <String > it = containers.keySet().iterator(); 265 while (it.hasNext()) 266 { 267 String name = it.next(); 268 Container con = containers.get(name); 269 con.stop(); 270 if (log.isDebugEnabled()) 271 { 272 log.debug("stopService, stopping container: " + con.getEjbName()); 273 } 274 } 275 278 } 279 280 public void destroy() throws Exception 281 { 282 for (InputStream is : hbmXmlFiles) 283 { 284 try 285 { 286 is.close(); 287 } catch (IOException ignored) 288 { 289 } 290 } 291 Iterator <String > it = containers.keySet().iterator(); 292 while (it.hasNext()) 293 { 294 String name = it.next(); 295 Container con = containers.get(name); 296 con.destroy(); 297 if (log.isDebugEnabled()) 298 { 299 log.debug("destroyService, destroying container: " 300 + con.getEjbName()); 301 } 302 } 303 } 304 305 } 306 | Popular Tags |