use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-core by datanucleus.
the class SchemaTool method getNucleusContextForMode.
/**
* Method to create a NucleusContext for the specified mode of SchemaTool
* @param mode Mode of operation of SchemaTool
* @param api Persistence API
* @param userProps Map containing user provided properties (usually input via a file)
* @param persistenceUnitName Name of the persistence-unit (if any)
* @param ddlFile Name of a file to output DDL to
* @param verbose Verbose mode
* @param ignoreMetaDataForMissingClasses Whether to ignore metadata for missing classes
* @return The NucleusContext to use
* @throws NucleusException Thrown if an error occurs in creating the required NucleusContext
*/
public static StoreNucleusContext getNucleusContextForMode(Mode mode, String api, Map userProps, String persistenceUnitName, String ddlFile, boolean verbose, boolean ignoreMetaDataForMissingClasses) {
// Extract any properties that affect NucleusContext startup
Map startupProps = null;
if (userProps != null) {
// Possible properties to check for
for (String startupPropName : AbstractNucleusContext.STARTUP_PROPERTIES) {
if (userProps.containsKey(startupPropName)) {
if (startupProps == null) {
startupProps = new HashMap();
}
startupProps.put(startupPropName, userProps.get(startupPropName));
}
}
}
// Initialise the context for this API
PersistenceNucleusContext nucleusCtx = new PersistenceNucleusContextImpl(api, startupProps);
Configuration propConfig = nucleusCtx.getConfiguration();
// Generate list of properties for SchemaTool usage
Map props = new HashMap();
// Get properties from PersistenceUnit first...
PersistenceUnitMetaData pumd = null;
if (persistenceUnitName != null) {
props.put(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME.toLowerCase(), persistenceUnitName);
// Extract the persistence-unit metadata
String filename = nucleusCtx.getConfiguration().getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_XML_FILENAME);
boolean validateXML = nucleusCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_VALIDATE);
boolean supportXMLNamespaces = nucleusCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_NAMESPACE_AWARE);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
pumd = MetaDataUtils.getMetaDataForPersistenceUnit(nucleusCtx.getPluginManager(), filename, persistenceUnitName, validateXML, supportXMLNamespaces, clr);
if (pumd != null) {
// Add the properties for the unit
if (pumd.getProperties() != null) {
props.putAll(pumd.getProperties());
}
} else {
throw new NucleusUserException("SchemaTool has been specified to use persistence-unit with name " + persistenceUnitName + " but none was found with that name");
}
if (api.equalsIgnoreCase("JPA")) {
// Don't use JARs when in JavaSE for JPA
pumd.clearJarFiles();
}
}
// Add/override with user properties
if (userProps != null) {
// Properties specified by the user in a file
for (Object key : userProps.keySet()) {
String propName = (String) key;
props.put(propName.toLowerCase(Locale.ENGLISH), userProps.get(propName));
}
}
// Finally add/override with system properties (only support particular ones, and in correct case)
String[] propNames = { PropertyNames.PROPERTY_CONNECTION_URL, PropertyNames.PROPERTY_CONNECTION_DRIVER_NAME, PropertyNames.PROPERTY_CONNECTION_USER_NAME, PropertyNames.PROPERTY_CONNECTION_PASSWORD, PropertyNames.PROPERTY_MAPPING, "javax.jdo.option.ConnectionURL", "javax.jdo.option.ConnectionDriverName", "javax.jdo.option.ConnectionUserName", "javax.jdo.option.ConnectionPassword", "javax.jdo.option.Mapping", "javax.persistence.jdbc.url", "javax.persistence.jdbc.driver", "javax.persistence.jdbc.user", "javax.persistence.jdbc.password" };
for (int i = 0; i < propNames.length; i++) {
if (System.getProperty(propNames[i]) != null) {
props.put(propNames[i].toLowerCase(Locale.ENGLISH), System.getProperty(propNames[i]));
}
}
// Interferes with usage
props.put(PropertyNames.PROPERTY_AUTOSTART_MECHANISM.toLowerCase(), "None");
// Tag on the mandatory props that we must have for each mode
if (mode == Mode.CREATE) {
if (ddlFile != null) {
// the tables must not be created in the DB, so do not validate (DDL is being output to a file)
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "false");
}
// use tables/columns/constraints settings
props.remove(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase());
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "true");
}
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "true");
}
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "true");
}
props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
props.put("datanucleus.rdbms.checkexisttablesorviews", "true");
} else if (mode == Mode.DELETE) {
props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
} else if (mode == Mode.DELETE_CREATE) {
if (ddlFile != null) {
// the tables must not be created in the DB, so do not validate (DDL is being output to a file)
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "false");
}
// use tables/columns/constraints settings
props.remove(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase());
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "true");
}
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "true");
}
if (!props.containsKey(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase())) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "true");
}
props.put(PropertyNames.PROPERTY_DATASTORE_READONLY.toLowerCase(), "false");
props.put("datanucleus.rdbms.checkexisttablesorviews", "true");
} else if (mode == Mode.VALIDATE) {
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS.toLowerCase(), "false");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_TABLES.toLowerCase(), "true");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_COLUMNS.toLowerCase(), "true");
props.put(PropertyNames.PROPERTY_SCHEMA_VALIDATE_CONSTRAINTS.toLowerCase(), "true");
}
if (ignoreMetaDataForMissingClasses) {
props.put(PropertyNames.PROPERTY_METADATA_IGNORE_METADATA_FOR_MISSING_CLASSES, "true");
}
// Apply remaining persistence properties
propConfig.setPersistenceProperties(props);
if (pumd != null) {
// Initialise the MetaDataManager with all files/classes for this persistence-unit
// This is done now that all persistence properties are set (including the persistence-unit props)
nucleusCtx.getMetaDataManager().loadPersistenceUnit(pumd, null);
}
// Initialise the NucleusContext for use
nucleusCtx.initialise();
if (verbose) {
String msg = Localiser.msg("014020");
LOGGER.info(msg);
System.out.println(msg);
// TODO Some persistence properties will be stored against the StoreManager
Map<String, Object> pmfProps = propConfig.getPersistenceProperties();
Set<String> keys = pmfProps.keySet();
List<String> keyNames = new ArrayList<String>(keys);
Collections.sort(keyNames);
Iterator keyNamesIter = keyNames.iterator();
while (keyNamesIter.hasNext()) {
String key = (String) keyNamesIter.next();
Object value = pmfProps.get(key);
boolean display = true;
if (!key.startsWith("datanucleus")) {
display = false;
} else if (key.equals(PropertyNames.PROPERTY_CONNECTION_PASSWORD.toLowerCase())) {
// Don't show passwords
display = false;
} else if (value == null) {
display = false;
} else if (value instanceof String && StringUtils.isWhitespace((String) value)) {
display = false;
}
if (display) {
// Print the property to sysout
msg = Localiser.msg("014022", key, value);
LOGGER.info(msg);
System.out.println(msg);
}
}
System.out.println();
}
return nucleusCtx;
}
use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHandlerTest method testParseDefaultNamespace.
public void testParseDefaultNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package1.jdo"), "jdo");
assertNotNull(md);
}
use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.
the class PersistenceFileMetaDataHandlerTest method testParseNamespace.
public void testParseNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence2.xml"), "persistence");
assertNotNull(md);
}
use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.
the class MetaDataManagerTest method testLocationsForClass.
/**
* Test of the valid locations for a specified class.
*/
public void testLocationsForClass() {
Map startupProps = new HashMap<>();
startupProps.put(JDOPropertyNames.PROPERTY_METADATA_XML_JDO_1_0, "true");
JDOMetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", startupProps));
// Try typical JDO class name
String className = "org.jpox.samples.store.Product";
List locations = mgr.getValidMetaDataLocationsForClass("jdo", null, className);
assertTrue("Locations returned from MetaData Manager was null!", locations != null);
List validLocations = new ArrayList();
validLocations.add("/META-INF/package.jdo");
validLocations.add("/WEB-INF/package.jdo");
validLocations.add("/package.jdo");
validLocations.add("/org.jdo");
validLocations.add("/org/package.jdo");
validLocations.add("/org/jpox.jdo");
validLocations.add("/org/jpox/package.jdo");
validLocations.add("/org/jpox/samples.jdo");
validLocations.add("/org/jpox/samples/package.jdo");
validLocations.add("/org/jpox/samples/store.jdo");
validLocations.add("/org/jpox/samples/store/package.jdo");
validLocations.add("/org/jpox/samples/store/Product.jdo");
checkLocations(className, locations, validLocations);
}
use of org.datanucleus.PersistenceNucleusContextImpl in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method readResolve.
/**
* Control deserialisation of the PMF where we have a singleton (in pmfByName).
* @return The PMF
* @throws InvalidObjectException if an error occurs
*/
private Object readResolve() throws InvalidObjectException {
JDOPersistenceManagerFactory pmf = null;
if (pmfByName != null) {
String name = (String) deserialisationProps.get(PropertyNames.PROPERTY_PMF_NAME);
if (name == null) {
name = (String) deserialisationProps.get(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME);
}
// Return singleton if present to save reinitialisation
pmf = pmfByName.get(name);
if (pmf != null) {
return pmf;
}
}
// Use deserialised object, so need to initialise it
configurable = true;
if (pmCache == null) {
pmCache = Collections.newSetFromMap(new ConcurrentHashMap());
}
nucleusContext = new PersistenceNucleusContextImpl("JDO", deserialisationProps);
PersistenceUnitMetaData pumd = null;
if (getPersistenceUnitName() != null) {
// Load the metadata for the persistence-unit
String filename = nucleusContext.getConfiguration().getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_XML_FILENAME);
boolean validateXML = nucleusContext.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_VALIDATE);
boolean supportXMLNamespaces = nucleusContext.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_XML_NAMESPACE_AWARE);
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
pumd = MetaDataUtils.getMetaDataForPersistenceUnit(nucleusContext.getPluginManager(), filename, getPersistenceUnitName(), validateXML, supportXMLNamespaces, clr);
}
initialiseMetaData(pumd);
processLifecycleListenersFromProperties(deserialisationProps);
freezeConfiguration();
deserialisationProps = null;
return this;
}
Aggregations