View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.ksb.messaging;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.core.config.ConfigContext;
24  import org.springframework.beans.factory.BeanInitializationException;
25  
26  /**
27   * A KSBExporter which only exports the service if the specified property is set
28   * to true.
29   * 
30   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
31   */
32  public class PropertyConditionalKSBExporter extends KSBExporter {
33  
34  	private List<String> exportIf = new ArrayList<String>();
35  	private List<String> exportUnless = new ArrayList<String>();
36  	private boolean exportIfPropertyNotSet = true;
37  
38  	public void afterPropertiesSet() throws Exception {
39  		if (shouldRemoteThisService()) {
40  			super.afterPropertiesSet();
41  		}
42  	}
43  	
44  	protected boolean shouldRemoteThisService() throws Exception {
45  		if (exportIf.isEmpty() && exportUnless.isEmpty()) {
46  			return true;
47  		}
48  		boolean remoteThisService = false;
49  		String serviceValue = null;
50  		// Check the value in the clients config file for services in the list
51  		// of property named 'exportIf' loaded by Spring.
52  		// if any are ="true" then set boolean to true and exit loop, so far the
53  		// service will be published.
54  		for (String expIf : exportIf) {
55  			serviceValue = ConfigContext.getCurrentContextConfig().getProperty(expIf);
56  			// if any are true, set boolean and exit loop.
57  			if (!StringUtils.isBlank(serviceValue)) {
58  				remoteThisService = new Boolean(serviceValue);
59  				if (remoteThisService) {
60  					break;
61  				}
62  			} else if (exportIfPropertyNotSet) {
63  				remoteThisService = true;
64  				break;
65  			}
66  		}
67  		// Check a second list, if any are ="true" DON"T publish the service.
68  		for (String expUnless : exportUnless) {
69  			serviceValue = ConfigContext.getCurrentContextConfig()
70  					.getProperty(expUnless);
71  			// if any are true, set boolean and exit loop.
72  			if (!StringUtils.isBlank(serviceValue)) {
73  				remoteThisService = new Boolean(serviceValue);
74  				if (remoteThisService) {
75  					remoteThisService = new Boolean("false");
76  					break;
77  				}
78  			}
79  		}
80  		return remoteThisService;
81  	}
82  
83  	public List getExportIf() {
84  		return this.exportIf;
85  	}
86  
87  	public void setExportIf(List exportIf) throws BeanInitializationException {
88  		this.exportIf = exportIf;
89  	}
90  
91  	public List getExportUnless() {
92  		return this.exportUnless;
93  	}
94  
95  	public void setExportUnless(List exportUnless)
96  			throws BeanInitializationException {
97  		this.exportUnless = exportUnless;
98  	}
99  
100 	public boolean isExportIfPropertyNotSet() {
101 		return this.exportIfPropertyNotSet;
102 	}
103 
104 	public void setExportIfPropertyNotSet(boolean exportIfPropertyNotSet) {
105 		this.exportIfPropertyNotSet = exportIfPropertyNotSet;
106 	}
107 
108 }