Search in sources :

Example 1 with SunCmpMappings

use of com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings in project Payara by payara.

the class MappingGenerator method generateMapping.

/**
 * This method will load mapping classes if there is sun-cmp-mappings.xml,
 * otherwise it will call the database generation backend to create
 * mapping classes and schema.  It also generates *.dbschema and
 * sun-cmp-mappings.xml in application dir if it is
 * in creating mapping classes mode.
 * @param ctx an object containing CLI options for
 * the database generation backend
 * @param inputFilesPath the directory where sun-cmp-mappings.xml is located
 * @param generatedXmlsPath the directory where the generated files are located
 * @param classout the directory where the classes are located
 * @param ignoreSunDeploymentDescriptors use java2db generation if set to <code>true</code>.
 * @return a SchemaElement for mapping classes mapped to
 * @throws IOException
 * @throws DBException
 * @throws ModelException
 * @throws Schema2BeansException
 * @throws SQLException
 * @throws GeneratorException
 * @throws ConversionException
 */
public SchemaElement generateMapping(DeploymentContext ctx, String inputFilesPath, String generatedXmlsPath, File classout, boolean ignoreSunDeploymentDescriptors) throws IOException, DBException, ModelException, Schema2BeansException, SQLException, GeneratorException, ConversionException {
    SchemaElement schema = null;
    if (ctx == null)
        isVerifyFlag = true;
    File cmpMappingFile = getSunCmpMappingFile(inputFilesPath);
    boolean mappedBeans = !ignoreSunDeploymentDescriptors && cmpMappingFile.exists();
    ResourceReferenceDescriptor cmpResource = checkOrCreateCMPResource(mappedBeans);
    // Remember whether or not this mapping was created by Java2DB.
    isJavaToDatabaseFlag = DeploymentHelper.isJavaToDatabase(cmpResource.getSchemaGeneratorProperties());
    // We *must* get a vendor name if either the beans are not mapped, or
    // they are mapped and the javaToDatabase flag is set.
    boolean mustHaveDBVendorName = !mappedBeans || (mappedBeans && isJavaToDatabaseFlag);
    // Read deployment settings from the deployment descriptor
    // and CLI options.
    Results deploymentArguments = getDeploymentArguments(ctx, cmpResource, mustHaveDBVendorName);
    dbVendorName = deploymentArguments.getDatabaseVendorName();
    if (mappedBeans) {
        // If it is from verify, skip deployment arguments check.
        if (!isVerifyFlag) {
            // Warning for user, if required.
            String warning = null;
            if (isJavaToDatabaseFlag) {
                // as per the mapping.
                if (deploymentArguments.hasUniqueTableNames()) {
                    warning = I18NHelper.getMessage(messages, // NOI18N
                    "EXC_DisallowJava2DBUniqueTableNames", bundle.getApplication().getRegistrationName(), JDOCodeGeneratorHelper.getModuleName(bundle));
                    logger.warning(warning);
                }
            } else if (deploymentArguments.hasJavaToDatabaseArgs()) {
                // If beans are already mapped but the user gave any Java2DB
                // command line arguments, warn the user that these args
                // should not be used when module is already mapped.
                warning = I18NHelper.getMessage(messages, // NOI18N
                "EXC_DisallowJava2DBCLIOverrides", bundle.getApplication().getRegistrationName(), JDOCodeGeneratorHelper.getModuleName(bundle));
                logger.warning(warning);
            }
            if (warning != null) {
                ActionReport subActionReport = ctx.getActionReport().addSubActionsReport();
                // Propagte warning to client side so that the deployer can see the warning.
                Java2DBProcessorHelper.warnUser(subActionReport, warning);
            }
        }
        // Sun-cmp-mapping.xml exists, use normal MappingClass loading
        SunCmpMappings sunCmpMappings = getSunCmpMappings(cmpMappingFile);
        // Ensure that there is a dbschema for each element of
        // sunCmpMappings.
        ensureDBSchemaExistence(cmpResource, sunCmpMappings, inputFilesPath, classout);
        // load real mapping model and jdo model in memory
        Map mappingClasses = loadMappingClasses(sunCmpMappings, getClassLoader());
        // Get schema from one of the mapping classes.
        // The mapping class element may be null if there is inconsistency
        // in sun-cmp-mappings.xml and ejb-jar.xml. For example,
        // the bean has mapping information in sun-cmp-mappings.xml but
        // no definition in the ejb-jar.xml.
        // So iterate over the mappings until the 1st non-null is found.
        MappingClassElement mc = null;
        Iterator iter = mappingClasses.values().iterator();
        while (iter.hasNext()) {
            mc = (MappingClassElement) iter.next();
            if (mc != null) {
                schema = SchemaElement.forName(mc.getDatabaseRoot());
                break;
            }
        }
        if (logger.isLoggable(Logger.FINE)) {
            logger.fine(// NOI18N
            "Loaded mapped beans for " + cmpResource.getJndiName() + ", isJavaToDatabase=" + // NOI18N
            isJavaToDatabaseFlag);
        }
    } else {
        // Generate mapping file and dbschema, since either
        // sun-cmp-mappings.xml does not exist (e.g. user didn't yet map)
        // or DeploymentContext is null (e.g. running under auspices of AVK).
        DatabaseGenerator.Results results = generateMappingClasses(dbVendorName, deploymentArguments.getUseUniqueTableNames(), deploymentArguments.getUserPolicy(), inputFilesPath);
        // java2db from verifier should not save anything to disk
        if (!isVerifyFlag) {
            // save SunCmpMapping to sun-cmp-mappings.xml
            // in generated XML dir
            writeSunCmpMappingFile(results.getMappingClasses(), getSunCmpMappingFile(generatedXmlsPath));
            schema = results.getSchema();
            // save schema to dbschema file in generated XML dir
            writeSchemaFile(schema, classout);
            setJavaToDatabase(cmpResource, true);
        }
    }
    return schema;
}
Also used : MappingClassElement(com.sun.jdo.api.persistence.model.mapping.MappingClassElement) ActionReport(org.glassfish.api.ActionReport) DatabaseGenerator(com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator) Iterator(java.util.Iterator) SchemaElement(org.netbeans.modules.dbschema.SchemaElement) ResourceReferenceDescriptor(com.sun.enterprise.deployment.ResourceReferenceDescriptor) MappingFile(com.sun.jdo.api.persistence.mapping.ejb.MappingFile) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) SunCmpMappings(com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings)

Example 2 with SunCmpMappings

use of com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings in project Payara by payara.

the class MappingGenerator method getSunCmpMappings.

/**
 * Loads sun-cmp-mapping.xml into memory as SunCmpMappings
 * @param cmpMappingFile a file of sun-cmp-mappings.xml
 * @return a SunCmpMappings object
 * @throws IOException
 * @throws Schema2BeansException
 */
private SunCmpMappings getSunCmpMappings(File cmpMappingFile) throws IOException, Schema2BeansException, GeneratorException {
    InputStream is = null;
    BufferedInputStream iasMapping = null;
    SunCmpMappings sunCmpMapping = null;
    if (cmpMappingFile.length() == 0) {
        throw JDOCodeGeneratorHelper.createGeneratorException("CMG.BeansFileSizeIsZero", // NOI18N
        bundle);
    }
    try {
        is = new FileInputStream(cmpMappingFile);
        iasMapping = new BufferedInputStream(is);
        sunCmpMapping = SunCmpMappings.createGraph(iasMapping);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ex) {
                if (logger.isLoggable(Logger.FINE))
                    logger.fine(ex.toString());
            }
        }
        if (iasMapping != null) {
            try {
                iasMapping.close();
            } catch (Exception ex) {
                if (logger.isLoggable(Logger.FINE))
                    logger.fine(ex.toString());
            }
        }
    }
    try {
        sunCmpMapping.validate();
    } catch (ValidateException ex) {
        throw JDOCodeGeneratorHelper.createGeneratorException("CMG.InvalidSunCmpMappingsFile", bundle, // NOI18N
        ex);
    }
    return sunCmpMapping;
}
Also used : ValidateException(org.netbeans.modules.schema2beans.ValidateException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SunCmpMappings(com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings) FileInputStream(java.io.FileInputStream) ModelException(com.sun.jdo.api.persistence.model.ModelException) ConversionException(com.sun.jdo.api.persistence.mapping.ejb.ConversionException) ValidateException(org.netbeans.modules.schema2beans.ValidateException) SQLException(java.sql.SQLException) DBException(org.netbeans.modules.dbschema.DBException) Schema2BeansException(org.netbeans.modules.schema2beans.Schema2BeansException) IOException(java.io.IOException) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException)

Aggregations

SunCmpMappings (com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings)2 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)1 ConversionException (com.sun.jdo.api.persistence.mapping.ejb.ConversionException)1 MappingFile (com.sun.jdo.api.persistence.mapping.ejb.MappingFile)1 ModelException (com.sun.jdo.api.persistence.model.ModelException)1 MappingClassElement (com.sun.jdo.api.persistence.model.mapping.MappingClassElement)1 DatabaseGenerator (com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator)1 GeneratorException (com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 ActionReport (org.glassfish.api.ActionReport)1 DBException (org.netbeans.modules.dbschema.DBException)1 SchemaElement (org.netbeans.modules.dbschema.SchemaElement)1