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.ksb.api.bus.support;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.core.api.config.property.ConfigContext;
023
024/**
025 * A ServiceBusExporter which only exports the service if the specified property is set
026 * to true.
027 * 
028 * @author Kuali Rice Team (kuali-rice@googlegroups.com)
029 */
030public class PropertyConditionalServiceBusExporter extends ServiceBusExporter {
031
032        private List<String> exportIf = new ArrayList<String>();
033        private List<String> exportUnless = new ArrayList<String>();
034        private boolean exportIfPropertyNotSet = true;
035
036        public void afterPropertiesSet() {
037                if (shouldRemoteThisService()) {
038                        super.afterPropertiesSet();
039                }
040        }
041        
042        protected boolean shouldRemoteThisService() {
043                if (exportIf.isEmpty() && exportUnless.isEmpty()) {
044                        return true;
045                }
046                boolean remoteThisService = false;
047                String serviceValue = null;
048                // Check the value in the clients config file for services in the list
049                // of property named 'exportIf' loaded by Spring.
050                // if any are ="true" then set boolean to true and exit loop, so far the
051                // service will be published.
052                for (String expIf : exportIf) {
053                        serviceValue = ConfigContext.getCurrentContextConfig().getProperty(expIf);
054                        // if any are true, set boolean and exit loop.
055                        if (!StringUtils.isBlank(serviceValue)) {
056                                remoteThisService = Boolean.parseBoolean(serviceValue);
057                                if (remoteThisService) {
058                                        break;
059                                }
060                        } else if (exportIfPropertyNotSet) {
061                                remoteThisService = true;
062                                break;
063                        }
064                }
065                // Check a second list, if any are ="true" DON"T publish the service.
066                for (String expUnless : exportUnless) {
067                        serviceValue = ConfigContext.getCurrentContextConfig()
068                                        .getProperty(expUnless);
069                        // if any are true, set boolean and exit loop.
070                        if (!StringUtils.isBlank(serviceValue)) {
071                                remoteThisService = Boolean.parseBoolean(serviceValue);
072                                if (remoteThisService) {
073                                        remoteThisService = false;
074                                        break;
075                                }
076                        }
077                }
078                return remoteThisService;
079        }
080
081        public List<String> getExportIf() {
082                return this.exportIf;
083        }
084
085        public void setExportIf(List<String> exportIf) {
086                this.exportIf = exportIf;
087        }
088
089        public List<String> getExportUnless() {
090                return this.exportUnless;
091        }
092
093        public void setExportUnless(List<String> exportUnless) {
094                this.exportUnless = exportUnless;
095        }
096
097        public boolean isExportIfPropertyNotSet() {
098                return this.exportIfPropertyNotSet;
099        }
100
101        public void setExportIfPropertyNotSet(boolean exportIfPropertyNotSet) {
102                this.exportIfPropertyNotSet = exportIfPropertyNotSet;
103        }
104
105}