use of org.datanucleus.metadata.MetaDataListener in project datanucleus-core by datanucleus.
the class PersistenceNucleusContextImpl method initialise.
public synchronized void initialise() {
final ClassLoaderResolver clr = getClassLoaderResolver(null);
clr.registerUserClassLoader((ClassLoader) config.getProperty(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY));
boolean generateSchema = false;
boolean generateScripts = false;
String generateModeStr = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DATABASE_MODE);
if (generateModeStr == null || generateModeStr.equalsIgnoreCase("none")) {
// Try scripts instead of database since that wasn't set
generateModeStr = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_MODE);
generateScripts = true;
}
if (generateModeStr != null && !generateModeStr.equalsIgnoreCase("none")) {
generateSchema = true;
// Add any properties that are needed by schema generation (before we create StoreManager)
if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL)) {
config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL, "true");
}
if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES)) {
config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES, "true");
}
if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS)) {
config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS, "true");
}
if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS)) {
config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS, "true");
}
if (!config.getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY)) {
config.setProperty(PropertyNames.PROPERTY_DATASTORE_READONLY, "false");
}
}
// Create the StoreManager
try {
Set<String> propNamesWithDatastore = config.getPropertyNamesWithPrefix("datanucleus.datastore.");
if (propNamesWithDatastore == null) {
// Find the StoreManager using the persistence property if specified
NucleusLogger.DATASTORE.debug("Creating StoreManager for datastore");
Map<String, Object> datastoreProps = config.getDatastoreProperties();
this.storeMgr = NucleusContextHelper.createStoreManagerForProperties(config.getPersistenceProperties(), datastoreProps, clr, this);
// Make sure the isolation level is valid for this StoreManager and correct if necessary
String transactionIsolation = config.getStringProperty(PropertyNames.PROPERTY_TRANSACTION_ISOLATION);
if (transactionIsolation != null) {
String reqdIsolation = NucleusContextHelper.getTransactionIsolationForStoreManager(storeMgr, transactionIsolation);
if (!transactionIsolation.equalsIgnoreCase(reqdIsolation)) {
config.setProperty(PropertyNames.PROPERTY_TRANSACTION_ISOLATION, reqdIsolation);
}
}
} else {
NucleusLogger.DATASTORE.debug("Creating FederatedStoreManager to handle federation of primary StoreManager and " + propNamesWithDatastore.size() + " secondary datastores");
this.storeMgr = new FederatedStoreManager(clr, this);
this.federated = true;
}
} catch (NucleusException ne) {
NucleusLogger.DATASTORE.error("Exception thrown creating StoreManager : " + StringUtils.getMessageFromRootCauseOfThrowable(ne));
throw ne;
}
NucleusLogger.DATASTORE.debug("StoreManager now created");
// Make sure MetaDataManager is initialised
MetaDataManager mmgr = getMetaDataManager();
final Level2Cache cache = getLevel2Cache();
if (cache != null) {
// Add listener for metadata loading so we can pin any classes in the L2 cache that are marked for that
mmgr.registerListener(new MetaDataListener() {
@Override
public void loaded(AbstractClassMetaData cmd) {
if (cmd.hasExtension("cache-pin") && cmd.getValueForExtension("cache-pin").equalsIgnoreCase("true")) {
// Register as auto-pinned in the L2 cache
Class cls = clr.classForName(cmd.getFullClassName());
cache.pinAll(cls, false);
}
}
});
}
// ========== Initialise the StoreManager contents ==========
// A). Load any classes specified by auto-start
String autoStartMechanism = config.getStringProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM);
if (autoStartMechanism != null && !autoStartMechanism.equals("None")) {
initialiseAutoStart(clr);
}
// B). Schema Generation
if (generateSchema) {
initialiseSchema(generateModeStr, generateScripts);
}
// C). Load up internal representations of all persistence-unit classes
if (config.getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME) != null && config.getBooleanProperty(PropertyNames.PROPERTY_PERSISTENCE_UNIT_LOAD_CLASSES)) {
// User is using a persistence-unit, so load up its persistence-unit classes into StoreManager
Collection<String> loadedClasses = getMetaDataManager().getClassesWithMetaData();
this.storeMgr.manageClasses(clr, loadedClasses.toArray(new String[loadedClasses.size()]));
}
if (config.getBooleanProperty(PropertyNames.PROPERTY_QUERY_COMPILE_NAMED_QUERIES_AT_STARTUP)) {
initialiseNamedQueries(clr);
}
if (ecPool == null) {
ecPool = new ExecutionContextPool(this);
}
if (opFactory == null) {
opFactory = new ObjectProviderFactoryImpl(this);
}
if (config.hasProperty(PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER)) {
try {
multiTenancyProvider = (MultiTenancyProvider) config.getProperty(PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER);
} catch (Throwable thr) {
NucleusLogger.PERSISTENCE.warn("Error accessing property " + PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER + "; should be an instance of MultiTenancyProvider but isnt! Ignored");
}
}
if (config.hasProperty(PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER)) {
try {
currentUserProvider = (CurrentUserProvider) config.getProperty(PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER);
} catch (Throwable thr) {
NucleusLogger.PERSISTENCE.warn("Error accessing property " + PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER + "; should be an instance of CurrentUserProvider but isnt! Ignored");
}
}
super.initialise();
}
use of org.datanucleus.metadata.MetaDataListener in project datanucleus-core by datanucleus.
the class AbstractNucleusContext method getMetaDataManager.
public synchronized MetaDataManager getMetaDataManager() {
if (metaDataManager == null) {
String apiName = getApiName();
try {
metaDataManager = (MetaDataManager) pluginManager.createExecutableExtension("org.datanucleus.metadata_manager", new String[] { "name" }, new String[] { apiName }, "class", new Class[] { ClassConstants.NUCLEUS_CONTEXT }, new Object[] { this });
if (metaDataManager != null && config.hasProperty(PropertyNames.PROPERTY_METADATA_LISTENER_OBJECT)) {
MetaDataListener mdl = (MetaDataListener) config.getProperty(PropertyNames.PROPERTY_METADATA_LISTENER_OBJECT);
metaDataManager.registerListener(mdl);
}
} catch (Exception e) {
throw new NucleusException(Localiser.msg("008010", apiName, e.getMessage()), e);
}
if (metaDataManager == null) {
throw new NucleusException(Localiser.msg("008009", apiName));
}
}
return metaDataManager;
}
Aggregations