1 9 package org.ozoneDB.xml.cli; 10 11 import java.util.StringTokenizer ; 12 13 import org.xmldb.api.base.Collection; 14 import org.xmldb.api.base.Database; 15 import org.xmldb.api.base.ErrorCodes; 16 import org.xmldb.api.base.XMLDBException; 17 18 import org.ozoneDB.ExternalDatabase; 19 import org.apache.log4j.Logger; 20 21 26 public class DatabaseImpl extends AbstractConfigurable implements Database { 27 28 private static final Logger logger = Logger.getLogger(DatabaseImpl.class); 29 private static String DB_HOST = "DB_HOST"; 30 private static String DB_NAME = "DB_NAME"; 31 private static String DB_PORT = "DB_PORT"; 32 private static String COL_NAME = "COL_NAME"; 33 34 public static String CONFORMANCE_LEVEL = "0"; 35 public static String DATABASE_NAME = "ozoneXML"; 36 37 static { 39 try { 40 org.xmldb.api.DatabaseManager.registerDatabase( new DatabaseImpl() ); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 } 45 46 49 public DatabaseImpl() { 50 super(); 51 } 52 53 55 public String getName() throws XMLDBException { 56 return DATABASE_NAME; 57 } 58 59 61 public Collection getCollection( String uri ) 62 throws XMLDBException { 63 if ( !acceptsURI( uri ) ) 65 throw new XMLDBException( ErrorCodes.INVALID_URI, "INVALID_URI" ); 66 67 Collection collection = null; 68 try { 69 ExternalDatabase db = null; 70 StringBuffer ozoneDB = new StringBuffer (); 71 if ( getProperty( DB_HOST ) == null ) 72 ozoneDB.append( "ozonedb:local:").append( getProperty( DB_NAME ) ); 73 else 74 ozoneDB.append( "ozonedb:remote://" ).append( getProperty( DB_HOST ) ).append( 75 ":").append( getProperty( DB_PORT ) ); 76 77 logger.debug("DatabaseImpl.getCollection() - Remote DB: " + getProperty(DB_HOST) + " " + getProperty(DB_PORT)); 78 logger.debug("DatabaseImpl.getCollection() - Local DB: " + getProperty(DB_NAME)); 79 logger.debug("DatabaseImpl.getCollection() - Load Collection: " + getProperty(COL_NAME)); 80 81 db = ExternalDatabase.openDatabase( ozoneDB.toString() ); 82 if (db == null) 83 throw new XMLDBException( ErrorCodes.INVALID_DATABASE, "INVALID_DATABASE" ); 84 85 db.reloadClasses(); 86 87 collection = CollectionImpl.forName( db, getProperty( COL_NAME ) ); 88 89 if (collection == null) 90 throw new XMLDBException( ErrorCodes.INVALID_COLLECTION, "INVALID_COLLECTION" ); 91 92 } catch ( XMLDBException e ) { 93 throw e; 95 } catch ( Exception e ) { 96 throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.toString() ); 98 } 99 return collection; 100 } 101 102 104 public boolean acceptsURI( String uri ) throws XMLDBException { 105 return parseURI( uri ); 106 } 107 108 110 public String getConformanceLevel() throws XMLDBException { 111 return CONFORMANCE_LEVEL; 112 } 113 114 117 125 private boolean parseURI( String uri ) { 126 StringTokenizer tokenizer = new StringTokenizer ( uri, ":/?", true ); 127 boolean found = false; 128 boolean remote = true; 129 StringBuffer coll = new StringBuffer (); 130 StringBuffer database = new StringBuffer (); 131 for (int count=0; tokenizer.hasMoreTokens(); count++) { 132 String token = tokenizer.nextToken(); 133 if ( count == 0 ) { 134 if ( !token.equals( "xmldb" ) ) 135 return false; 136 } 137 else if ( count <= 3) { 138 if ( (count % 2) == 1 && token.equals( ":" ) ) 139 ; else if ( (count % 2) == 0 ) { 141 if ( token.equals( "ozonexml" ) ) 142 found = true; 143 } 144 } 145 else if ( count > 3 ) { 146 if ( count == 4 && !token.equals( "/") ) 147 return false; else if ( count == 4 ) 149 ; 150 else if ( count == 5 && token.equals( "/" ) ) 151 remote = true; else if ( count == 5 ) { 153 remote = false; database.append( "/" ); 155 database.append( token ); 156 } 157 else if ( count == 6 && remote ) 158 setProperty( DB_HOST, token ); else if ( count == 6 ) 160 database.append( token ); 161 else if ( count == 7 && remote && token.equals( ":" ) ) 162 ; 163 else if ( count == 7 ) 164 database.append( token ); 165 else if ( count == 8 && remote ) 166 setProperty( DB_PORT, token ); else if ( count == 8 ) 168 database.append( token ); 169 else if ( count == 9 && token.equals( "?" ) ) 170 ; 171 else if ( count == 9 ) 172 database.append( token ); 173 else if ( token.equals( "?" ) ) 174 ; 175 else 176 coll.append( token ); 177 } 178 } 179 180 if ( !remote ) 181 if ( database.length() > 0 ) 182 setProperty( DB_NAME, database.toString() ); 183 else 184 return false; 185 186 if ( coll.length() > 0 ) 187 setProperty( COL_NAME, coll.toString() ); 188 189 return true; 190 } 191 192 } 193 | Popular Tags |