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);
59 //input stores original content as received over the wire
64 /** The target of the HTTP request **/
67 // Reference count for things like JPA connections
69 private int transactionConnectionRefCount = 0;
74 JaxRsContext jaxRsContext;
75 ResourceMap resourceMap = null;
78 public void setJaxRsContext(JaxRsContext theJaxRsContext) {
79 this.jaxRsContext = theJaxRsContext;
83 public JaxRsContext getJaxRsContext() {
84 return this.jaxRsContext;
88 * Instantiates a new remote service context impl.
90 * @param serviceName the service name
92 * @throws UnauthorizedException the unauthorized exception
94 protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
95 super(serviceName, uriInfo);
99 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
101 * @param serviceName the service name
103 * @throws UnauthorizedException the unauthorized exception
105 protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
106 this(serviceName, uriInfo);
107 this.input = theInput;
111 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
113 * @param serviceName the service name
114 * @param theInput the the input
115 * @param queryParams the query params
117 * @throws UnauthorizedException the unauthorized exception
119 protected RemoteServiceContextImpl(String serviceName,
121 ResourceMap resourceMap,
122 UriInfo uriInfo) throws UnauthorizedException {
123 this(serviceName, theInput, uriInfo);
124 this.setResourceMap(resourceMap);
125 this.setUriInfo(uriInfo);
126 if (uriInfo != null) {
127 this.setQueryParams(uriInfo.getQueryParameters());
132 * Returns the name of the service's acting repository. Gets this from the tenant and service bindings files
135 public String getRepositoryName() throws Exception {
136 String result = null;
138 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
139 String tenantId = this.getTenantId();
140 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
141 ServiceBindingType serviceBindingType = this.getServiceBinding();
142 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
143 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
144 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
146 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
147 serviceBindingType.getName(), tenantId);
148 throw new Exception(errMsg);
155 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
158 public IT getInput() {
163 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
166 public void setInput(IT input) {
167 if (logger.isDebugEnabled()) {
168 if (this.input != null) {
169 String msg = "\n#\n# Resetting or changing an context's input is not advised.\n#";
177 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
180 public OT getOutput() {
185 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
188 public void setOutput(OT output) {
189 this.output = output;
193 * Return the JAX-RS resource for the current context.
199 @SuppressWarnings("unchecked")
200 public CollectionSpaceResource<IT, OT> getResource(ServiceContext<?, ?> ctx) throws Exception {
201 CollectionSpaceResource<IT, OT> result = null;
203 ResourceMap resourceMap = ctx.getResourceMap();
204 String resourceName = ctx.getClient().getServiceName();
205 result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
211 * @return the map of service names to resource classes.
214 public ResourceMap getResourceMap() {
215 ResourceMap result = resourceMap;
217 if (result == null) {
218 result = ServiceMain.getInstance().getJaxRSResourceMap();
225 * @param map the map of service names to resource instances.
228 public void setResourceMap(ResourceMap map) {
229 this.resourceMap = map;
235 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
237 @SuppressWarnings("unchecked")
239 public ServiceContext<IT, OT> getLocalContext(String localContextClassName) throws Exception {
240 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
241 Class<?> ctxClass = cloader.loadClass(localContextClassName);
242 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
243 throw new IllegalArgumentException("getLocalContext requires "
244 + " implementation of " + ServiceContext.class.getName());
247 Constructor<?> ctor = ctxClass.getConstructor(java.lang.String.class);
248 ServiceContext<IT, OT> ctx = (ServiceContext<IT, OT>) ctor.newInstance(getServiceName());
253 public CollectionSpaceResource<IT, OT> getResource() throws Exception {
254 // TODO Auto-generated method stub
255 throw new RuntimeException("Unimplemented method.");
259 public CollectionSpaceResource<IT, OT> getResource(String serviceName)
261 // TODO Auto-generated method stub
262 throw new RuntimeException("Unimplemented method.");
266 // Transaction management methods
270 public TransactionContext getCurrentTransactionContext() {
271 return (TransactionContext) this.getProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY);
275 synchronized public void closeConnection() throws TransactionException {
276 if (transactionConnectionRefCount == 0) {
277 throw new TransactionException("Attempted to release a connection that doesn't exist or has already been released.");
280 if (isTransactionContextShared() == true) {
282 // If it's a shared connection, we can't close it. Just reduce the refcount by 1
284 if (logger.isTraceEnabled()) {
285 String traceMsg = "Attempted to release a shared storage connection. Only the originator can release the connection";
286 logger.trace(traceMsg);
288 transactionConnectionRefCount--;
290 TransactionContext transactionCtx = getCurrentTransactionContext();
291 if (transactionCtx != null) {
292 if (--transactionConnectionRefCount == 0) {
293 transactionCtx.close();
294 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, null);
297 throw new TransactionException("Attempted to release a non-existent storage connection. Transaction context missing from service context.");
303 synchronized public TransactionContext openConnection() throws TransactionException {
304 TransactionContext result = getCurrentTransactionContext();
306 if (result == null) {
307 result = new JPATransactionContext(this);
308 this.setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, result);
310 transactionConnectionRefCount++;
316 public void setTransactionContext(TransactionContext transactionCtx) throws TransactionException {
317 TransactionContext currentTransactionCtx = this.getCurrentTransactionContext();
318 if (currentTransactionCtx == null) {
319 setProperty(StorageClient.SC_TRANSACTION_CONTEXT_KEY, transactionCtx);
320 } else if (currentTransactionCtx != transactionCtx) {
321 throw new TransactionException("Transaction context already set from service context.");
326 * Returns true if the TransactionContext is shared with another ServiceContext instance
327 * @throws TransactionException
330 public boolean isTransactionContextShared() throws TransactionException {
331 boolean result = true;
333 TransactionContext transactionCtx = getCurrentTransactionContext();
334 if (transactionCtx != null) {
335 if (transactionCtx.getServiceContext() == this) { // check to see if the service context used to create the connection is the same as the current service context
339 throw new TransactionException("Transaction context missing from service context.");
346 public boolean hasActiveConnection() {
347 return getCurrentTransactionContext() != null;