package org.apache.xindice.core;
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xindice" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999-2001, The dbXML
* Group, L.L.C., http://www.dbxmlgroup.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CollectionManager.java,v 1.1 2001/12/06 21:00:11 bradford Exp $
*/
import org.apache.xindice.core.objects.*;
import org.apache.xindice.core.Collection;
import org.apache.xindice.util.*;
import java.util.*;
import org.w3c.dom.*;
/**
* CollectionManager is the base class for both Database and Collection.
*/
public class CollectionManager implements Configurable, Disposable {
private static final String COLLECTIONS = "collections";
private static final String COLLECTION = "collection";
private static final String NAME = "name";
private static final String[] EmptyStrings = new String[0];
protected Map collections = new HashMap(); // Collection
protected Configuration config = null;
protected CollectionManager() {
}
public void setConfig(Configuration config) throws XindiceException {
this.config = config;
Configuration colConfig = config.getChild(COLLECTIONS);
if ( colConfig != null ) {
colConfig.processChildren(COLLECTION,
new ConfigurationCallback() {
public void process(Configuration cfg) throws XindiceException {
Collection col = new Collection((Collection)CollectionManager.this);
col.setConfig(cfg);
collections.put(col.getName(), col);
}
});
}
}
public Configuration getConfig() {
return config;
}
/**
* getCollection retrieves a Collection by name.
*
* @param path The Collection path
* @return The Collection (or null)
*/
public Collection getCollection(String path) throws DBException {
if ( path.indexOf("/") != -1 ) {
CollectionManager cm = this;
StringTokenizer st = new StringTokenizer(path, "/");
while ( cm != null && st.hasMoreTokens()) {
path = st.nextToken();
cm = (CollectionManager)cm.collections.get(path);
}
return (Collection)cm;
}
else
return (Collection)collections.get(path);
}
/**
* listCollections retrieves a list of Collections as an array of
* Strings.
*
* @return The Collection list
*/
public final String[] listCollections() throws DBException {
return (String[])collections.keySet().toArray(EmptyStrings);
}
/**
* dropCollection physically removes the specified Collection and any
* associated system resources that the Collection uses.
*
* @param collection The Collection to drop
* @return Whether or not the Collection was dropped
*/
public boolean dropCollection(Collection collection) throws DBException {
if ( collection == null )
throw new DBException(FaultCodes.COL_COLLECTION_NOT_FOUND, "Collection Value Null");
Collection cm = collection.getParentCollection();
if ( cm == null )
throw new DBException(FaultCodes.DBE_CANNOT_DROP, "You Cannot Drop The Database");
if ( cm != this )
return cm.dropCollection(collection);
else {
String path = collection.getCanonicalName();
final String name = collection.getName();
boolean dropped = collection.drop();
if ( dropped ) {
collections.remove(name);
Configuration colConfig = config.getChild(COLLECTIONS);
colConfig.processChildren(COLLECTION,
new ConfigurationCallback() {
public void process(Configuration cfg) {
try {
if ( cfg.getAttribute(NAME).equals(name) )
cfg.delete();
}
catch ( Exception e ) {
org.apache.xindice.Debug.printStackTrace(e);
}
}
});
}
return dropped;
}
}
/**
* createCollection creates a new Collection object and any associated
* system resources that the Collection will need.
*
* @param path The relative path of the Collection
* @param cfg The Collection's configuration
* @return The newly created Collection
*/
public Collection createCollection(String path, Configuration cfg)
throws DBException {
if ( path.indexOf("/") != -1 ) {
CollectionManager cm = this;
StringTokenizer st = new StringTokenizer(path, "/");
while ( cm != null && st.hasMoreTokens()) {
path = st.nextToken().trim();
if ( path.length() == 0 )
continue;
if ( st.hasMoreTokens() )
cm = (CollectionManager)cm.collections.get(path);
else
return cm.createCollection(path, cfg);
}
throw new DBException(FaultCodes.COL_COLLECTION_NOT_FOUND, "Parent Collection '"+path+"' doesn't exist");
}
String url = "";
Collection collection = null;
if ( CollectionManager.this instanceof Database )
collection = new Collection((Database)CollectionManager.this);
else
collection = new Collection((Collection)CollectionManager.this);
try {
// Do a name check to see if all is well
String n = cfg.getAttribute(NAME);
if ( n == null || n.trim().equals("") )
throw new DBException(FaultCodes.COL_CANNOT_CREATE, "No name specified");
if ( getCollection(n) != null )
throw new DBException(FaultCodes.COL_DUPLICATE_COLLECTION, "Duplicate Collection '"+n+"'");
Configuration colConfig = this.config.getChild(COLLECTIONS, true);
colConfig.add(cfg);
collection.setConfig(cfg);
collection.create();
collections.put(n, collection);
}
catch ( DBException d ) {
throw d;
}
catch ( Exception e ) {
throw new DBException(FaultCodes.COL_CANNOT_CREATE, "Error Creating Collection '"+path+"'");
}
return collection;
}
public void dispose() {
}
}