001    /*
002     * Copyright 2005-2008 The Kuali Foundation
003     * 
004     * 
005     * Licensed under the Educational Community License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     * http://www.opensource.org/licenses/ecl2.php
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.kuali.rice.ksb.messaging;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.core.config.ConfigContext;
024    import org.springframework.beans.factory.BeanInitializationException;
025    
026    /**
027     * A KSBExporter which only exports the service if the specified property is set
028     * to true.
029     * 
030     * @author Kuali Rice Team (kuali-rice@googlegroups.com)
031     */
032    public class PropertyConditionalKSBExporter extends KSBExporter {
033    
034            private List<String> exportIf = new ArrayList<String>();
035            private List<String> exportUnless = new ArrayList<String>();
036            private boolean exportIfPropertyNotSet = true;
037    
038            public void afterPropertiesSet() throws Exception {
039                    if (shouldRemoteThisService()) {
040                            super.afterPropertiesSet();
041                    }
042            }
043            
044            protected boolean shouldRemoteThisService() throws Exception {
045                    if (exportIf.isEmpty() && exportUnless.isEmpty()) {
046                            return true;
047                    }
048                    boolean remoteThisService = false;
049                    String serviceValue = null;
050                    // Check the value in the clients config file for services in the list
051                    // of property named 'exportIf' loaded by Spring.
052                    // if any are ="true" then set boolean to true and exit loop, so far the
053                    // service will be published.
054                    for (String expIf : exportIf) {
055                            serviceValue = ConfigContext.getCurrentContextConfig().getProperty(expIf);
056                            // if any are true, set boolean and exit loop.
057                            if (!StringUtils.isBlank(serviceValue)) {
058                                    remoteThisService = new Boolean(serviceValue);
059                                    if (remoteThisService) {
060                                            break;
061                                    }
062                            } else if (exportIfPropertyNotSet) {
063                                    remoteThisService = true;
064                                    break;
065                            }
066                    }
067                    // Check a second list, if any are ="true" DON"T publish the service.
068                    for (String expUnless : exportUnless) {
069                            serviceValue = ConfigContext.getCurrentContextConfig()
070                                            .getProperty(expUnless);
071                            // if any are true, set boolean and exit loop.
072                            if (!StringUtils.isBlank(serviceValue)) {
073                                    remoteThisService = new Boolean(serviceValue);
074                                    if (remoteThisService) {
075                                            remoteThisService = new Boolean("false");
076                                            break;
077                                    }
078                            }
079                    }
080                    return remoteThisService;
081            }
082    
083            public List getExportIf() {
084                    return this.exportIf;
085            }
086    
087            public void setExportIf(List exportIf) throws BeanInitializationException {
088                    this.exportIf = exportIf;
089            }
090    
091            public List getExportUnless() {
092                    return this.exportUnless;
093            }
094    
095            public void setExportUnless(List exportUnless)
096                            throws BeanInitializationException {
097                    this.exportUnless = exportUnless;
098            }
099    
100            public boolean isExportIfPropertyNotSet() {
101                    return this.exportIfPropertyNotSet;
102            }
103    
104            public void setExportIfPropertyNotSet(boolean exportIfPropertyNotSet) {
105                    this.exportIfPropertyNotSet = exportIfPropertyNotSet;
106            }
107    
108    }