View Javadoc

1   /*
2    * CollectionHandler.java
3    *
4    * Created on April 16, 2004, 1:16 PM
5    */
6   
7   package gov.noaa.gdsg.xmldbremote.service;
8   
9   import gov.noaa.gdsg.xmldbremote.service.transport.CollectionTransport;
10  import gov.noaa.gdsg.xmldbremote.service.transport.BaseTransport;
11  
12  import org.xmldb.api.base.Collection;
13  import org.xmldb.api.base.Resource;
14  import org.xmldb.api.base.Service;
15  
16  import org.xmldb.api.base.XMLDBException;
17  
18  import org.apache.log4j.Logger;
19  
20  
21  /***
22   * Handles the saving and involking of Collection objects.
23   *
24   * @version $Id: CollectionHandler.java,v 1.2 2004/11/04 00:06:00 mrxtravis Exp $
25   * @author  tns
26   */
27  public class CollectionHandler extends BaseHandler {
28      
29      private static Logger log = Logger.getLogger(CollectionHandler.class);
30      
31      
32      /*** Creates a new instance of CollectionHandler */
33      public CollectionHandler() {
34      }
35      
36      /***
37       * Needed by the super class to create the appropriate object
38       * @param object The object to base the transport object on.
39       * @throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
40       * @return A CollectionTransport object representing the given object.
41       */
42      protected BaseTransport createTransportObject(Object object) throws XMLDBException {
43          Collection collection = (Collection) object;
44          CollectionTransport transport = new CollectionTransport();
45          transport.setName(collection.getName());
46          return transport;
47      }    
48      
49      /***Does the typecasting */
50      private Collection getCollectionFromSession(CollectionTransport transport) throws XMLDBException {
51          Collection collection = (Collection) getObjectFromSession(transport);
52          return collection;
53      }
54      
55      
56      /***
57       * Closes the specified collection
58       * @param transport The object representing the collection to close
59       * @throws org.xmldb.api.base.XMLDBException If the collection object throws an exception...
60       */
61      public void close(CollectionTransport transport) throws XMLDBException {
62          Collection collection = getCollectionFromSession(transport);
63          collection.close();
64      }
65      
66      /***
67       * Creates an Id for the given collection{
68       * @param transport The object representing the collection to close
69       * @throws org.xmldb.api.base.XMLDBException If {@see Collection#createId} throws one.
70       * @return The created id from the actual Collection object.
71       */
72      public String createId(CollectionTransport transport) throws XMLDBException {
73          Collection collection = getCollectionFromSession(transport);
74          return collection.createId();
75      }
76      
77      /***
78       * Creates a new empty resource with the given id
79       * @param transport The object representing the collection to close
80       * @param id The id of the new resource
81       * @param type The type of resource to create
82       * @throws org.xmldb.api.base.XMLDBException If {@see Collection#createResource} throws one.
83       * @return Returns the newly created resource.
84       */
85      public Resource createResource(CollectionTransport transport,
86      String id, String type) throws XMLDBException {
87          
88          Collection collection = getCollectionFromSession(transport);
89          Resource resource = collection.createResource(id,type);
90          return resource;
91      }
92      
93      
94      
95      /***
96       * Returns the child collection
97       * @param transport The object representing the real object.
98       * @param name The name of the child collection to get.
99       * @throws org.xmldb.api.base.XMLDBException If {@see Collectiion#getChildCollection} throws one.
100      * @return The child collection.
101      */
102     public Collection getChildCollection(CollectionTransport transport, String name) throws XMLDBException {
103         Collection collection = this.getCollectionFromSession(transport);
104         Collection child = collection.getChildCollection(name);
105         return child;
106     }
107     
108     /***
109      * Returns whatever is returned by the wrapped Collection.
110      * @param transport The transport object which represents the actual object.
111      * @throws org.xmldb.api.base.XMLDBException If the wrapped colelction throws an exception.
112      * @return Whatever the wrapped {@see Collection#getchildCollectionCount} returns.
113      */
114     public int getChildCollectionCount(CollectionTransport transport) throws XMLDBException {
115         Collection collection = this.getCollectionFromSession(transport);
116         return collection.getChildCollectionCount();
117     }
118     
119     /***Taken care of in the transport object */
120     //public String getName(CollectionTransport transport) throws XMLDBException {}
121     
122     
123     /***
124      * Finds the parent collection for the specified collection
125      * @return transport The object representing the collection
126      * @param transport The transport object representin the actual object.
127      * @throws org.xmldb.api.base.XMLDBException If the wrapped {@see Collection#getParentCollection} object thows something.
128      */
129     public Collection getParentCollection(CollectionTransport transport) throws XMLDBException {
130         Collection collection = this.getCollectionFromSession(transport);
131         return collection;
132     }
133     
134     /***
135      * Finds the specified resource
136      * @return the resource
137      * @param transport The transport object representing the real object.
138      * @param id The id to pass to the wrapped {@see Collection#getResource} method.
139      * @throws org.xmldb.api.base.XMLDBException IF the wrapped method throws one.
140      */
141     public Resource getResource(CollectionTransport transport, String id) throws XMLDBException {
142         Collection collection = this.getCollectionFromSession(transport);
143         Resource resource = collection.getResource(id);
144         return resource;
145     }
146     
147     /***
148      * Returns the number of resources in the specified collection
149      * by calling the wrapped {@see Collection#getResourceCount} method.
150      * @param transport The object representing the actual object.
151      * @throws org.xmldb.api.base.XMLDBException If the wrapped method throws one.
152      * @return Whatevere the wrapped method throws.
153      */
154     public int getResourceCount(CollectionTransport transport) throws XMLDBException {
155         Collection collection = this.getCollectionFromSession(transport);
156         return collection.getResourceCount();
157     }
158     
159     /***
160      * Returns the specified service of the specified name by
161      * calling the {@see Collection#getService} method.
162      * @param transport The transport representing the actual object.
163      * @param version The version passed to the wrapped method.
164      * @param name The name of the service, passed to  the wrapped method.
165      * @throws org.xmldb.api.base.XMLDBException If the wrapped method throws one.
166      * @return Whatever the wrapped method returns.
167      */
168     public Service getService(CollectionTransport transport, String name, String version) throws XMLDBException {
169         Collection collection = this.getCollectionFromSession(transport);
170         Service service = collection.getService(name, version);
171         return service;
172     }
173     
174     /***
175      * A wrapper for the {@see Collection#getServices} method.
176      * @param transport The transport object representing the actual object.
177      * @throws org.xmldb.api.base.XMLDBException Whatever the wrapped method throws.
178      * @return Whatever is returned by the wrapped method.
179      */
180     public Service[] getServices(CollectionTransport transport) throws XMLDBException {
181         Service[] services = this.getCollectionFromSession(transport).getServices();
182         return services;
183     }
184     
185     /***
186      * Wrapper for the {@see Collection#isOpen} method.
187      * @param transport The transport object representing the actual object.
188      * @throws org.xmldb.api.base.XMLDBException Whatever is thrown by the wrapped method.
189      * @return Whatever is returned by the wrapped method.
190      */
191     public boolean isOpen(CollectionTransport transport) throws XMLDBException {
192         return this.getCollectionFromSession(transport).isOpen();
193     }
194     
195     /***
196      * Returns a list of collection names naming all child collections of the
197      * specified collection by wrapping {@see Collection#listChildCollections}.
198      * @param transport Represents the actual object.
199      * @throws org.xmldb.api.base.XMLDBException Whatever the wrapped method throws.
200      * @return Whatever the wrapped method returns.
201      */
202     public String[] listChildCollections(CollectionTransport transport) throws XMLDBException {
203         Collection collection = this.getCollectionFromSession(transport);
204         return collection.listChildCollections();
205         
206     }
207     
208     /***
209      * Returns a list of the ids for all resources store in the specified collection.
210      * This method wraps {@see Collection#listResources}.
211      * @param transport The transport object which represents the actual object.
212      * @throws org.xmldb.api.base.XMLDBException Whatever the wrapped method throws.
213      * @return whatever is returned by the wrapped method.
214      */
215     public String[] listResources(CollectionTransport transport) throws XMLDBException {
216                 Collection collection = this.getCollectionFromSession(transport);
217                 return collection.listResources();
218     }
219     
220     /***
221      * Stores the provided resource
222      * @param transport which represents the actual object.
223      * @param resource Passed to the wrapped method.
224      * @throws org.xmldb.api.base.XMLDBException Whatever the wrapped method throws.
225      */
226     public void storeResource(CollectionTransport transport, Resource resource) throws XMLDBException {
227         Collection collection = this.getCollectionFromSession(transport);
228         collection.storeResource(resource);
229     }
230     
231     /***
232      * Removes the resource
233      * @param transport which represents the actual object.
234      * @param resource passed to the wrapped method.
235      * @throws org.xmldb.api.base.XMLDBException whatever the wrapped method throws.
236      */
237     public void removeResource(CollectionTransport transport, Resource resource) throws XMLDBException {
238         Collection collection = this.getCollectionFromSession(transport);
239         collection.removeResource(resource);
240         
241     }
242     
243     /***
244      *Closes all known collections -- use for cleanup.
245      */
246     public void closeAllCollections(){
247         Object[] collectionObjects = this.getAllStoredObjects();
248         for (int i = 0; i < collectionObjects.length; i++){
249             Collection collection = (Collection) collectionObjects[i];
250             try {
251                 collection.close();
252             } catch (XMLDBException e){
253                 log.warn("Error closing collection on cleanup",e);
254             }
255         }
256     }
257     
258     
259     
260 }