package com.dbxml.db.client.local;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: dbXMLClientImpl.java,v 1.3 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ContentClient;
import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.CollectionManager;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.Database;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.db.server.dbXML;
import com.dbxml.util.dbXMLException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
/**
* dbXMLClientImpl
*/
public class dbXMLClientImpl implements dbXMLClient {
private static final String[] EmptyStrings = new String[0];
private Map props = new TreeMap();
private boolean connected;
public void setProperty(String name, String value) {
props.put(name, value);
}
public String getProperty(String name) {
return (String)props.get(name);
}
public String[] listProperties() {
return (String[])props.keySet().toArray(EmptyStrings);
}
public Map getProperties() {
return props;
}
public void connect() throws dbXMLException {
if ( connected )
throw new dbXMLException("Already connected");
connected = true;
}
public void disconnect() throws dbXMLException {
if ( !connected )
throw new dbXMLException("Not connected");
connected = false;
}
public String getServerVersion() throws dbXMLException {
return dbXML.VersionString;
}
public void shutdown(int exitCode) throws dbXMLException {
/** @todo Should this method do anything? */
}
public CollectionClient getDatabase() throws dbXMLException {
Database db = Database.getInstance();
if ( db != null )
return new CollectionClientImpl(this, db);
else
throw new dbXMLException("Database not found");
}
public CollectionClient getCollection(String path) throws dbXMLException {
Collection col = getAbsoluteCollection(path);
if ( col != null )
return new CollectionClientImpl(this, col);
else
throw new dbXMLException("Collection '" + path + "' not found");
}
public ContentClient getContent(String path) throws dbXMLException {
Transaction tx = new Transaction();
try {
int idx = path.lastIndexOf('/');
if ( idx != -1 ) {
String docName = path.substring(idx + 1);
path = path.substring(0, idx);
Collection col = getAbsoluteCollection(path);
if ( col != null ) {
Container con = col.getContainer(tx, docName);
if ( con != null )
return new ContentClientImpl(new CollectionClientImpl(this, col), con);
else
throw new dbXMLException("Document '" + docName + "' not found");
}
else
throw new dbXMLException("Collection '" + path + "' not found");
}
else
throw new dbXMLException("Invalid Document Path '" + path + "'");
}
catch ( dbXMLException e ) {
tx.cancel();
throw e;
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE )
tx.commit();
}
}
private Collection getAbsoluteCollection(String path) throws dbXMLException {
if ( path.startsWith("/") ) {
StringTokenizer st = new StringTokenizer(path, "/");
CollectionManager cm = Database.getInstance();
if ( !st.hasMoreTokens() )
return (Collection)cm;
while ( cm != null && st.hasMoreTokens() ) {
path = st.nextToken();
cm = cm.getCollection(path);
}
return (Collection)cm;
}
else
throw new dbXMLException("Invalid Collection Path '"+path+"'");
}
}