FileDocCategorySizeDatePackage
CMPProcessor.javaAPI DocGlassfish v2 API11403Fri May 04 22:34:48 BST 2007com.sun.jdo.spi.persistence.support.ejb.ejbc

CMPProcessor

public class CMPProcessor extends BaseProcessor
If the application contains cmp 2.x beans process them. Check if tables have to created or dropped depending on where we are called in a deploy/undeploy case.
author
pramodg

Fields Summary
Constructors Summary
public CMPProcessor(com.sun.enterprise.deployment.backend.DeploymentEventInfo info, boolean create, String cliCreateTables, String cliDropAndCreateTables, String cliDropTables)
Creates a new instance of CMPProcessor

param
info the deployment info object.
param
create true if tables must be created as part of this event
param
cliCreateTables the cli string related to creating tables at deployment time.
param
cliDropAndCreateTables the cli string that indicates that old tables have to be dropped and new tables created.
param
cliDropTables the cli string to indicate that the tables have to dropped at undeploy time.

        super(info, create, cliCreateTables,
            cliDropAndCreateTables, cliDropTables);
    
Methods Summary
private voidconstructJdbcFileNames(com.sun.enterprise.deployment.EjbBundleDescriptor ejbBundle)
Construct the name of the create and drop jdbc ddl files that would be created. These name would be either obtained from the persistence.xml file (if the user has defined them) or we would create default filenames

param
ejbBundle the ejb bundle descriptor being worked on.

        String filePrefix = EJBHelper.getDDLNamePrefix(ejbBundle);
        
        createJdbcFileName = filePrefix + CREATE_DDL_JDBC_FILE_SUFFIX;
        dropJdbcFileName = filePrefix + DROP_DDL_JDBC_FILE_SUFFIX;
    
private booleanexecuteDDLStatement(java.io.File fileName, java.lang.String resourceName)
Get the ddl files eventually executed against the database. This method deals with both create and drop ddl files.

param
fileName the create or drop jdbc ddl file.
param
resourceName the jdbc resource name that would be used to get a connection to the database.
return
true if the tables were successfully created/dropped from the database.

        boolean result = false;
        Connection conn = null;
        Statement sql = null;
        try {
            try {           
                    conn = DeploymentHelper.getConnection(resourceName);
                    sql = conn.createStatement();
                    result = true;
                } catch (SQLException ex) {
                    cannotConnect(resourceName, ex);
                } catch (JDOFatalUserException ex) {
                    cannotConnect(resourceName, ex);
                }
        
                if(result) {               
                    executeDDLs(fileName, sql);
                }
        } catch (IOException e) {
            fileIOError(application.getRegistrationName(), e);            
        } finally { 
            closeConn(conn);
        }
        return result;        
    
private voidexecuteStatements(java.lang.String fileName, java.lang.String resourceName)
If the file is present, execute the corresponding statements in the database.

param
fileName the name of the file to execute.
param
resourceName the jdbc resource name that would be used to get a connection to the database.

        File file = getDDLFile(
                appGeneratedLocation + fileName, false);
        if (file.exists()) {
            executeDDLStatement(file, resourceName);
        } else {
            logI18NWarnMessage(
                 ((create)? "ejb.BaseProcessor.cannotcreatetables" //NOI18N
                 : "ejb.BaseProcessor.cannotdroptables"), //NOI18N
                appRegisteredName, file.getName(), null);
        }
    
protected booleangetCreateTablesValue(com.sun.enterprise.deployment.ResourceReferenceDescriptor cmpResource)
We need to create tables only on deploy, and only if the CLI options cliCreateTables or cliDropAndCreateTables are not set to false. If those options are not set (UNDEFINED) the value is taken from the create-tables-at-deploy element of the sun-ejb-jar.xml (cmpResource.isCreateTablesAtDeploy()).

param
cmpResource the cmp resource reference descriptor.
return
true if tables have to created.

            boolean createTables = 
                create 
                    && (cliCreateTables.equals(Constants.TRUE) 
                        || (cmpResource.isCreateTablesAtDeploy()
                            && cliCreateTables.equals(Constants.UNDEFINED)));
            return createTables;
    
protected booleangetDropTablesValue(com.sun.enterprise.deployment.ResourceReferenceDescriptor cmpResource)
We need to drop tables on undeploy and redeploy, if the corresponding CLI options cliDropAndCreateTables (for redeploy) or cliDropTables (for undeploy) are not set to false. If the corresponding option is not set (UNDEFINED) the value is taken from the drop-tables-at-undeploy element of the sun-ejb-jar.xml (cmpResource.isDropTablesAtUndeploy()).

param
cmpResource the cmp resource reference descriptor.
return
true if the tables have to be dropped.

        boolean dropTables = 
            (!create 
                && (cliDropAndCreateTables.equals(Constants.TRUE) 
                    || cliDropTables.equals(Constants.TRUE) 
                    || (cmpResource.isDropTablesAtUndeploy()
                        && cliDropAndCreateTables.equals(Constants.UNDEFINED)
                        && cliDropTables.equals(Constants.UNDEFINED))));
       return dropTables;
    
private voidprocessAppBundle(com.sun.enterprise.deployment.EjbBundleDescriptor bundle)
This method does all the work of checking and processing each ejb bundle descriptor.

param
bundle the ejb bundle descriptor that is being worked on.

           if (!bundle.containsCMPEntity()) {
                return;
            }

            ResourceReferenceDescriptor cmpResource = 
                    bundle.getCMPResourceReference();

            // If this bundle's beans are not created by Java2DB, then skip to
            // next bundle.
            if (!DeploymentHelper.isJavaToDatabase(
                    cmpResource.getSchemaGeneratorProperties())) {
                return;
            }
      
            boolean createTables = getCreateTablesValue(cmpResource) ;                   
            boolean dropTables = getDropTablesValue(cmpResource);

            if (debug) {                
                logger.fine("ejb.CMPProcessor.createanddroptables", //NOI18N
                    new Object[] {new Boolean(createTables), new Boolean(dropTables)});
            }

            if (!createTables && !dropTables) { 
                // Nothing to do.
                return;
            }
        
            // At this point of time we are sure that we would need to create 
            // the sql/jdbc files required to create or drop objects from the 
            // database. Hence setup the required directories from the info object.
            setApplicationLocation();
            setGeneratedLocation();
            
            constructJdbcFileNames(bundle);
            if (debug) {
                logger.fine("ejb.CMPProcessor.createanddropfilenames", 
                    createJdbcFileName, dropJdbcFileName); //NOI18N            
            }            

            String resourceName = cmpResource.getJndiName();
            if (dropTables) {
                executeStatements(dropJdbcFileName, resourceName);
            } else { 
                // else can only be createTables as otherwise we'll not reach here
                executeStatements(createJdbcFileName, resourceName);
            }
   
protected voidprocessApplication()
The entry point into this class. Process any ejb bundle descriptors if defined for this application.

        
        Collection bundleCollection = application.getEjbBundleDescriptors();
        if ( bundleCollection  == null) {
            return;
        }
        Iterator bundleItr = bundleCollection.iterator();

        // Process only those Bundle descriptors that contain CMP beans.
        while ( bundleItr.hasNext() ) {
             processAppBundle((EjbBundleDescriptor)bundleItr.next()); 
        }