]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3565db4863eb9aa7cb70a2a1d2a254f3f29ed1b0
[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 AbstractCollectionSpaceResourceImpl.\r
42  *\r
43  * @param <IT> the generic type\r
44  * @param <OT> the generic type\r
45  */\r
46 public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>\r
47         implements CollectionSpaceResource<IT, OT> {\r
48 \r
49     // Fields for default client factory and client\r
50     /** The repository client factory. */\r
51     private RepositoryClientFactory repositoryClientFactory;\r
52     \r
53     /** The repository client. */\r
54     private RepositoryClient repositoryClient;\r
55     \r
56     /** The storage client. */\r
57     private StorageClient storageClient;\r
58 \r
59     /**\r
60      * Instantiates a new abstract collection space resource.\r
61      */\r
62     public AbstractCollectionSpaceResourceImpl() {\r
63         repositoryClientFactory = RepositoryClientFactory.getInstance();\r
64     }\r
65 \r
66     /* (non-Javadoc)\r
67      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceName()\r
68      */\r
69     @Override\r
70     abstract public String getServiceName();\r
71 \r
72 \r
73     /* (non-Javadoc)\r
74      * @see org.collectionspace.services.common.CollectionSpaceResource#getRepositoryClient(org.collectionspace.services.common.context.ServiceContext)\r
75      */\r
76     @Override\r
77     synchronized public RepositoryClient getRepositoryClient(ServiceContext<IT, OT> ctx) {\r
78         if(repositoryClient != null){\r
79             return repositoryClient;\r
80         }\r
81         repositoryClient = repositoryClientFactory.getClient(ctx.getRepositoryClientName());\r
82         return repositoryClient;\r
83     }\r
84 \r
85     /* (non-Javadoc)\r
86      * @see org.collectionspace.services.common.CollectionSpaceResource#getStorageClient(org.collectionspace.services.common.context.ServiceContext)\r
87      */\r
88     @Override\r
89     synchronized public StorageClient getStorageClient(ServiceContext<IT, OT> ctx) {\r
90         if(storageClient != null) {\r
91             return storageClient;\r
92         }\r
93         storageClient = new JpaStorageClientImpl();\r
94         return storageClient;\r
95     }\r
96     \r
97     /* (non-Javadoc)\r
98      * @see org.collectionspace.services.common.CollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)\r
99      */\r
100     @Override\r
101     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx) throws Exception {\r
102         DocumentHandler docHandler = createDocumentHandler(ctx, ctx.getInput());\r
103         return docHandler;\r
104     }\r
105     \r
106     /**\r
107      * Creates the document handler.\r
108      * \r
109      * @param ctx the ctx\r
110      * @param commonPart the common part\r
111      * \r
112      * @return the document handler\r
113      * \r
114      * @throws Exception the exception\r
115      */\r
116     public DocumentHandler createDocumentHandler(ServiceContext<IT, OT> ctx,\r
117                 Object commonPart) throws Exception {\r
118         DocumentHandler docHandler = ctx.getDocumentHandler();\r
119         docHandler.setCommonPart(commonPart);\r
120         return docHandler;\r
121     }    \r
122     \r
123     /**\r
124      * Creates the service context.\r
125      * \r
126      * @return the service context< i t, o t>\r
127      * \r
128      * @throws Exception the exception\r
129      */\r
130     protected ServiceContext<IT, OT> createServiceContext() throws Exception {          \r
131         ServiceContext<IT, OT> ctx = createServiceContext(this.getServiceName(),\r
132                         (IT)null, //inputType\r
133                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
134                         this.getCommonPartClass());\r
135         return ctx;\r
136     }    \r
137     \r
138     /**\r
139      * Creates the service context.\r
140      * \r
141      * @param serviceName the service name\r
142      * \r
143      * @return the service context< i t, o t>\r
144      * \r
145      * @throws Exception the exception\r
146      */\r
147     protected ServiceContext<IT, OT> createServiceContext(String serviceName) throws Exception {        \r
148         ServiceContext<IT, OT> ctx = createServiceContext(\r
149                         serviceName,\r
150                         (IT)null, /*input*/\r
151                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
152                         (Class<?>)null  /*input type's Class*/);\r
153         return ctx;\r
154     }\r
155     \r
156     /**\r
157      * Creates the service context.\r
158      * \r
159      * @param serviceName the service name\r
160      * @param input the input\r
161      * \r
162      * @return the service context< i t, o t>\r
163      * \r
164      * @throws Exception the exception\r
165      */\r
166     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
167                 IT input) throws Exception {            \r
168         ServiceContext<IT, OT> ctx = createServiceContext(serviceName, input,\r
169                         (MultivaluedMap<String, String>)null, /*queryParams*/\r
170                         (Class<?>)null  /*input type's Class*/);\r
171         return ctx;\r
172     }\r
173     \r
174     /**\r
175      * Creates the service context.\r
176      * \r
177      * @param serviceName the service name\r
178      * @param input the input\r
179      * \r
180      * @return the service context< i t, o t>\r
181      * \r
182      * @throws Exception the exception\r
183      */\r
184     protected ServiceContext<IT, OT> createServiceContext(String serviceName,\r
185                 MultivaluedMap<String, String> queryParams) throws Exception {          \r
186         ServiceContext<IT, OT> ctx = createServiceContext(serviceName,\r
187                         (IT)null,\r
188                         queryParams,\r
189                         (Class<?>)null  /*input type's Class*/);\r
190         return ctx;\r
191     }    \r
192 \r
193     /**\r
194      * Creates the service context.\r
195      * \r
196      * @param queryParams the query params\r
197      * \r
198      * @return the service context< i t, o t>\r
199      * \r
200      * @throws Exception the exception\r
201      */\r
202     protected ServiceContext<IT, OT> createServiceContext(MultivaluedMap<String, String> queryParams) throws Exception {        \r
203         ServiceContext<IT, OT> ctx = createServiceContext(\r
204                         (IT)null, /*input*/\r
205                         queryParams,\r
206                         (Class<?>)null  /*input type's Class*/);\r
207         return ctx;\r
208     }    \r
209         \r
210     /**\r
211      * Creates the service context.\r
212      * \r
213      * @param input the input\r
214      * \r
215      * @return the service context< i t, o t>\r
216      * \r
217      * @throws Exception the exception\r
218      */\r
219     protected ServiceContext<IT, OT> createServiceContext(IT input) throws Exception {          \r
220         ServiceContext<IT, OT> ctx = createServiceContext(\r
221                         input,\r
222                         (Class<?>)null /*input type's Class*/);\r
223         return ctx;\r
224     }\r
225     \r
226     /**\r
227      * Creates the service context.\r
228      * \r
229      * @param input the input\r
230      * @param theClass the the class\r
231      * \r
232      * @return the service context\r
233      * \r
234      * @throws Exception the exception\r
235      */\r
236     protected ServiceContext<IT, OT> createServiceContext(IT input, Class<?> theClass) throws Exception {       \r
237         ServiceContext<IT, OT> ctx = createServiceContext(\r
238                         input,\r
239                         (MultivaluedMap<String, String>)null, //queryParams,\r
240                         theClass);\r
241         return ctx;\r
242     }\r
243     \r
244     /**\r
245      * Creates the service context.\r
246      * \r
247      * @param input the input\r
248      * @param queryParams the query params\r
249      * @param theClass the the class\r
250      * \r
251      * @return the service context< i t, o t>\r
252      * \r
253      * @throws Exception the exception\r
254      */\r
255     protected ServiceContext<IT, OT> createServiceContext(\r
256                 IT input,\r
257                 MultivaluedMap<String, String> queryParams,\r
258                 Class<?> theClass) throws Exception {\r
259         return createServiceContext(this.getServiceName(),\r
260                         input,\r
261                         queryParams,\r
262                         theClass);\r
263     }\r
264 \r
265     /**\r
266      * Creates the service context.\r
267      * \r
268      * @param serviceName the service name\r
269      * @param input the input\r
270      * @param queryParams the query params\r
271      * @param theClass the the class\r
272      * \r
273      * @return the service context< i t, o t>\r
274      * \r
275      * @throws Exception the exception\r
276      */\r
277     private ServiceContext<IT, OT> createServiceContext(\r
278                 String serviceName,\r
279                 IT input,\r
280                 MultivaluedMap<String, String> queryParams,\r
281                 Class<?> theClass) throws Exception {\r
282         ServiceContext<IT, OT> ctx = getServiceContextFactory().createServiceContext(\r
283                         serviceName,\r
284                         input,\r
285                         queryParams,\r
286                         theClass != null ? theClass.getPackage().getName() : null,\r
287                         theClass != null ? theClass.getName() : null);\r
288         return ctx;\r
289     }\r
290         \r
291     /**\r
292      * Gets the version string.\r
293      * \r
294      * @return the version string\r
295      */\r
296     abstract protected String getVersionString();\r
297     \r
298     /**\r
299      * Gets the version.\r
300      * \r
301      * @return the version\r
302      */\r
303     @GET\r
304     @Path("/version")    \r
305     @Produces("application/xml")\r
306     public Version getVersion() {\r
307         Version result = new Version();\r
308         \r
309         result.setVersionString(getVersionString());\r
310         \r
311         return result;\r
312     }\r
313 }\r