]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5419b46e352a25087c8023e4ad4cedba66c97355
[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.collectionspace.services.common.types.PropertyType;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * ServicesConfigReader reads service layer specific configuration
46  *
47  * $LastChangedRevision: $
48  * $LastChangedDate: $
49  */
50 public class TenantBindingConfigReaderImpl
51         extends AbstractConfigReaderImpl<TenantBindingConfig> {
52
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>();
64
65     public TenantBindingConfigReaderImpl(String serverRootDir) {
66         super(serverRootDir);
67     }
68
69     @Override
70     public String getFileName() {
71         return CONFIG_FILE_NAME;
72     }
73
74     @Override
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;
80             logger.error(msg);
81             throw new RuntimeException(msg);
82         }
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());
90             }
91         }
92     }
93
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 "
101                         + " name=" + key
102                         + " workspace=" + serviceBinding.getName());
103             }
104         }
105     }
106
107     /**
108      * retrieveWorkspaceIds is called at initialization time to retrieve
109      * workspace ids of all the tenants
110      * @throws Exception
111      */
112     public void retrieveAllWorkspaceIds() throws Exception {
113         for (TenantBindingType tenantBinding : tenantBindings.values()) {
114             retrieveWorkspaceIds(tenantBinding);
115         }
116     }
117
118     /**
119      * retrieveWorkspaceIds retrieves workspace ids for services used by
120      * the given tenant
121      * @param tenantBinding
122      * @throws Exception
123      */
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());
134         }
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
143                             + " skipping...");
144                 }
145                 continue;
146             }
147
148             if (repositoryClientName.isEmpty()) {
149                 String msg = "Invalid repositoryClient " + serviceName;
150                 logger.error(msg);
151                 continue;
152             }
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;
159                 logger.error(msg);
160                 continue;
161             }
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);
175                         continue;
176                     }
177                 }
178             } else {
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?
183                     continue;
184                 }
185             }
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);
192             }
193         }
194     }
195
196     @Override
197     public TenantBindingConfig getConfiguration() {
198         return tenantBindingConfig;
199     }
200
201     /**
202      * getTenantBinding gets tenant binding for given tenant
203      * @param tenantId
204      * @return
205      */
206     public TenantBindingType getTenantBinding(
207             String tenantId) {
208         return tenantBindings.get(tenantId);
209     }
210
211     /**
212      * getServiceBinding gets service binding for given tenant for a given service
213      * @param tenantId
214      * @param serviceName
215      * @return
216      */
217     public ServiceBindingType getServiceBinding(
218             String tenantId, String serviceName) {
219         String key = getTenantQualifiedServiceName(tenantId, serviceName);
220         return serviceBindings.get(key);
221     }
222
223     /**
224      * getServiceBinding gets service binding for given tenant for a given service
225      * @param tenantId
226      * @param serviceName
227      * @return
228      */
229     public List<ServiceBindingType> getServiceBindingsByType(
230             String tenantId, String serviceType) {
231         ArrayList<ServiceBindingType> list = null;
232         TenantBindingType tenant = tenantBindings.get(tenantId);
233         if(tenant!=null) {
234                 for(ServiceBindingType sb:tenant.getServiceBindings()) {
235                         if(sb.getType().equals(serviceType)) {
236                                 if(list==null)
237                                 list = new ArrayList<ServiceBindingType>();
238                                 list.add(sb);
239                         }
240                 }
241         }
242         return list;
243     }
244
245     /**
246      * getWorkspaceId retrieves workspace id for given tenant for given service
247      * @param tenantId
248      * @param serviceName
249      * @return
250      */
251     public String getWorkspaceId(String tenantId, String serviceName) {
252         String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
253         return serviceWorkspaces.get(tenantService);
254     }
255
256     public static String getTenantQualifiedServiceName(
257             String tenantId, String serviceName) {
258         return tenantId + "." + serviceName.toLowerCase();
259     }
260
261     private RepositoryClient getRepositoryClient(String clientName) {
262         return RepositoryClientFactory.getInstance().getClient(clientName);
263     }
264     
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())
269                 return;
270         for(TenantBindingType tenant:tenantBindings.values()) {
271                 for(PropertyItemType prop:propList){
272                         TenantBindingUtils.setPropertyValue(tenant,
273                                 prop, TenantBindingUtils.SET_PROP_IF_MISSING );
274                 }
275                 if(propagateToServices) {
276                         TenantBindingUtils.propagatePropertiesToServices(tenant, 
277                                         TenantBindingUtils.SET_PROP_IF_MISSING);
278                 }
279         }
280     }
281 }