/*
* Copyright 2012 Adaptrex, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core;
import com.adaptrex.core.config.AdaptrexConfig;
import com.adaptrex.core.persistence.AdaptrexPersistence;
import com.adaptrex.core.persistence.AdaptrexPersistenceManager;
import com.adaptrex.core.persistence.jpa.JpaPersistenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An instance of this class is the main entry point into Adaptrex services. Your webapp
* needs to bootstrap Adaptrex before it can be used. For
* more information on bootstrapping, see {@link AdaptrexContextListener AdaptrexContextListener}
* <br /><br />
* In general, most interactions with Adaptrex use the following:
* <ul>
* <li>Built in presentation components (JSF, JSP, Tapestry, etc)</li>
* <li>Store and Model CRUD methods (REST API)</li>
* </ul>
* In those cases, you do not need to access the Adaptrex service directly. If you do
* need access to some services (such as AdaptrexPersistence) you can retrieve them directly
* from the Adaptrex service instance. You can always retrieve the service by calling
* Adaptrex.getAdaptrex() or by whatever other mechanism you have enabled in your application
*/
public class Adaptrex {
private AdaptrexConfig config;
private AdaptrexPersistenceManager persistenceManager;
private Logger log = LoggerFactory.getLogger(Adaptrex.class);
public Adaptrex() {
//System.out.println("Initializing Adaptrex");
}
private void initConfig() {
config = new AdaptrexConfig();
}
private void initPersistenceManager(AdaptrexConfig config) {
/*
* Initialize Persistence
*/
String persistenceImplName = config.get(AdaptrexConfig.PERSISTENCE_ORM);
try {
if (persistenceImplName == null || persistenceImplName.isEmpty()) {
persistenceManager = new JpaPersistenceManager(config);
} else {
persistenceManager = (AdaptrexPersistenceManager) Class.forName(persistenceImplName).getDeclaredConstructor(AdaptrexConfig.class).newInstance(config);
}
} catch (Exception ex) {
log.warn("Error", ex);
}
}
public AdaptrexConfig getConfig() {
if (this.config == null) initConfig();
return this.config;
}
public AdaptrexPersistenceManager getPersistenceManager() {
if (this.persistenceManager == null) initPersistenceManager(getConfig());
return this.persistenceManager;
}
public AdaptrexPersistence getPersistence() {
if (this.persistenceManager == null) initPersistenceManager(getConfig());
return this.persistenceManager.getPersistence();
}
public AdaptrexPersistence getPersistence(String factoryName) {
if (this.persistenceManager == null) initPersistenceManager(getConfig());
return this.persistenceManager.getPersistence(factoryName);
}
/*
* This is only used by the adaptrex framework
*/
private static Adaptrex adaptrex = null;
public static Adaptrex getAdaptrex() {
if (Adaptrex.adaptrex == null) Adaptrex.adaptrex = new Adaptrex();
return Adaptrex.adaptrex;
}
}