]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
aeea00ff358b902791137e5ec4bd179f1bc03a0b
[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 getCommonPartLabel(getServiceName());
86     }
87
88     /**
89      * getCommonPartLabel get common part label
90      * @return
91      */
92     public String getCommonPartLabel(String schemaName) {
93         return schemaName.toLowerCase() + PART_LABEL_SEPERATOR + PART_COMMON_LABEL;
94     }
95
96     @Override
97     public Map<String, ObjectPartType> getPartsMetadata() {
98         if(objectPartMap.size() != 0){
99             return objectPartMap;
100         }
101         ServiceBindingType serviceBinding = getServiceBinding();
102         List<ServiceObjectType> objectTypes = serviceBinding.getObject();
103         for(ServiceObjectType objectType : objectTypes){
104             List<ObjectPartType> objectPartTypes = objectType.getPart();
105             for(ObjectPartType objectPartType : objectPartTypes){
106                 objectPartMap.put(objectPartType.getLabel(), objectPartType);
107             }
108         }
109         return objectPartMap;
110     }
111
112     @Override
113     public String getQualifiedServiceName() {
114         return TenantBindingConfigReader.getTenantQualifiedServiceName(getTenantId(), getServiceName());
115     }
116
117     @Override
118     public String getRepositoryClientName() {
119         return serviceBinding.getRepositoryClient();
120     }
121
122     @Override
123     public ClientType getRepositoryClientType() {
124         //assumption: there is only one repository client configured
125         return ServiceMain.getInstance().getClientType();
126     }
127
128     @Override
129     public String getRepositoryDomainName() {
130         return tenantBinding.getRepositoryDomain();
131     }
132
133     @Override
134     public String getRepositoryWorkspaceId() {
135         TenantBindingConfigReader tbConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
136         return tbConfigReader.getWorkspaceId(getTenantId(), getServiceName());
137     }
138
139     @Override
140     public String getRepositoryWorkspaceName() {
141         //service name is workspace name by convention
142         return serviceBinding.getName();
143     }
144
145     @Override
146     public ServiceBindingType getServiceBinding() {
147         return serviceBinding;
148     }
149
150     @Override
151     public String getServiceName() {
152         return serviceBinding.getName();
153     }
154
155     @Override
156     public String getTenantId() {
157         return tenantBinding.getId();
158     }
159
160     @Override
161     public String getTenantName() {
162         return tenantBinding.getName();
163     }
164
165     @Override
166     public abstract T1 getInput();
167
168     @Override
169     public abstract void setInput(T1 input) throws Exception;
170
171     @Override
172     public abstract T2 getOutput();
173
174     @Override
175     public abstract void setOutput(T2 output) throws Exception;
176
177     @Override
178     public String toString() {
179         return "AbstractServiceContext [" +
180                 "service name=" + serviceBinding.getName() + " " +
181                 "service version=" + serviceBinding.getVersion() + " " +
182                 "tenant id=" + tenantBinding.getId() + " " +
183                 "tenant name=" + tenantBinding.getName() + " " +
184                 tenantBinding.getDisplayName() + " " +
185                 "tenant repository domain=" + tenantBinding.getRepositoryDomain() + " " +
186                 "]";
187     }
188 }