]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fdebfe5d420d899ab7f7be365a4b0192c8aa9f39
[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.context;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import org.collectionspace.services.common.ClientType;
30 import org.collectionspace.services.common.ServiceMain;
31 import org.collectionspace.services.common.config.TenantBindingConfigReader;
32 import org.collectionspace.services.common.service.ObjectPartType;
33 import org.collectionspace.services.common.service.ServiceBindingType;
34 import org.collectionspace.services.common.service.ServiceObjectType;
35 import org.collectionspace.services.common.tenant.TenantBindingType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * AbstractServiceContext
41  *
42  * $LastChangedRevision: $
43  * $LastChangedDate: $
44  */
45 public abstract class AbstractServiceContext<T1, T2>
46         implements ServiceContext<T1, T2> {
47
48     final Logger logger = LoggerFactory.getLogger(AbstractServiceContext.class);
49     Map<String, ObjectPartType> objectPartMap = new HashMap<String, ObjectPartType>();
50     private ServiceBindingType serviceBinding;
51     private TenantBindingType tenantBinding;
52
53     public AbstractServiceContext(String serviceName) {
54         TenantBindingConfigReader tReader =
55                 ServiceMain.getInstance().getTenantBindingConfigReader();
56         //TODO: get tenant binding from security context (Subject.g
57         String tenantId = "1"; //hardcoded for movingimages.us
58         tenantBinding = tReader.getTenantBinding(tenantId);
59         if(tenantBinding == null){
60             String msg = "No tenant binding found while processing request for " +
61                     serviceName;
62             logger.error(msg);
63             throw new IllegalStateException(msg);
64         }
65         serviceBinding = tReader.getServiceBinding(tenantId, serviceName);
66         if(serviceBinding == null){
67             String msg = "No service binding found while processing request for " +
68                     serviceName + " for tenant id=" + getTenantId() +
69                     " name=" + getTenantName();
70             logger.error(msg);
71             throw new IllegalStateException(msg);
72         }
73         if(logger.isDebugEnabled()){
74             logger.debug("tenantId=" + tenantId +
75                     " service binding=" + serviceBinding.getName());
76         }
77     }
78
79     /**
80      * getCommonPartLabel get common part label
81      * @return
82      */
83     @Override
84     public String getCommonPartLabel() {
85         return getServiceName().toLowerCase() + "-common";
86     }
87
88     @Override
89     public Map<String, ObjectPartType> getPartsMetadata() {
90         if(objectPartMap.size() != 0){
91             return objectPartMap;
92         }
93         ServiceBindingType serviceBinding = getServiceBinding();
94         List<ServiceObjectType> objectTypes = serviceBinding.getObject();
95         for(ServiceObjectType objectType : objectTypes){
96             List<ObjectPartType> objectPartTypes = objectType.getPart();
97             for(ObjectPartType objectPartType : objectPartTypes){
98                 objectPartMap.put(objectPartType.getLabel(), objectPartType);
99             }
100         }
101         return objectPartMap;
102     }
103
104     @Override
105     public String getQualifiedServiceName() {
106         return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
107     }
108
109     @Override
110     public String getRepositoryClientName() {
111         return serviceBinding.getRepositoryClient();
112     }
113
114     @Override
115     public ClientType getRepositoryClientType() {
116         //assumption: there is only one repository client configured
117         return ServiceMain.getInstance().getClientType();
118     }
119
120     @Override
121     public String getRepositoryDomainName() {
122         return tenantBinding.getRepositoryDomain();
123     }
124
125     @Override
126     public String getRepositoryWorkspaceId() {
127         TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
128         return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
129     }
130
131     @Override
132     public String getRepositoryWorkspaceName() {
133         //service name is workspace name by convention
134         return serviceBinding.getName();
135     }
136
137     @Override
138     public ServiceBindingType getServiceBinding() {
139         return serviceBinding;
140     }
141
142     @Override
143     public String getServiceName() {
144         return serviceBinding.getName();
145     }
146
147     @Override
148     public String getTenantId() {
149         return tenantBinding.getId();
150     }
151
152     @Override
153     public String getTenantName() {
154         return tenantBinding.getName();
155     }
156
157     @Override
158     public abstract T1 getInput();
159
160     @Override
161     public abstract void setInput(T1 input) throws Exception;
162
163     @Override
164     public abstract T2 getOutput();
165
166     @Override
167     public abstract void setOutput(T2 output) throws Exception;
168
169     @Override
170     public String toString() {
171         return "AbstractServiceContext [" +
172                 "service name=" + serviceBinding.getName() + " " +
173                 "service version=" + serviceBinding.getVersion() + " " +
174                 "tenant id=" + tenantBinding.getId() + " " +
175                 "tenant name=" + tenantBinding.getName() + " " +
176                 tenantBinding.getDisplayName() + " " +
177                 "tenant repository domain=" + tenantBinding.getRepositoryDomain() + " " +
178                 "]";
179     }
180 }