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.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.security.DigestInputStream;
31 import java.security.MessageDigest;
32 import java.security.NoSuchAlgorithmException;
33 import java.util.ArrayList;
34 import java.util.Enumeration;
35 import java.util.Hashtable;
36 import java.util.List;
38 import org.apache.commons.codec.binary.Hex;
39 import org.apache.commons.codec.digest.DigestUtils;
40 import org.apache.commons.io.FileUtils;
41 import org.collectionspace.services.common.api.JEEServerDeployment;
42 import org.collectionspace.services.common.api.Tools;
43 import org.collectionspace.services.config.service.ServiceBindingType;
44 import org.collectionspace.services.config.service.ServiceObjectType;
45 import org.collectionspace.services.config.tenant.RepositoryDomainType;
46 import org.collectionspace.services.config.tenant.TenantBindingConfig;
47 import org.collectionspace.services.config.tenant.TenantBindingType;
48 import org.collectionspace.services.config.types.PropertyItemType;
50 import ch.elca.el4j.services.xmlmerge.Configurer;
51 import ch.elca.el4j.services.xmlmerge.config.AttributeMergeConfigurer;
52 import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * ServicesConfigReader reads service layer specific configuration
60 * $LastChangedRevision: $ $LastChangedDate: $
62 public class TenantBindingConfigReaderImpl extends AbstractConfigReaderImpl<List<TenantBindingType>> {
63 final public static boolean INCLUDE_CREATE_DISABLED_TENANTS = true;
64 final public static boolean EXCLUDE_CREATE_DISABLED_TENANTS = false;
66 final private static String TENANT_BINDINGS_ERROR = "Tenant bindings error(s) for tenant: ";
67 final private static String TENANT_BINDINGS_DELTA_FILENAME = JEEServerDeployment.TENANT_BINDINGS_FILENAME_PREFIX
69 final private static String MERGED_SUFFIX = ".merged.xml";
70 private static final String NO_SERVICE_BINDINGS_FOUND_ERR = "No Service bindings found.";
72 private static final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class);
73 private List<TenantBindingType> tenantBindingTypeList;
74 // tenant id, tenant binding, for tenants not marked as createDisabled
75 private Hashtable<String, TenantBindingType> enabledTenantBindings = new Hashtable<String, TenantBindingType>();
76 // tenant id, tenant binding
77 private Hashtable<String, TenantBindingType> allTenantBindings = new Hashtable<String, TenantBindingType>();
79 private Hashtable<String, RepositoryDomainType> domains = new Hashtable<String, RepositoryDomainType>();
80 // tenant-qualified servicename, service binding
81 private Hashtable<String, ServiceBindingType> serviceBindings = new Hashtable<String, ServiceBindingType>();
83 // tenant-qualified service object name to service name, service binding
84 private Hashtable<String, ServiceBindingType> docTypes = new Hashtable<String, ServiceBindingType>();
86 public TenantBindingConfigReaderImpl(String serverRootDir) {
91 public String getFileName() {
92 return TENANT_BINDINGS_DELTA_FILENAME;
95 private String getFileName(String tenantName, boolean useAppGeneratedBindings) {
96 String result = getFileName();
98 if (useAppGeneratedBindings == true) {
99 result = tenantName + "-" + result;
105 protected File getTenantsRootDir() {
107 String errMessage = null;
109 String tenantsRootPath = getConfigRootDir() + File.separator
110 + JEEServerDeployment.TENANT_BINDINGS_ROOTDIRNAME;
111 File tenantsRootDir = new File(tenantsRootPath);
112 if (tenantsRootDir.exists() == true) {
113 result = tenantsRootDir;
114 if (logger.isDebugEnabled() == true) {
115 logger.debug("The home directory for all tenants is at: " + result.getCanonicalPath());
118 errMessage = "The home directory for all tenants is missing or inaccessible: ";
120 errMessage = errMessage + tenantsRootDir.getCanonicalPath();
121 } catch (IOException ioException) {
122 errMessage = errMessage + tenantsRootDir.getAbsolutePath();
125 } catch (IOException e) {
126 // Log this exception, but continue anyway. Caller should handle the
127 // null result gracefully.
131 if (errMessage != null) {
132 logger.error(errMessage);
139 * Take the directory of the prototype bindings and the directory of the
140 * delta bindings. Merge the two and create (replace) a file named
141 * "tenant-bindings.xml"
144 private InputStream merge(File srcFile, File deltaFile) throws IOException {
145 InputStream result = null;
147 FileInputStream srcStream = new FileInputStream(srcFile);
148 FileInputStream deltaStream = new FileInputStream(deltaFile);
149 InputStream[] inputStreamArray = { srcStream, deltaStream };
151 Configurer configurer = new AttributeMergeConfigurer();
152 result = new ConfigurableXmlMerge(configurer).merge(inputStreamArray);
153 } catch (Exception e) {
154 logger.error("Could not merge tenant configuration delta file: " + deltaFile.getCanonicalPath(), e);
157 // Try to save the merge output to a file that is suffixed with
158 // ".merged.xml" in the same directory
159 // as the delta file.
161 if (result != null) {
162 File outputDir = deltaFile.getParentFile();
163 String mergedFileName = outputDir.getAbsolutePath() + File.separator
164 + JEEServerDeployment.TENANT_BINDINGS_FILENAME_PREFIX + MERGED_SUFFIX;
165 File mergedOutFile = new File(mergedFileName);
167 FileUtils.copyInputStreamToFile(result, mergedOutFile); // Save the merge file for debugging
168 } catch (IOException e) {
169 logger.warn("Could not create a copy of the merged tenant configuration at: " + mergedFileName, e);
171 result.reset(); // reset the stream even if the file create failed.
178 public void read(boolean useAppGeneratedBindings) throws Exception {
179 String tenantsRootPath = getTenantsRootDir().getAbsolutePath();
180 read(tenantsRootPath, useAppGeneratedBindings);
184 public void read(String tenantRootDirPath, boolean useAppGeneratedBindings) throws Exception {
185 File tenantsRootDir = new File(tenantRootDirPath);
186 if (tenantsRootDir.exists() == false) {
187 throw new Exception("Cound not find tenant bindings root directory: " + tenantRootDirPath);
190 List<File> tenantDirs = getDirectories(tenantsRootDir);
191 tenantBindingTypeList = readTenantConfigs(new File(tenantRootDirPath), tenantDirs, useAppGeneratedBindings);
192 if (tenantBindingTypeList == null || tenantBindingTypeList.size() < 1) {
193 throw new Exception(NO_SERVICE_BINDINGS_FOUND_ERR);
196 for (TenantBindingType tenantBinding : tenantBindingTypeList) {
197 if (allTenantBindings.get(tenantBinding.getId()) != null) {
198 TenantBindingType tenantBindingOld = allTenantBindings.get(tenantBinding.getId());
199 logger.error("Ignoring duplicate binding definition for tenant id=" + tenantBinding.getId()
200 + " existing name=" + tenantBindingOld.getName() + " conflicting (ignored) name="
201 + tenantBinding.getName());
204 allTenantBindings.put(tenantBinding.getId(), tenantBinding);
205 if (!tenantBinding.isCreateDisabled()) {
206 enabledTenantBindings.put(tenantBinding.getId(), tenantBinding);
208 logger.warn(String.format("The tenant '%s':'%s' is marked as disabled in its bindings file.",
209 tenantBinding.getName(), tenantBinding.getId()));
211 readDomains(tenantBinding);
212 readServiceBindings(tenantBinding);
213 if (logger.isInfoEnabled()) {
214 logger.info("Finished reading tenant bindings for tenant id=" + tenantBinding.getId() + " name="
215 + tenantBinding.getName());
216 if (tenantBinding.isCreateDisabled())
217 logger.info("Tenant tenant id={} is marked createDisabled.", tenantBinding.getId());
222 // Ensure that at least one tenant is enabled, otherwise abort the startup.
224 if (enabledTenantBindings.isEmpty() == true) {
225 throw new Exception("All of the configured tenants are marked as disabled in their tenant bindings. At least one tenant needs to be enabled.");
230 * Take the directory of the prototype bindings and the directory of the
231 * delta bindings. Merge the two and create (replace) a file named
232 * "tenant-bindings.xml"
234 * private static String merge(String original, String patch) { InputStream
235 * result = null; try { Configurer configurer = new
236 * AttributeMergeConfigurer();
239 * FileInputStream ins1 = new
240 * FileInputStream(".\\src\\main\\resources\\File1.xml"); FileInputStream
241 * ins2 = new FileInputStream(".\\src\\main\\resources\\File2.xml");
242 * InputStream[] inputStreamArray = {ins1, ins2};
244 * result = new ConfigurableXmlMerge(configurer).merge(inputStreamArray); //
245 * result = new ConfigurableXmlMerge(configurer).merge(new String[]
246 * {original, patch}); } catch (Exception e) { // TODO Auto-generated catch
247 * block e.printStackTrace(); } File mergedOutFile = new
248 * File(".\\target\\merged.xml"); try {
249 * FileUtils.copyInputStreamToFile(result, mergedOutFile); } catch
250 * (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
257 * Merge and read the prototype bindsings with each tenant specific bindings
258 * delta to create the final tenant bindings.
260 * @param protoBindingsFile
261 * - The prototypical bindings file.
262 * @param tenantDirList
263 * - The list of tenant directories containing tenant specific
265 * @return A list of tenant bindings.
266 * @throws IOException
267 * Signals that an I/O exception has occurred.
269 List<TenantBindingType> readTenantConfigs(File protoBindingsDir, List<File> tenantDirList,
270 boolean useAppGeneratedBindings) throws IOException {
271 List<TenantBindingType> result = new ArrayList<TenantBindingType>();
274 // Iterate through a list of directories.
276 for (File tenantDir : tenantDirList) {
277 boolean found = false;
278 String errMessage = null;
280 File tenantBindingsProtoFile = null;
281 String tenantName = tenantDir.getName(); // By convention, the
282 // directory name should
283 // be the tenant name
284 if (useAppGeneratedBindings == true) {
285 tenantBindingsProtoFile = new File(protoBindingsDir.getAbsolutePath() + File.separator + tenantName
286 + "-" + JEEServerDeployment.TENANT_BINDINGS_PROTOTYPE_FILENAME);
288 tenantBindingsProtoFile = new File(protoBindingsDir + File.separator
289 + JEEServerDeployment.TENANT_BINDINGS_PROTOTYPE_FILENAME);
292 if (tenantBindingsProtoFile.exists() == true) {
293 File configFile = new File(tenantDir.getAbsoluteFile() + File.separator
294 + getFileName(tenantName, useAppGeneratedBindings));
295 if (configFile.exists() == true) {
296 InputStream tenantBindingsStream = this.merge(tenantBindingsProtoFile, configFile);
297 TenantBindingConfig tenantBindingConfig = null;
299 tenantBindingConfig = (TenantBindingConfig) parse(tenantBindingsStream,
300 TenantBindingConfig.class);
302 // Compute the MD5 hash of the tenant's binding file. We'll persist this a little later during startup. If the value hasn't
303 // changed since we last startedup, we can skip some of the startup steps.
305 tenantBindingsStream.reset();
306 String md5hash = new String(Hex.encodeHex(DigestUtils.md5(tenantBindingsStream)));
307 tenantBindingConfig.getTenantBinding().setConfigMD5Hash(md5hash); // use this to compare with the last persisted one and to persist as the new hash
308 } catch (Exception e) {
309 logger.error("Could not parse the merged tenant bindings.", e);
311 if (tenantBindingConfig != null) {
312 TenantBindingType binding = tenantBindingConfig.getTenantBinding();
313 if (binding != null) {
316 if (logger.isInfoEnabled() == true) {
317 logger.info("Parsed tenant configuration for: " + binding.getDisplayName());
320 errMessage = "Cound not parse the tenant bindings in: ";
323 errMessage = "Could not parse the tenant bindings file: ";
326 errMessage = "Expected to, but could not, find the tenant delta configuration file: "
327 + configFile.getAbsolutePath();
330 errMessage = "Expected to, but could not, find the tenant proto configuration file: "
331 + tenantBindingsProtoFile.getAbsolutePath();
334 if (found == false) {
335 if (logger.isErrorEnabled() == true) {
336 errMessage = errMessage != null ? errMessage : TENANT_BINDINGS_ERROR;
337 logger.error(errMessage + " - For tenant: " + tenantName);
345 private void readDomains(TenantBindingType tenantBinding) throws Exception {
346 for (RepositoryDomainType domain : tenantBinding.getRepositoryDomain()) {
347 String key = getTenantQualifiedIdentifier(tenantBinding.getId(), domain.getName());
348 domains.put(key, domain);
352 private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
353 for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
354 String key = getTenantQualifiedServiceName(tenantBinding.getId(), serviceBinding.getName());
358 serviceBindings.put(key, serviceBinding);
360 if (serviceBinding != null) {
361 ServiceObjectType objectType = serviceBinding.getObject();
362 if (objectType != null) {
363 String docType = objectType.getName();
364 String docTypeKey = getTenantQualifiedIdentifier(tenantBinding.getId(), docType);
365 docTypes.put(docTypeKey, serviceBinding);
368 if (logger.isTraceEnabled()) {
369 logger.trace("readServiceBindings() added service " + " name=" + key + " workspace="
370 + serviceBinding.getName());
376 public List<TenantBindingType> getConfiguration() {
377 return tenantBindingTypeList;
381 * getTenantBindings returns all the tenant bindings read from configuration
385 public Hashtable<String, TenantBindingType> getTenantBindings() {
386 return getTenantBindings(EXCLUDE_CREATE_DISABLED_TENANTS);
390 * getTenantBindings returns all the tenant bindings read from configuration
394 public Hashtable<String, TenantBindingType> getTenantBindings(boolean includeDisabled) {
395 return includeDisabled ? allTenantBindings : enabledTenantBindings;
399 * getTenantBinding gets tenant binding for given tenant
404 public TenantBindingType getTenantBinding(String tenantId) {
405 return allTenantBindings.get(tenantId);
409 * getRepositoryDomain gets repository domain configuration for the given
415 public RepositoryDomainType getRepositoryDomain(String domainName) {
416 return domains.get(domainName.trim());
420 * getRepositoryDomain gets repository domain configuration for the given
421 * service and given tenant id
427 public RepositoryDomainType getRepositoryDomain(String tenantId, String serviceName) {
428 ServiceBindingType serviceBinding = getServiceBinding(tenantId, serviceName);
429 if (serviceBinding == null) {
430 throw new IllegalArgumentException("no service binding found for " + serviceName + " of tenant with id="
433 String repoDomain = serviceBinding.getRepositoryDomain();
434 if (repoDomain == null) {
435 if (logger.isTraceEnabled()) {
436 logger.trace("No repository domain configured for " + serviceName + " of tenant with id=" + tenantId);
440 String key = this.getTenantQualifiedIdentifier(tenantId, repoDomain.trim());
441 return domains.get(key);
445 * getServiceBinding gets service binding for given tenant for a given
452 public ServiceBindingType getServiceBinding(String tenantId, String serviceName) {
453 String key = getTenantQualifiedServiceName(tenantId, serviceName);
454 return serviceBindings.get(key);
458 * getServiceBinding gets service binding for given tenant for a given
465 public ServiceBindingType getServiceBindingForDocType(String tenantId, String docType) {
466 String key = getTenantQualifiedIdentifier(tenantId, docType);
467 return docTypes.get(key);
471 * getServiceBinding gets service binding for given tenant for a given
478 public List<ServiceBindingType> getServiceBindingsByType(String tenantId, String serviceType) {
479 List<String> serviceTypes = new ArrayList<String>(1);
480 serviceTypes.add(serviceType);
481 return getServiceBindingsByType(tenantId, serviceTypes);
485 * getServiceBindingsByType gets service bindings for a given tenant for the
486 * services that fall within a supplied set of service type(s)
489 * @param serviceTypes
492 public List<ServiceBindingType> getServiceBindingsByType(String tenantId, List<String> serviceTypes) {
493 ArrayList<ServiceBindingType> list = null;
494 TenantBindingType tenant = allTenantBindings.get(tenantId);
495 if (tenant != null) {
496 for (ServiceBindingType sb : tenant.getServiceBindings()) {
497 if (serviceTypes.contains(sb.getType())) {
499 list = new ArrayList<ServiceBindingType>();
511 * @return the properly qualified service name
513 public static String getTenantQualifiedServiceName(String tenantId, String serviceName) {
514 String result = null;
516 if (serviceName != null) {
517 logger.trace(String.format(" * tenant:serviceBindings '%s'", serviceName));
518 result = getTenantQualifiedIdentifier(tenantId, serviceName.toLowerCase());
524 public static String getTenantQualifiedIdentifier(String tenantId, String identifier) {
525 return tenantId + "." + identifier;
529 * Sets properties in the passed list on the local properties for this
530 * TenantBinding. Note: will only set properties not already set on the
534 * @param propagateToServices
535 * If true, recurses to set set properties on the associated
538 public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList, boolean propagateToServices) {
539 // For each tenant, set properties in list that are not already set
540 if (propList == null || propList.isEmpty()) {
543 for (TenantBindingType tenant : allTenantBindings.values()) {
544 for (PropertyItemType prop : propList) {
545 TenantBindingUtils.setPropertyValue(tenant, prop, TenantBindingUtils.SET_PROP_IF_MISSING);
547 if (propagateToServices) {
548 TenantBindingUtils.propagatePropertiesToServices(tenant, TenantBindingUtils.SET_PROP_IF_MISSING);
553 public String getResourcesDir() {
554 return getConfigRootDir() + File.separator + RESOURCES_DIR_NAME;
558 * Returns a list of tenant identifiers (tenant IDs).
560 * @return a list of tenant IDs
562 public List<String> getTenantIds() {
563 return getTenantIds(EXCLUDE_CREATE_DISABLED_TENANTS);
567 * Returns a list of tenant identifiers (tenant IDs).
569 * @return a list of tenant IDs
571 public List<String> getTenantIds(boolean includeDisabled) {
572 List<String> tenantIds = new ArrayList<String>();
574 Hashtable<String, TenantBindingType> tenantBindings = getTenantBindings(includeDisabled);
575 if (tenantBindings != null && !tenantBindings.isEmpty()) {
576 Enumeration keys = tenantBindings.keys();
577 while (keys.hasMoreElements()) {
578 tenantId = (String) keys.nextElement();
579 if (Tools.notBlank(tenantId)) {
580 tenantIds.add(tenantId);