com.adaptrex.core.Adaptrex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of adaptrex-core Show documentation
Show all versions of adaptrex-core Show documentation
The Core Adaptrex Framework
The newest version!
/*
* 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 java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.adaptrex.core.persistence.api.AdaptrexPersistenceManager;
import com.adaptrex.core.rest.RestService;
/**
* 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}
*
* In general, most interactions with Adaptrex use the following:
*
* - Built in presentation components (JSF, JSP, Tapestry, etc)
* - Store and Model CRUD methods (REST API)
*
* 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 {
public static final String ATTRIBUTE = "com.adaptrex";
private Logger log = Logger.getLogger(Adaptrex.class);
private RestService restService;
private AdaptrexPersistenceManager defaultPersistenceManager;
private Map persistenceManagers;
private AdaptrexProperties properties;
/*
* Create the adaptrex service
*/
public Adaptrex() throws Exception {
this((AdaptrexPersistenceManager) null);
}
/*
* Create the adaptrex service and initialize persistence managers
*/
public Adaptrex(AdaptrexPersistenceManager... opms) throws Exception {
/*
* Get our adaptrex properties
*/
properties = new AdaptrexProperties();
/*
* Initialize default persistence managers
*/
persistenceManagers = new HashMap();
if (opms.length > 0 && opms[0] != null) {
this.defaultPersistenceManager = opms[0];
this.persistenceManagers = new HashMap();
for (AdaptrexPersistenceManager opm : opms) {
persistenceManagers.put(opm.getName(), opm);
}
} else {
String defaultOPMName = this.properties.get(AdaptrexProperties.PERSISTENCE_DEFAULTFACTORY);
try {
AdaptrexPersistenceManager opm = getOPMConstructor().newInstance(defaultOPMName);
this.defaultPersistenceManager = opm;
String defaultKey = defaultOPMName != null
? defaultOPMName : AdaptrexPersistenceManager.DEFAULT_NAME;
persistenceManagers.put(defaultKey, opm);
} catch (Exception e) {
throw new RuntimeException("Error loading ORMPersistenceManager implementation", e);
}
}
}
public void initialize(ServletContext context) {
this.restService = new RestService(context);
context.setAttribute(Adaptrex.ATTRIBUTE, this);
log.info("Adaptrex Initialized: " + properties.get(AdaptrexProperties.ADAPTREX_VERSION));
}
@SuppressWarnings("unchecked")
private Constructor getOPMConstructor() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
String persistenceManagerImplName =
this.properties.get(AdaptrexProperties.PERSISTENCE_ORM,
"com.adaptrex.core.persistence.jpa.JPAPersistenceManager");
Class opmClazz =
(Class) Class.forName(persistenceManagerImplName);
return opmClazz.getDeclaredConstructor(String.class);
}
/*
* Get the default persistence manager
*/
public AdaptrexPersistenceManager getPersistenceManager() {
return this.defaultPersistenceManager;
}
/*
* Get a persistence manager specified by the factory name
*/
public AdaptrexPersistenceManager getPersistenceManager(String factoryName) {
if (factoryName == null) {
return this.defaultPersistenceManager;
}
AdaptrexPersistenceManager opm = this.persistenceManagers.get(factoryName);
if (opm != null) return opm;
synchronized(Adaptrex.class) {
opm = this.persistenceManagers.get(factoryName);
if (opm != null) return opm;
try {
opm = getOPMConstructor().newInstance(factoryName);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Couldn't find AdaptrexPersistenceManager implementation");
}
persistenceManagers.put(factoryName, opm);
}
log.debug("Created Persistence Manager: " + factoryName);
return opm;
}
/*
* Get a rest service
*/
public RestService getRestService() {
return restService;
}
/*
* Get properties
*/
public AdaptrexProperties getProperties() {
return this.properties;
}
/*
* Shutdown adaptrex
*/
public void shutdown() {
for (String key : this.persistenceManagers.keySet()) {
this.persistenceManagers.get(key).shutdown();
}
log.info("Adaptrex Shutdown");
}
public static Adaptrex get(ServletContext context) {
return (Adaptrex) context.getAttribute(Adaptrex.ATTRIBUTE);
}
public static Adaptrex get(HttpServletRequest request) {
return Adaptrex.get(request.getServletContext());
}
}