001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.impl.util;
017
018import org.apache.log4j.Logger;
019import org.kuali.rice.core.api.config.module.RunMode;
020import org.kuali.rice.core.api.config.property.ConfigContext;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.krms.impl.provider.repository.RepositoryToEngineTranslator;
023
024import javax.xml.namespace.QName;
025
026/**
027 * Like {@link org.kuali.rice.krms.api.KrmsApiServiceLocator} only for non-remotable.
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 *
030 */
031public class KRMSServiceLocatorInternal {
032
033    public static final String REPOSITORY_TO_ENGINE_TRANSLATOR = "repositoryToEngineTranslator";
034
035        public static final String KRMS_RUN_MODE_PROPERTY = "krms.mode";
036        public static final String KRMS_MODULE_NAMESPACE = "KRMS";
037
038        private static final Logger LOG = Logger.getLogger(KRMSServiceLocatorInternal.class);
039
040        
041        @SuppressWarnings("unchecked")
042        public static <A> A getService(String serviceName) {
043                return (A)getBean(serviceName, false);
044        }
045        
046        public static <A> A getBean(String serviceName, boolean forceLocal) {
047                if ( LOG.isDebugEnabled() ) {
048                        LOG.debug("Fetching service " + serviceName);
049                }
050        QName name = new QName(serviceName);
051        RunMode krmsRunMode = RunMode.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KRMS_RUN_MODE_PROPERTY));
052        if (!forceLocal) {
053            if (krmsRunMode == RunMode.REMOTE || krmsRunMode == RunMode.THIN) {
054                name = new QName(KRMS_MODULE_NAMESPACE, serviceName);
055            }
056        }
057                return GlobalResourceLoader.getResourceLoader().getService(name);
058        }
059        
060//    public static BusinessObjectService getBusinessObjectService() {
061//      return getService(KRMS_BO_SERVICE);
062//    }
063
064    public static RepositoryToEngineTranslator getRepositoryToEngineTranslator() {
065        return getBean(REPOSITORY_TO_ENGINE_TRANSLATOR, true);
066    }
067        
068}