]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b819b823495a23337b49f67a723ff2ad6d77b00b
[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             domains.put(domain.getName(), domain);
162         }
163     }
164
165     private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
166         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
167             String key = getTenantQualifiedServiceName(tenantBinding.getId(),
168                     serviceBinding.getName());
169             serviceBindings.put(key, serviceBinding);
170
171             if (serviceBinding!=null){
172                 ServiceObjectType objectType = serviceBinding.getObject();
173                 if (objectType!=null){
174                     String docType = objectType.getName();
175                     String docTypeKey = getTenantQualifiedIdentifier(tenantBinding.getId(), docType);
176                     docTypes.put(docTypeKey, serviceBinding);
177                 }
178             }
179             if (logger.isDebugEnabled()) {
180                 logger.debug("readServiceBindings() added service "
181                         + " name=" + key
182                         + " workspace=" + serviceBinding.getName());
183             }
184         }
185     }
186
187     @Override
188     public List<TenantBindingType> getConfiguration() {
189         return tenantBindingTypeList;
190     }
191
192     /**
193      * getTenantBindings returns all the tenant bindings read from configuration
194      * @return
195      */
196     public Hashtable<String, TenantBindingType> getTenantBindings() {
197         return tenantBindings;
198     }
199
200     /**
201      * getTenantBinding gets tenant binding for given tenant
202      * @param tenantId
203      * @return
204      */
205     public TenantBindingType getTenantBinding(
206             String tenantId) {
207         return tenantBindings.get(tenantId);
208     }
209
210     /**
211      * getRepositoryDomain gets repository domain configuration for the given name
212      * @param domainName
213      * @return
214      */
215     public RepositoryDomainType getRepositoryDomain(String domainName) {
216         return domains.get(domainName.trim());
217     }
218
219     /**
220      * getRepositoryDomain gets repository domain configuration for the given service
221      * and given tenant id
222      * @param tenantId
223      * @param serviceName
224      * @return
225      */
226     public RepositoryDomainType getRepositoryDomain(String tenantId, String serviceName) {
227         ServiceBindingType serviceBinding = getServiceBinding(tenantId, serviceName);
228         if (serviceBinding == null) {
229             throw new IllegalArgumentException("no service binding found for " + serviceName
230                     + " of tenant with id=" + tenantId);
231         }
232         String repoDomain = serviceBinding.getRepositoryDomain(); 
233         if (repoDomain == null) {
234                 /* This is excessive - every call to a JPA based service dumps this msg.
235             if (logger.isDebugEnabled()) {
236                 logger.debug("No repository domain configured for " + serviceName
237                         + " of tenant with id=" + tenantId);
238             }
239             */
240             return null;
241         }
242         return domains.get(repoDomain.trim());
243     }
244
245     /**
246      * getServiceBinding gets service binding for given tenant for a given service
247      * @param tenantId
248      * @param serviceName
249      * @return
250      */
251     public ServiceBindingType getServiceBinding(
252             String tenantId, String serviceName) {
253         String key = getTenantQualifiedServiceName(tenantId, serviceName);
254         return serviceBindings.get(key);
255     }
256
257     /**
258      * getServiceBinding gets service binding for given tenant for a given service
259      * @param tenantId
260      * @param docType
261      * @return
262      */
263     public ServiceBindingType getServiceBindingForDocType (String tenantId, String docType) {
264         String key = getTenantQualifiedIdentifier(tenantId, docType);
265         return docTypes.get(key);
266     }
267
268     /**
269      * getServiceBinding gets service binding for given tenant for a given service
270      * @param tenantId
271      * @param serviceName
272      * @return
273      */
274     public List<ServiceBindingType> getServiceBindingsByType(
275             String tenantId, String serviceType) {
276         ArrayList<ServiceBindingType> list = null;
277         TenantBindingType tenant = tenantBindings.get(tenantId);
278         if (tenant != null) {
279             for (ServiceBindingType sb : tenant.getServiceBindings()) {
280                 if (serviceType.equals(sb.getType())) {
281                     if (list == null) {
282                         list = new ArrayList<ServiceBindingType>();
283                     }
284                     list.add(sb);
285                 }
286             }
287         }
288         return list;
289     }
290
291     /**
292      * @param tenantId
293      * @param serviceName
294      * @return the properly qualified service name
295      */
296     public static String getTenantQualifiedServiceName(
297             String tenantId, String serviceName) {
298         return tenantId + "." + serviceName.toLowerCase();
299     }
300
301     public static String getTenantQualifiedIdentifier(String tenantId, String identifier) {
302         return tenantId + "." + identifier;
303     }
304     /**
305      * Sets properties in the passed list on the local properties for this TenantBinding.
306      * Note: will only set properties not already set on the TenantBinding.
307      * 
308      * @param propList
309      * @param propagateToServices If true, recurses to set set properties 
310      *                  on the associated services.
311      */
312     public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
313             boolean propagateToServices) {
314         // For each tenant, set properties in list that are not already set
315         if (propList == null || propList.isEmpty()) {
316             return;
317         }
318         for (TenantBindingType tenant : tenantBindings.values()) {
319             for (PropertyItemType prop : propList) {
320                 TenantBindingUtils.setPropertyValue(tenant,
321                         prop, TenantBindingUtils.SET_PROP_IF_MISSING);
322             }
323             if (propagateToServices) {
324                 TenantBindingUtils.propagatePropertiesToServices(tenant,
325                         TenantBindingUtils.SET_PROP_IF_MISSING);
326             }
327         }
328     }
329
330     public String getResourcesDir(){
331         return getConfigRootDir() + File.separator + "resources";
332     }
333 }