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