FileDocCategorySizeDatePackage
CMPBeanHelper.javaAPI DocGlassfish v2 API15319Fri May 04 22:34:48 BST 2007com.sun.jdo.spi.persistence.support.ejb.cmp

CMPBeanHelper

public class CMPBeanHelper extends Object
Provides static helper methods for CMP bean implementation to simplify the generated code.

Fields Summary
private static final ResourceBundle
cmpMessages
I18N message handlers
private static com.sun.jdo.spi.persistence.utility.logging.Logger
cmpLifecycleLogger
The lifecycle logger used to log messages from ejbCreate(), ejbRemove() and other lifecycle methods.
private static com.sun.jdo.spi.persistence.utility.logging.Logger
cmpFinderLogger
The finder logger used to log messages from ejbFindXXX and/or ejbSelectXXX methods.
private static com.sun.jdo.spi.persistence.utility.logging.Logger
cmpInternalLogger
The internal logger used to log messages from setters and getter and other generated methods.
Constructors Summary
Methods Summary
public static voidassertCollectionNotNull(java.util.Collection c, java.lang.String beanName)
Called from a CMP bean to verify that the argument for a Collection set method is not null. Throws IllegalArgumentException if the argument is null.

param
c the Collection to check.
param
beanName the name of the caller bean.
throws
IllegalArgumentException if the argument is null.

        if (c == null) {
            String msg = I18NHelper.getMessage(cmpMessages,
                "GEN.cmrsettercol_nullexception", beanName, findCallingMethodName()); // NOI18N

            cmpInternalLogger.log(Logger.SEVERE, msg);
            throw new IllegalArgumentException(msg);
        }
    
public static voidassertNotContainerTransaction(java.lang.Object bean)
Called from a 1.1 CMP bean to verify that the bean method is not called in a container transaction. Throws IllegalStateException otherwise.

param
bean the calling bean instance.
throws
IllegalStateException if the bean method is called in a container transaction.

        if (EJBHelper.getTransaction() != null) {
            String msg = I18NHelper.getMessage(cmpMessages,
                "JDO.containertransaction_exception", bean); // NOI18N

            cmpInternalLogger.log(Logger.SEVERE, msg);
            throw new IllegalStateException(msg);
        }
    
public static voidassertPersistenceManagerIsNull(com.sun.jdo.api.persistence.support.PersistenceManager pm, java.lang.Object bean)
Called from a CMP bean to verify that the PersistenceManager is null. Throws IllegalStateException if the argument is not null.

param
pm the PersistenceManager to check.
param
bean the calling bean instance.
throws
IllegalStateException if the PersistenceManager is not null.

        if (pm != null) {
            String msg = I18NHelper.getMessage(cmpMessages,
                "JDO.beaninuse_exception", bean); // NOI18N

            cmpInternalLogger.log(Logger.SEVERE, msg);
            throw new IllegalStateException(msg);
        }
    
public static voidassertPersistenceManagerNotNull(com.sun.jdo.api.persistence.support.PersistenceManager pm, java.lang.Object bean)
Called from a CMP bean to verify that the PersistenceManager is not null. Throws IllegalStateException if the argument is null.

param
pm the PersistenceManager to check.
param
bean the calling bean instance.
throws
IllegalStateException if the PersistenceManager is null.

        if (pm == null) {
            String msg = I18NHelper.getMessage(cmpMessages,
                "JDO.beannotloaded_exception", bean); // NOI18N

            cmpInternalLogger.log(Logger.SEVERE, msg);
            throw new IllegalStateException(msg);
        }
    
public static voidassertPersistent(com.sun.jdo.api.persistence.support.PersistenceCapable pc, java.lang.String beanName)
Called from a CMP bean to verify that the PersistenceCapable instance is already persistent. Throws IllegalStateException otherwise.

param
pc the PersistenceCapable instance to be checked.
param
beanName the name of the caller bean.
throws
IllegalStateException if the instance is not persistent.

        if (!JDOHelper.isPersistent(pc)) {
            String msg = I18NHelper.getMessage(cmpMessages, 
                "GEN.cmrgettersetter_exception", beanName, findCallingMethodName()); // NOI18N

            cmpInternalLogger.log(Logger.SEVERE, msg);
            throw new IllegalStateException(msg);
        }
    
private static java.lang.StringfindCallingMethodName()
Calculates the method name of the calling method.

return
method name as String.

        StackTraceElement[] ste = (new Throwable()).getStackTrace();
        return ste[2].getMethodName();
    
public static voidhandleCloneException(java.lang.Object primaryKey, java.lang.String beanName, java.lang.Exception ex)
Throws EJBException on failed clone of persistence state in read-only beans.

param
primaryKey the PrimaryKey instance.
param
beanName the name of the calling bean.
param
ex the Exception.
throws
EJBException.

 
        String msg = I18NHelper.getMessage(cmpMessages,
            "GEN.clone_exception", beanName, // NOI18N
            primaryKey.toString());
 
        cmpLifecycleLogger.log(Logger.SEVERE, msg, ex);
        throw new EJBException(msg);
    
public static voidhandleJDODuplicateObjectIdAsDuplicateKeyException(java.lang.String beanName, java.lang.String paramList, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to process JDODuplicateObjectIdException. Logs the message and throws DuplicateKeyException.

param
beanName the name of the calling bean.
param
paramList the list of the concatenated parameters.
param
ex the JDOException.
throws
DuplicateKeyException.


        String msg = I18NHelper.getMessage(cmpMessages,
            "GEN.ejbcreate_exception_dup", beanName, // NOI18N
            findCallingMethodName(), paramList);

        cmpLifecycleLogger.log(Logger.FINER, msg, ex);
        throw new DuplicateKeyException(msg);
    
public static voidhandleJDODuplicateObjectIdAsEJBException(java.lang.String beanName, java.lang.String paramList, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to process JDODuplicateObjectIdException. Logs the message and throws EJBException.

param
beanName the name of the calling bean.
param
paramList the list of the concatenated parameters.
param
ex the JDOException.
throws
EJBException.


        String msg = I18NHelper.getMessage(cmpMessages,
            "GEN.ejbcreate_exception_dup", beanName, // NOI18N
            findCallingMethodName(), paramList);

        cmpLifecycleLogger.log(Logger.FINER, msg, ex);
        throw new EJBException(msg);
    
public static voidhandleJDOObjectNotFoundException(java.lang.Object primaryKey, java.lang.String beanName, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to process JDOObjectNotFoundException. Logs the message and throws ObjectNotFoundException

param
primaryKey the PrimaryKey instance.
param
beanName the name of the calling bean.
param
ex the JDOException.
throws
ObjectNotFoundException.


        String msg = I18NHelper.getMessage(cmpMessages,
            "GEN.findbypk_exception_notfound", beanName, // NOI18N
            primaryKey.toString()); 

        cmpLifecycleLogger.log(Logger.FINER, msg, ex);
        throw new ObjectNotFoundException(msg);
    
public static voidhandleUpdateNotAllowedException(java.lang.String beanName)
Throws EJBException on attempted updates to the calling bean.

param
beanName the name of the calling bean.
throws
EJBException.

        String msg = I18NHelper.getMessage(cmpMessages,
            "GEN.update_not_allowed", beanName, // NOI18N
            findCallingMethodName());

        cmpLifecycleLogger.log(Logger.SEVERE, msg);
        throw new EJBException(msg);
    
public static voidlogFinderException(int level, java.lang.String beanName, java.lang.Exception ex)
Called from a CMP bean to log JDOException message thrown from a any finder or selector method, with the FinderLogger.

param
level the logging level as int.
param
beanName the name of the calling bean.
param
ex the Exception.


        if (cmpFinderLogger.isLoggable(level)) {
            cmpFinderLogger.log(level,
                    I18NHelper.getMessage(cmpMessages, 
                            "GEN.generic_method_exception", // NOI18N
                            beanName, findCallingMethodName()), ex);
        }
    
public static java.lang.StringlogJDOExceptionFromPKSetter(java.lang.String beanName, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to log JDOException message thrown from a PK setter method, with the InternalLogger. Returns generated message to the caller to be used for a IllegalStateException.

param
beanName the name of the calling bean.
param
ex the JDOException.
return
logged message as String.


        String msg = I18NHelper.getMessage(cmpMessages, "EXC_PKUpdate", // NOI18N
            beanName, findCallingMethodName()); 
        if (cmpInternalLogger.isLoggable(Logger.FINE)) {
            cmpInternalLogger.log(Logger.FINE, msg, ex);
        }

        return msg;
    
public static voidlogJDOExceptionWithFinderLogger(java.lang.String beanName, java.lang.Object[] params, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to log JDOException message thrown from a any finder or selector method, with the FinderLogger.

param
beanName the name of the calling bean.
param
params the Object[] of the parameter values for the finder or selector method.
param
ex the JDOException.


        String msg = null;
        if (params != null) {
            msg = I18NHelper.getMessage(cmpMessages,
                    "GEN.ejbSSReturnBody_exception", beanName, // NOI18N
                    findCallingMethodName(), 
                    java.util.Arrays.asList(params).toString());
        } else {
            msg = I18NHelper.getMessage(cmpMessages,
                    "GEN.ejbSSReturnBody_exception_woparams", beanName, // NOI18N
                    findCallingMethodName());
        }
        cmpFinderLogger.log(Logger.WARNING, msg, ex);
    
public static voidlogJDOExceptionWithInternalLogger(java.lang.String beanName, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to log JDOException message thrown from a any getter or setter method, with the InternalLogger.

param
beanName the name of the calling bean.
param
ex the JDOException.


        cmpInternalLogger.log(Logger.WARNING, 
                I18NHelper.getMessage(cmpMessages, 
                "GEN.generic_method_exception", // NOI18N
                beanName, findCallingMethodName()), ex);
    
public static voidlogJDOExceptionWithLifecycleLogger(java.lang.String key, java.lang.String beanName, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to log JDOException message with the LifecycleLogger.

param
key the key for the corresponding Bundle.
param
beanName the name of the calling bean.
param
ex the JDOException.


                                           
       
                  

        cmpLifecycleLogger.log(Logger.WARNING, 
                I18NHelper.getMessage(cmpMessages, key, 
                        beanName, findCallingMethodName()), ex);
    
public static voidlogJDOExceptionWithLifecycleLogger(java.lang.String key, java.lang.String beanName, java.lang.String paramList, com.sun.jdo.api.persistence.support.JDOException ex)
Called from a CMP bean to log JDOException message with the LifecycleLogger.

param
key the key for the corresponding Bundle.
param
beanName the name of the calling bean.
param
paramList the list of the concatenated parameters.
param
ex the JDOException.


        cmpLifecycleLogger.log(Logger.WARNING, 
                I18NHelper.getMessage(cmpMessages, key, 
                        beanName, findCallingMethodName(), paramList), 
                ex);