Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertyConditionalKSBExporter |
|
| 2.375;2.375 |
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 | 0 | public class PropertyConditionalKSBExporter extends KSBExporter { |
33 | ||
34 | 0 | private List<String> exportIf = new ArrayList<String>(); |
35 | 0 | private List<String> exportUnless = new ArrayList<String>(); |
36 | 0 | private boolean exportIfPropertyNotSet = true; |
37 | ||
38 | public void afterPropertiesSet() throws Exception { | |
39 | 0 | if (shouldRemoteThisService()) { |
40 | 0 | super.afterPropertiesSet(); |
41 | } | |
42 | 0 | } |
43 | ||
44 | protected boolean shouldRemoteThisService() throws Exception { | |
45 | 0 | if (exportIf.isEmpty() && exportUnless.isEmpty()) { |
46 | 0 | return true; |
47 | } | |
48 | 0 | boolean remoteThisService = false; |
49 | 0 | 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 | 0 | for (String expIf : exportIf) { |
55 | 0 | serviceValue = ConfigContext.getCurrentContextConfig().getProperty(expIf); |
56 | // if any are true, set boolean and exit loop. | |
57 | 0 | if (!StringUtils.isBlank(serviceValue)) { |
58 | 0 | remoteThisService = new Boolean(serviceValue); |
59 | 0 | if (remoteThisService) { |
60 | 0 | break; |
61 | } | |
62 | 0 | } else if (exportIfPropertyNotSet) { |
63 | 0 | remoteThisService = true; |
64 | 0 | break; |
65 | } | |
66 | } | |
67 | // Check a second list, if any are ="true" DON"T publish the service. | |
68 | 0 | for (String expUnless : exportUnless) { |
69 | 0 | serviceValue = ConfigContext.getCurrentContextConfig() |
70 | .getProperty(expUnless); | |
71 | // if any are true, set boolean and exit loop. | |
72 | 0 | if (!StringUtils.isBlank(serviceValue)) { |
73 | 0 | remoteThisService = new Boolean(serviceValue); |
74 | 0 | if (remoteThisService) { |
75 | 0 | remoteThisService = new Boolean("false"); |
76 | 0 | break; |
77 | } | |
78 | } | |
79 | } | |
80 | 0 | return remoteThisService; |
81 | } | |
82 | ||
83 | public List getExportIf() { | |
84 | 0 | return this.exportIf; |
85 | } | |
86 | ||
87 | public void setExportIf(List exportIf) throws BeanInitializationException { | |
88 | 0 | this.exportIf = exportIf; |
89 | 0 | } |
90 | ||
91 | public List getExportUnless() { | |
92 | 0 | return this.exportUnless; |
93 | } | |
94 | ||
95 | public void setExportUnless(List exportUnless) | |
96 | throws BeanInitializationException { | |
97 | 0 | this.exportUnless = exportUnless; |
98 | 0 | } |
99 | ||
100 | public boolean isExportIfPropertyNotSet() { | |
101 | 0 | return this.exportIfPropertyNotSet; |
102 | } | |
103 | ||
104 | public void setExportIfPropertyNotSet(boolean exportIfPropertyNotSet) { | |
105 | 0 | this.exportIfPropertyNotSet = exportIfPropertyNotSet; |
106 | 0 | } |
107 | ||
108 | } |