Package com.adaptrex.core

Source Code of com.adaptrex.core.AdaptrexRegistry

/*
* 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.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.adaptrex.core.persistence.api.ORMPersistenceManager;
import com.adaptrex.core.rest.AdaptrexRestService;
import com.adaptrex.core.rest.RestService;

public class AdaptrexRegistry {

  private static Logger log = Logger.getLogger(AdaptrexRegistry.class);
 
  private static AdaptrexConfig config;
 
  private static Map<String,ORMPersistenceManager> persistenceManagers
    = new HashMap<String,ORMPersistenceManager>();

  private static RestService restService;
 
  public static AdaptrexConfig getConfig() {
    if (config == null) {
      synchronized(AdaptrexRegistry.class) {
        if (config == null) {
          config = new AdaptrexConfig();
        }
      }
    }
    return config;
  }
 
  public static ORMPersistenceManager getPersistenceManager() {
    return getPersistenceManager(null);
  }
 
  public static ORMPersistenceManager getPersistenceManager(String factoryName) {
    if (factoryName == null) factoryName = getConfig().get(AdaptrexConfig.PERSISTENCE_ORM_DEFAULT);
    String key = factoryName != null ? factoryName : "_default";
   
    ORMPersistenceManager persistenceManager = persistenceManagers.get(key);
    if (persistenceManager == null) {
      synchronized(AdaptrexRegistry.class) {
        if (persistenceManagers.get(key) != null) {
          return persistenceManagers.get(key);
        }
       
        log.debug("Creating Persistence Manager: " + (factoryName == null ? "_default" : factoryName));
       
        String persistenceManagerImplName = getConfig().get(AdaptrexConfig.PERSISTENCE_ORM,
            "com.adaptrex.core.persistence.jpa.JPAPersistenceManager");
       
        try {
          Class<?> ormClazz = Class.forName(persistenceManagerImplName);
         
          persistenceManager = (ORMPersistenceManager) (factoryName == null
              ? ormClazz.getDeclaredConstructor().newInstance()
              : ormClazz.getDeclaredConstructor(String.class).newInstance(factoryName));
              ;
         
        } catch (ClassNotFoundException e) {
          throw new RuntimeException("Couldn't find ORMPersistenceFactory implementation for your ORM (" +
              persistenceManagerImplName + ")", e);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }

        persistenceManagers.put(key, persistenceManager);       
      }
    }
   
    return persistenceManager;
  }
 
  public static RestService getRestService() {
    if (restService == null) {
      synchronized(AdaptrexRegistry.class) {
        if (restService == null) {
          restService = new AdaptrexRestService();
        }
      }
    }
    return restService;
  }
}
TOP

Related Classes of com.adaptrex.core.AdaptrexRegistry

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.