001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.ksb.messaging;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.rice.core.api.config.ConfigurationException;
022    import org.kuali.rice.core.api.config.module.RunMode;
023    import org.kuali.rice.core.api.config.property.ConfigContext;
024    import org.kuali.rice.ksb.api.bus.support.PropertyConditionalServiceBusExporter;
025    
026    /**
027     * A KSBExporter which will exports the service in the case where the specified
028     * run mode is set.  The run mode order proceeds as follows:
029     * 
030     * <p>remote -> embedded -> local
031     * 
032     * <p>If any of the earlier run modes in the list is set, then the ones after it
033     * will be applied.  So, if remote is set, then it will automatically be exported
034     * for embedded and local run modes.
035     * 
036     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
037     */
038    public class RunModeServiceExporter extends PropertyConditionalServiceBusExporter {
039            
040        private String runModePropertyName;
041        private RunMode validRunMode;
042        private static final List<RunMode> runModeHierarchy = new ArrayList<RunMode>();
043        static {
044            runModeHierarchy.add(RunMode.THIN);
045            runModeHierarchy.add(RunMode.REMOTE);
046            runModeHierarchy.add(RunMode.EMBEDDED);
047            runModeHierarchy.add(RunMode.LOCAL);
048        }
049    
050        @Override
051            protected boolean shouldRemoteThisService() {
052            if (validRunMode == null) {
053                    throw new ConfigurationException("The validRunMode property was not set.");
054            }
055            if (!runModeHierarchy.contains(getValidRunMode())) {
056                    throw new ConfigurationException("Given validRunMode is not a valid run mode.  Value was: " + getValidRunMode());
057            }
058                    RunMode runModePropertyValue = RunMode.valueOf(ConfigContext.getCurrentContextConfig().getProperty(getRunModePropertyName()));
059    
060            if (!runModeHierarchy.contains(runModePropertyValue)) {
061                    throw new ConfigurationException("Run mode value set on runModePropertyName of '" + getRunModePropertyName() + "' is not a valid run mode.  Value was: " + runModePropertyValue);
062            }
063            List<RunMode> validRunModeSubList = runModeHierarchy.subList(runModeHierarchy.indexOf(getValidRunMode()), runModeHierarchy.size());
064            return validRunModeSubList.contains(runModePropertyValue) && super.shouldRemoteThisService();
065            }
066    
067            public String getRunModePropertyName() {
068            return this.runModePropertyName;
069        }
070    
071        public void setRunModePropertyName(String runModePropertyName) {
072            this.runModePropertyName = runModePropertyName;
073        }
074    
075        public RunMode getValidRunMode() {
076            return this.validRunMode;
077        }
078    
079        public void setValidRunMode(RunMode validRunMode) {
080            this.validRunMode = validRunMode;
081        }
082        
083    }