]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
df575594aae1b4a04eb165812352f4a2ce1cc14a
[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 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>();
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         File configFile = new File(configFileName);
70         if (!configFile.exists()) {
71             String msg = "Could not find configuration file " + configFileName;
72             logger.error(msg);
73             throw new RuntimeException(msg);
74         }
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());
82             }
83         }
84     }
85
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 "
93                         + " name=" + key
94                         + " workspace=" + serviceBinding.getName());
95             }
96         }
97     }
98
99     @Override
100     public TenantBindingConfig getConfiguration() {
101         return tenantBindingConfig;
102     }
103
104     /**
105      * getTenantBindings returns all the tenant bindings read from configuration
106      * @return
107      */
108     public Hashtable<String, TenantBindingType> getTenantBindings() {
109         return tenantBindings;
110     }
111     
112     /**
113      * getTenantBinding gets tenant binding for given tenant
114      * @param tenantId
115      * @return
116      */
117     public TenantBindingType getTenantBinding(
118             String tenantId) {
119         return tenantBindings.get(tenantId);
120     }
121
122     /**
123      * getServiceBinding gets service binding for given tenant for a given service
124      * @param tenantId
125      * @param serviceName
126      * @return
127      */
128     public ServiceBindingType getServiceBinding(
129             String tenantId, String serviceName) {
130         String key = getTenantQualifiedServiceName(tenantId, serviceName);
131         return serviceBindings.get(key);
132     }
133
134     /**
135      * getServiceBinding gets service binding for given tenant for a given service
136      * @param tenantId
137      * @param serviceName
138      * @return
139      */
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())) {
147                     if (list == null) {
148                         list = new ArrayList<ServiceBindingType>();
149                     }
150                     list.add(sb);
151                 }
152             }
153         }
154         return list;
155     }
156
157
158     /**
159      * @param tenantId
160      * @param serviceName
161      * @return the properly qualified service name
162      */
163     public static String getTenantQualifiedServiceName(
164             String tenantId, String serviceName) {
165         return tenantId + "." + serviceName.toLowerCase();
166     }
167
168     /**
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.
171      * 
172      * @param propList
173      * @param propagateToServices If true, recurses to set set properties 
174      *                  on the associated services.
175      */
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()) {
180             return;
181         }
182         for (TenantBindingType tenant : tenantBindings.values()) {
183             for (PropertyItemType prop : propList) {
184                 TenantBindingUtils.setPropertyValue(tenant,
185                         prop, TenantBindingUtils.SET_PROP_IF_MISSING);
186             }
187             if (propagateToServices) {
188                 TenantBindingUtils.propagatePropertiesToServices(tenant,
189                         TenantBindingUtils.SET_PROP_IF_MISSING);
190             }
191         }
192     }
193 }