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