]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d7559124befbd6ac5e45145345869bd2ce4bad32
[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.Hashtable;
28 import org.collectionspace.services.common.ClientType;
29 import org.collectionspace.services.common.RepositoryClientConfigType;
30 import org.collectionspace.services.common.ServiceMain;
31 import org.collectionspace.services.common.repository.RepositoryClient;
32 import org.collectionspace.services.common.repository.RepositoryClientFactory;
33 import org.collectionspace.services.common.service.ServiceBindingType;
34 import org.collectionspace.services.common.tenant.TenantBindingType;
35 import org.collectionspace.services.common.tenant.TenantBindingConfig;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * ServicesConfigReader reads service layer specific configuration
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  */
45 public class TenantBindingConfigReader
46         extends AbstractConfigReader<TenantBindingConfig> {
47
48     final private static String CONFIG_FILE_NAME = "tenant-bindings.xml";
49     final Logger logger = LoggerFactory.getLogger(TenantBindingConfigReader.class);
50     private TenantBindingConfig tenantBindingConfig;
51     //tenant name, tenant binding
52     private Hashtable<String, TenantBindingType> tenantBindings =
53             new Hashtable<String, TenantBindingType>();
54     //tenant-qualified servicename, service binding
55     private Hashtable<String, ServiceBindingType> serviceBindings =
56             new Hashtable<String, ServiceBindingType>();
57     //tenant-qualified service, workspace
58     private Hashtable<String, String> serviceWorkspaces = new Hashtable<String, String>();
59
60     public TenantBindingConfigReader(String serverRootDir) {
61         super(serverRootDir);
62     }
63
64     @Override
65     public String getFileName() {
66         return CONFIG_FILE_NAME;
67     }
68
69     @Override
70     public void read() throws Exception {
71         String configFileName = getAbsoluteFileName(CONFIG_FILE_NAME);
72         File configFile = new File(configFileName);
73         if (!configFile.exists()) {
74             String msg = "Could not find configuration file " + configFileName;
75             logger.error(msg);
76             throw new RuntimeException(msg);
77         }
78         tenantBindingConfig = (TenantBindingConfig) parse(configFile, TenantBindingConfig.class);
79         for (TenantBindingType tenantBinding : tenantBindingConfig.getTenantBinding()) {
80             tenantBindings.put(tenantBinding.getId(), tenantBinding);
81             readServiceBindings(tenantBinding);
82             if (logger.isDebugEnabled()) {
83                 logger.debug("read() added tenant id=" + tenantBinding.getId()
84                         + " name=" + tenantBinding.getName());
85             }
86         }
87     }
88
89     private void readServiceBindings(TenantBindingType tenantBinding) throws Exception {
90         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
91             String key = getTenantQualifiedServiceName(tenantBinding.getId(),
92                     serviceBinding.getName());
93             serviceBindings.put(key, serviceBinding);
94             if (logger.isDebugEnabled()) {
95                 logger.debug("readServiceBindings() added service "
96                         + " name=" + key
97                         + " workspace=" + serviceBinding.getName());
98             }
99         }
100     }
101
102     /**
103      * retrieveWorkspaceIds is called at initialization time to retrieve
104      * workspace ids of all the tenants
105      * @throws Exception
106      */
107     public void retrieveAllWorkspaceIds() throws Exception {
108         for (TenantBindingType tenantBinding : tenantBindings.values()) {
109             retrieveWorkspaceIds(tenantBinding);
110         }
111     }
112
113     /**
114      * retrieveWorkspaceIds retrieves workspace ids for services used by
115      * the given tenant
116      * @param tenantBinding
117      * @throws Exception
118      */
119     public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
120         Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
121         ServiceMain svcMain = ServiceMain.getInstance();
122         RepositoryClientConfigType rclientConfig = svcMain.getServicesConfigReader().getConfiguration().getRepositoryClient();
123         ClientType clientType = svcMain.getClientType();
124         if (clientType.equals(ClientType.JAVA)
125                 && rclientConfig.getName().equalsIgnoreCase("nuxeo-java")) {
126             //FIXME only one repository client is recognized
127             workspaceIds = svcMain.getNuxeoConnector().retrieveWorkspaceIds(
128                     tenantBinding.getRepositoryDomain());
129         }
130         //verify if workspace exists for each service in the tenant binding
131         for (ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()) {
132             String serviceName = serviceBinding.getName();
133             String repositoryClientName = serviceBinding.getRepositoryClient();
134             if (repositoryClientName == null) {
135                 //no repository needed for this service...skip
136                 if (logger.isDebugEnabled()) {
137                     logger.debug("No repository configured for service " + serviceName
138                             + " skipping...");
139                 }
140                 continue;
141             }
142
143             if (repositoryClientName.isEmpty()) {
144                 String msg = "Invalid repositoryClient " + serviceName;
145                 logger.error(msg);
146                 continue;
147             }
148             repositoryClientName = repositoryClientName.trim();
149             RepositoryClient repositoryClient = getRepositoryClient(
150                     repositoryClientName);
151             if (repositoryClient == null) {
152                 String msg = "Could not find repositoryClient " + repositoryClientName
153                         + " for service=" + serviceName;
154                 logger.error(msg);
155                 continue;
156             }
157             String workspaceId = null;
158             //workspace name is service name by convention
159             String workspace = serviceBinding.getName().toLowerCase();
160             if (clientType.equals(ClientType.JAVA)) {
161                 workspaceId = workspaceIds.get(workspace);
162                 if (workspaceId == null) {
163                     logger.warn("Failed to retrieve workspace ID for " + workspace);
164                     logger.warn("Trying to create a new workspace ...");
165                     workspaceId = repositoryClient.createWorkspace(
166                             tenantBinding.getRepositoryDomain(),
167                             serviceBinding.getName());
168                     if (workspaceId == null) {
169                         logger.warn("Failed to create workspace for " + workspace);
170                         continue;
171                     }
172                 }
173             } else {
174                 workspaceId = serviceBinding.getRepositoryWorkspaceId();
175                 if (workspaceId == null || "".equals(workspaceId)) {
176                     logger.error("Could not find workspace ID for " + workspace);
177                     //FIXME: should we throw an exception here?
178                     continue;
179                 }
180             }
181             String tenantService = getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
182             serviceWorkspaces.put(tenantService, workspaceId);
183             if (logger.isDebugEnabled()) {
184                 logger.debug("retrieved workspace id=" + workspaceId
185                         + " service=" + serviceName
186                         + " workspace=" + workspace);
187             }
188         }
189     }
190
191     @Override
192     public TenantBindingConfig getConfiguration() {
193         return tenantBindingConfig;
194     }
195
196     /**
197      * getTenantBinding gets tenant binding for given tenant
198      * @param tenantId
199      * @return
200      */
201     public TenantBindingType getTenantBinding(
202             String tenantId) {
203         return tenantBindings.get(tenantId);
204     }
205
206     /**
207      * getServiceBinding gets service binding for given tenant for a given service
208      * @param tenantId
209      * @param serviceName
210      * @return
211      */
212     public ServiceBindingType getServiceBinding(
213             String tenantId, String serviceName) {
214         String key = getTenantQualifiedServiceName(tenantId, serviceName);
215         return serviceBindings.get(key);
216     }
217
218     /**
219      * getWorkspaceId retrieves workspace id for given tenant for given service
220      * @param tenantId
221      * @param serviceName
222      * @return
223      */
224     public String getWorkspaceId(String tenantId, String serviceName) {
225         String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
226         return serviceWorkspaces.get(tenantService);
227     }
228
229     public static String getTenantQualifiedServiceName(
230             String tenantId, String serviceName) {
231         return tenantId + "." + serviceName.toLowerCase();
232     }
233
234     private RepositoryClient getRepositoryClient(String clientName) {
235         return RepositoryClientFactory.getInstance().getClient(clientName);
236     }
237 }