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.IOException;
28 import java.util.ArrayList;
29 import java.util.Hashtable;
30 import java.util.List;
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;
42 * ServicesConfigReader reads service layer specific configuration
44 * $LastChangedRevision: $
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";
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>();
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>();
65 //tenant-qualified service object name to service name, service binding
66 private Hashtable<String, ServiceBindingType> docTypes =
67 new Hashtable<String, ServiceBindingType>();
70 public TenantBindingConfigReaderImpl(String serverRootDir) {
75 public String getFileName() {
76 return TENANT_BINDINGS_FILENAME;
79 protected File getTenantsRootDir() {
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
87 logger.error("Tenants home directory is missing. Can't find: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isError() == true) check
93 public void read() throws Exception {
94 String tenantsRootPath = getTenantsRootDir().getAbsolutePath();
95 read(tenantsRootPath);
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: " +
105 List<File> tenantDirs = getDirectories(tenantsRootDir);
106 tenantBindingTypeList = readTenantConfigs(tenantDirs);
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());
119 List<TenantBindingType> readTenantConfigs(List<File> tenantDirList) throws IOException {
120 List<TenantBindingType> result = new ArrayList<TenantBindingType>();
122 // Iterate through a list of directories.
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) {
136 if (logger.isInfoEnabled() == true) {
137 logger.info("Parsed tenant configureation for: " + binding.getDisplayName());
140 errMessage = "Cound not parse the tentant bindings in: ";
143 errMessage = "Could not parse the tenant bindings file: ";
146 errMessage = "Cound not find a tenant configuration file: ";
148 if (found == false) {
149 if (logger.isErrorEnabled() == true) {
150 errMessage = errMessage != null ? errMessage : TENANT_BINDINGS_ERROR;
151 logger.error(errMessage + configFile.getAbsolutePath());
159 private void readDomains(TenantBindingType tenantBinding) throws Exception {
160 for (RepositoryDomainType domain : tenantBinding.getRepositoryDomain()) {
161 String key = getTenantQualifiedIdentifier(tenantBinding.getId(),
163 domains.put(key, domain);
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);
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);
181 if (logger.isDebugEnabled()) {
182 logger.debug("readServiceBindings() added service "
184 + " workspace=" + serviceBinding.getName());
190 public List<TenantBindingType> getConfiguration() {
191 return tenantBindingTypeList;
195 * getTenantBindings returns all the tenant bindings read from configuration
198 public Hashtable<String, TenantBindingType> getTenantBindings() {
199 return tenantBindings;
203 * getTenantBinding gets tenant binding for given tenant
207 public TenantBindingType getTenantBinding(
209 return tenantBindings.get(tenantId);
213 * getRepositoryDomain gets repository domain configuration for the given name
217 public RepositoryDomainType getRepositoryDomain(String domainName) {
218 return domains.get(domainName.trim());
222 * getRepositoryDomain gets repository domain configuration for the given service
223 * and given tenant id
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);
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);
244 String key = this.getTenantQualifiedIdentifier(tenantId, repoDomain.trim());
245 return domains.get(key);
249 * getServiceBinding gets service binding for given tenant for a given service
254 public ServiceBindingType getServiceBinding(
255 String tenantId, String serviceName) {
256 String key = getTenantQualifiedServiceName(tenantId, serviceName);
257 return serviceBindings.get(key);
261 * getServiceBinding gets service binding for given tenant for a given service
266 public ServiceBindingType getServiceBindingForDocType (String tenantId, String docType) {
267 String key = getTenantQualifiedIdentifier(tenantId, docType);
268 return docTypes.get(key);
272 * getServiceBinding gets service binding for given tenant for a given service
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())) {
285 list = new ArrayList<ServiceBindingType>();
297 * @return the properly qualified service name
299 public static String getTenantQualifiedServiceName(
300 String tenantId, String serviceName) {
301 // return tenantId + "." + serviceName.toLowerCase();
302 return getTenantQualifiedIdentifier(tenantId, serviceName.toLowerCase());
305 public static String getTenantQualifiedIdentifier(String tenantId, String identifier) {
306 return tenantId + "." + identifier;
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.
313 * @param propagateToServices If true, recurses to set set properties
314 * on the associated services.
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()) {
322 for (TenantBindingType tenant : tenantBindings.values()) {
323 for (PropertyItemType prop : propList) {
324 TenantBindingUtils.setPropertyValue(tenant,
325 prop, TenantBindingUtils.SET_PROP_IF_MISSING);
327 if (propagateToServices) {
328 TenantBindingUtils.propagatePropertiesToServices(tenant,
329 TenantBindingUtils.SET_PROP_IF_MISSING);
334 public String getResourcesDir(){
335 return getConfigRootDir() + File.separator + "resources";