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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.context;
26 import java.lang.reflect.Constructor;
28 import javax.ws.rs.core.UriInfo;
30 import org.collectionspace.services.common.CollectionSpaceResource;
31 import org.collectionspace.services.common.ResourceMap;
32 import org.collectionspace.services.common.ServiceMain;
33 import org.collectionspace.services.common.config.ConfigUtils;
34 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
35 import org.collectionspace.services.common.document.TransactionException;
36 import org.collectionspace.services.common.security.UnauthorizedException;
37 import org.collectionspace.services.common.storage.StorageClient;
38 import org.collectionspace.services.common.storage.TransactionContext;
39 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
40 import org.collectionspace.services.config.service.ServiceBindingType;
41 import org.collectionspace.services.config.tenant.TenantBindingType;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * RemoteServiceContextImpl
49 * $LastChangedRevision: $
52 public class RemoteServiceContextImpl<IT, OT>
53 extends AbstractServiceContextImpl<IT, OT>
54 implements RemoteServiceContext<IT, OT> {
57 final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
58 //input stores original content as received over the wire
63 /** The target of the HTTP request **/
64 JaxRsContext jaxRsContext;
66 ResourceMap resourceMap = null;
69 public void setJaxRsContext(JaxRsContext theJaxRsContext) {
70 this.jaxRsContext = theJaxRsContext;
74 public JaxRsContext getJaxRsContext() {
75 return this.jaxRsContext;
79 * Instantiates a new remote service context impl.
81 * @param serviceName the service name
83 * @throws UnauthorizedException the unauthorized exception
85 protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
86 super(serviceName, uriInfo);
90 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
92 * @param serviceName the service name
94 * @throws UnauthorizedException the unauthorized exception
96 protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
97 this(serviceName, uriInfo);
98 this.input = theInput;
102 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
104 * @param serviceName the service name
105 * @param theInput the the input
106 * @param queryParams the query params
108 * @throws UnauthorizedException the unauthorized exception
110 protected RemoteServiceContextImpl(String serviceName,
112 ResourceMap resourceMap,
113 UriInfo uriInfo) throws UnauthorizedException {
114 this(serviceName, theInput, uriInfo);
115 this.setResourceMap(resourceMap);
116 this.setUriInfo(uriInfo);
117 if (uriInfo != null) {
118 this.setQueryParams(uriInfo.getQueryParameters());
123 * Returns the name of the service's acting repository. Gets this from the tenant and service bindings files
126 public String getRepositoryName() throws Exception {
127 String result = null;
129 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
130 String tenantId = this.getTenantId();
131 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
132 ServiceBindingType serviceBindingType = this.getServiceBinding();
133 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
134 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
135 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
137 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
138 serviceBindingType.getName(), tenantId);
139 throw new Exception(errMsg);
146 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
149 public IT getInput() {
154 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
157 public void setInput(IT input) {
158 //for security reasons, do not allow to set input again (from handlers)
159 if (this.input != null) {
160 String msg = "Resetting or changing an context's input is not allowed.";
162 throw new IllegalStateException(msg);
168 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
171 public OT getOutput() {
176 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
179 public void setOutput(OT output) {
180 this.output = output;
184 * Return the JAX-RS resource for the current context.
190 public CollectionSpaceResource<IT, OT> getResource(ServiceContext<?, ?> ctx) throws Exception {
191 CollectionSpaceResource<IT, OT> result = null;
193 ResourceMap resourceMap = ctx.getResourceMap();
194 String resourceName = ctx.getClient().getServiceName();
195 result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
201 * @return the map of service names to resource classes.
204 public ResourceMap getResourceMap() {
205 ResourceMap result = resourceMap;
207 if (result == null) {
208 result = ServiceMain.getInstance().getJaxRSResourceMap();
215 * @param map the map of service names to resource instances.
218 public void setResourceMap(ResourceMap map) {
219 this.resourceMap = map;
225 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
228 public ServiceContext<IT, OT> getLocalContext(String localContextClassName) throws Exception {
229 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
230 Class<?> ctxClass = cloader.loadClass(localContextClassName);
231 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
232 throw new IllegalArgumentException("getLocalContext requires "
233 + " implementation of " + ServiceContext.class.getName());
236 Constructor<?> ctor = ctxClass.getConstructor(java.lang.String.class);
237 ServiceContext<IT, OT> ctx = (ServiceContext<IT, OT>) ctor.newInstance(getServiceName());
242 public CollectionSpaceResource<IT, OT> getResource() throws Exception {
243 // TODO Auto-generated method stub
244 throw new RuntimeException("Unimplemented method.");
248 public CollectionSpaceResource<IT, OT> getResource(String serviceName)
250 // TODO Auto-generated method stub
251 throw new RuntimeException("Unimplemented method.");
255 // Transaction management methods
258 private TransactionContext getCurrentTransactionContext() {
259 return (TransactionContext) this.getProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY);
263 public void releaseConnection() throws TransactionException {
264 if (isTransactionContextShared() == true) {
265 throw new TransactionException("Attempted to release a shared storage connection. Only the originator can release the connection");
268 TransactionContext transactionCtx = getCurrentTransactionContext();
269 if (transactionCtx != null) {
270 transactionCtx.close();
271 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, null);
273 throw new TransactionException("Attempted to release a non-existent storage connection. Transaction context missing from service context.");
278 public TransactionContext openConnection() throws TransactionException {
279 TransactionContext result = getCurrentTransactionContext();
280 if (result != null) {
281 throw new TransactionException("Attempted to open a new connection when a current connection is still part of the current service context. The current connection must be closed with the releaseConnection() method.");
284 result = new JPATransactionContext(this);
285 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, result);
291 public void setTransactionContext(TransactionContext transactionCtx) {
292 // TODO Auto-generated method stub
297 * Returns true if the TransactionContext is shared with another ServiceContext instance
298 * @throws TransactionException
301 public boolean isTransactionContextShared() throws TransactionException {
302 boolean result = true;
304 TransactionContext transactionCtx = getCurrentTransactionContext();
305 if (transactionCtx != null) {
306 if (transactionCtx.getServiceContext() == this) { // check to see if the service context used to create the connection is the same as the current service context
310 throw new TransactionException("Transaction context missing from service context.");
317 public boolean hasActiveConnection() {
318 return getCurrentTransactionContext() != null;