1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb; 31 32 import java.util.Iterator ; 33 import java.util.ResourceBundle ; 34 35 import javax.ejb.EJBObject ; 36 import javax.ejb.EJBLocalObject ; 37 import javax.ejb.EJBContext ; 38 import javax.ejb.EJBException ; 39 import javax.ejb.EntityContext ; 40 41 import javax.naming.InitialContext ; 42 import javax.naming.NamingException ; 43 44 import javax.sql.DataSource ; 45 46 import com.sun.enterprise.*; 47 import com.sun.enterprise.deployment.*; 48 import com.sun.enterprise.resource.ResourceInstaller; 49 import com.sun.enterprise.server.ApplicationRegistry; 50 51 import com.sun.ejb.Container; 52 53 import com.sun.jdo.api.persistence.support.JDOException; 54 import com.sun.jdo.api.persistence.support.JDOFatalInternalException; 55 import com.sun.jdo.api.persistence.support.JDOFatalUserException; 56 import com.sun.jdo.api.persistence.support.PersistenceManager; 57 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory; 58 59 import com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerFactoryImpl; 60 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager; 61 import com.sun.jdo.spi.persistence.support.sqlstore.utility.NumericConverter; 62 import com.sun.jdo.spi.persistence.utility.logging.Logger; 63 import com.sun.jdo.spi.persistence.utility.I18NHelper; 64 65 72 public class SunContainerHelper extends SunTransactionHelper implements ContainerHelper 73 { 74 75 76 private final static ResourceBundle messages = I18NHelper.loadBundle( 77 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", SunContainerHelper.class.getClassLoader()); 79 80 81 private static Logger logger = LogHelperPersistenceManager.getLogger(); 82 83 86 static { 87 CMPHelper.registerContainerHelper (new SunContainerHelper()); 88 } 89 90 91 SunContainerHelper() { } 92 93 108 public Object getContainer(Object info) { 109 110 Object [] params = (Object [])info; 111 Class cls = (Class )params[0]; 112 113 ApplicationRegistry reg = ApplicationRegistry.getInstance(); 114 Application app = reg.getApplication(cls.getClassLoader()); 115 EjbCMPEntityDescriptor desc = app.getCMPDescriptorFor((String )params[1]); 116 117 return reg.getContainer(desc); 118 } 119 120 129 public EJBObject getEJBObject(Object pk, Object container) { 130 try { 131 return ((Container)container).getEJBObjectForPrimaryKey(pk); 132 } catch (Exception ex) { 133 throw new JDOFatalInternalException(ex.getMessage(), ex); 134 } 135 } 136 137 146 public EJBLocalObject getEJBLocalObject(Object pk, Object container) { 147 try { 148 return ((Container)container).getEJBLocalObjectForPrimaryKey(pk); 149 } catch (Exception ex) { 150 throw new JDOFatalInternalException(ex.getMessage(), ex); 151 } 152 } 153 154 167 public EJBLocalObject getEJBLocalObject(Object pk, Object container, EJBContext context) { 168 EJBLocalObject rc = null; 169 try { 170 rc = ((Container)container).getEJBLocalObjectForPrimaryKey(pk, context); 171 } catch (Exception ex) { 172 processContainerException(ex); 173 } 174 175 return rc; 176 } 177 178 186 public void removeByEJBLocalObject(EJBLocalObject ejb, Object container) { 187 try { 188 ((Container)container).removeBeanUnchecked(ejb); 189 } catch (Exception ex) { 190 processContainerException(ex); 191 } 192 } 193 194 202 public void removeByPK(Object pk, Object container) { 203 try { 204 ((Container)container).removeBeanUnchecked(pk); 205 } catch (Exception ex) { 206 processContainerException(ex); 207 } 208 } 209 210 219 public void assertValidLocalObject(Object o, Object container) { 220 ((Container)container).assertValidLocalObject(o); 221 } 222 223 232 public void assertValidRemoteObject(Object o, Object container) { 233 ((Container)container).assertValidRemoteObject(o); 234 } 235 236 245 public void setCascadeDeleteAfterSuperEJBRemove(EntityContext context) { 246 try { 247 ((com.sun.ejb.containers.EntityContextImpl)context).setCascadeDeleteAfterSuperEJBRemove(true); 248 } catch (Exception ex) { 249 processContainerException(ex); 250 } 251 } 252 253 262 public void preSelect(Object container) { 263 ((Container)container).preSelect(); 264 } 265 266 275 public PersistenceManagerFactory getPersistenceManagerFactory(Object container) { 276 Object rc = null; 277 PersistenceManagerFactoryImpl pmf = null; 278 279 ResourceReferenceDescriptor cmpResource = ((Container)container).getEjbDescriptor(). 280 getEjbBundleDescriptor().getCMPResourceReference(); 281 282 String name = cmpResource.getJndiName(); 283 284 try { 285 InitialContext ic = new InitialContext (); 286 rc = ic.lookup(name); 287 288 if (rc instanceof PersistenceManagerFactoryImpl) { 289 pmf = (PersistenceManagerFactoryImpl)rc; 290 291 } else if (rc instanceof javax.sql.DataSource ) { 292 pmf = new PersistenceManagerFactoryImpl(); 293 pmf.setConnectionFactoryName(ResourceInstaller.getPMJndiName(name)); 294 295 Iterator it = cmpResource.getProperties(); 296 if (it != null) { 297 while (it.hasNext()) { 298 NameValuePairDescriptor prop = (NameValuePairDescriptor)it.next(); 299 String n = prop.getName(); 300 301 boolean value = Boolean.valueOf(prop.getValue()).booleanValue(); 303 pmf.setBooleanProperty(n, value); 304 305 } 306 } 307 308 } else { 309 RuntimeException e = new JDOFatalUserException(I18NHelper.getMessage( 310 messages, "ejb.jndi.unexpectedinstance", name, rc.getClass().getName())); 312 logger.severe(e.toString()); 313 314 throw e; 315 } 316 } catch (javax.naming.NamingException ex) { 317 RuntimeException e = new JDOFatalUserException(I18NHelper.getMessage( 318 messages, "ejb.jndi.lookupfailed", name), ex); logger.severe(e.toString()); 320 321 throw e; 322 } 323 324 return pmf; 325 326 } 327 328 335 public int getNumericConverterPolicy(Object container) { 336 return NumericConverter.DEFAULT_POLICY; 337 } 338 339 348 public void beginInternalTransaction(PersistenceManager pm) { 349 pm.currentTransaction().begin(); 350 } 351 352 361 public void commitInternalTransaction(PersistenceManager pm) { 362 pm.currentTransaction().commit(); 363 } 364 365 374 public void rollbackInternalTransaction(PersistenceManager pm) { 375 pm.currentTransaction().rollback(); 376 } 377 378 387 public javax.transaction.Transaction suspendCurrentTransaction() { 388 javax.transaction.Transaction tx = null; 389 try { 390 tx = getLocalTransactionManager().suspend(); 391 } catch (Exception ex) { 392 processContainerException(ex); 393 } 394 395 return tx; 396 } 397 398 407 public void resumeCurrentTransaction(javax.transaction.Transaction tx) { 408 try { 409 if (tx != null) { 411 getLocalTransactionManager().resume(tx); 412 } 413 } catch (Exception ex) { 414 processContainerException(ex); 415 } 416 } 417 418 426 private void processContainerException(Exception ex) { 427 if (ex instanceof EJBException ) { 428 throw (EJBException )ex; 429 430 } else if (ex instanceof IllegalArgumentException 431 || ex instanceof IllegalStateException ) { 432 throw (RuntimeException )ex; 433 434 } else if (ex instanceof JDOException) { 435 throw (JDOException)ex; 436 437 } else { 438 throw new EJBException (ex); 439 } 440 } 441 } 442 | Popular Tags |