1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import org.jboss.ejb.EntityEnterpriseContext; 25 import org.jboss.ejb.Container; 26 import org.jboss.ejb.EntityContainer; 27 import org.jboss.ejb.EjbModule; 28 import org.jboss.ejb.GenericEntityObjectFactory; 29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCTypeFactory; 30 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore; 31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStartCommand; 32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStopCommand; 33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractEntityBridge; 34 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityMetaData; 35 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCApplicationMetaData; 36 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCXmlFileLoader; 37 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData; 38 import org.jboss.ejb.plugins.cmp.ejbql.Catalog; 39 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2; 40 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema; 41 import org.jboss.ejb.plugins.cmp.jdbc2.schema.EntityTable; 42 import org.jboss.logging.Logger; 43 import org.jboss.deployment.DeploymentException; 44 import org.jboss.metadata.ApplicationMetaData; 45 import org.jboss.tm.TransactionLocal; 46 47 import javax.ejb.DuplicateKeyException ; 48 import javax.ejb.FinderException ; 49 import javax.ejb.EJBException ; 50 import javax.ejb.CreateException ; 51 import javax.ejb.RemoveException ; 52 import java.lang.reflect.Method ; 53 import java.util.Collection ; 54 import java.util.HashMap ; 55 import java.util.ArrayList ; 56 import java.util.List ; 57 import java.util.Map ; 58 import java.util.Iterator ; 59 import java.sql.SQLException ; 60 61 62 66 public class JDBCStoreManager2 67 implements JDBCEntityPersistenceStore 68 { 69 private static final String CATALOG = "CATALOG"; 70 private static final String SCHEMA = "SCHEMA"; 71 private static final String CREATED_MANAGERS = "CREATED_JDBCStoreManagers"; 72 private static final String CMP_JDBC = "CMP-JDBC"; 73 74 private EntityContainer container; 75 private EjbModule ejbModule; 76 private Logger log; 77 private JDBCEntityMetaData metaData; 78 private JDBCEntityBridge2 entityBridge; 79 private JDBCTypeFactory typeFactory; 80 private Schema schema; 81 82 private InstanceFactory instanceFactory; 83 private QueryFactory queryFactory; 84 private CreateCommand createCmd; 85 private JDBCStartCommand startCmd; 86 private JDBCStopCommand stop; 87 88 private final TransactionLocal cascadeDeleteRegistry = new TransactionLocal() 89 { 90 protected Object initialValue() 91 { 92 return new HashMap (); 93 } 94 }; 95 96 98 public Schema getSchema() 99 { 100 schema = (Schema)getApplicationData(SCHEMA); 101 if(schema == null) 102 { 103 schema = new Schema(); 104 putApplicationData(SCHEMA, schema); 105 } 106 return schema; 107 } 108 109 public Catalog getCatalog() 110 { 111 Catalog catalog = (Catalog)getApplicationData(CATALOG); 112 if(catalog == null) 113 { 114 catalog = new Catalog(); 115 putApplicationData(CATALOG, catalog); 116 } 117 return catalog; 118 } 119 120 public QueryFactory getQueryFactory() 121 { 122 return queryFactory; 123 } 124 125 public boolean registerCascadeDelete(Object key, Object value) 126 { 127 Map map = (Map )cascadeDeleteRegistry.get(); 128 return map.put(key, value) == null; 129 } 130 131 public boolean isCascadeDeleted(Object key) 132 { 133 Map map = (Map )cascadeDeleteRegistry.get(); 134 return map.containsKey(key); 135 } 136 137 public void unregisterCascadeDelete(Object key) 138 { 139 Map map = (Map )cascadeDeleteRegistry.get(); 140 map.remove(key); 141 } 142 143 145 public void setContainer(Container con) 146 { 147 this.container = (EntityContainer)con; 148 if(container != null) 149 { 150 ejbModule = container.getEjbModule(); 151 log = Logger.getLogger(this.getClass().getName() + "." + container.getBeanMetaData().getEjbName()); 152 } 153 else 154 { 155 ejbModule = null; 157 } 158 } 159 160 162 public void create() throws Exception 163 { 164 HashMap managersMap = (HashMap )getApplicationData(CREATED_MANAGERS); 165 if(managersMap == null) 166 { 167 managersMap = new HashMap (); 168 putApplicationData(CREATED_MANAGERS, managersMap); 169 } 170 managersMap.put(container.getBeanMetaData().getEjbName(), this); 171 } 172 173 public void start() throws Exception 174 { 175 initStoreManager(); 176 177 HashMap managersMap = (HashMap )getApplicationData(CREATED_MANAGERS); 178 Catalog catalog = getCatalog(); 179 if(catalog.getEntityCount() == managersMap.size() && catalog.getEJBNames().equals(managersMap.keySet())) 180 { 181 List managers = new ArrayList (managersMap.values()); 183 184 for(int i = 0; i < managers.size(); ++i) 186 { 187 JDBCStoreManager2 manager = (JDBCStoreManager2)managers.get(i); 188 manager.resolveRelationships(); 189 } 190 191 for(int i = 0; i < managers.size(); ++i) 193 { 194 JDBCStoreManager2 manager = (JDBCStoreManager2)managers.get(i); 195 manager.startEntity(); 196 } 197 198 for(int i = 0; i < managers.size(); ++i) 200 { 201 JDBCStoreManager2 manager = (JDBCStoreManager2)managers.get(i); 202 manager.startStoreManager(); 203 } 204 205 for(int i = 0; i < managers.size(); ++i) 207 { 208 JDBCStoreManager2 manager = (JDBCStoreManager2)managers.get(i); 209 manager.startCmd.addForeignKeyConstraints(); 210 } 211 } 212 } 213 214 public void stop() 215 { 216 if(stop != null) 217 { 218 Map managersMap = (HashMap )getApplicationData(CREATED_MANAGERS); 219 while(!managersMap.isEmpty()) 220 { 221 int stoppedInIteration = 0; 222 for(Iterator i = managersMap.values().iterator(); i.hasNext();) 223 { 224 JDBCStoreManager2 manager = (JDBCStoreManager2)i.next(); 225 if(manager.stop.execute()) 226 { 227 i.remove(); 228 try 229 { 230 manager.entityBridge.stop(); 231 } 232 catch(Exception e) 233 { 234 log.error("Failed to stop entity bridge.", e); 235 } 236 ++stoppedInIteration; 237 } 238 } 239 240 if(stoppedInIteration == 0) 241 { 242 break; 243 } 244 } 245 } 246 } 247 248 public void destroy() 249 { 250 } 252 253 255 public JDBCAbstractEntityBridge getEntityBridge() 256 { 257 return entityBridge; 258 } 259 260 public JDBCEntityMetaData getMetaData() 261 { 262 return metaData; 263 } 264 265 public JDBCTypeFactory getJDBCTypeFactory() 266 { 267 return typeFactory; 268 } 269 270 public EntityContainer getContainer() 271 { 272 return container; 273 } 274 275 public Object getApplicationData(Object key) 276 { 277 return ejbModule.getModuleData(key); 278 } 279 280 public void putApplicationData(Object key, Object value) 281 { 282 ejbModule.putModuleData(key, value); 283 } 284 285 287 public Object createBeanClassInstance() throws Exception 288 { 289 return instanceFactory.newInstance(); 290 } 291 292 public void initEntity(EntityEnterpriseContext ctx) 293 { 294 entityBridge.initPersistenceContext(ctx); 295 entityBridge.initInstance(ctx); 296 } 297 298 public Object createEntity(Method m, Object [] args, EntityEnterpriseContext ctx) 299 throws CreateException 300 { 301 339 return createCmd.execute(m, args, ctx); 340 } 341 342 public Object postCreateEntity(Method m, Object [] args, EntityEnterpriseContext ctx) throws CreateException 343 { 344 return null; 345 } 346 347 public Object findEntity(Method finderMethod, 348 Object [] args, 349 EntityEnterpriseContext instance, 350 GenericEntityObjectFactory factory) 351 throws FinderException 352 { 353 QueryCommand query = queryFactory.getQueryCommand(finderMethod); 354 return query.fetchOne(schema, factory, args); 355 } 356 357 public Collection findEntities(Method finderMethod, 358 Object [] args, 359 EntityEnterpriseContext instance, 360 GenericEntityObjectFactory factory) 361 throws FinderException 362 { 363 QueryCommand query = queryFactory.getQueryCommand(finderMethod); 364 return query.fetchCollection(schema, factory, args); 365 } 366 367 public void activateEntity(EntityEnterpriseContext ctx) 368 { 369 entityBridge.initPersistenceContext(ctx); 370 } 371 372 public void loadEntity(EntityEnterpriseContext ctx) 373 { 374 try 375 { 376 EntityTable.Row row = entityBridge.getTable().loadRow(ctx.getId()); 377 PersistentContext pctx = new PersistentContext(entityBridge, row); 378 ctx.setPersistenceContext(pctx); 379 } 380 catch(EJBException e) 381 { 382 log.error("Failed to load instance of " + entityBridge.getEntityName() + " with pk=" + ctx.getId(), e); 383 throw e; 384 } 385 catch(Exception e) 386 { 387 throw new EJBException ( 388 "Failed to load instance of " + entityBridge.getEntityName() + " with pk=" + ctx.getId(), e 389 ); 390 } 391 } 392 393 public boolean isStoreRequired(EntityEnterpriseContext instance) 394 { 395 return entityBridge.isStoreRequired(instance); 396 } 397 398 public boolean isModified(EntityEnterpriseContext instance) throws Exception 399 { 400 return entityBridge.isModified(instance); 401 } 402 403 public void storeEntity(EntityEnterpriseContext instance) 404 { 405 } 407 408 public void passivateEntity(EntityEnterpriseContext ctx) 409 { 410 JDBCEntityBridge2.destroyPersistenceContext(ctx); 411 } 412 413 public void removeEntity(EntityEnterpriseContext ctx) throws RemoveException 414 { 415 entityBridge.remove(ctx); 416 PersistentContext pctx = (PersistentContext)ctx.getPersistenceContext(); 417 pctx.remove(); 418 } 419 420 422 protected void initStoreManager() throws Exception 423 { 424 if(log.isDebugEnabled()) 425 { 426 log.debug("Initializing CMP plugin for " + container.getBeanMetaData().getEjbName()); 427 } 428 429 metaData = loadJDBCEntityMetaData(); 430 431 typeFactory = new JDBCTypeFactory(metaData.getTypeMapping(), 433 metaData.getJDBCApplication().getValueClasses(), 434 metaData.getJDBCApplication().getUserTypeMappings() 435 ); 436 437 entityBridge = new JDBCEntityBridge2(this, metaData); 438 entityBridge.init(); 439 440 Catalog catalog = getCatalog(); 441 catalog.addEntity(entityBridge); 442 443 stop = new JDBCStopCommand(this); 444 } 445 446 private void resolveRelationships() throws Exception 447 { 448 entityBridge.resolveRelationships(); 449 } 450 451 protected void startStoreManager() throws Exception 452 { 453 queryFactory = new QueryFactory(entityBridge); 454 queryFactory.init(); 455 456 instanceFactory = new InstanceFactory(this, entityBridge); 457 458 startCmd = new JDBCStartCommand(this); 459 startCmd.execute(); 460 461 final JDBCEntityCommandMetaData entityCommand = getMetaData().getEntityCommand(); 462 if(entityCommand == null || "default".equals(entityCommand.getCommandName())) 463 { 464 createCmd = new ApplicationPkCreateCommand(); 465 } 466 else 467 { 468 final Class cmdClass = entityCommand.getCommandClass(); 469 if(cmdClass == null) 470 { 471 throw new DeploymentException( 472 "entity-command class name is not specified for entity " + entityBridge.getEntityName() 473 ); 474 } 475 476 try 477 { 478 createCmd = (CreateCommand)cmdClass.newInstance(); 479 } 480 catch(ClassCastException cce) 481 { 482 throw new DeploymentException("Entity command " + cmdClass + " does not implement " + CreateCommand.class); 483 } 484 } 485 486 createCmd.init(this); 487 } 488 489 private void startEntity() 490 throws DeploymentException 491 { 492 entityBridge.start(); 493 } 494 495 private JDBCEntityMetaData loadJDBCEntityMetaData() 496 throws DeploymentException 497 { 498 ApplicationMetaData amd = container.getBeanMetaData().getApplicationMetaData(); 499 500 JDBCApplicationMetaData jamd = (JDBCApplicationMetaData)amd.getPluginData(CMP_JDBC); 502 503 if(jamd == null) 504 { 505 JDBCXmlFileLoader jfl = new JDBCXmlFileLoader(container, log); 508 509 jamd = jfl.load(); 510 amd.addPluginData(CMP_JDBC, jamd); 511 } 512 513 String ejbName = container.getBeanMetaData().getEjbName(); 515 JDBCEntityMetaData metadata = jamd.getBeanByEjbName(ejbName); 516 if(metadata == null) 517 { 518 throw new DeploymentException("No metadata found for bean " + ejbName); 519 } 520 return metadata; 521 } 522 } 523 | Popular Tags |