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