1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.astrogrid.community.server.policy.manager ;
36
37 import org.apache.commons.logging.Log ;
38 import org.apache.commons.logging.LogFactory ;
39
40 import java.util.Vector;
41 import java.util.Collection ;
42
43 import org.exolab.castor.jdo.QueryResults;
44 import org.exolab.castor.jdo.OQLQuery;
45 import org.exolab.castor.jdo.Database;
46 import org.exolab.castor.jdo.ObjectNotFoundException ;
47
48 import org.astrogrid.community.common.identifier.ResourceIdentifier ;
49 import org.astrogrid.community.common.policy.data.ResourceData ;
50
51 import org.astrogrid.community.common.policy.manager.ResourceManager ;
52
53 import org.astrogrid.community.server.service.CommunityServiceImpl ;
54 import org.astrogrid.community.server.database.configuration.DatabaseConfiguration ;
55
56 import org.astrogrid.community.common.exception.CommunityServiceException ;
57 import org.astrogrid.community.common.exception.CommunityResourceException ;
58 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
59
60 public class ResourceManagerImpl
61 extends CommunityServiceImpl
62 implements ResourceManager
63 {
64 /***
65 * Our debug logger.
66 *
67 */
68 private static Log log = LogFactory.getLog(ResourceManagerImpl.class);
69
70 /***
71 * Public constructor, using default database configuration.
72 *
73 */
74 public ResourceManagerImpl()
75 {
76 super() ;
77 }
78
79 /***
80 * Public constructor, using specific database configuration.
81 *
82 */
83 public ResourceManagerImpl(DatabaseConfiguration config)
84 {
85 super(config) ;
86 }
87
88 /***
89 * Public constructor, using a parent service.
90 *
91 */
92 public ResourceManagerImpl(CommunityServiceImpl parent)
93 {
94 super(parent) ;
95 }
96
97 /***
98 * Register a new Resource.
99 * @return A new ResourceData object to represent the resource.
100 * @throws CommunityServiceException If there is an internal error in the service.
101 *
102 */
103 public ResourceData addResource()
104 throws CommunityServiceException
105 {
106 log.debug("") ;
107 log.debug("----\"----") ;
108 log.debug("ResourceManagerImpl.addResource()") ;
109 Database database = null ;
110 ResourceData resource = null ;
111
112
113 ResourceIdentifier ident = new ResourceIdentifier() ;
114 log.debug(" ident : " + ident) ;
115
116
117 resource = new ResourceData(ident) ;
118
119
120
121 try {
122
123
124 database = this.getDatabase() ;
125
126
127 database.begin();
128
129
130 database.create(resource);
131
132
133 database.commit() ;
134 }
135
136
137 catch (Exception ouch)
138 {
139
140
141 logException(ouch, "ResourceManagerImpl.addResource()") ;
142
143
144 rollbackTransaction(database) ;
145
146
147 throw new CommunityServiceException(
148 "Unable to create new resource",
149 ouch
150 ) ;
151 }
152
153
154 finally
155 {
156 closeConnection(database) ;
157 }
158 return resource ;
159 }
160
161 /***
162 * Request a Resource details.
163 * @param The resource identifier.
164 * @return The requested ResourceData object.
165 * @throws CommunityResourceException If unable to locate the resource.
166 * @throws CommunityServiceException If there is an internal error in the service.
167 * @throws CommunityIdentifierException If the identifier is not valid.
168 *
169 */
170 public ResourceData getResource(String ident)
171 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
172 {
173 log.debug("") ;
174 log.debug("----\"----") ;
175 log.debug("ResourceManagerImpl.getResource()") ;
176 log.debug(" ident : " + ident) ;
177
178
179 if (null == ident)
180 {
181 throw new CommunityIdentifierException(
182 "Null identifier"
183 ) ;
184 }
185 Database database = null ;
186 ResourceData resource = null ;
187 try {
188
189
190 database = this.getDatabase() ;
191
192
193 database.begin();
194
195
196 resource = (ResourceData) database.load(ResourceData.class, ident) ;
197
198
199 database.commit() ;
200 }
201
202
203 catch (ObjectNotFoundException ouch)
204 {
205
206
207 logException(ouch, "ResourceManagerImpl.getResource()") ;
208
209
210 rollbackTransaction(database) ;
211
212
213 throw new CommunityResourceException(
214 "Unable to locate resource",
215 ident
216 ) ;
217 }
218
219
220 catch (Exception ouch)
221 {
222
223
224 logException(ouch, "ResourceManagerImpl.getResource()") ;
225
226
227 rollbackTransaction(database) ;
228
229
230 throw new CommunityServiceException(
231 "Unable to locate resource",
232 ouch
233 ) ;
234 }
235
236
237 finally
238 {
239 closeConnection(database) ;
240 }
241 return resource ;
242 }
243
244 /***
245 * Update a Resource details.
246 * @param The ResourceData to update.
247 * @return The updated ResourceData.
248 * @throws CommunityResourceException If unable to locate the resource.
249 * @throws CommunityServiceException If there is an internal error in the service.
250 * @throws CommunityIdentifierException If the resource identifier is not valid.
251 *
252 */
253 public ResourceData setResource(ResourceData update)
254 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
255 {
256 log.debug("") ;
257 log.debug("----\"----") ;
258 log.debug("ResourceManagerImpl.setResource()") ;
259
260
261 if (null == update)
262 {
263 throw new CommunityResourceException(
264 "Null resource"
265 ) ;
266 }
267 log.debug(" ident : " + update.getIdent()) ;
268
269
270 if (null == update.getIdent())
271 {
272 throw new CommunityIdentifierException(
273 "Null identifier"
274 ) ;
275 }
276 Database database = null ;
277 ResourceData resource = null ;
278
279
280 try {
281
282
283 database = this.getDatabase() ;
284
285
286 database.begin();
287
288
289 resource = (ResourceData) database.load(ResourceData.class, update.getIdent()) ;
290
291
292 resource.setDescription(update.getDescription()) ;
293
294
295 database.commit() ;
296 }
297
298
299 catch (ObjectNotFoundException ouch)
300 {
301
302
303 logException(ouch, "ResourceManagerImpl.setResource()") ;
304
305
306 rollbackTransaction(database) ;
307
308
309 throw new CommunityResourceException(
310 "Unable to locate resource",
311 update.getIdent()
312 ) ;
313 }
314
315
316 catch (Exception ouch)
317 {
318
319
320 logException(ouch, "ResourceManagerImpl.setResource()") ;
321
322
323 rollbackTransaction(database) ;
324
325
326 throw new CommunityServiceException(
327 "Unable to update resource",
328 ouch
329 ) ;
330 }
331
332
333 finally
334 {
335 closeConnection(database) ;
336 }
337 return resource ;
338 }
339
340 /***
341 * Request a list of Resources.
342 * @return An array of ResourceData objects.
343 * @throws CommunityServiceException If there is an internal error in the service.
344 * @todo Return empty array rather than null.
345 *
346 */
347 public Object[] getResources()
348 {
349 log.debug("") ;
350 log.debug("----\"----") ;
351 log.debug("ResourceManagerImpl.getResources()") ;
352
353
354 Object[] array = null ;
355 Database database = null ;
356 try {
357
358
359 database = this.getDatabase() ;
360
361
362 database.begin();
363
364
365 OQLQuery query = database.getOQLQuery(
366 "SELECT resources FROM org.astrogrid.community.common.policy.data.ResourceData resources"
367 );
368
369
370 QueryResults results = query.execute();
371
372
373 Collection collection = new Vector() ;
374 while (results.hasMore())
375 {
376 collection.add(
377 (ResourceData)results.next()
378 ) ;
379 }
380
381
382 array = collection.toArray() ;
383
384
385 database.commit() ;
386 }
387
388
389 catch (Exception ouch)
390 {
391
392
393 logException(ouch, "ResourceManagerImpl.getResources()") ;
394
395
396 array = null ;
397
398
399 rollbackTransaction(database) ;
400 }
401
402
403 finally
404 {
405 closeConnection(database) ;
406 }
407
408
409 return array ;
410 }
411
412
413
414 /***
415 * Delete a Resource object.
416 * @param The resource identifier.
417 * @return The original ResourceData.
418 * @throws CommunityResourceException If unable to locate the resource.
419 * @throws CommunityServiceException If there is an internal error in the service.
420 * @throws CommunityIdentifierException If the resource identifier is not valid.
421 *
422 */
423 public ResourceData delResource(String ident)
424 throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
425 {
426 log.debug("") ;
427 log.debug("----\"----") ;
428 log.debug("ResourceManagerImpl.delResource()") ;
429 log.debug(" ident : " + ident) ;
430
431
432 if (null == ident)
433 {
434 throw new CommunityIdentifierException(
435 "Null identifier"
436 ) ;
437 }
438 Database database = null ;
439 ResourceData resource = null ;
440 try {
441
442
443 database = this.getDatabase() ;
444
445
446 database.begin();
447
448
449 resource = (ResourceData) database.load(ResourceData.class, ident) ;
450
451
452 database.remove(resource) ;
453
454
455 database.commit() ;
456 }
457
458
459 catch (ObjectNotFoundException ouch)
460 {
461
462
463 logException(ouch, "ResourceManagerImpl.delResource()") ;
464
465
466 rollbackTransaction(database) ;
467
468
469 throw new CommunityResourceException(
470 "Unable to locate resource",
471 ident
472 ) ;
473 }
474
475
476 catch (Exception ouch)
477 {
478
479
480 logException(ouch, "ResourceManagerImpl.delResource()") ;
481
482
483 rollbackTransaction(database) ;
484
485
486 throw new CommunityServiceException(
487 "Unable to update resource",
488 ouch
489 ) ;
490 }
491
492
493 finally
494 {
495 closeConnection(database) ;
496 }
497 return resource ;
498 }
499 }