]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c30b1fbe88088f0360ab1dad2b078b72c629e9df
[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.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
30
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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * ServicesConfigReader reads service layer specific configuration
45  *
46  * $LastChangedRevision: $
47  * $LastChangedDate: $
48  */
49 public class TenantBindingConfigReaderImpl
50         extends AbstractConfigReaderImpl<TenantBindingConfig> {
51
52     final private static String CONFIG_FILE_NAME = "tenant-bindings.xml";
53     final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReaderImpl.class);
54     private TenantBindingConfig tenantBindingConfig;
55     //tenant name, tenant binding
56     private Hashtable<String, TenantBindingType> tenantBindings =
57             new Hashtable<String, TenantBindingType>();
58     //tenant-qualified servicename, service binding
59     private Hashtable<String, ServiceBindingType> serviceBindings =
60             new Hashtable<String, ServiceBindingType>();
61     //tenant-qualified service, workspace
62     private Hashtable<String, String> serviceWorkspaces = new Hashtable<String, String>();
63
64     public TenantBindingConfigReaderImpl(String serverRootDir) {
65         super(serverRootDir);
66     }
67
68     @Override
69     public String getFileName() {
70         return CONFIG_FILE_NAME;
71     }
72
73     @Override
74     public void read() throws Exception {
75         String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME);
76         File configFile = new File(configFileName);
77         if (!configFile.exists()) {
78             String msg = "Could not find configuration file " + configFileName;
79             logger.error(msg);
80             throw new RuntimeException(msg);
81         }
82         tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class);
83         for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) {
84             tenantBindings.put(tenantBinding.getId(), tenantBinding);
85             readServiceBindings(tenantBinding);
86             if (logger.isDebugEnabled()) {
87                 logger.debug("read() added tenant id=" + tenantBinding.getId()
88                         + " name=" + tenantBinding.getName());
89             }
90         }
91     }
92
93     private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
94         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
95             String key = getTenantQualifiedServiceName(tenantBinding.getId(),
96                     serviceBinding.getName());
97             serviceBindings.put(key, serviceBinding);
98             if (logger.isDebugEnabled()) {
99                 logger.debug("readServiceBindings() added service "
100                         + " name=" + key
101                         + " workspace=" + serviceBinding.getName());
102             }
103         }
104     }
105
106     /**
107      * retrieveWorkspaceIds is called at initialization time to retrieve
108      * workspace ids of all the tenants
109      * @throws Exception
110      */
111     public void retrieveAllWorkspaceIds() throws Exception {
112         for (TenantBindingType tenantBinding : tenantBindings.values()) {
113             retrieveWorkspaceIds(tenantBinding);
114         }
115     }
116
117     /**
118      * retrieveWorkspaceIds retrieves workspace ids for services used by
119      * the given tenant
120      * @param tenantBinding
121      * @throws Exception
122      */
123     public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
124         Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
125         ServiceMain svcMain = ServiceMain.getInstance();
126         RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
127         ClientType clientType = svcMain.getClientType();
128         if (clientType.equals(ClientType.JAVA)
129                 && rclientConfig.getName().equalsIgnoreCase("nuxeo-java")) {
130             //FIXME only one repository client is recognized
131             workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
132                     tenantBinding.getRepositoryDomain());
133         }
134         //verify if workspace exists for each service in the tenant binding
135         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
136             String serviceName = serviceBinding.getName();
137             String repositoryClientName = serviceBinding.getRepositoryClient();
138             if (repositoryClientName == null) {
139                 //no repository needed for this service...skip
140                 if (logger.isInfoEnabled()) {
141                     logger.info("The service " + serviceName
142                             + " does not seem to require a document repository.");
143                 }
144                 continue;
145             }
146
147             if (repositoryClientName.isEmpty()) {
148                 String msg = "Invalid repositoryClient " + serviceName;
149                 logger.error(msg);
150                 continue;
151             }
152             repositoryClientName = repositoryClientName.trim();
153             RepositoryClient repositoryClient = getRepositoryClient(
154                     repositoryClientName);
155             if (repositoryClient == null) {
156                 String msg = "Could not find repositoryClient " + repositoryClientName
157                         + " for service=" + serviceName;
158                 logger.error(msg);
159                 continue;
160             }
161             String workspaceId = null;
162             //workspace name is service name by convention
163             String workspace = serviceName.toLowerCase();
164             if (clientType.equals(ClientType.JAVA)) {
165                 workspaceId = workspaceIds.get(workspace);
166                 if (workspaceId == null) {
167                     if (logger.isWarnEnabled()) {
168                         logger.warn("Failed to retrieve workspace ID for " + workspace
169                                 + " from repository, trying to create a new workspace ...");
170                     }
171                     workspaceId = repositoryClient.createWorkspace(
172                             tenantBinding.getRepositoryDomain(),
173                             serviceBinding.getName());
174                     if (workspaceId == null) {
175                         if (logger.isWarnEnabled()) {
176                             logger.warn("Failed to create workspace in repository"
177                                     + " for service=" + workspace);
178                         }
179                         continue;
180                     }
181                     if (logger.isDebugEnabled()) {
182                         logger.debug("Successfully created workspace in repository" +
183                                 " id=" + workspaceId + " for service=" + workspace);
184                     }
185                 }
186             } else {
187                 workspaceId = serviceBinding.getRepositoryWorkspaceId();
188                 if (workspaceId == null || "".equals(workspaceId)) {
189                     logger.error("Could not find workspace in repository for" +
190                             " service=" + workspace);
191                     //FIXME: should we throw an exception here?
192                     continue;
193                 }
194             }
195             String tenantService = getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
196             serviceWorkspaces.put(tenantService, workspaceId);
197             if (logger.isInfoEnabled()) {
198                 logger.info("Created/retrieved repository workspace=" +
199                         workspace + " id=" + workspaceId
200                         + " for service=" + serviceName);
201             }
202         }
203     }
204
205     @Override
206     public TenantBindingConfig getConfiguration() {
207         return tenantBindingConfig;
208     }
209
210     /**
211      * getTenantBinding gets tenant binding for given tenant
212      * @param tenantId
213      * @return
214      */
215     public TenantBindingType getTenantBinding(
216             String tenantId) {
217         return tenantBindings.get(tenantId);
218     }
219
220     /**
221      * getServiceBinding gets service binding for given tenant for a given service
222      * @param tenantId
223      * @param serviceName
224      * @return
225      */
226     public ServiceBindingType getServiceBinding(
227             String tenantId, String serviceName) {
228         String key = getTenantQualifiedServiceName(tenantId, serviceName);
229         return serviceBindings.get(key);
230     }
231
232     /**
233      * getServiceBinding gets service binding for given tenant for a given service
234      * @param tenantId
235      * @param serviceName
236      * @return
237      */
238     public List<ServiceBindingType> getServiceBindingsByType(
239             String tenantId, String serviceType) {
240         ArrayList<ServiceBindingType> list = null;
241         TenantBindingType tenant = tenantBindings.get(tenantId);
242         if (tenant != null) {
243             for (ServiceBindingType sb : tenant.getServiceBindings()) {
244                 if (serviceType.equals(sb.getType())) {
245                     if (list == null) {
246                         list = new ArrayList<ServiceBindingType>();
247                     }
248                     list.add(sb);
249                 }
250             }
251         }
252         return list;
253     }
254
255     /**
256      * getWorkspaceId retrieves workspace id for given tenant for given service
257      * @param tenantId
258      * @param serviceName
259      * @return
260      */
261     public String getWorkspaceId(String tenantId, String serviceName) {
262         String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
263         return serviceWorkspaces.get(tenantService);
264     }
265
266     /**
267      * @param tenantId
268      * @param serviceName
269      * @return the properly qualified service name
270      */
271     public static String getTenantQualifiedServiceName(
272             String tenantId, String serviceName) {
273         return tenantId + "." + serviceName.toLowerCase();
274     }
275
276     private RepositoryClient getRepositoryClient(String clientName) {
277         return RepositoryClientFactory.getInstance().getClient(clientName);
278     }
279
280     /**
281      * Sets properties in the passed list on the local properties for this TenantBinding.
282      * Note: will only set properties not already set on the TenantBinding.
283      * 
284      * @param propList
285      * @param propagateToServices If true, recurses to set set properties 
286      *                  on the associated services.
287      */
288     public void setDefaultPropertiesOnTenants(List<PropertyItemType> propList,
289             boolean propagateToServices) {
290         // For each tenant, set properties in list that are not already set
291         if (propList == null || propList.isEmpty()) {
292             return;
293         }
294         for (TenantBindingType tenant : tenantBindings.values()) {
295             for (PropertyItemType prop : propList) {
296                 TenantBindingUtils.setPropertyValue(tenant,
297                         prop, TenantBindingUtils.SET_PROP_IF_MISSING);
298             }
299             if (propagateToServices) {
300                 TenantBindingUtils.propagatePropertiesToServices(tenant,
301                         TenantBindingUtils.SET_PROP_IF_MISSING);
302             }
303         }
304     }
305 }