]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7b4b1567bf23aab7ab34b4b63661ea20f785a61d
[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.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Hashtable;
30 import java.util.List;
31
32 import org.collectionspace.services.common.service.ServiceBindingType;
33 import org.collectionspace.services.common.service.ServiceObjectType;
34 import org.collectionspace.services.common.tenant.RepositoryDomainType;
35 import org.collectionspace.services.common.tenant.TenantBindingType;
36 import org.collectionspace.services.common.tenant.TenantBindingConfig;
37 import org.collectionspace.services.common.types.PropertyItemType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * ServicesConfigReader reads service layer specific configuration
43  *
44  * $LastChangedRevision: $
45  * $LastChangedDate: $
46  */
47 public class TenantBindingConfigReaderImpl
48         extends AbstractConfigReaderImpl<List<TenantBindingType>> {
49     final private static String TENANT_BINDINGS_ERROR = "Tenant bindings error: ";
50     final private static String TENANT_BINDINGS_FILENAME = "tenant-bindings.xml";
51     final private static String TENANT_BINDINGS_ROOTDIRNAME = "tenants";
52     
53     final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class);
54     private List<TenantBindingType> tenantBindingTypeList;
55     //tenant id, tenant binding
56     private Hashtable<String, TenantBindingType> tenantBindings =
57             new Hashtable<String, TenantBindingType>();
58     //repository domains
59     private Hashtable<String, RepositoryDomainType> domains =
60             new Hashtable<String, RepositoryDomainType>();
61     //tenant-qualified servicename, service binding
62     private Hashtable<String, ServiceBindingType> serviceBindings =
63             new Hashtable<String, ServiceBindingType>();
64
65     //tenant-qualified service object name to service name, service binding
66     private Hashtable<String, ServiceBindingType> docTypes =
67             new Hashtable<String, ServiceBindingType>();
68
69
70     public TenantBindingConfigReaderImpl(String serverRootDir) {
71         super(serverRootDir);
72     }
73
74     @Override
75     public String getFileName() {
76         return TENANT_BINDINGS_FILENAME;
77     }
78     
79         protected File getTenantsRootDir() {
80                 File result = null;
81                 String tenantsRootPath = getConfigRootDir() + File.separator + TENANT_BINDINGS_ROOTDIRNAME;
82                 File tenantsRootDir = new File(tenantsRootPath);
83                 if (tenantsRootDir.exists() == true) {
84                         result = tenantsRootDir;
85                         logger.debug("Tenants home directory is: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isDebug() == true) check
86                 } else {
87                         logger.error("Tenants home directory is missing.  Can't find: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isError() == true) check
88                 }
89                 return result;
90         }
91     
92     @Override
93     public void read() throws Exception {
94         String tenantsRootPath = getTenantsRootDir().getAbsolutePath();
95         read(tenantsRootPath);
96     }
97
98     @Override
99     public void read(String tenantRootDirPath) throws Exception {
100         File tenantsRootDir = new File(tenantRootDirPath);
101         if (tenantsRootDir.exists() == false) {
102                 throw new Exception("Cound not find tenant bindings root directory: " +
103                                 tenantRootDirPath);
104         }
105         List<File> tenantDirs = getDirectories(tenantsRootDir);
106         tenantBindingTypeList = readTenantConfigs(tenantDirs);
107         
108         for (TenantBindingType tenantBinding : tenantBindingTypeList) {
109             tenantBindings.put(tenantBinding.getId(), tenantBinding);
110             readDomains(tenantBinding);
111             readServiceBindings(tenantBinding);
112             if (logger.isDebugEnabled()) {
113                 logger.debug("read() added tenant id=" + tenantBinding.getId()
114                         + " name=" + tenantBinding.getName());
115             }
116         }
117     }
118
119         List<TenantBindingType> readTenantConfigs(List<File> tenantDirList) throws IOException {
120                 List<TenantBindingType> result = new ArrayList<TenantBindingType>();            
121                 //
122                 // Iterate through a list of directories.
123                 //
124                 for (File tenantDir : tenantDirList) {
125                         boolean found = false;
126                         String errMessage = null;
127                         File configFile = new File(tenantDir.getAbsoluteFile() + File.separator + TENANT_BINDINGS_FILENAME);
128                         if (configFile.exists() == true) {
129                                 TenantBindingConfig tenantBindingConfig = (TenantBindingConfig) parse(
130                                                 configFile, TenantBindingConfig.class);
131                                 if (tenantBindingConfig != null) {
132                                         TenantBindingType binding = tenantBindingConfig.getTenantBinding();
133                                         if (binding != null) {
134                                                 result.add(binding);
135                                                 found = true;
136                                                 if (logger.isInfoEnabled() == true) {
137                                                         logger.info("Parsed tenant configureation for: " + binding.getDisplayName());
138                                                 }
139                                         } else {
140                                                 errMessage = "Cound not parse the tentant bindings in: ";
141                                         }
142                                 } else {
143                                         errMessage = "Could not parse the tenant bindings file: ";                              
144                                 }
145                         } else {
146                                 errMessage = "Cound not find a tenant configuration file: ";
147                         }
148                         if (found == false) {
149                                 if (logger.isErrorEnabled() == true) {
150                                         errMessage = errMessage != null ? errMessage : TENANT_BINDINGS_ERROR;
151                                         logger.error(errMessage + configFile.getAbsolutePath());
152                                 }
153                         }
154                 } // else-for
155                 
156                 return result;
157         }
158     
159     private void readDomains(TenantBindingType tenantBinding) throws Exception {
160         for (RepositoryDomainType domain : tenantBinding.getRepositoryDomain()) {
161                 String key = getTenantQualifiedIdentifier(tenantBinding.getId(),
162                                 domain.getName());
163             domains.put(key, domain);
164         }
165     }
166
167     private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
168         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
169             String key = getTenantQualifiedServiceName(tenantBinding.getId(),
170                     serviceBinding.getName());
171             serviceBindings.put(key, serviceBinding);
172
173             if (serviceBinding!=null){
174                 ServiceObjectType objectType = serviceBinding.getObject();
175                 if (objectType!=null){
176                     String docType = objectType.getName();
177                     String docTypeKey = getTenantQualifiedIdentifier(tenantBinding.getId(), docType);
178                     docTypes.put(docTypeKey, serviceBinding);
179                 }
180             }
181             if (logger.isDebugEnabled()) {
182                 logger.debug("readServiceBindings() added service "
183                         + " name=" + key
184                         + " workspace=" + serviceBinding.getName());
185             }
186         }
187     }
188
189     @Override
190     public List<TenantBindingType> getConfiguration() {
191         return tenantBindingTypeList;
192     }
193
194     /**
195      * getTenantBindings returns all the tenant bindings read from configuration
196      * @return
197      */
198     public Hashtable<String, TenantBindingType> getTenantBindings() {
199         return tenantBindings;
200     }
201
202     /**
203      * getTenantBinding gets tenant binding for given tenant
204      * @param tenantId
205      * @return
206      */
207     public TenantBindingType getTenantBinding(
208             String tenantId) {
209         return tenantBindings.get(tenantId);
210     }
211
212     /**
213      * getRepositoryDomain gets repository domain configuration for the given name
214      * @param domainName
215      * @return
216      */
217     public RepositoryDomainType getRepositoryDomain(String domainName) {
218         return domains.get(domainName.trim());
219     }
220
221     /**
222      * getRepositoryDomain gets repository domain configuration for the given service
223      * and given tenant id
224      * @param tenantId
225      * @param serviceName
226      * @return
227      */
228     public RepositoryDomainType getRepositoryDomain(String tenantId, String serviceName) {
229         ServiceBindingType serviceBinding = getServiceBinding(tenantId, serviceName);
230         if (serviceBinding == null) {
231             throw new IllegalArgumentException("no service binding found for " + serviceName
232                     + " of tenant with id=" + tenantId);
233         }
234         String repoDomain = serviceBinding.getRepositoryDomain(); 
235         if (repoDomain == null) {
236                 /* This is excessive - every call to a JPA based service dumps this msg.
237             if (logger.isDebugEnabled()) {
238                 logger.debug("No repository domain configured for " + serviceName
239                         + " of tenant with id=" + tenantId);
240             }
241             */
242             return null;
243         }
244         String key = this.getTenantQualifiedIdentifier(tenantId, repoDomain.trim());
245         return domains.get(key);
246     }
247
248     /**
249      * getServiceBinding gets service binding for given tenant for a given service
250      * @param tenantId
251      * @param serviceName
252      * @return
253      */
254     public ServiceBindingType getServiceBinding(
255             String tenantId, String serviceName) {
256         String key = getTenantQualifiedServiceName(tenantId, serviceName);
257         return serviceBindings.get(key);
258     }
259
260     /**
261      * getServiceBinding gets service binding for given tenant for a given service
262      * @param tenantId
263      * @param docType
264      * @return
265      */
266     public ServiceBindingType getServiceBindingForDocType (String tenantId, String docType) {
267         String key = getTenantQualifiedIdentifier(tenantId, docType);
268         return docTypes.get(key);
269     }
270
271     /**
272      * getServiceBinding gets service binding for given tenant for a given service
273      * @param tenantId
274      * @param serviceName
275      * @return
276      */
277     public List<ServiceBindingType> getServiceBindingsByType(
278             String tenantId, String serviceType) {
279         ArrayList<ServiceBindingType> list = null;
280         TenantBindingType tenant = tenantBindings.get(tenantId);
281         if (tenant != null) {
282             for (ServiceBindingType sb : tenant.getServiceBindings()) {
283                 if (serviceType.equals(sb.getType())) {
284                     if (list == null) {
285                         list = new ArrayList<ServiceBindingType>();
286                     }
287                     list.add(sb);
288                 }
289             }
290         }
291         return list;
292     }
293
294     /**
295      * @param tenantId
296      * @param serviceName
297      * @return the properly qualified service name
298      */
299     public static String getTenantQualifiedServiceName(
300             String tenantId, String serviceName) {
301 //        return tenantId + "." + serviceName.toLowerCase();
302         return getTenantQualifiedIdentifier(tenantId, serviceName.toLowerCase());
303     }
304
305     public static String getTenantQualifiedIdentifier(String tenantId, String identifier) {
306         return tenantId + "." + identifier;
307     }
308     /**
309      * Sets properties in the passed list on the local properties for this TenantBinding.
310      * Note: will only set properties not already set on the TenantBinding.
311      * 
312      * @param propList
313      * @param propagateToServices If true, recurses to set set properties 
314      *                  on the associated services.
315      */
316     public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
317             boolean propagateToServices) {
318         // For each tenant, set properties in list that are not already set
319         if (propList == null || propList.isEmpty()) {
320             return;
321         }
322         for (TenantBindingType tenant : tenantBindings.values()) {
323             for (PropertyItemType prop : propList) {
324                 TenantBindingUtils.setPropertyValue(tenant,
325                         prop, TenantBindingUtils.SET_PROP_IF_MISSING);
326             }
327             if (propagateToServices) {
328                 TenantBindingUtils.propagatePropertiesToServices(tenant,
329                         TenantBindingUtils.SET_PROP_IF_MISSING);
330             }
331         }
332     }
333
334     public String getResourcesDir(){
335         return getConfigRootDir() + File.separator + "resources";
336     }
337 }