2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.config;
27 import java.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
31 import org.collectionspace.services.common.service.ServiceBindingType;
32 import org.collectionspace.services.common.tenant.TenantBindingType;
33 import org.collectionspace.services.common.tenant.TenantBindingConfig;
34 import org.collectionspace.services.common.types.PropertyItemType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * ServicesConfigReader reads service layer specific configuration
41 * $LastChangedRevision: $
44 public class TenantBindingConfigReaderImpl
45 extends AbstractConfigReaderImpl<TenantBindingConfig> {
47 final private static String CONFIG_FILE_NAME = "tenant-bindings.xml";
48 final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class);
49 private TenantBindingConfig tenantBindingConfig;
50 //tenant id, tenant binding
51 private Hashtable<String, TenantBindingType> tenantBindings =
52 new Hashtable<String, TenantBindingType>();
53 //tenant-qualified servicename, service binding
54 private Hashtable<String, ServiceBindingType> serviceBindings =
55 new Hashtable<String, ServiceBindingType>();
57 public TenantBindingConfigReaderImpl(String serverRootDir) {
62 public String getFileName() {
63 return CONFIG_FILE_NAME;
67 public void read() throws Exception {
68 String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME);
73 public void read(String configFileName) throws Exception {
74 if (logger.isDebugEnabled()) {
75 logger.debug("read() config file=" + configFileName);
77 File configFile = new File(configFileName);
78 if (!configFile.exists()) {
79 String msg = "Could not find configuration file " + configFileName;
81 throw new RuntimeException(msg);
83 tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class);
84 for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) {
85 tenantBindings.put(tenantBinding.getId(), tenantBinding);
86 readServiceBindings(tenantBinding);
87 if (logger.isDebugEnabled()) {
88 logger.debug("read() added tenant id=" + tenantBinding.getId()
89 + " name=" + tenantBinding.getName());
94 private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
95 for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
96 String key = getTenantQualifiedServiceName(tenantBinding.getId(),
97 serviceBinding.getName());
98 serviceBindings.put(key, serviceBinding);
99 if (logger.isDebugEnabled()) {
100 logger.debug("readServiceBindings() added service "
102 + " workspace=" + serviceBinding.getName());
108 public TenantBindingConfig getConfiguration() {
109 return tenantBindingConfig;
113 * getTenantBindings returns all the tenant bindings read from configuration
116 public Hashtable<String, TenantBindingType> getTenantBindings() {
117 return tenantBindings;
121 * getTenantBinding gets tenant binding for given tenant
125 public TenantBindingType getTenantBinding(
127 return tenantBindings.get(tenantId);
131 * getServiceBinding gets service binding for given tenant for a given service
136 public ServiceBindingType getServiceBinding(
137 String tenantId, String serviceName) {
138 String key = getTenantQualifiedServiceName(tenantId, serviceName);
139 return serviceBindings.get(key);
143 * getServiceBinding gets service binding for given tenant for a given service
148 public List<ServiceBindingType> getServiceBindingsByType(
149 String tenantId, String serviceType) {
150 ArrayList<ServiceBindingType> list = null;
151 TenantBindingType tenant = tenantBindings.get(tenantId);
152 if (tenant != null) {
153 for (ServiceBindingType sb : tenant.getServiceBindings()) {
154 if (serviceType.equals(sb.getType())) {
156 list = new ArrayList<ServiceBindingType>();
168 * @return the properly qualified service name
170 public static String getTenantQualifiedServiceName(
171 String tenantId, String serviceName) {
172 return tenantId + "." + serviceName.toLowerCase();
176 * Sets properties in the passed list on the local properties for this TenantBinding.
177 * Note: will only set properties not already set on the TenantBinding.
180 * @param propagateToServices If true, recurses to set set properties
181 * on the associated services.
183 public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
184 boolean propagateToServices) {
185 // For each tenant, set properties in list that are not already set
186 if (propList == null || propList.isEmpty()) {
189 for (TenantBindingType tenant : tenantBindings.values()) {
190 for (PropertyItemType prop : propList) {
191 TenantBindingUtils.setPropertyValue(tenant,
192 prop, TenantBindingUtils.SET_PROP_IF_MISSING);
194 if (propagateToServices) {
195 TenantBindingUtils.propagatePropertiesToServices(tenant,
196 TenantBindingUtils.SET_PROP_IF_MISSING);