Package com.adaptrex.core

Source Code of com.adaptrex.core.Adaptrex

/*
* 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}
* <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 {
  public static final String ATTRIBUTE = "com.adaptrex";
 
  private Logger log = Logger.getLogger(Adaptrex.class);

  private RestService restService;
 
  private AdaptrexPersistenceManager defaultPersistenceManager;
  private Map<String,AdaptrexPersistenceManager> 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<String,AdaptrexPersistenceManager>();
    if (opms.length > 0 && opms[0] != null) {
      this.defaultPersistenceManager = opms[0];
      this.persistenceManagers = new HashMap<String,AdaptrexPersistenceManager>();
      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<AdaptrexPersistenceManager> getOPMConstructor() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    String persistenceManagerImplName =
        this.properties.get(AdaptrexProperties.PERSISTENCE_ORM,
            "com.adaptrex.core.persistence.jpa.JPAPersistenceManager");
    Class<AdaptrexPersistenceManager> opmClazz =
        (Class<AdaptrexPersistenceManager>) 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) {
        throw new RuntimeException("Couldn't find ORMPersistenceManager 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());
  }
}
TOP

Related Classes of com.adaptrex.core.Adaptrex

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.