]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9ad8bcce7b8ba000de59f8a021e1481499e3875b
[tmp/jakarta-migration.git] /
1 /**\r
2  *  This document is a part of the source code and related artifacts\r
3  *  for CollectionSpace, an open source collections management system\r
4  *  for museums and related institutions:\r
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \r
11  *  Licensed under the Educational Community License (ECL), Version 2.0.\r
12  *  You may not use this file except in compliance with this License.\r
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17 \r
18  *  Unless required by applicable law or agreed to in writing, software\r
19  *  distributed under the License is distributed on an "AS IS" BASIS,\r
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
21  *  See the License for the specific language governing permissions and\r
22  *  limitations under the License.\r
23  */\r
24 package org.collectionspace.services.common;\r
25 \r
26 import javax.ws.rs.GET;\r
27 import javax.ws.rs.Path;\r
28 import javax.ws.rs.Produces;\r
29 import javax.ws.rs.core.MultivaluedMap;\r
30 \r
31 import org.collectionspace.services.common.context.RemoteServiceContext;\r
32 import org.collectionspace.services.common.context.ServiceContext;\r
33 import org.collectionspace.services.common.context.ServiceContextFactory;\r
34 import org.collectionspace.services.common.document.DocumentHandler;\r
35 import org.collectionspace.services.common.repository.RepositoryClient;\r
36 import org.collectionspace.services.common.repository.RepositoryClientFactory;\r
37 import org.collectionspace.services.common.storage.StorageClient;\r
38 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;\r
39 \r
40 /**\r
41  * The Class AbstractCollectionSpaceResource.\r
42  */\r
43 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>\r
44         implements CollectionSpaceResource<IT, OT> {\r
45 \r
46     // Fields for default client factory and client\r
47     /** The repository client factory. */\r
48     private RepositoryClientFactory repositoryClientFactory;\r
49     \r
50     /** The repository client. */\r
51     private RepositoryClient repositoryClient;\r
52     \r
53     /** The storage client. */\r
54     private StorageClient storageClient;\r
55 \r
56     /**\r
57      * Instantiates a new abstract collection space resource.\r
58      */\r
59     public AbstractCollectionSpaceResourceImpl() {\r
60         repositoryClientFactory = RepositoryClientFactory.getInstance();\r
61     }\r
62 \r
63     /* (non-Javadoc)\r
64      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()\r
65      */\r
66     @Override\r
67     abstract public String getServiceName();\r
68 \r
69 \r
70     /* (non-Javadoc)\r
71      * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)\r
72      */\r
73     @Override\r
74     synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {\r
75         if(repositoryClient != null){\r
76             return repositoryClient;\r
77         }\r
78         repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());\r
79         return repositoryClient;\r
80     }\r
81 \r
82     /* (non-Javadoc)\r
83      * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)\r
84      */\r
85     @Override\r
86     synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {\r
87         if(storageClient != null) {\r
88             return storageClient;\r
89         }\r
90         storageClient = new JpaStorageClientImpl();\r
91         return storageClient;\r
92     }\r
93     \r
94     /* (non-Javadoc)\r
95      * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)\r
96      */\r
97     @Override\r
98     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {\r
99         DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());\r
100         return docHandler;\r
101     }\r
102     \r
103     /**\r
104      * Creates the document handler.\r
105      * \r
106      * @param ctx the ctx\r
107      * @param commonPart the common part\r
108      * \r
109      * @return the document handler\r
110      * \r
111      * @throws Exception the exception\r
112      */\r
113     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,\r
114                 Object commonPart) throws Exception {\r
115         DocumentHandler docHandler = ctx.getDocumentHandler();\r
116         docHandler.setCommonPart(commonPart);\r
117         return docHandler;\r
118     }    \r
119     \r
120     /**\r
121      * Creates the service context.\r
122      * \r
123      * @return the service context< i t, o t>\r
124      * \r
125      * @throws Exception the exception\r
126      */\r
127     protected ServiceContext<IT, OT> createServiceContext() throws Exception {          \r
128         ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),\r
129                         (IT)null, //inputType\r
130                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
131                         this.getCommonPartClass());\r
132         return ctx;\r
133     }    \r
134     \r
135     /**\r
136      * Creates the service context.\r
137      * \r
138      * @param serviceName the service name\r
139      * \r
140      * @return the service context< i t, o t>\r
141      * \r
142      * @throws Exception the exception\r
143      */\r
144     protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {        \r
145         ServiceContext<IT, OT> ctx = createServiceContext(\r
146                         serviceName,\r
147                         (IT)null, /*input*/\r
148                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
149                         (Class<?>)null  /*input type's Class*/);\r
150         return ctx;\r
151     }\r
152     \r
153     /**\r
154      * Creates the service context.\r
155      * \r
156      * @param serviceName the service name\r
157      * @param input the input\r
158      * \r
159      * @return the service context< i t, o t>\r
160      * \r
161      * @throws Exception the exception\r
162      */\r
163     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
164                 IT input) throws Exception {            \r
165         ServiceContext<IT, OT> ctx = createServiceContext(serviceName, input,\r
166                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
167                         (Class<?>)null  /*input type's Class*/);\r
168         return ctx;\r
169     }\r
170     \r
171     /**\r
172      * Creates the service context.\r
173      * \r
174      * @param serviceName the service name\r
175      * @param input the input\r
176      * \r
177      * @return the service context< i t, o t>\r
178      * \r
179      * @throws Exception the exception\r
180      */\r
181     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
182                 MultivaluedMap<String, String> queryParams) throws Exception {          \r
183         ServiceContext<IT, OT> ctx = createServiceContext(serviceName,\r
184                         (IT)null,\r
185                         queryParams,\r
186                         (Class<?>)null  /*input type's Class*/);\r
187         return ctx;\r
188     }    \r
189 \r
190     /**\r
191      * Creates the service context.\r
192      * \r
193      * @param queryParams the query params\r
194      * \r
195      * @return the service context< i t, o t>\r
196      * \r
197      * @throws Exception the exception\r
198      */\r
199     protected ServiceContext<IT, OT> createServiceContext(MultivaluedMap<String, String> queryParams) throws Exception {        \r
200         ServiceContext<IT, OT> ctx = createServiceContext(\r
201                         (IT)null, /*input*/\r
202                         queryParams,\r
203                         (Class<?>)null  /*input type's Class*/);\r
204         return ctx;\r
205     }    \r
206         \r
207     /**\r
208      * Creates the service context.\r
209      * \r
210      * @param input the input\r
211      * \r
212      * @return the service context< i t, o t>\r
213      * \r
214      * @throws Exception the exception\r
215      */\r
216     protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {          \r
217         ServiceContext<IT, OT> ctx = createServiceContext(\r
218                         input,\r
219                         (Class<?>)null /*input type's Class*/);\r
220         return ctx;\r
221     }\r
222     \r
223     /**\r
224      * Creates the service context.\r
225      * \r
226      * @param input the input\r
227      * @param theClass the the class\r
228      * \r
229      * @return the service context\r
230      * \r
231      * @throws Exception the exception\r
232      */\r
233     protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {       \r
234         ServiceContext<IT, OT> ctx = createServiceContext(\r
235                         input,\r
236                         (MultivaluedMap<String, String>)null, //queryParams,\r
237                         theClass);\r
238         return ctx;\r
239     }\r
240     \r
241     /**\r
242      * Creates the service context.\r
243      * \r
244      * @param input the input\r
245      * @param queryParams the query params\r
246      * @param theClass the the class\r
247      * \r
248      * @return the service context< i t, o t>\r
249      * \r
250      * @throws Exception the exception\r
251      */\r
252     protected ServiceContext<IT, OT> createServiceContext(\r
253                 IT input,\r
254                 MultivaluedMap<String, String> queryParams,\r
255                 Class<?> theClass) throws Exception {\r
256         return createServiceContext(this.getServiceName(),\r
257                         input,\r
258                         queryParams,\r
259                         theClass);\r
260     }\r
261 \r
262     /**\r
263      * Creates the service context.\r
264      * \r
265      * @param serviceName the service name\r
266      * @param input the input\r
267      * @param queryParams the query params\r
268      * @param theClass the the class\r
269      * \r
270      * @return the service context< i t, o t>\r
271      * \r
272      * @throws Exception the exception\r
273      */\r
274     private ServiceContext<IT, OT> createServiceContext(\r
275                 String serviceName,\r
276                 IT input,\r
277                 MultivaluedMap<String, String> queryParams,\r
278                 Class<?> theClass) throws Exception {\r
279         ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(\r
280                         serviceName,\r
281                         input,\r
282                         queryParams,\r
283                         theClass != null ? theClass.getPackage().getName() : null,\r
284                         theClass != null ? theClass.getName() : null);\r
285         return ctx;\r
286     }\r
287         \r
288     /**\r
289      * Gets the version string.\r
290      * \r
291      * @return the version string\r
292      */\r
293     abstract protected String getVersionString();\r
294     \r
295     /**\r
296      * Gets the version.\r
297      * \r
298      * @return the version\r
299      */\r
300     @GET\r
301     @Path("/version")    \r
302     @Produces("application/xml")\r
303     public Version getVersion() {\r
304         Version result = new Version();\r
305         \r
306         result.setVersionString(getVersionString());\r
307         \r
308         return result;\r
309     }\r
310 }\r