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;
023import org.kuali.rice.krms.impl.ui.CustomOperatorUiTranslator;
024
025import javax.xml.namespace.QName;
026
027/**
028 * Like {@link org.kuali.rice.krms.api.KrmsApiServiceLocator} only for non-remotable.
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 *
031 */
032public class KrmsServiceLocatorInternal {
033
034    public static final String REPOSITORY_TO_ENGINE_TRANSLATOR = "repositoryToEngineTranslator";
035    public static final String CUSTOM_OPERATOR_UI_TRANSLATOR = "rice.krms.customOperatorUiTranslator";
036
037        public static final String KRMS_RUN_MODE_PROPERTY = "krms.mode";
038        public static final String KRMS_MODULE_NAMESPACE = "KRMS";
039
040        private static final Logger LOG = Logger.getLogger(KrmsServiceLocatorInternal.class);
041
042        
043        @SuppressWarnings("unchecked")
044        public static <A> A getService(String serviceName) {
045                return (A)getBean(serviceName, false);
046        }
047        
048        public static <A> A getBean(String serviceName, boolean forceLocal) {
049                if ( LOG.isDebugEnabled() ) {
050                        LOG.debug("Fetching service " + serviceName);
051                }
052        QName name = new QName(serviceName);
053        RunMode krmsRunMode = RunMode.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KRMS_RUN_MODE_PROPERTY));
054        if (!forceLocal) {
055            if (krmsRunMode == RunMode.REMOTE || krmsRunMode == RunMode.THIN) {
056                name = new QName(KRMS_MODULE_NAMESPACE, serviceName);
057            }
058        }
059                return GlobalResourceLoader.getResourceLoader().getService(name);
060        }
061
062    public static RepositoryToEngineTranslator getRepositoryToEngineTranslator() {
063        return getBean(REPOSITORY_TO_ENGINE_TRANSLATOR, true);
064    }
065
066    public static CustomOperatorUiTranslator getCustomOperatorUiTranslator() {
067        return getBean(CUSTOM_OPERATOR_UI_TRANSLATOR, true);
068    }
069        
070}