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 **/
66 // Reference count for things like JPA connections
68 private int transactionConnectionRefCount = 0;
73 JaxRsContext jaxRsContext;
74 ResourceMap resourceMap = null;
77 public void setJaxRsContext(JaxRsContext theJaxRsContext) {
78 this.jaxRsContext = theJaxRsContext;
82 public JaxRsContext getJaxRsContext() {
83 return this.jaxRsContext;
87 * Instantiates a new remote service context impl.
89 * @param serviceName the service name
91 * @throws UnauthorizedException the unauthorized exception
93 protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
94 super(serviceName, uriInfo);
98 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
100 * @param serviceName the service name
102 * @throws UnauthorizedException the unauthorized exception
104 protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
105 this(serviceName, uriInfo);
106 this.input = theInput;
110 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
112 * @param serviceName the service name
113 * @param theInput the the input
114 * @param queryParams the query params
116 * @throws UnauthorizedException the unauthorized exception
118 protected RemoteServiceContextImpl(String serviceName,
120 ResourceMap resourceMap,
121 UriInfo uriInfo) throws UnauthorizedException {
122 this(serviceName, theInput, uriInfo);
123 this.setResourceMap(resourceMap);
124 this.setUriInfo(uriInfo);
125 if (uriInfo != null) {
126 this.setQueryParams(uriInfo.getQueryParameters());
131 * Returns the name of the service's acting repository. Gets this from the tenant and service bindings files
134 public String getRepositoryName() throws Exception {
135 String result = null;
137 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
138 String tenantId = this.getTenantId();
139 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
140 ServiceBindingType serviceBindingType = this.getServiceBinding();
141 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
142 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
143 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
145 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
146 serviceBindingType.getName(), tenantId);
147 throw new Exception(errMsg);
154 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
157 public IT getInput() {
162 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
165 public void setInput(IT input) {
166 //for security reasons, do not allow to set input again (from handlers)
167 if (this.input != null) {
168 String msg = "Resetting or changing an context's input is not allowed.";
170 throw new IllegalStateException(msg);
176 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
179 public OT getOutput() {
184 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
187 public void setOutput(OT output) {
188 this.output = output;
192 * Return the JAX-RS resource for the current context.
198 public CollectionSpaceResource<IT, OT> getResource(ServiceContext<?, ?> ctx) throws Exception {
199 CollectionSpaceResource<IT, OT> result = null;
201 ResourceMap resourceMap = ctx.getResourceMap();
202 String resourceName = ctx.getClient().getServiceName();
203 result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
209 * @return the map of service names to resource classes.
212 public ResourceMap getResourceMap() {
213 ResourceMap result = resourceMap;
215 if (result == null) {
216 result = ServiceMain.getInstance().getJaxRSResourceMap();
223 * @param map the map of service names to resource instances.
226 public void setResourceMap(ResourceMap map) {
227 this.resourceMap = map;
233 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
236 public ServiceContext<IT, OT> getLocalContext(String localContextClassName) throws Exception {
237 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
238 Class<?> ctxClass = cloader.loadClass(localContextClassName);
239 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
240 throw new IllegalArgumentException("getLocalContext requires "
241 + " implementation of " + ServiceContext.class.getName());
244 Constructor<?> ctor = ctxClass.getConstructor(java.lang.String.class);
245 ServiceContext<IT, OT> ctx = (ServiceContext<IT, OT>) ctor.newInstance(getServiceName());
250 public CollectionSpaceResource<IT, OT> getResource() throws Exception {
251 // TODO Auto-generated method stub
252 throw new RuntimeException("Unimplemented method.");
256 public CollectionSpaceResource<IT, OT> getResource(String serviceName)
258 // TODO Auto-generated method stub
259 throw new RuntimeException("Unimplemented method.");
263 // Transaction management methods
267 public TransactionContext getCurrentTransactionContext() {
268 return (TransactionContext) this.getProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY);
272 synchronized public void closeConnection() throws TransactionException {
273 if (transactionConnectionRefCount == 0) {
274 throw new TransactionException("Attempted to release a connection that doesn't exist or has already been released.");
277 if (isTransactionContextShared() == true) {
279 // If it's a shared connection, we can't close it. Just reduce the refcount by 1
281 String warnMsg = "Attempted to release a shared storage connection. Only the originator can release the connection";
282 logger.warn(warnMsg);
283 transactionConnectionRefCount--;
285 TransactionContext transactionCtx = getCurrentTransactionContext();
286 if (transactionCtx != null) {
287 if (--transactionConnectionRefCount == 0) {
288 transactionCtx.close();
289 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, null);
292 throw new TransactionException("Attempted to release a non-existent storage connection. Transaction context missing from service context.");
298 synchronized public TransactionContext openConnection() throws TransactionException {
299 TransactionContext result = getCurrentTransactionContext();
301 if (result == null) {
302 result = new JPATransactionContext(this);
303 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, result);
305 transactionConnectionRefCount++;
311 public void setTransactionContext(TransactionContext transactionCtx) throws TransactionException {
312 TransactionContext currentTransactionCtx = this.getCurrentTransactionContext();
313 if (currentTransactionCtx == null) {
314 setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, transactionCtx);
315 } else if (currentTransactionCtx != transactionCtx) {
316 throw new TransactionException("Transaction context already set from service context.");
321 * Returns true if the TransactionContext is shared with another ServiceContext instance
322 * @throws TransactionException
325 public boolean isTransactionContextShared() throws TransactionException {
326 boolean result = true;
328 TransactionContext transactionCtx = getCurrentTransactionContext();
329 if (transactionCtx != null) {
330 if (transactionCtx.getServiceContext() == this) { // check to see if the service context used to create the connection is the same as the current service context
334 throw new TransactionException("Transaction context missing from service context.");
341 public boolean hasActiveConnection() {
342 return getCurrentTransactionContext() != null;