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 name, 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);
69 File configFile = new File(configFileName);
70 if (!configFile.exists()) {
71 String msg = "Could not find configuration file " + configFileName;
73 throw new RuntimeException(msg);
75 tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class);
76 for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) {
77 tenantBindings.put(tenantBinding.getId(), tenantBinding);
78 readServiceBindings(tenantBinding);
79 if (logger.isDebugEnabled()) {
80 logger.debug("read() added tenant id=" + tenantBinding.getId()
81 + " name=" + tenantBinding.getName());
86 private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
87 for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
88 String key = getTenantQualifiedServiceName(tenantBinding.getId(),
89 serviceBinding.getName());
90 serviceBindings.put(key, serviceBinding);
91 if (logger.isDebugEnabled()) {
92 logger.debug("readServiceBindings() added service "
94 + " workspace=" + serviceBinding.getName());
100 public TenantBindingConfig getConfiguration() {
101 return tenantBindingConfig;
105 * getTenantBindings returns all the tenant bindings read from configuration
108 public Hashtable<String, TenantBindingType> getTenantBindings() {
109 return tenantBindings;
113 * getTenantBinding gets tenant binding for given tenant
117 public TenantBindingType getTenantBinding(
119 return tenantBindings.get(tenantId);
123 * getServiceBinding gets service binding for given tenant for a given service
128 public ServiceBindingType getServiceBinding(
129 String tenantId, String serviceName) {
130 String key = getTenantQualifiedServiceName(tenantId, serviceName);
131 return serviceBindings.get(key);
135 * getServiceBinding gets service binding for given tenant for a given service
140 public List<ServiceBindingType> getServiceBindingsByType(
141 String tenantId, String serviceType) {
142 ArrayList<ServiceBindingType> list = null;
143 TenantBindingType tenant = tenantBindings.get(tenantId);
144 if (tenant != null) {
145 for (ServiceBindingType sb : tenant.getServiceBindings()) {
146 if (serviceType.equals(sb.getType())) {
148 list = new ArrayList<ServiceBindingType>();
161 * @return the properly qualified service name
163 public static String getTenantQualifiedServiceName(
164 String tenantId, String serviceName) {
165 return tenantId + "." + serviceName.toLowerCase();
169 * Sets properties in the passed list on the local properties for this TenantBinding.
170 * Note: will only set properties not already set on the TenantBinding.
173 * @param propagateToServices If true, recurses to set set properties
174 * on the associated services.
176 public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
177 boolean propagateToServices) {
178 // For each tenant, set properties in list that are not already set
179 if (propList == null || propList.isEmpty()) {
182 for (TenantBindingType tenant : tenantBindings.values()) {
183 for (PropertyItemType prop : propList) {
184 TenantBindingUtils.setPropertyValue(tenant,
185 prop, TenantBindingUtils.SET_PROP_IF_MISSING);
187 if (propagateToServices) {
188 TenantBindingUtils.propagatePropertiesToServices(tenant,
189 TenantBindingUtils.SET_PROP_IF_MISSING);