1 9 package org.ozoneDB.xml.cli; 10 11 import java.util.Map ; 12 import java.util.Set ; 13 import java.util.HashMap ; 14 15 import org.ozoneDB.OzoneInterface; 16 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 ; 34 35 50 public class CollectionImpl extends AbstractConfigurable 51 implements Collection { 52 53 private static final Logger logger = Logger.getLogger(CollectionImpl.class); 54 55 private Map services = null; 56 private ExternalDatabase database = null; 57 private XMLCollection collection = null; 58 60 64 protected static Collection forName( ExternalDatabase database, String collectionName ) 65 throws Exception { 66 XMLCollection collection = ( XMLCollection )database.objectForName( collectionName ); 67 return collection != null ? 68 new CollectionImpl ( database, collection ) : null; 69 } 70 71 74 protected static Collection newCollection( ExternalDatabase database, 75 String collectionName ) throws Exception { 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 85 protected CollectionImpl( ExternalDatabase database, XMLCollection collection ) { 86 87 this.database = database; 88 this.collection = collection; 89 90 services = new HashMap (); 91 } 92 93 98 public String getName() throws XMLDBException { 99 return collection.getName(); 100 } 101 102 105 public synchronized Service[] getServices() throws XMLDBException { 106 return (Service[]) services.values().toArray( new Service[services.values().size()] ); 107 } 108 109 115 public Service getService( String name, String version ) throws XMLDBException { 116 return (Service) services.get( name + version ); 117 } 118 119 124 public synchronized void registerService( Service service ) throws XMLDBException { 125 service.setCollection( this ); 126 services.put( service.getName() + service.getVersion(), service ); 127 } 128 129 136 public Collection getParentCollection() throws XMLDBException { 137 XMLCollection parentCollection = collection.getParentCollection(); 138 return parentCollection == null ? null : 139 new CollectionImpl( database, parentCollection ); 140 } 141 142 149 public int getChildCollectionCount() throws XMLDBException { 150 return collection.getChildCollectionCount(); 151 } 152 153 162 public String [] listChildCollections() throws XMLDBException { 163 return collection.listChildCollections(); 164 } 165 166 174 public Collection getChildCollection( String name ) throws XMLDBException { 175 XMLCollection cCollection = collection.getChildCollection( name ); 176 return cCollection == null ? null : 177 new CollectionImpl( database, cCollection ); 178 } 179 180 182 public int getResourceCount() throws XMLDBException { 183 return collection.getResourceCount(); 184 } 185 186 195 public String [] listResources() throws XMLDBException { 196 Set resourceSet = collection.getResources(); 197 String [] resourceArray = (String [])resourceSet.toArray(); 198 return resourceArray; 199 } 200 201 208 public Resource createResource( String id, String type ) throws XMLDBException { 209 try { 210 if ( ( id == null ) || ( id.length() == 0 ) ) { 211 id = createId(); 212 } 213 if ( type.equals(XMLResource.RESOURCE_TYPE) ) { 214 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 collection.addResource(id); 223 return new XMLResourceImpl(id, database, this, container); 224 } else if ( type.equals(BinaryResource.RESOURCE_TYPE) ) { 225 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, "BinaryResource: Not yet implemented"); 227 } else { 228 throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE); 229 } 230 } catch (Exception e) { 231 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage()); 232 } 233 } 234 235 247 public void removeResource( Resource res ) throws XMLDBException { 248 try { 249 logger.debug("CollectionImpl.removeResource() - Getting XMLContainer"); 250 XMLContainer container = null; 251 String 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 e) { 259 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString()); 260 } 261 } 262 263 268 public void storeResource( Resource res ) throws XMLDBException { 269 try { 270 String 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 e) { 277 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString()); 278 } 279 } 280 281 283 public Resource getResource( String id ) throws XMLDBException { 284 try { 285 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 return resource; 299 } 300 301 } catch (Exception e) { 302 throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.toString()); 303 } 304 } 305 306 311 public String createId () throws XMLDBException { 312 logger.warn("CollectionImpl.createId() - fix the quick hack"); 313 return collection.getName() + System.currentTimeMillis() + new Random ().nextLong(); 314 } 315 316 325 public void close() throws XMLDBException { 326 if (database instanceof ExternalDatabase) { 328 try { 329 database.close(); 330 logger.debug("CollectionImpl.close() - connection closed"); 331 } catch (Exception e) { 332 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.getMessage()); 333 } 334 } 335 } 336 337 } 338 | Popular Tags |