FileDocCategorySizeDatePackage
SunContainerHelper.javaAPI DocGlassfish v2 API17678Fri May 04 22:35:04 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.ejb

SunContainerHelper

public class SunContainerHelper extends SunTransactionHelper implements ContainerHelper
Implementation for Sun specific CMP and Container interactions as defined by the ContainerHelper interface. IMPORTANT: This class extends SunTransactionHelper class. Any changes to the TransactionHelper implementation must be done in the SunTransactionHelper.

Fields Summary
private static final ResourceBundle
messages
I18N message handler
private static com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger
Constructors Summary
SunContainerHelper()
Default constructor should not be public


                  
     
        CMPHelper.registerContainerHelper (new SunContainerHelper());
     
Methods Summary
public voidassertValidLocalObject(java.lang.Object o, java.lang.Object container)
Verify that this instance is of a valid local interface type for a given Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
o the instance to be verified.
param
container a Container instance for the request.

        ((Container)container).assertValidLocalObject(o);
    
public voidassertValidRemoteObject(java.lang.Object o, java.lang.Object container)
Verify that this instance is of a valid remote interface type for a given Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
o the instance to be verified.
param
container a Container instance for the request.

        ((Container)container).assertValidRemoteObject(o);
    
public voidbeginInternalTransaction(com.sun.jdo.api.persistence.support.PersistenceManager pm)
Called in a unspecified transaction context of a managed environment. According to p.364 of EJB 2.0 spec, CMP may need to manage its own transaction when its transaction attribute is NotSupported, Never, Supports. This is a generic implementation. Application server may like to overwrite this if necessary.

param
pm PersistenceManager

        pm.currentTransaction().begin();
    
public voidcommitInternalTransaction(com.sun.jdo.api.persistence.support.PersistenceManager pm)
Called in a unspecified transaction context of a managed environment. According to p.364 of EJB 2.0 spec, CMP may need to manage its own transaction when its transaction attribute is NotSupported, Never, Supports. This is a generic implementation. Application server may like to overwrite this if necessary.

param
pm PersistenceManager

        pm.currentTransaction().commit();
    
public java.lang.ObjectgetContainer(java.lang.Object info)
Get a Container helper instance that will be passed unchanged to the required methods. This is SunContainerHelper specific code. The info argument is an Object array that consistes of a class to use for the class loader and concreteImpl bean class name.

see
getEJBObject(Object, Object)
see
getEJBLocalObject(Object, Object)
see
getEJBLocalObject(Object, Object, EJBObject)
see
removeByEJBLocalObject(EJBLocalObject, Object)
see
removeByPK(Object, Object)
param
info Object with the request information that is application server specific.
return
a Container helper instance as an Object.


        Object[] params = (Object[])info;
        Class cls = (Class)params[0];

        ApplicationRegistry reg = ApplicationRegistry.getInstance();
        Application app = reg.getApplication(cls.getClassLoader());
        EjbCMPEntityDescriptor desc = app.getCMPDescriptorFor((String)params[1]);

        return reg.getContainer(desc);
    
public javax.ejb.EJBLocalObjectgetEJBLocalObject(java.lang.Object pk, java.lang.Object container)
Get an EJBLocalObject reference for this primary key and Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
pk the primary key instance.
param
containerHelper a helper instance for the request.
return
a corresponding EJBLocalObject instance to be used by the client.

        try {
            return ((Container)container).getEJBLocalObjectForPrimaryKey(pk);
        } catch (Exception ex) {
            throw new JDOFatalInternalException(ex.getMessage(), ex);
        }
    
public javax.ejb.EJBLocalObjectgetEJBLocalObject(java.lang.Object pk, java.lang.Object container, javax.ejb.EJBContext context)
Get an EJBLocalObject reference for this primary key and Container helper, and EJBContext of the calling bean. Allows the container to check if this method is called during ejbRemove that is part of a cascade-delete remove. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
pk the primary key instance.
param
containerHelper a helper instance for the request.
param
context an EJBContext of the calling bean.
return
a corresponding EJBLocalObject instance to be used by the client.

        EJBLocalObject rc = null;
        try {
            rc = ((Container)container).getEJBLocalObjectForPrimaryKey(pk, context);
        } catch (Exception ex) {
            processContainerException(ex);
        }

        return rc;
    
public javax.ejb.EJBObjectgetEJBObject(java.lang.Object pk, java.lang.Object container)
Get an EJBObject reference for this primary key and Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
pk the primary key instance.
param
container a Container instance for the request.
return
a corresponding EJBObject instance to be used by the client.

        try {
            return ((Container)container).getEJBObjectForPrimaryKey(pk);
        } catch (Exception ex) {
            throw new JDOFatalInternalException(ex.getMessage(), ex);
        }
    
public intgetNumericConverterPolicy(java.lang.Object container)
Called in CMP environment to get NumericConverter policy referenced by this Container instance.

see
getContainer(Object)
param
container a Container instance for the request
return
a valid NumericConverter policy type

        return NumericConverter.DEFAULT_POLICY;
    
public com.sun.jdo.api.persistence.support.PersistenceManagerFactorygetPersistenceManagerFactory(java.lang.Object container)
Called in a CMP environment to lookup PersistenceManagerFactory referenced by this Container instance as the CMP resource. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
container a Container instance for the request.

        Object rc = null;
        PersistenceManagerFactoryImpl pmf = null;

        ResourceReferenceDescriptor cmpResource = ((Container)container).getEjbDescriptor().
            getEjbBundleDescriptor().getCMPResourceReference();

        String name = cmpResource.getJndiName();

        try {
            InitialContext ic = new InitialContext();
            rc = ic.lookup(name);

            if (rc instanceof PersistenceManagerFactoryImpl) {
                pmf = (PersistenceManagerFactoryImpl)rc;

            } else if (rc instanceof javax.sql.DataSource) {
                pmf = new PersistenceManagerFactoryImpl();
                pmf.setConnectionFactoryName(ResourceInstaller.getPMJndiName(name));

                Iterator it = cmpResource.getProperties();
                if (it != null) {
                    while (it.hasNext()) {
                        NameValuePairDescriptor prop = (NameValuePairDescriptor)it.next();
                        String n = prop.getName();

                        // Any value that is not "true" is treated as "false":
                        boolean value = Boolean.valueOf(prop.getValue()).booleanValue();
                        pmf.setBooleanProperty(n, value);

                    }
                }

            } else {
                RuntimeException e = new JDOFatalUserException(I18NHelper.getMessage(
                    messages, "ejb.jndi.unexpectedinstance", //NOI18N
                    name, rc.getClass().getName())); 
                logger.severe(e.toString());

                throw e;
            }
        } catch (javax.naming.NamingException ex) {
            RuntimeException e = new JDOFatalUserException(I18NHelper.getMessage(
                messages, "ejb.jndi.lookupfailed", name), ex); //NOI18N
            logger.severe(e.toString());

            throw e;
        }

        return pmf;

    
public voidpreSelect(java.lang.Object container)
Called in a CMP supported environment. Notifies the container that ejbSelect had been called. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
container a Container instance for the request.

        ((Container)container).preSelect();
    
private voidprocessContainerException(java.lang.Exception ex)
Checks the caught Exception, and rethrows it if it is of one of known types, or converts to an EJBException otherwise.

param
ex the Exception to process.
throws
RuntimeException of the appropriate type.

        if (ex instanceof EJBException) {
            throw (EJBException)ex;

        } else if (ex instanceof IllegalArgumentException
                || ex instanceof IllegalStateException) {
            throw (RuntimeException)ex;

        } else if (ex instanceof JDOException) {
            throw (JDOException)ex;

        } else {
            throw new EJBException(ex);
        }
    
public voidremoveByEJBLocalObject(javax.ejb.EJBLocalObject ejb, java.lang.Object container)
Remove a bean for a given EJBLocalObject and Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
ejb the EJBLocalObject for the bean to be removed.
param
container a Container instance for the request.

        try {
            ((Container)container).removeBeanUnchecked(ejb);
        } catch (Exception ex) {
            processContainerException(ex);
        }
    
public voidremoveByPK(java.lang.Object pk, java.lang.Object container)
Remove a bean for a given primary key and Container helper. The Container instance is acquired via #getContainer(Object). This is SunContainerHelper specific code.

see
getContainer(Object)
param
pk the primary key for the bean to be removed.
param
container a Container instance for the request.

        try {
            ((Container)container).removeBeanUnchecked(pk);
        } catch (Exception ex) {
            processContainerException(ex);
        }
    
public voidresumeCurrentTransaction(javax.transaction.Transaction tx)
Called from read-only beans to resume current transaction. This will guarantee that the transaction continues to run after read-only bean accessed its PersistenceManager.

param
tx - The javax.transaction.Transaction object that represents the transaction to be resumed. If this object had been returned by #suspendCurrentTransaction() call it will be null in case calling thread was not associated with a transaction.

        try {
            // Resume only real (i.e. not null transaction)
            if (tx != null) {
                getLocalTransactionManager().resume(tx);
            }
        } catch (Exception ex) {
            processContainerException(ex);
        }
    
public voidrollbackInternalTransaction(com.sun.jdo.api.persistence.support.PersistenceManager pm)
Called in a unspecified transaction context of a managed environment. According to p.364 of EJB 2.0 spec, CMP may need to manage its own transaction when its transaction attribute is NotSupported, Never, Supports. This is a generic implementation. Application server may like to overwrite this if necessary.

param
pm PersistenceManager

        pm.currentTransaction().rollback();
    
public voidsetCascadeDeleteAfterSuperEJBRemove(javax.ejb.EntityContext context)
Mark the bean as already removed during cascade-delete operation for a given EntityContext. Called by the generated ejbRemove method before calling ejbRemove of the related beans (via removeByEJBLocalObject) that are to be cascade-deleted. This is SunContainerHelper specific code.

param
context the EntityContext of the bean beeing removed.

        try {
            ((com.sun.ejb.containers.EntityContextImpl)context).setCascadeDeleteAfterSuperEJBRemove(true);
        } catch (Exception ex) {
            processContainerException(ex);
        }
    
public javax.transaction.TransactionsuspendCurrentTransaction()
Called from read-only beans to suspend current transaction. This will guarantee that PersistenceManager is not bound to any transaction.

return
javax.transaction.Transaction object representing the suspended transaction. Returns null if the calling thread is not associated with a transaction.

        javax.transaction.Transaction tx = null;
        try {
            tx = getLocalTransactionManager().suspend();
        } catch (Exception ex) {
            processContainerException(ex);
        }

        return tx;