}
}
// Initialise the context for this API
NucleusContext nucleusCtx = new NucleusContext(api, startupProps);
PersistenceConfiguration propConfig = nucleusCtx.getPersistenceConfiguration();
// Generate list of properties for SchemaTool usage
Map props = new HashMap();
PersistenceUnitMetaData pumd = null;
if (persistenceUnitName != null)
{
// Obtain any props defined for the persistence-unit
props.put("javax.jdo.option.persistenceunitname", persistenceUnitName);
pumd = nucleusCtx.getMetaDataManager().getMetaDataForPersistenceUnit(persistenceUnitName);
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"))
{
pumd.clearJarFiles(); // Don't use JARs when in J2SE for JPA
}
}
else 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));
}
}
else
{
// Properties specified via System properties (only support particular ones, and in correct case)
String[] propNames =
{
"datanucleus.ConnectionURL",
"datanucleus.ConnectionDriverName",
"datanucleus.ConnectionUserName",
"datanucleus.ConnectionPassword",
"datanucleus.Mapping",
"javax.jdo.option.ConnectionURL",
"javax.jdo.option.ConnectionDriverName",
"javax.jdo.option.ConnectionUserName",
"javax.jdo.option.ConnectionPassword",
"javax.jdo.option.Mapping"
};
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]));
}
}
}
props.put("datanucleus.autostartmechanism", "None"); // Interferes with usage
// Tag on the mandatory props that we must have for each mode
if (mode == SCHEMATOOL_CREATE_MODE)
{
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("datanucleus.validateconstraints", "false");
props.put("datanucleus.validatecolumns", "false");
props.put("datanucleus.validatetables", "false");
}
props.remove("datanucleus.autocreateschema"); // use tables/columns/constraints settings
if (!props.containsKey("datanucleus.autocreatetables"))
{
props.put("datanucleus.autocreatetables", "true");
}
if (!props.containsKey("datanucleus.autocreatecolumns"))
{
props.put("datanucleus.autocreatecolumns", "true");
}
if (!props.containsKey("datanucleus.autocreateconstraints"))
{
props.put("datanucleus.autocreateconstraints", "true");
}
props.put("datanucleus.fixeddatastore", "false");
props.put("datanucleus.readonlydatastore", "false");
props.put("datanucleus.rdbms.checkexisttablesorviews", "true");
}
else if (mode == SCHEMATOOL_DELETE_MODE)
{
props.put("datanucleus.fixeddatastore", "false");
props.put("datanucleus.readonlydatastore", "false");
}
else if (mode == SCHEMATOOL_VALIDATE_MODE)
{
props.put("datanucleus.autocreateschema", "false");
props.put("datanucleus.autocreatetables", "false");
props.put("datanucleus.autocreateconstraints", "false");
props.put("datanucleus.autocreatecolumns", "false");
props.put("datanucleus.validatetables", "true");
props.put("datanucleus.validatecolumns", "true");
props.put("datanucleus.validateconstraints", "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(false, "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())