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