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.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
31 import org.collectionspace.services.common.ClientType;
32 import org.collectionspace.services.common.RepositoryClientConfigType;
33 import org.collectionspace.services.common.ServiceMain;
34 import org.collectionspace.services.common.repository.RepositoryClient;
35 import org.collectionspace.services.common.repository.RepositoryClientFactory;
36 import org.collectionspace.services.common.service.ServiceBindingType;
37 import org.collectionspace.services.common.tenant.TenantBindingType;
38 import org.collectionspace.services.common.tenant.TenantBindingConfig;
39 import org.collectionspace.services.common.types.PropertyItemType;
40 import org.collectionspace.services.common.types.PropertyType;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * ServicesConfigReader reads service layer specific configuration
47 * $LastChangedRevision: $
50 public class TenantBindingConfigReaderImpl
51 extends AbstractConfigReaderImpl<TenantBindingConfig> {
53 final private static String CONFIG_FILE_NAME = "tenant-bindings.xml";
54 final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class);
55 private TenantBindingConfig tenantBindingConfig;
56 //tenant name, tenant binding
57 private Hashtable<String, TenantBindingType> tenantBindings =
58 new Hashtable<String, TenantBindingType>();
59 //tenant-qualified servicename, service binding
60 private Hashtable<String, ServiceBindingType> serviceBindings =
61 new Hashtable<String, ServiceBindingType>();
62 //tenant-qualified service, workspace
63 private Hashtable<String, String> serviceWorkspaces = new Hashtable<String, String>();
65 public TenantBindingConfigReaderImpl(String serverRootDir) {
70 public String getFileName() {
71 return CONFIG_FILE_NAME;
75 public void read() throws Exception {
76 String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME);
77 File configFile = new File(configFileName);
78 if (!configFile.exists()) {
79 String msg = "Could not find configuration file " + configFileName;
81 throw new RuntimeException(msg);
83 tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class);
84 for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) {
85 tenantBindings.put(tenantBinding.getId(), tenantBinding);
86 readServiceBindings(tenantBinding);
87 if (logger.isDebugEnabled()) {
88 logger.debug("read() added tenant id=" + tenantBinding.getId()
89 + " name=" + tenantBinding.getName());
94 private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
95 for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
96 String key = getTenantQualifiedServiceName(tenantBinding.getId(),
97 serviceBinding.getName());
98 serviceBindings.put(key, serviceBinding);
99 if (logger.isDebugEnabled()) {
100 logger.debug("readServiceBindings() added service "
102 + " workspace=" + serviceBinding.getName());
108 * retrieveWorkspaceIds is called at initialization time to retrieve
109 * workspace ids of all the tenants
112 public void retrieveAllWorkspaceIds() throws Exception {
113 for (TenantBindingType tenantBinding : tenantBindings.values()) {
114 retrieveWorkspaceIds(tenantBinding);
119 * retrieveWorkspaceIds retrieves workspace ids for services used by
121 * @param tenantBinding
124 public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
125 Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
126 ServiceMain svcMain = ServiceMain.getInstance();
127 RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
128 ClientType clientType = svcMain.getClientType();
129 if (clientType.equals(ClientType.JAVA)
130 && rclientConfig.getName().equalsIgnoreCase("nuxeo-java")) {
131 //FIXME only one repository client is recognized
132 workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
133 tenantBinding.getRepositoryDomain());
135 //verify if workspace exists for each service in the tenant binding
136 for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
137 String serviceName = serviceBinding.getName();
138 String repositoryClientName = serviceBinding.getRepositoryClient();
139 if (repositoryClientName == null) {
140 //no repository needed for this service...skip
141 if (logger.isDebugEnabled()) {
142 logger.debug("No repository configured for service " + serviceName
148 if (repositoryClientName.isEmpty()) {
149 String msg = "Invalid repositoryClient " + serviceName;
153 repositoryClientName = repositoryClientName.trim();
154 RepositoryClient repositoryClient = getRepositoryClient(
155 repositoryClientName);
156 if (repositoryClient == null) {
157 String msg = "Could not find repositoryClient " + repositoryClientName
158 + " for service=" + serviceName;
162 String workspaceId = null;
163 //workspace name is service name by convention
164 String workspace = serviceBinding.getName().toLowerCase();
165 if (clientType.equals(ClientType.JAVA)) {
166 workspaceId = workspaceIds.get(workspace);
167 if (workspaceId == null) {
168 logger.warn("Failed to retrieve workspace ID for " + workspace);
169 logger.warn("Trying to create a new workspace ...");
170 workspaceId = repositoryClient.createWorkspace(
171 tenantBinding.getRepositoryDomain(),
172 serviceBinding.getName());
173 if (workspaceId == null) {
174 logger.warn("Failed to create workspace for " + workspace);
179 workspaceId = serviceBinding.getRepositoryWorkspaceId();
180 if (workspaceId == null || "".equals(workspaceId)) {
181 logger.error("Could not find workspace ID for " + workspace);
182 //FIXME: should we throw an exception here?
186 String tenantService = getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
187 serviceWorkspaces.put(tenantService, workspaceId);
188 if (logger.isDebugEnabled()) {
189 logger.debug("retrieved workspace id=" + workspaceId
190 + " service=" + serviceName
191 + " workspace=" + workspace);
197 public TenantBindingConfig getConfiguration() {
198 return tenantBindingConfig;
202 * getTenantBinding gets tenant binding for given tenant
206 public TenantBindingType getTenantBinding(
208 return tenantBindings.get(tenantId);
212 * getServiceBinding gets service binding for given tenant for a given service
217 public ServiceBindingType getServiceBinding(
218 String tenantId, String serviceName) {
219 String key = getTenantQualifiedServiceName(tenantId, serviceName);
220 return serviceBindings.get(key);
224 * getServiceBinding gets service binding for given tenant for a given service
229 public List<ServiceBindingType> getServiceBindingsByType(
230 String tenantId, String serviceType) {
231 ArrayList<ServiceBindingType> list = null;
232 TenantBindingType tenant = tenantBindings.get(tenantId);
234 for(ServiceBindingType sb:tenant.getServiceBindings()) {
235 if(sb.getType().equals(serviceType)) {
237 list = new ArrayList<ServiceBindingType>();
246 * getWorkspaceId retrieves workspace id for given tenant for given service
251 public String getWorkspaceId(String tenantId, String serviceName) {
252 String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
253 return serviceWorkspaces.get(tenantService);
256 public static String getTenantQualifiedServiceName(
257 String tenantId, String serviceName) {
258 return tenantId + "." + serviceName.toLowerCase();
261 private RepositoryClient getRepositoryClient(String clientName) {
262 return RepositoryClientFactory.getInstance().getClient(clientName);
265 public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
266 boolean propagateToServices) {
267 // For each tenant, set properties in list that are not already set
268 if(propList == null || propList.isEmpty())
270 for(TenantBindingType tenant:tenantBindings.values()) {
271 for(PropertyItemType prop:propList){
272 TenantBindingUtils.setPropertyValue(tenant,
273 prop, TenantBindingUtils.SET_PROP_IF_MISSING );
275 if(propagateToServices) {
276 TenantBindingUtils.propagatePropertiesToServices(tenant,
277 TenantBindingUtils.SET_PROP_IF_MISSING);