use of org.datanucleus.exceptions.NucleusException in project datanucleus-core by datanucleus.
the class MetaDataMerger method mergeMemberAnnotationsData.
/**
* Method to take a field/property XML metadata definition and merge in the Annotations metadata definition.
* This is tied pretty intrinsically to the AbstractMemberMetaData class and so could have been included there.
* @param primaryFmd The XML metadata Field definition (to be updated)
* @param annotFmd The Annotations metadata Field definition (to be merged into the XML definition)
* @throws NucleusException if an error occurs while merging the annotation info
*/
static void mergeMemberAnnotationsData(AbstractMemberMetaData primaryFmd, AbstractMemberMetaData annotFmd) {
if (annotFmd == null || primaryFmd == null) {
return;
}
if (primaryFmd.isInitialised() || primaryFmd.isPopulated()) {
throw new NucleusException(Localiser.msg("044107", primaryFmd.getClassName(), primaryFmd.getName())).setFatal();
}
if (primaryFmd.className == null && annotFmd.className != null) {
// If the Annotation is an overriding field, make sure we have the (real) class name
primaryFmd.className = annotFmd.className;
}
if (primaryFmd.containerMetaData == null && annotFmd.containerMetaData != null) {
primaryFmd.containerMetaData = annotFmd.containerMetaData;
primaryFmd.containerMetaData.parent = primaryFmd;
}
if (annotFmd.storeInLob) {
primaryFmd.storeInLob = true;
}
if (annotFmd.persistenceModifier != FieldPersistenceModifier.DEFAULT && primaryFmd.persistenceModifier == FieldPersistenceModifier.DEFAULT) {
primaryFmd.persistenceModifier = annotFmd.persistenceModifier;
}
if (annotFmd.defaultFetchGroup != null && primaryFmd.defaultFetchGroup == null) {
primaryFmd.defaultFetchGroup = annotFmd.defaultFetchGroup;
}
if (annotFmd.primaryKey != null) {
// "primary-key" will always have a value in XML so we just override if the annotation had it set
// This means that we don't allow overriding of the primary-key via XML
primaryFmd.primaryKey = annotFmd.primaryKey;
}
if (primaryFmd.table == null && annotFmd.table != null) {
primaryFmd.table = annotFmd.table;
}
if (primaryFmd.catalog == null && annotFmd.catalog != null) {
primaryFmd.catalog = annotFmd.catalog;
}
if (primaryFmd.schema == null && annotFmd.schema != null) {
primaryFmd.schema = annotFmd.schema;
}
if (primaryFmd.column == null && annotFmd.column != null) {
primaryFmd.column = annotFmd.column;
}
if (primaryFmd.dependent == null && annotFmd.dependent != null) {
primaryFmd.dependent = annotFmd.dependent;
}
if (primaryFmd.mappedBy == null && annotFmd.mappedBy != null) {
primaryFmd.mappedBy = annotFmd.mappedBy;
}
if (primaryFmd.valueStrategy == null && annotFmd.valueStrategy != null) {
primaryFmd.valueStrategy = annotFmd.valueStrategy;
}
if (primaryFmd.sequence == null && annotFmd.sequence != null) {
primaryFmd.sequence = annotFmd.sequence;
}
if (primaryFmd.valueGeneratorName == null && annotFmd.valueGeneratorName != null) {
primaryFmd.valueGeneratorName = annotFmd.valueGeneratorName;
}
if (primaryFmd.indexed == null && annotFmd.indexed != null) {
primaryFmd.indexed = annotFmd.indexed;
}
if (annotFmd.nullValue != NullValue.NONE) {
primaryFmd.nullValue = annotFmd.nullValue;
}
if (annotFmd.cascadePersist != null && primaryFmd.cascadePersist == null) {
primaryFmd.cascadePersist = annotFmd.cascadePersist;
}
if (annotFmd.cascadeUpdate != null && primaryFmd.cascadeUpdate == null) {
primaryFmd.cascadeUpdate = annotFmd.cascadeUpdate;
}
if (annotFmd.cascadeDelete != null && primaryFmd.cascadeDelete == null) {
primaryFmd.cascadeDelete = annotFmd.cascadeDelete;
}
if (annotFmd.cascadeRefresh != null && primaryFmd.cascadeRefresh == null) {
primaryFmd.cascadeRefresh = annotFmd.cascadeRefresh;
}
if (primaryFmd.joinMetaData == null && annotFmd.joinMetaData != null) {
primaryFmd.setJoinMetaData(annotFmd.joinMetaData);
}
if (primaryFmd.embeddedMetaData == null && annotFmd.embeddedMetaData != null) {
primaryFmd.setEmbeddedMetaData(annotFmd.embeddedMetaData);
}
if (primaryFmd.elementMetaData == null && annotFmd.elementMetaData != null) {
primaryFmd.setElementMetaData(annotFmd.elementMetaData);
}
if (primaryFmd.keyMetaData == null && annotFmd.keyMetaData != null) {
primaryFmd.setKeyMetaData(annotFmd.keyMetaData);
}
if (primaryFmd.valueMetaData == null && annotFmd.valueMetaData != null) {
primaryFmd.setValueMetaData(annotFmd.valueMetaData);
}
if (primaryFmd.orderMetaData == null && annotFmd.orderMetaData != null) {
primaryFmd.setOrderMetaData(annotFmd.orderMetaData);
}
if (primaryFmd.foreignKeyMetaData == null && annotFmd.foreignKeyMetaData != null) {
primaryFmd.foreignKeyMetaData = annotFmd.foreignKeyMetaData;
primaryFmd.foreignKeyMetaData.parent = primaryFmd;
}
if (primaryFmd.indexMetaData == null && annotFmd.indexMetaData != null) {
primaryFmd.indexMetaData = annotFmd.indexMetaData;
primaryFmd.indexMetaData.parent = primaryFmd;
}
if (primaryFmd.uniqueMetaData == null && annotFmd.uniqueMetaData != null) {
primaryFmd.uniqueMetaData = annotFmd.uniqueMetaData;
primaryFmd.uniqueMetaData.parent = primaryFmd;
}
if (primaryFmd.columns.isEmpty() && !annotFmd.columns.isEmpty()) {
// Columns specified in annotations but not in XML
ColumnMetaData[] annotColumns = annotFmd.getColumnMetaData();
if (annotColumns != null) {
for (int i = 0; i < annotColumns.length; i++) {
primaryFmd.columns.add(annotColumns[i]);
}
}
}
// Add any extensions supplied in the annotations
Map<String, String> annotExtensions = annotFmd.getExtensions();
if (annotExtensions != null) {
primaryFmd.addExtensions(annotExtensions);
}
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-core by datanucleus.
the class MetaDataMerger method mergeClassORMData.
/**
* Method to take a class JDO MetaData definition and merge in the ORM MetaData definition.
* If something is specified in the JDO MetaData and also in the ORM MetaData then the ORM MetaData takes precedence.
* This is tied pretty intrinsically to the AbstractClassMetaData class and so could have been included there.
* @param primaryCmd The JDO Class definition (to be updated)
* @param ormCmd The ORM Class definition (to be merged into the JDO Class definition)
* @param mmgr MetaData manager
* @throws NucleusException if an error occurs while merging the ORM info
*/
public static void mergeClassORMData(AbstractClassMetaData primaryCmd, AbstractClassMetaData ormCmd, MetaDataManager mmgr) {
if (ormCmd == null || primaryCmd == null) {
return;
}
if (primaryCmd.isInitialised() || primaryCmd.isPopulated()) {
throw new NucleusException(Localiser.msg("044068", primaryCmd.name)).setFatal();
}
if (NucleusLogger.METADATA.isDebugEnabled()) {
NucleusLogger.METADATA.debug(Localiser.msg("044096", primaryCmd.getFullClassName()));
}
// A). Simple data
if (ormCmd.getCatalog() != null) {
primaryCmd.catalog = ormCmd.getCatalog();
}
if (ormCmd.getSchema() != null) {
primaryCmd.schema = ormCmd.getSchema();
}
if (ormCmd.getTable() != null) {
primaryCmd.table = ormCmd.getTable();
}
if (ormCmd.detachable) {
primaryCmd.detachable = true;
}
if (!ormCmd.requiresExtent) {
primaryCmd.requiresExtent = false;
}
if (ormCmd.embeddedOnly) {
primaryCmd.embeddedOnly = true;
}
// B). Object data. Assume that if it exists at all we copy it all
if (ormCmd.getPrimaryKeyMetaData() != null) {
primaryCmd.setPrimaryKeyMetaData(ormCmd.getPrimaryKeyMetaData());
}
if (ormCmd.getInheritanceMetaData() != null) {
// TODO Support merging bit by bit
primaryCmd.setInheritanceMetaData(ormCmd.getInheritanceMetaData());
}
if (ormCmd.getIdentityMetaData() != null) {
// TODO Support merging bit by bit
primaryCmd.setIdentityMetaData(ormCmd.getIdentityMetaData());
}
if (ormCmd.getVersionMetaData() != null) {
VersionMetaData primVermd = primaryCmd.getVersionMetaData();
VersionMetaData ormVermd = ormCmd.getVersionMetaData();
if (primVermd != null) {
// Merge bit by bit
if (ormVermd.getVersionStrategy() != null) {
primVermd.setStrategy(ormVermd.getVersionStrategy());
}
if (ormVermd.getColumnName() != null) {
primVermd.setColumnName(ormVermd.getColumnName());
}
if (ormVermd.getColumnMetaData() != null) {
primVermd.setColumnMetaData(ormVermd.getColumnMetaData());
}
if (ormVermd.getIndexMetaData() != null) {
primVermd.setIndexMetaData(ormVermd.getIndexMetaData());
}
} else {
// Just use ORM version metadata
primaryCmd.setVersionMetaData(ormVermd);
}
}
if (ormCmd.listeners != null) {
if (primaryCmd.listeners == null) {
primaryCmd.listeners = new ArrayList();
}
primaryCmd.listeners.addAll(ormCmd.listeners);
}
if (ormCmd.queries != null) {
if (primaryCmd.queries == null) {
primaryCmd.queries = new ArrayList();
} else {
primaryCmd.queries.clear();
}
primaryCmd.queries.addAll(ormCmd.queries);
}
if (ormCmd.joins != null) {
primaryCmd.joins = null;
Iterator iter = ormCmd.joins.iterator();
while (iter.hasNext()) {
primaryCmd.addJoin((JoinMetaData) iter.next());
}
}
if (ormCmd.indexes != null) {
primaryCmd.indexes = null;
Iterator iter = ormCmd.indexes.iterator();
while (iter.hasNext()) {
primaryCmd.addIndex((IndexMetaData) iter.next());
}
}
if (ormCmd.foreignKeys != null) {
primaryCmd.foreignKeys = null;
Iterator iter = ormCmd.foreignKeys.iterator();
while (iter.hasNext()) {
primaryCmd.addForeignKey((ForeignKeyMetaData) iter.next());
}
}
if (ormCmd.uniqueConstraints != null) {
primaryCmd.uniqueConstraints = null;
Iterator iter = ormCmd.uniqueConstraints.iterator();
while (iter.hasNext()) {
primaryCmd.addUniqueConstraint((UniqueMetaData) iter.next());
}
}
if (ormCmd.fetchGroups != null) {
primaryCmd.fetchGroups = null;
Iterator iter = ormCmd.fetchGroups.iterator();
while (iter.hasNext()) {
primaryCmd.addFetchGroup((FetchGroupMetaData) iter.next());
}
}
if (ormCmd.unmappedColumns != null) {
primaryCmd.unmappedColumns = null;
Iterator<ColumnMetaData> iter = ormCmd.unmappedColumns.iterator();
while (iter.hasNext()) {
primaryCmd.addUnmappedColumn(iter.next());
}
}
// C). Add on any fields that weren't defined previously
for (int i = 0; i < ormCmd.getNoOfMembers(); i++) {
AbstractMemberMetaData ormFmd = ormCmd.getMetaDataForMemberAtRelativePosition(i);
AbstractMemberMetaData primaryFmd = primaryCmd.getMetaDataForMember(ormFmd.getName());
if (Boolean.TRUE.equals(ormFmd.primaryKey) && (primaryFmd == null || Boolean.FALSE.equals(primaryFmd.primaryKey))) {
// Root metadata (annotations/JDO file) had no PK info for this field but the ORM is trying to set it as the PK!
throw new NucleusUserException(Localiser.msg("044025", ormFmd.getFullFieldName())).setFatal();
}
if (primaryFmd == null) {
// Field not specified in JDO MetaData but is in ORM MetaData
AbstractMemberMetaData fmd = null;
if (ormFmd.className != null) {
// Overridden field for superclass that we have no JDO field for
// Copy the fmd for the actual class (if any).
// TODO Replace this with a copy of the JDO version of the field if available
AbstractMemberMetaData jdoFmd = mmgr.readMetaDataForMember(ormFmd.className, ormFmd.name);
if (jdoFmd == null) {
jdoFmd = mmgr.readMetaDataForMember(ormCmd.getPackageName() + "." + ormFmd.className, ormFmd.name);
}
if (jdoFmd != null) {
// Make a copy of the base field definition and merge the ORM
if (jdoFmd instanceof FieldMetaData) {
// Copy the JDO definition of the superclass since no JDO definition in this class
fmd = new FieldMetaData(primaryCmd, jdoFmd);
} else {
// Copy the JDO definition of the superclass since no JDO definition in this class
fmd = new PropertyMetaData(primaryCmd, (PropertyMetaData) jdoFmd);
}
fmd.className = ormFmd.className;
MetaDataMerger.mergeMemberORMData(fmd, ormFmd);
} else {
// No base field definition so just copy the ORM
if (ormFmd instanceof FieldMetaData) {
// Copy ORM field since no available definition
fmd = new FieldMetaData(primaryCmd, ormFmd);
} else {
// Copy ORM property since no available definition
fmd = new PropertyMetaData(primaryCmd, (PropertyMetaData) ormFmd);
}
fmd.className = ormFmd.className;
}
} else {
// Create a copy of the ORM field MetaData and add to JDO MetaData
if (ormFmd instanceof FieldMetaData) {
fmd = new FieldMetaData(primaryCmd, ormFmd);
} else {
fmd = new PropertyMetaData(primaryCmd, (PropertyMetaData) ormFmd);
}
}
primaryCmd.addMember(fmd);
} else {
// Field specified in JDO MetaData so merge in ORM MetaData
MetaDataMerger.mergeMemberORMData(primaryFmd, ormFmd);
}
}
// Add any extensions supplied in the ORM MetaData
Map<String, String> ormExtensions = ormCmd.getExtensions();
if (ormExtensions != null) {
primaryCmd.addExtensions(ormExtensions);
}
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-core by datanucleus.
the class MetaDataUtils method getFileMetaDataForInputFiles.
/**
* Method to take the provided input files and returns the FileMetaData that they implies.
* Loads the files into the provided MetaDataManager in the process.
* @param metaDataMgr Manager for MetaData
* @param clr ClassLoader resolver
* @param inputFiles Input metadata/class files
* @return The FileMetaData for the input
* @throws NucleusException Thrown if error(s) occur in processing the input
*/
public static FileMetaData[] getFileMetaDataForInputFiles(MetaDataManager metaDataMgr, ClassLoaderResolver clr, String[] inputFiles) {
FileMetaData[] filemds = null;
// Read in the specified MetaData files - errors in MetaData will return exceptions and so we stop
String msg = null;
try {
// Split the input files into MetaData files and classes
Set<String> metadataFiles = new HashSet<>();
Set<String> classNames = new HashSet<>();
for (int i = 0; i < inputFiles.length; i++) {
if (inputFiles[i].endsWith(".class")) {
// Class file
URL classFileURL = null;
try {
classFileURL = new URL("file:" + inputFiles[i]);
} catch (Exception e) {
msg = Localiser.msg("014013", inputFiles[i]);
NucleusLogger.METADATA.error(msg);
throw new NucleusUserException(msg);
}
String className = null;
try {
className = ClassUtils.getClassNameForFileURL(classFileURL);
classNames.add(className);
} catch (Throwable e) {
// Fallback to method that parses along the filename
className = ClassUtils.getClassNameForFileName(inputFiles[i], clr);
if (className != null) {
classNames.add(className);
} else {
NucleusLogger.METADATA.info("File \"" + inputFiles[i] + "\" could not be resolved to a class name, so ignoring." + " Specify it as a class explicitly using persistence.xml to overcome this", e);
}
}
} else {
// MetaData file
metadataFiles.add(inputFiles[i]);
}
}
// Initialise the MetaDataManager using the mapping files and class names
FileMetaData[] filemds1 = metaDataMgr.loadMetadataFiles(metadataFiles.toArray(new String[metadataFiles.size()]), null);
FileMetaData[] filemds2 = metaDataMgr.loadClasses(classNames.toArray(new String[classNames.size()]), null);
filemds = new FileMetaData[filemds1.length + filemds2.length];
int pos = 0;
for (int i = 0; i < filemds1.length; i++) {
filemds[pos++] = filemds1[i];
}
for (int i = 0; i < filemds2.length; i++) {
filemds[pos++] = filemds2[i];
}
} catch (Exception e) {
// Error reading input files
msg = Localiser.msg("014014", e.getMessage());
NucleusLogger.METADATA.error(msg, e);
throw new NucleusUserException(msg, e);
}
return filemds;
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-core by datanucleus.
the class MetaDataParser method parseMetaDataURL.
/**
* Method to parse a MetaData file given the URL of the file.
* @param url Url of the metadata file
* @param handlerName Name of the handler plugin to use when parsing
* @return The MetaData for this file
* @throws NucleusException thrown if error occurred
*/
public MetaData parseMetaDataURL(URL url, String handlerName) {
if (url == null) {
String msg = Localiser.msg("044031");
NucleusLogger.METADATA.error(msg);
throw new NucleusException(msg);
}
InputStream in = null;
try {
in = url.openStream();
} catch (Exception ignore) {
}
if (in == null) {
try {
in = new FileInputStream(StringUtils.getFileForFilename(url.getFile()));
} catch (Exception ignore) {
}
}
if (in == null) {
NucleusLogger.METADATA.error(Localiser.msg("044032", url.toString()));
throw new NucleusException(Localiser.msg("044032", url.toString()));
}
// Parse the file
return parseMetaDataStream(in, url.toString(), handlerName);
}
use of org.datanucleus.exceptions.NucleusException in project datanucleus-core by datanucleus.
the class MetaDataParser method parseMetaDataStream.
/**
* Method to parse a MetaData file given an InputStream.
* Closes the input stream when finished.
* @param in input stream
* @param filename Name of the file (if applicable)
* @param handlerName Name of the handler plugin to use when parsing
* @return The MetaData for this file
* @throws NucleusException thrown if error occurred
*/
public MetaData parseMetaDataStream(InputStream in, String filename, String handlerName) {
if (in == null) {
throw new NullPointerException("input stream is null");
}
if (NucleusLogger.METADATA.isDebugEnabled()) {
NucleusLogger.METADATA.debug(Localiser.msg("044030", filename, handlerName, validate ? "true" : "false"));
}
try {
synchronized (parser) {
// Generate the required handler to process this metadata
DefaultHandler handler = null;
try {
parser.getXMLReader().setEntityResolver(entityResolver);
if ("persistence".equalsIgnoreCase(handlerName)) {
// "persistence.xml"
handler = new org.datanucleus.metadata.xml.PersistenceFileMetaDataHandler(mgr, filename, entityResolver);
} else {
// Fallback to the plugin mechanism for other MetaData handlers
Class[] argTypes = new Class[] { ClassConstants.METADATA_MANAGER, ClassConstants.JAVA_LANG_STRING, EntityResolver.class };
Object[] argValues = new Object[] { mgr, filename, entityResolver };
handler = (DefaultHandler) pluginMgr.createExecutableExtension("org.datanucleus.metadata_handler", "name", handlerName, "class-name", argTypes, argValues);
if (handler == null) {
// Plugin of this name not found
throw new NucleusUserException(Localiser.msg("044028", handlerName)).setFatal();
}
}
} catch (Exception e) {
String msg = Localiser.msg("044029", handlerName, e.getMessage());
throw new NucleusException(msg, e);
}
// Set whether to validate
((AbstractMetaDataHandler) handler).setValidate(validate);
// Parse the metadata
parser.parse(in, handler);
// Return the FileMetaData that has been parsed
return ((AbstractMetaDataHandler) handler).getMetaData();
}
} catch (NucleusException e) {
throw e;
} catch (Exception e) {
Throwable cause = e;
if (e instanceof SAXException) {
cause = ((SAXException) e).getException();
}
cause = e.getCause() == null ? cause : e.getCause();
NucleusLogger.METADATA.error(Localiser.msg("044040", filename, cause));
if (cause instanceof InvalidMetaDataException) {
throw (InvalidMetaDataException) cause;
}
throw new NucleusException(Localiser.msg("044033", e), cause);
} finally {
try {
in.close();
} catch (Exception ignore) {
// Do nothing
}
}
}
Aggregations