*
* @see org.jboss.dna.graph.connector.RepositorySource#getConnection()
*/
public synchronized RepositoryConnection getConnection() throws RepositorySourceException {
if (this.name == null || this.name.trim().length() == 0) {
throw new RepositorySourceException(JpaConnectorI18n.repositorySourceMustHaveName.text());
}
assert rootNodeUuid != null;
assert rootUuid != null;
EntityManager entityManager = null;
if (entityManagerFactory == null || !entityManagerFactory.isOpen()) {
// Create the JPA EntityManagerFactory by programmatically configuring Hibernate Entity Manager ...
Ejb3Configuration configurator = new Ejb3Configuration();
// Configure the entity classes ...
configurator.addAnnotatedClass(StoreOptionEntity.class);
if (model != null) model.configure(configurator);
// Configure additional properties, which may be overridden by subclasses ...
configure(configurator);
// Now set the mandatory information, overwriting anything that the subclasses may have tried ...
if (this.dataSource == null && this.dataSourceJndiName != null) {
// Try to load the DataSource from JNDI ...
try {
Context context = new InitialContext();
dataSource = (DataSource)context.lookup(this.dataSourceJndiName);
} catch (Throwable t) {
Logger.getLogger(getClass())
.error(t, JpaConnectorI18n.errorFindingDataSourceInJndi, name, dataSourceJndiName);
}
}
if (this.dataSource != null) {
// Set the data source ...
configurator.setDataSource(this.dataSource);
} else {
// Set the context class loader, so that the driver could be found ...
if (this.repositoryContext != null && this.driverClassloaderName != null) {
try {
ExecutionContext context = this.repositoryContext.getExecutionContext();
ClassLoader loader = context.getClassLoader(this.driverClassloaderName);
if (loader != null) {
Thread.currentThread().setContextClassLoader(loader);
}
} catch (Throwable t) {
I18n msg = JpaConnectorI18n.errorSettingContextClassLoader;
Logger.getLogger(getClass()).error(t, msg, name, driverClassloaderName);
}
}
// Set the connection properties ...
setProperty(configurator, "hibernate.dialect", this.dialect);
setProperty(configurator, "hibernate.connection.driver_class", this.driverClassName);
setProperty(configurator, "hibernate.connection.username", this.username);
setProperty(configurator, "hibernate.connection.password", this.password);
setProperty(configurator, "hibernate.connection.url", this.url);
setProperty(configurator, "hibernate.connection.max_fetch_depth", DEFAULT_MAXIMUM_FETCH_DEPTH);
setProperty(configurator, "hibernate.connection.pool_size", 0); // don't use the built-in pool
}
entityManagerFactory = configurator.buildEntityManagerFactory();
// Establish a connection and obtain the store options...
entityManager = entityManagerFactory.createEntityManager();
// Find and update/set the root node's UUID ...
StoreOptions options = new StoreOptions(entityManager);
UUID actualUuid = options.getRootNodeUuid();
if (actualUuid != null) {
this.setRootNodeUuid(actualUuid.toString());
} else {
options.setRootNodeUuid(this.rootUuid);
}
// Find or set the type of model that will be used.
String actualModelName = options.getModelName();
if (actualModelName == null) {
// This is a new store, so set to the specified model ...
if (model == null) setModel(Models.DEFAULT.getName());
assert model != null;
options.setModelName(model);
} else {
try {
setModel(actualModelName);
} catch (Throwable e) {
// The actual model name doesn't match what's available in the software ...
entityManagerFactory.close();
String msg = JpaConnectorI18n.existingStoreSpecifiesUnknownModel.text(name, actualModelName);
throw new RepositorySourceException(msg);
}
}
entityManagerFactory.close();
// Now, create another entity manager with the classes from the correct model