]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
50c1a704c41e11f770a8d163658f55e74f1a30a7
[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.lang.reflect.Constructor;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.collectionspace.services.common.ResourceMap;
31 import org.collectionspace.services.common.security.UnauthorizedException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * RemoteServiceContextImpl
37  *
38  * $LastChangedRevision: $
39  * $LastChangedDate: $
40  */
41 public class RemoteServiceContextImpl<IT, OT>
42         extends AbstractServiceContextImpl<IT, OT>
43         implements RemoteServiceContext<IT, OT> {
44
45     /** The logger. */
46     final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
47     //input stores original content as received over the wire
48     /** The input. */
49     private IT input;    
50     /** The output. */
51     private OT output;
52     /** The target of the HTTP request **/
53     JaxRsContext jaxRsContext;
54     
55     ResourceMap resourceMap = null;
56     
57     @Override
58     public void setJaxRsContext(JaxRsContext theJaxRsContext) {
59         this.jaxRsContext = theJaxRsContext;
60     }
61     
62     @Override
63     public JaxRsContext getJaxRsContext() {
64         return this.jaxRsContext;
65     }
66
67     /**
68      * Instantiates a new remote service context impl.
69      * 
70      * @param serviceName the service name
71      * 
72      * @throws UnauthorizedException the unauthorized exception
73      */
74     protected RemoteServiceContextImpl(String serviceName) throws UnauthorizedException {
75         super(serviceName);
76     }
77
78     /**
79      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
80      * 
81      * @param serviceName the service name
82      * 
83      * @throws UnauthorizedException the unauthorized exception
84      */
85     protected RemoteServiceContextImpl(String serviceName, IT theInput) throws UnauthorizedException {
86         this(serviceName);
87         this.input = theInput;        
88     }
89
90     /**
91      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
92      * 
93      * @param serviceName the service name
94      * @param theInput the the input
95      * @param queryParams the query params
96      * 
97      * @throws UnauthorizedException the unauthorized exception
98      */
99     protected RemoteServiceContextImpl(String serviceName,
100                 IT theInput,
101                 ResourceMap resourceMap,
102                 MultivaluedMap<String, String> queryParams) throws UnauthorizedException {
103         this(serviceName, theInput);
104         this.setResourceMap(resourceMap);
105         this.setQueryParams(queryParams);
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
110      */
111     @Override
112     public IT getInput() {
113         return input;
114     }
115
116     /* (non-Javadoc)
117      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
118      */
119     @Override
120     public void setInput(IT input) {
121         //for security reasons, do not allow to set input again (from handlers)
122         if (this.input != null) {
123             String msg = "Non-null input cannot be set!";
124             logger.error(msg);
125             throw new IllegalStateException(msg);
126         }
127         this.input = input;
128     }
129
130     /* (non-Javadoc)
131      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
132      */
133     @Override
134     public OT getOutput() {
135         return output;
136     }
137
138     /* (non-Javadoc)
139      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
140      */
141     @Override
142     public void setOutput(OT output) {
143         this.output = output;
144     }
145
146     /**
147      * @return the map of service names to resource classes.
148      */
149     public ResourceMap getResourceMap() {
150         return resourceMap;
151     }
152
153     /**
154      * @param map the map of service names to resource instances.
155      */
156     public void setResourceMap(ResourceMap map) {
157         this.resourceMap = map;
158     }
159
160  
161     
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
164      */
165     @Override
166     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
167         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
168         Class<?> ctxClass = cloader.loadClass(localContextClassName);
169         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
170             throw new IllegalArgumentException("getLocalContext requires "
171                     + " implementation of " + ServiceContext.class.getName());
172         }
173
174         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
175         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
176         return ctx;
177     }
178 }