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);
}
}
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;
}
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;
}
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());
}
}
}
}
Aggregations