Search in sources :

Example 1 with GeneratorException

use of com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException in project Payara by payara.

the class JDOCodeGenerator method generate.

/**
 * @see CMPGenerator#generate(IASEjbCMPEntityDescriptor, File, File)
 */
public void generate(IASEjbCMPEntityDescriptor ejbcmp, File srcout, File classout) throws GeneratorException {
    String beanName = ejbcmp.getName();
    // StringBuffer to store validation exception messages if there are any.
    // If there are no validation exceptions, the reference will be null.
    StringBuffer validateex = null;
    boolean debug = logger.isLoggable(Logger.FINE);
    if (debug)
        // NOI18N
        logger.fine("gen file in " + srcout.getAbsolutePath());
    // We need to create a new ArrayList because model validation
    // returns an unmodifiable list.  This may be a place to look
    // for a performance improvement later for the case of empty
    // or singleton collection (extra copies).
    Collection c = new ArrayList(validateModel(ejbcmp));
    // if the mapping info is not present, throw the exception and
    // stop the generation process
    GeneratorException mappingMissingEx = getMappingMissingException(c);
    if (mappingMissingEx != null)
        throw mappingMissingEx;
    c.addAll(validateSupported(ejbcmp));
    JDOConcreteBeanGenerator cmpGenerator = getCMPGenerator(ejbcmp);
    MethodHelper mh = new MethodHelper(ejbcmp);
    c.addAll(cmpGenerator.validate(mh, beanName));
    if (!c.isEmpty()) {
        // Validation failed the test. We will try to display all the
        // exceptions in a concatenated message and a GeneratorException
        // is thrown.
        validateex = new StringBuffer();
        Iterator iter = c.iterator();
        while (iter.hasNext()) {
            Exception ex = (Exception) iter.next();
            if (debug)
                // NOI18N
                logger.log(Logger.FINE, "validation exception: ", ex);
            // NOI18N
            validateex.append(ex.getMessage()).append('\n');
        }
        if (!ignoreValidationResults)
            throw JDOCodeGeneratorHelper.createGeneratorException(// NOI18N
            "CMG.ExceptionInValidate", beanName, bundle, validateex.toString());
    }
    try {
        Collection<File> newfiles = null;
        if (!ejbcmp.isEJB20())
            ejbcmp.setQueryParser(jdoqlParamDeclParser);
        // IMPORTANT:
        // Concrete impl class generation must happen before generation of
        // PC class as cmpGenerator will override cascadeDelete (DeleteAction)
        // property if it is set, and generatePC() saves .mapping file.
        newfiles = cmpGenerator.generate(mh, beanName, appName, srcout, classout);
        files.addAll(newfiles);
        newfiles = generatePC(ejbcmp, srcout, classout);
        files.addAll(newfiles);
        if (validateex != null)
            throw JDOCodeGeneratorHelper.createGeneratorException(// NOI18N
            "CMG.ExceptionInValidate", beanName, bundle, validateex.toString());
    } catch (JDOUserException e) {
        // messages if there are any.
        throw JDOCodeGeneratorHelper.createGeneratorException(// NOI18N
        "CMG.ExceptionInGenerate", beanName, bundle, e, validateex);
    } catch (EJBQLException e) {
        // validation messages if there are any.
        throw JDOCodeGeneratorHelper.createGeneratorException(// NOI18N
        "CMG.ExceptionInGenerate", beanName, bundle, e, validateex);
    } catch (IOException e) {
        // validation messages if there are any.
        throw JDOCodeGeneratorHelper.createGeneratorException(// NOI18N
        "CMG.IOExceptionInGenerate", beanName, bundle, e, validateex);
    }
}
Also used : ArrayList(java.util.ArrayList) EJBQLException(com.sun.jdo.spi.persistence.support.ejb.ejbqlc.EJBQLException) IOException(java.io.IOException) JDOUserException(com.sun.jdo.api.persistence.support.JDOUserException) ModelException(com.sun.jdo.api.persistence.model.ModelException) EJBQLException(com.sun.jdo.spi.persistence.support.ejb.ejbqlc.EJBQLException) SQLException(java.sql.SQLException) DBException(org.netbeans.modules.dbschema.DBException) JDOUserException(com.sun.jdo.api.persistence.support.JDOUserException) Schema2BeansException(org.netbeans.modules.schema2beans.Schema2BeansException) IOException(java.io.IOException) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException) ConversionException(com.sun.jdo.api.persistence.mapping.ejb.ConversionException) Iterator(java.util.Iterator) Collection(java.util.Collection) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException) File(java.io.File)

Example 2 with GeneratorException

use of com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException in project Payara by payara.

the class JDOCodeGenerator method validateEJB.

/**
 * Validate the bean. For now, validate EJBQL for all finders and
 * selectors of this bean.
 *
 * @param descr the IASEjbCMPEntityDescriptor for this CMP bean.
 * @return a Collection of Exception instances for each found
 * validation error.
 */
private Collection validateEJB(IASEjbCMPEntityDescriptor descr) {
    Collection c = null;
    try {
        JDOConcreteBeanGenerator cmpGenerator = getCMPGenerator(descr);
        c = cmpGenerator.validate(new MethodHelper(descr), descr.getName());
    } catch (GeneratorException e) {
        c = new ArrayList();
        c.add(e);
    }
    return c;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException)

Example 3 with GeneratorException

use of com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException 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)

Example 4 with GeneratorException

use of com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException in project Payara by payara.

the class MappingGenerator method ensureDBSchemaExistence.

/**
 * Check that there is a dbschema for each element of the SunCmpMappings.
 * For those which are missing, create a corresponding .dbschema file.
 * @param cmpResource Provides JNDI name for getting database connection
 * @param sunCmpMappings SunCmpMappings which is checked for having schema
 * @param inputFilesPath the directory where this bundle's files are located
 * @param classout the directory where the classes are located
 * @exception DBException Thrown if database model throws it
 * @exception IOException Thrown if .dbschema file cannot be created.
 * @exception SQLException Thrown if we cannot get get required info from
 * the database.
 */
private void ensureDBSchemaExistence(ResourceReferenceDescriptor cmpResource, SunCmpMappings sunCmpMappings, String inputFilesPath, File classout) throws DBException, SQLException, GeneratorException {
    String generatedSchemaName = getInfoHelper().getSchemaNameToGenerate();
    Set tables = new HashSet();
    int size = sunCmpMappings.sizeSunCmpMapping();
    // and save it.
    for (int i = 0; i < size; i++) {
        SunCmpMapping sunCmpMapping = sunCmpMappings.getSunCmpMapping(i);
        String schemaName = sunCmpMapping.getSchema();
        if (StringHelper.isEmpty(schemaName)) {
            if (!isVerifyFlag) {
                // The tables in this section need to be captured.
                addAllTables(sunCmpMapping, tables);
                sunCmpMapping.setSchema(generatedSchemaName);
            } else {
                // If it is from verifier, capture schema internally
                // to perform sun-cmp-mappings.xml and EJB validation
                getConversionHelper().setEnsureValidation(false);
            }
        } else {
            File dbschemaFile = new File(new StringBuffer(inputFilesPath).append(File.separator).append(schemaName).append(DBSCHEMA_EXTENSION).toString());
            if (!(dbschemaFile.exists() && dbschemaFile.isFile() && dbschemaFile.canRead())) {
                throw new GeneratorException(I18NHelper.getMessage(// NOI18N
                messages, // NOI18N
                "CMG.MissingDBSchema", bundle.getApplication().getRegistrationName(), JDOCodeGeneratorHelper.getModuleName(bundle), schemaName));
            }
        }
    }
    // Now we need to go and capture those tables.
    if (tables.size() > 0) {
        String userSchema = null;
        Connection con = DeploymentHelper.getConnection(cmpResource.getJndiName());
        DatabaseMetaData dmd = con.getMetaData();
        if (DBVendorTypeHelper.requireUpperCaseSchema(dmd)) {
            userSchema = dmd.getUserName().trim().toUpperCase();
        }
        ConnectionProvider cp = new ConnectionProvider(con, dmd.getDriverName().trim());
        if (userSchema != null) {
            cp.setSchema(userSchema);
        }
        OutputStream outstream = null;
        try {
            SchemaElementImpl outSchemaImpl = new SchemaElementImpl(cp);
            SchemaElement schemaElement = new SchemaElement(outSchemaImpl);
            schemaElement.setName(DBIdentifier.create(generatedSchemaName));
            if (dmd.getDatabaseProductName().compareToIgnoreCase("MYSQL") == 0)
                outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), true);
            else
                outSchemaImpl.initTables(cp, new LinkedList(tables), new LinkedList(), false);
            outstream = new FileOutputStream(new File(classout, new StringBuffer(generatedSchemaName).append(DBSCHEMA_EXTENSION).toString()));
            // XXX Unfortunately, if SchemaElement.save gets an
            // IOException, it prints the stack trace but does not
            // let us handle it :-(
            schemaElement.save(outstream);
        } catch (IOException ex) {
            // Catch FileNotFound, etc.
            throw JDOCodeGeneratorHelper.createGeneratorException("CMG.CannotSaveDBSchema", bundle, // NOI18N
            ex);
        } finally {
            cp.closeConnection();
            try {
                if (outstream != null) {
                    outstream.close();
                }
            } catch (IOException ex) {
                if (logger.isLoggable(Logger.FINE))
                    logger.fine(ex.toString());
            }
        }
    }
}
Also used : SunCmpMapping(com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping) Set(java.util.Set) HashSet(java.util.HashSet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) IOException(java.io.IOException) DatabaseMetaData(java.sql.DatabaseMetaData) LinkedList(java.util.LinkedList) ConnectionProvider(org.netbeans.modules.dbschema.jdbcimpl.ConnectionProvider) SchemaElementImpl(org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl) FileOutputStream(java.io.FileOutputStream) SchemaElement(org.netbeans.modules.dbschema.SchemaElement) GeneratorException(com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException) MappingFile(com.sun.jdo.api.persistence.mapping.ejb.MappingFile) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

GeneratorException (com.sun.jdo.spi.persistence.support.ejb.codegen.GeneratorException)4 IOException (java.io.IOException)3 ConversionException (com.sun.jdo.api.persistence.mapping.ejb.ConversionException)2 ModelException (com.sun.jdo.api.persistence.model.ModelException)2 File (java.io.File)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 DBException (org.netbeans.modules.dbschema.DBException)2 Schema2BeansException (org.netbeans.modules.schema2beans.Schema2BeansException)2 MappingFile (com.sun.jdo.api.persistence.mapping.ejb.MappingFile)1 SunCmpMapping (com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMapping)1 SunCmpMappings (com.sun.jdo.api.persistence.mapping.ejb.beans.SunCmpMappings)1 JDOUserException (com.sun.jdo.api.persistence.support.JDOUserException)1 EJBQLException (com.sun.jdo.spi.persistence.support.ejb.ejbqlc.EJBQLException)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1