View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/server/src/java/org/astrogrid/community/server/policy/manager/ResourceManagerImpl.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2004/10/29 15:50:05 $</cvs:date>
5    * <cvs:version>$Revision: 1.9 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: ResourceManagerImpl.java,v $
9    *   Revision 1.9  2004/10/29 15:50:05  jdt
10   *   merges from Community_AdminInterface (bug 579)
11   *
12   *   Revision 1.8.18.1  2004/10/15 10:13:51  KevinBenson
13   *   adding the admin interface into a jsp fashion.  Correcting a few mistakes on the other
14   *   java files.
15   *
16   *   Revision 1.8  2004/09/16 23:18:08  dave
17   *   Replaced debug logging in Community.
18   *   Added stream close() to FileStore.
19   *
20   *   Revision 1.7.82.1  2004/09/16 09:58:48  dave
21   *   Replaced debug with commons logging ....
22   *
23   *   Revision 1.7  2004/06/18 13:45:20  dave
24   *   Merged development branch, dave-dev-200406081614, into HEAD
25   *
26   *   Revision 1.6.54.3  2004/06/17 15:24:31  dave
27   *   Removed unused imports (PMD report).
28   *
29   *   Revision 1.6.54.2  2004/06/17 13:38:59  dave
30   *   Tidied up old CVS log entries
31   *
32   * </cvs:log>
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         // Create a new ResourceIdentifier for our Resource.
113         ResourceIdentifier ident = new ResourceIdentifier() ;
114         log.debug("  ident : " + ident) ;
115         //
116         // Create our new Resource object.
117         resource = new ResourceData(ident) ;
118         //
119         //
120         // Try performing our transaction.
121         try {
122             //
123             // Open our database connection.
124             database = this.getDatabase() ;
125             //
126             // Begin a new database transaction.
127             database.begin();
128             //
129             // Try creating the resource in the database.
130             database.create(resource);
131             //
132             // Commit the transaction.
133             database.commit() ;
134             }
135         //
136         // If anything went bang.
137         catch (Exception ouch)
138             {
139             //
140             // Log the exception.
141             logException(ouch, "ResourceManagerImpl.addResource()") ;
142             //
143             // Cancel the database transaction.
144             rollbackTransaction(database) ;
145             //
146             // Throw a service exception.
147             throw new CommunityServiceException(
148                 "Unable to create new resource",
149                 ouch
150                 ) ;
151             }
152         //
153         // Always close our database connection.
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         // Check for null ident.
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             // Open our database connection.
190             database = this.getDatabase() ;
191             //
192             // Begin a new database transaction.
193             database.begin();
194             //
195             // Load the Resource from the database.
196             resource = (ResourceData) database.load(ResourceData.class, ident) ;
197             //
198             // Commit the transaction.
199             database.commit() ;
200             }
201         //
202         // If we couldn't find the object.
203         catch (ObjectNotFoundException ouch)
204             {
205             //
206             // Log the exception.
207             logException(ouch, "ResourceManagerImpl.getResource()") ;
208             //
209             // Cancel the database transaction.
210             rollbackTransaction(database) ;
211             //
212             // Throw a resource exception.
213             throw new CommunityResourceException(
214                 "Unable to locate resource",
215                 ident
216                 ) ;
217             }
218         //
219         // If anything else went bang.
220         catch (Exception ouch)
221             {
222             //
223             // Log the exception.
224             logException(ouch, "ResourceManagerImpl.getResource()") ;
225             //
226             // Cancel the database transaction.
227             rollbackTransaction(database) ;
228             //
229             // Throw a service exception.
230             throw new CommunityServiceException(
231                 "Unable to locate resource",
232                 ouch
233                 ) ;
234             }
235         //
236         // Always close our database connection.
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         // Check for null update.
261         if (null == update)
262             {
263             throw new CommunityResourceException(
264                 "Null resource"
265                 ) ;
266             }
267         log.debug("    ident : " + update.getIdent()) ;
268         //
269         // Check for null ident.
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         // Try update the database.
280         try {
281             //
282             // Open our database connection.
283             database = this.getDatabase() ;
284             //
285             // Begin a new database transaction.
286             database.begin();
287             //
288             // Load the Resource from the database.
289             resource = (ResourceData) database.load(ResourceData.class, update.getIdent()) ;
290             //
291             // Update the resource data.
292             resource.setDescription(update.getDescription()) ;
293             //
294             // Commit the transaction.
295             database.commit() ;
296             }
297         //
298         // If we couldn't find the object.
299         catch (ObjectNotFoundException ouch)
300             {
301             //
302             // Log the exception.
303             logException(ouch, "ResourceManagerImpl.setResource()") ;
304             //
305             // Cancel the database transaction.
306             rollbackTransaction(database) ;
307             //
308             // Throw a resource exception.
309             throw new CommunityResourceException(
310                 "Unable to locate resource",
311                 update.getIdent()
312                 ) ;
313             }
314         //
315         // If anything else went bang.
316         catch (Exception ouch)
317             {
318             //
319             // Log the exception.
320             logException(ouch, "ResourceManagerImpl.setResource()") ;
321             //
322             // Cancel the database transaction.
323             rollbackTransaction(database) ;
324             //
325             // Throw a service exception.
326             throw new CommunityServiceException(
327                 "Unable to update resource",
328                 ouch
329                 ) ;
330             }
331         //
332         // Always close our database connection.
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         // Try to query the database.
354         Object[] array    = null ;
355         Database database = null ;
356         try {
357             //
358             // Open our database connection.
359             database = this.getDatabase() ;
360             //
361             // Begin a new database transaction.
362             database.begin();
363             //
364             // Create our OQL query.
365             OQLQuery query = database.getOQLQuery(
366                 "SELECT resources FROM org.astrogrid.community.common.policy.data.ResourceData resources"
367                 );
368             //
369             // Execute our query.
370             QueryResults results = query.execute();
371             //
372             // Transfer our results to a vector.
373             Collection collection = new Vector() ;
374             while (results.hasMore())
375                 {
376                 collection.add(
377                     (ResourceData)results.next()
378                     ) ;
379                 }
380             //
381             // Convert it into an array.
382             array = collection.toArray() ;
383             //
384             // Commit the transaction.
385             database.commit() ;
386             }
387         //
388         // If anything went bang.
389         catch (Exception ouch)
390             {
391             //
392             // Log the exception.
393             logException(ouch, "ResourceManagerImpl.getResources()") ;
394             //
395             // Set the response to null.
396             array = null ;
397             //
398             // Cancel the database transaction.
399             rollbackTransaction(database) ;
400             }
401         //
402         // Close our database connection.
403         finally
404             {
405             closeConnection(database) ;
406             }
407         //
408         // Return the array.
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         // Check for null ident.
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             // Open our database connection.
443             database = this.getDatabase() ;
444             //
445             // Begin a new database transaction.
446             database.begin();
447             //
448             // Load the Resource from the database.
449             resource = (ResourceData) database.load(ResourceData.class, ident) ;
450             //
451             // Delete the Resource.
452             database.remove(resource) ;
453             //
454             // Commit the transaction.
455             database.commit() ;
456             }
457         //
458         // If we couldn't find the object.
459         catch (ObjectNotFoundException ouch)
460             {
461             //
462             // Log the exception.
463             logException(ouch, "ResourceManagerImpl.delResource()") ;
464             //
465             // Cancel the database transaction.
466             rollbackTransaction(database) ;
467             //
468             // Throw a resource exception.
469             throw new CommunityResourceException(
470                 "Unable to locate resource",
471                 ident
472                 ) ;
473             }
474         //
475         // If anything else went bang.
476         catch (Exception ouch)
477             {
478             //
479             // Log the exception.
480             logException(ouch, "ResourceManagerImpl.delResource()") ;
481             //
482             // Cancel the database transaction.
483             rollbackTransaction(database) ;
484             //
485             // Throw a service exception.
486             throw new CommunityServiceException(
487                 "Unable to update resource",
488                 ouch
489                 ) ;
490             }
491         //
492         // Always close our database connection.
493         finally
494             {
495             closeConnection(database) ;
496             }
497         return resource ;
498         }
499     }