]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
38bd41569111b765315e016c300f6bf79b39c421
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.config;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
30
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;
37
38 /**
39  * ServicesConfigReader reads service layer specific configuration
40  *
41  * $LastChangedRevision: $
42  * $LastChangedDate: $
43  */
44 public class TenantBindingConfigReaderImpl
45         extends AbstractConfigReaderImpl<TenantBindingConfig> {
46
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>();
56
57     public TenantBindingConfigReaderImpl(String serverRootDir) {
58         super(serverRootDir);
59     }
60
61     @Override
62     public String getFileName() {
63         return CONFIG_FILE_NAME;
64     }
65
66     @Override
67     public void read() throws Exception {
68         String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME);
69         read(configFileName);
70     }
71
72     @Override
73     public void read(String configFileName) throws Exception {
74         if (logger.isDebugEnabled()) {
75             logger.debug("read() config file=" + configFileName);
76         }
77         File configFile = new File(configFileName);
78         if (!configFile.exists()) {
79             String msg = "Could not find configuration file " + configFileName;
80             logger.error(msg);
81             throw new RuntimeException(msg);
82         }
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());
90             }
91         }
92     }
93
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 "
101                         + " name=" + key
102                         + " workspace=" + serviceBinding.getName());
103             }
104         }
105     }
106
107     @Override
108     public TenantBindingConfig getConfiguration() {
109         return tenantBindingConfig;
110     }
111
112     /**
113      * getTenantBindings returns all the tenant bindings read from configuration
114      * @return
115      */
116     public Hashtable<String, TenantBindingType> getTenantBindings() {
117         return tenantBindings;
118     }
119
120     /**
121      * getTenantBinding gets tenant binding for given tenant
122      * @param tenantId
123      * @return
124      */
125     public TenantBindingType getTenantBinding(
126             String tenantId) {
127         return tenantBindings.get(tenantId);
128     }
129
130     /**
131      * getServiceBinding gets service binding for given tenant for a given service
132      * @param tenantId
133      * @param serviceName
134      * @return
135      */
136     public ServiceBindingType getServiceBinding(
137             String tenantId, String serviceName) {
138         String key = getTenantQualifiedServiceName(tenantId, serviceName);
139         return serviceBindings.get(key);
140     }
141
142     /**
143      * getServiceBinding gets service binding for given tenant for a given service
144      * @param tenantId
145      * @param serviceName
146      * @return
147      */
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())) {
155                     if (list == null) {
156                         list = new ArrayList<ServiceBindingType>();
157                     }
158                     list.add(sb);
159                 }
160             }
161         }
162         return list;
163     }
164
165     /**
166      * @param tenantId
167      * @param serviceName
168      * @return the properly qualified service name
169      */
170     public static String getTenantQualifiedServiceName(
171             String tenantId, String serviceName) {
172         return tenantId + "." + serviceName.toLowerCase();
173     }
174
175     /**
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.
178      * 
179      * @param propList
180      * @param propagateToServices If true, recurses to set set properties 
181      *                  on the associated services.
182      */
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()) {
187             return;
188         }
189         for (TenantBindingType tenant : tenantBindings.values()) {
190             for (PropertyItemType prop : propList) {
191                 TenantBindingUtils.setPropertyValue(tenant,
192                         prop, TenantBindingUtils.SET_PROP_IF_MISSING);
193             }
194             if (propagateToServices) {
195                 TenantBindingUtils.propagatePropertiesToServices(tenant,
196                         TenantBindingUtils.SET_PROP_IF_MISSING);
197             }
198         }
199     }
200 }