]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d6186a87366c482f5087913621a7388a4a98137f
[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 java.util.List;
29 import org.collectionspace.services.common.ClientType;
30 import org.collectionspace.services.common.RepositoryWorkspaceType;
31 import org.collectionspace.services.common.ServiceConfig;
32 import org.collectionspace.services.common.ServiceMain;
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     public void retrieveWorkspaceIds(TenantBindingType tenantBinding) throws Exception {
114         String tenantDomain = tenantBinding.getRepositoryDomain();
115         Hashtable<String, String> workspaceIds = new Hashtable<String, String>();
116         ServiceMain svcMain = ServiceMain.getInstance();
117         ClientType clientType = svcMain.getClientType();
118         if(clientType.equals(ClientType.JAVA)){
119             workspaceIds = svcMain.retrieveWorkspaceIds(tenantDomain);
120         }
121         for(ServiceBindingType serviceBinding : tenantBinding.getServiceBindings()){
122             String serviceName = serviceBinding.getName();
123             String workspaceId = null;
124             //workspace name is service name by convention
125             String workspace = serviceBinding.getName().toLowerCase();
126             if(clientType.equals(ClientType.JAVA)){
127                 workspaceId = workspaceIds.get(workspace);
128                 if(workspaceId == null){
129                     logger.warn("failed to retrieve workspace id for " + workspace);
130                     //FIXME: should we throw an exception here?
131                     continue;
132                 }
133             }else{
134                 workspaceId = serviceBinding.getRepositoryWorkspaceId();
135                 if(workspaceId == null || "".equals(workspaceId)){
136                     logger.error("could not find workspace id for " + workspace);
137                     //FIXME: should we throw an exception here?
138                     continue;
139                 }
140             }
141             String tenantService = getTenantQualifiedServiceName(tenantBinding.getId(), serviceName);
142             serviceWorkspaces.put(tenantService, workspaceId);
143             if(logger.isDebugEnabled()){
144                 logger.debug("retrieved workspace id=" + workspaceId +
145                         " service=" + serviceName +
146                         " workspace=" + workspace);
147             }
148         }
149     }
150
151     @Override
152     public TenantBindingConfig getConfiguration() {
153         return tenantBindingConfig;
154     }
155
156     /**
157      * getTenantBinding gets tenant binding for given tenant
158      * @param tenantId
159      * @return
160      */
161     public TenantBindingType getTenantBinding(
162             String tenantId) {
163         return tenantBindings.get(tenantId);
164     }
165
166     /**
167      * getServiceBinding gets service binding for given tenant for a given service
168      * @param tenantId
169      * @param serviceName
170      * @return
171      */
172     public ServiceBindingType getServiceBinding(
173             String tenantId, String serviceName) {
174         String key = getTenantQualifiedServiceName(tenantId, serviceName);
175         return serviceBindings.get(key);
176     }
177
178     /**
179      * getWorkspaceId retrieves workspace id for given tenant for given service
180      * @param tenantId
181      * @param serviceName
182      * @return
183      */
184     public String getWorkspaceId(String tenantId, String serviceName) {
185         String tenantService = getTenantQualifiedServiceName(tenantId, serviceName);
186         return serviceWorkspaces.get(tenantService);
187     }
188
189     public static String getTenantQualifiedServiceName(
190             String tenantId, String serviceName) {
191         return tenantId + "." + serviceName.toLowerCase();
192     }
193 }