KickJava   Java API By Example, From Geeks To Geeks.

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


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: DatabaseImpl.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.StringTokenizer JavaDoc;
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 /**
22  *
23  * @author <a HREF="http://www.smb-tec.com">SMB</a>
24  * @version $Revision: 1.1 $
25  */

26 public class DatabaseImpl extends AbstractConfigurable implements Database {
27
28     private static final Logger logger = Logger.getLogger(DatabaseImpl.class);
29     private static String JavaDoc DB_HOST = "DB_HOST";
30     private static String JavaDoc DB_NAME = "DB_NAME";
31     private static String JavaDoc DB_PORT = "DB_PORT";
32     private static String JavaDoc COL_NAME = "COL_NAME";
33
34     public static String JavaDoc CONFORMANCE_LEVEL = "0";
35     public static String JavaDoc DATABASE_NAME = "ozoneXML";
36
37     // automatically register the database when the class is loaded
38
static {
39         try {
40             org.xmldb.api.DatabaseManager.registerDatabase( new DatabaseImpl() );
41         } catch (Exception JavaDoc e) {
42             e.printStackTrace();
43         }
44     }
45
46     /**
47      * Zero-argument constructor.
48      */

49     public DatabaseImpl() {
50         super();
51     }
52
53     /**
54      */

55     public String JavaDoc getName() throws XMLDBException {
56         return DATABASE_NAME;
57     }
58
59     /**
60      */

61     public Collection getCollection( String JavaDoc uri )
62         throws XMLDBException {
63         // we don't know how to handle this uri
64
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 JavaDoc ozoneDB = new StringBuffer JavaDoc();
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             // re-throw this exception
94
throw e;
95         } catch ( Exception JavaDoc e ) {
96             // encapsulate all other exceptions
97
throw new XMLDBException( ErrorCodes.VENDOR_ERROR, e.toString() );
98         }
99         return collection;
100     }
101
102     /**
103      */

104     public boolean acceptsURI( String JavaDoc uri ) throws XMLDBException {
105         return parseURI( uri );
106     }
107
108     /**
109      */

110     public String JavaDoc getConformanceLevel() throws XMLDBException {
111         return CONFORMANCE_LEVEL;
112     }
113
114     /**
115      * NON API CONFORM EXTENSION!
116      */

117 // public Collection createRootCollection( String uri, String collectionName )
118
// throws XMLDBException {
119
// // we don't know how to handle this uri
120
// if ( !acceptsURI( uri ) )
121
// throw new XMLDBException( ErrorCodes.INVALID_URI, "INVALID_URI" );
122
// return null;
123
// }
124

125     private boolean parseURI( String JavaDoc uri ) {
126         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( uri, ":/?", true );
127         boolean found = false;
128         boolean remote = true;
129         StringBuffer JavaDoc coll = new StringBuffer JavaDoc();
130         StringBuffer JavaDoc database = new StringBuffer JavaDoc();
131         for (int count=0; tokenizer.hasMoreTokens(); count++) {
132             String JavaDoc 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                     ; // ignore this token
140
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; // malformatted uri
148
else if ( count == 4 )
149                     ;
150                 else if ( count == 5 && token.equals( "/" ) )
151                     remote = true; // ok, remote datebase
152
else if ( count == 5 ) {
153                     remote = false; // ok, local database
154
database.append( "/" );
155                     database.append( token );
156                 }
157                 else if ( count == 6 && remote )
158                     setProperty( DB_HOST, token ); // remote host found
159
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 ); // remote port found
167
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