KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > cli > CollectionImpl


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: CollectionImpl.java,v 1.1 2003/11/22 19:29:51 per_nyfelt Exp $
8

9 package org.ozoneDB.xml.cli;
10
11 import java.util.Map JavaDoc;
12 import java.util.Set JavaDoc;
13 import java.util.HashMap JavaDoc;
14
15 import org.ozoneDB.OzoneInterface;
16 // don't know if using ExternalDb is the best way but the interface does not have close()
17
import org.ozoneDB.ExternalDatabase;
18 import org.ozoneDB.xml.cli.resources.XMLResourceImpl;
19 import org.ozoneDB.xml.core.XMLCollection;
20
21 import org.ozoneDB.xml.util.XMLContainer;
22
23 import org.xmldb.api.base.ErrorCodes;
24 import org.xmldb.api.base.Collection;
25 import org.xmldb.api.base.Resource;
26 import org.xmldb.api.base.Service;
27 import org.xmldb.api.base.XMLDBException;
28
29 import org.xmldb.api.modules.XMLResource;
30 import org.xmldb.api.modules.BinaryResource;
31 import org.apache.log4j.Logger;
32
33 import java.util.Random JavaDoc;
34
35 /**
36  * A <code>Collection</code> represents a collection of <code>Resource</code>s
37  * stored within an XML database. An XML database may expose collections as a
38  * hierarchical set of parent and child collections.<p />
39  *
40  * A <code>Collection</code> provides access to the <code>Resource</code>s
41  * stored by the <code>Collection</code> and to <code>Service</code> instances
42  * that can operate against the <code>Collection</code> and the
43  * <code>Resource</code>s stored within it. The <code>Service</code> mechanism
44  * provides the ability to extend the functionality of a <code>Collection</code>
45  * in ways that allows optional functionality to be enabled for the <code>Collection</code>.
46  *
47  * @author <a HREF="http://www.smb-tec.com">SMB</a>, Per Nyfelt
48  * @version $Revision: 1.1 $
49  */

50 public class CollectionImpl extends AbstractConfigurable
51                             implements Collection {
52
53     private static final Logger logger = Logger.getLogger(CollectionImpl.class);
54
55     private Map JavaDoc services = null;
56     private ExternalDatabase database = null;
57     private XMLCollection collection = null;
58     //private Map resources = null;
59

60     /**
61      * gets the <code>Collection</code> using the corresponding name from the given
62      * database
63      */

64     protected static Collection forName( ExternalDatabase database, String JavaDoc collectionName )
65         throws Exception JavaDoc {
66         XMLCollection collection = ( XMLCollection )database.objectForName( collectionName );
67         return collection != null ?
68                 new CollectionImpl ( database, collection ) : null;
69     }
70
71     /**
72      * creates a new <code>Collection</code> and stores it in the database
73      */

74     protected static Collection newCollection( ExternalDatabase database,
75         String JavaDoc collectionName ) throws Exception JavaDoc {
76         XMLCollection collection = ( XMLCollection ) database.createObject(
77                 org.ozoneDB.xml.core.XMLCollectionImpl.class.getName(), OzoneInterface.Public, collectionName );
78         collection.setName(collectionName);
79         return new CollectionImpl( database, collection );
80     }
81
82     /**
83      * Constuctor
84      */

85     protected CollectionImpl( ExternalDatabase database, XMLCollection collection ) {
86
87         this.database = database;
88         this.collection = collection;
89
90         services = new HashMap JavaDoc();
91     }
92
93     /**
94      * Returns the name of this Collection. The name is unique.
95      *
96      * @return The name of this Collection.
97      */

98     public String JavaDoc getName() throws XMLDBException {
99         return collection.getName();
100     }
101
102     /**
103      * @return a String array of Services registered for this Collection.
104      */

105     public synchronized Service[] getServices() throws XMLDBException {
106         return (Service[]) services.values().toArray( new Service[services.values().size()] );
107     }
108
109     /**
110      * Returns the Service with the provided name and version.
111      *
112      * @param name the name of the requested Service
113      * @param version the version of the requested Service if any
114      */

115     public Service getService( String JavaDoc name, String JavaDoc version ) throws XMLDBException {
116         return (Service) services.get( name + version );
117     }
118
119     /**
120      * Registers the provided Service for this Collection.
121      *
122      * @param service The Service to be registered for this Collection.
123      */

124     public synchronized void registerService( Service service ) throws XMLDBException {
125         service.setCollection( this );
126         services.put( service.getName() + service.getVersion(), service );
127     }
128
129    /**
130     * Returns the parent collection for this collection or null if no parent
131     * collection exists.
132     *
133     * @return the parent Collection instance.
134     * @exception XMLDBException
135     */

136     public Collection getParentCollection() throws XMLDBException {
137         XMLCollection parentCollection = collection.getParentCollection();
138         return parentCollection == null ? null :
139                 new CollectionImpl( database, parentCollection );
140     }
141
142    /**
143     * Returns the number of child collections under this collection or 0 if no
144     * child collections exist.
145     *
146     * @return the number of child collections.
147     * @exception XMLDBException
148     */

149     public int getChildCollectionCount() throws XMLDBException {
150         return collection.getChildCollectionCount();
151     }
152
153    /**
154     * Returns a list of Collection names naming all child collections
155     * of the current collection. If no child collections exist an empty list is
156     * returned.
157     *
158     * @return an array containing Collection names for all child
159     * collections.
160     * @exception XMLDBException
161     */

162     public String JavaDoc[] listChildCollections() throws XMLDBException {
163         return collection.listChildCollections();
164     }
165
166    /**
167     * Returns a Collection instance for the requested child collection
168     * if it exists.
169     *
170     * @param name the name of the child collection to retrieve.
171     * @return the requested child collection or null if it couldn't be found.
172     * @exception XMLDBException
173     */

174     public Collection getChildCollection( String JavaDoc name ) throws XMLDBException {
175         XMLCollection cCollection = collection.getChildCollection( name );
176         return cCollection == null ? null :
177                 new CollectionImpl( database, cCollection );
178     }
179
180     /**
181      */

182     public int getResourceCount() throws XMLDBException {
183         return collection.getResourceCount();
184     }
185
186    /**
187     * Returns a list of the ids for all resources stored in the collection.
188     *
189     * @return a string array containing the names for all
190     * <code>Resource</code>s in the collection.
191     * @exception XMLDBException with expected error codes.<br />
192     * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
193     * specific errors that occur.<br />
194     */

195     public String JavaDoc[] listResources() throws XMLDBException {
196         Set JavaDoc resourceSet = collection.getResources();
197         String JavaDoc[] resourceArray = (String JavaDoc[])resourceSet.toArray();
198         return resourceArray;
199     }
200
201    /**
202     * Creates a new empty <code>Resource</code> with the provided id.
203     *
204     * @param id the unique id to associate with the created <code>Resource</code>.
205     * @param type the <code>Resource</code> type to create.
206     * @return an empty <code>Resource</code> instance.
207     */

208     public Resource createResource( String JavaDoc id, String JavaDoc type ) throws XMLDBException {
209         try {
210             if ( ( id == null ) || ( id.length() == 0 ) ) {
211                 id = createId();
212             }
213             if ( type.equals(XMLResource.RESOURCE_TYPE) ) {
214                 //System.out.println("CollectionImpl.createResource() - Creating container");
215
XMLContainer container = XMLContainer.forName(database, id);
216                 if (container != null ) {
217                     logger.error("container " + id + " exists already");
218                     throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "resource " + id + " already exists");
219                 }
220                 container = XMLContainer.newContainer(database, id);
221                 //System.out.println("CollectionImpl.createResource() - created XMLContainer, adding id to the XMLCollection");
222
collection.addResource(id);
223                 return new XMLResourceImpl(id, database, this, container);
224             } else if ( type.equals(BinaryResource.RESOURCE_TYPE) ) {
225                 //return new BinaryResourceImpl(this, id);
226
throw new XMLDBException( ErrorCodes.VENDOR_ERROR, "BinaryResource: Not yet implemented");
227             } else {
228                 throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE);
229             }
230         } catch (Exception JavaDoc e) {
231             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
232         }
233     }
234
235    /**
236     * Removes the <code>Resource</code> from the database.
237     *
238     * @param res the resource to remove.
239     * @exception XMLDBException with expected error codes.<br />
240     * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
241     * specific errors that occur.<br />
242     * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is
243     * not valid.<br />
244     * <code>ErrorCodes.NO_SUCH_RESOURCE</code> if the <code>Resource</code> is
245     * not known to this <code>Collection</code>.
246     */

247     public void removeResource( Resource res ) throws XMLDBException {
248         try {
249             logger.debug("CollectionImpl.removeResource() - Getting XMLContainer");
250             XMLContainer container = null;
251             String JavaDoc id = res.getId();
252             if ( collection.hasResource(id) ) {
253                 container = XMLContainer.forName(database, id);
254                 container.delete();
255             } else {
256                 throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, "the resource is not a part of this collection");
257             }
258         } catch (Exception JavaDoc e) {
259             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
260         }
261     }
262
263    /**
264     * Stores the provided resource into the database.
265     *
266     * @param res the resource to store in the database.
267     */

268     public void storeResource( Resource res ) throws XMLDBException {
269         try {
270             String JavaDoc id = res.getId();
271             if ((id == null) || (id.length() == 0)) {
272                 id = createId();
273             }
274             logger.warn("CollectionImpl.storeResource() - Dont know what to do here");
275
276         } catch (Exception JavaDoc e) {
277             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
278         }
279     }
280
281     /**
282      */

283     public Resource getResource( String JavaDoc id ) throws XMLDBException {
284         try {
285             //System.out.println("CollectionImpl.getResource() - Getting XMLContainer");
286
XMLContainer container = null;
287             if ( collection.hasResource(id) )
288                 container = XMLContainer.forName(database, id);
289             else
290                 return null;
291             if (container == null) {
292                 logger.debug("container is null");
293                 return null;
294             } else {
295                 logger.debug("creating new XMLResourceImpl");
296                 Resource resource = new XMLResourceImpl( id, database, this, container );
297                 //System.out.println("CollectionImpl.getResource() - created new XMLResourceImpl");
298
return resource;
299             }
300
301         } catch (Exception JavaDoc e) {
302             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString());
303         }
304     }
305
306    /**
307     * Creates a new unique ID within the context of the <code>Collection</code>
308     *
309     * @return the created id as a string.
310     */

311     public String JavaDoc createId () throws XMLDBException {
312         logger.warn("CollectionImpl.createId() - fix the quick hack");
313         return collection.getName() + System.currentTimeMillis() + new Random JavaDoc().nextLong();
314     }
315
316    /**
317     * Releases all resources consumed by the <code>Collection</code>.
318     * The <code>close</code> method must
319     * always be called when use of a <code>Collection</code> is complete.
320     *
321     * @exception XMLDBException with expected error codes.<br />
322     * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor
323     * specific errors that occur.<br />
324     */

325     public void close() throws XMLDBException {
326         // don't know if this is the best way but OzoneInterface does not have close()
327
if (database instanceof ExternalDatabase) {
328             try {
329                 database.close();
330                 logger.debug("CollectionImpl.close() - connection closed");
331             } catch (Exception JavaDoc e) {
332                 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage());
333             }
334         }
335     }
336
337 }
338
Popular Tags