001: /*
002: * DbManager.java
003: *
004: * Created on February 28, 2005, 6:26 PM
005: */
006:
007: package com.sun.portal.search.db;
008:
009: import com.sun.portal.search.util.*;
010: import com.sun.portal.search.rdm.*;
011: import com.sun.portal.search.soif.SOIF;
012: import com.sun.portal.search.soif.SOIFOutputStream;
013: import java.util.HashMap;
014: import java.util.Map;
015: import java.util.logging.Level;
016:
017: /**
018: * Database factory and config utility
019: */
020: public class DbManager {
021: static private Map RootDbEntrymap = new HashMap();
022:
023: private DbManager() {
024: }
025:
026: static public void getRootDbEntries(SToken st, SOIFOutputStream sout)
027: throws RDMException {
028: int rcode = 0;
029: Datum key, data;
030: SOIF soif;
031:
032: PartitionedDb rootdb = null;
033:
034: try {
035:
036: rootdb = new PartitionedDb();
037: //rootdb = new IndexedSOIFDb();
038: rootdb.open(null,
039: SearchConfig.getValue(SearchConfig.DBDIR), "root",
040: RDMDb.WRCREAT, 0644);
041: rootdb.dump(sout);
042: } catch (Exception e) {
043:
044: } finally {
045: if (rootdb != null)
046: rootdb.close(st);
047: }
048: }
049:
050: static public SOIF getRootDbEntry(SToken st, String dbname)
051: throws RDMException {
052:
053: int rcode = 0;
054: Datum key, data;
055: SOIF soif;
056:
057: soif = (SOIF) RootDbEntrymap.get(dbname);
058: if (soif != null) {
059: return soif;
060: }
061:
062: PartitionedDb rootdb = null;
063:
064: try {
065:
066: rootdb = new PartitionedDb();
067: //rootdb = new IndexedSOIFDb();
068: rootdb.open(null,
069: SearchConfig.getValue(SearchConfig.DBDIR), "root",
070: RDMDb.WRCREAT, 0644);
071:
072: key = new Datum(dbname);
073: key = DbUtil.key_create(dbname);
074: data = new Datum();
075:
076: rcode = rootdb.fetch(st, key, data, 0, null);
077:
078: /*
079: if (rcode == Db.DB_NOTFOUND && (rw & RDMDb.WRCREAT) != 0) {
080: // should we really create an empty db entry here???
081: // XXX partitioned db is not using this mechanism...
082: try {
083: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSB0041", dbname );
084: soif = new SOIF(PartitionedDb.DBASE, dbname);
085:
086: data.set_data(soif.toByteArray(SOIFDb.ENCODING));
087: key = new Datum(dbname);
088: key = DbUtil.key_create(dbname);
089: rootdb.store(st, key, data, 0, null); // XXX do we need a txn arg?
090: rcode = rootdb.fetch(st, key, data, 0, null); // refetch the db meta data
091: }
092: catch (Exception e) {
093: SearchLogger.getLogger().log(Level.WARNING, "PSSH_CSPSB0043", dbname );
094: //CSxLog.error(1, 1, "Failed to create db: " + dbname, e);
095: throw new RDMException("Failed to create db: " + dbname);
096: }
097: }
098: else if (rcode != 0)
099: throw new RDMException(new DbException("Failed to open db", rcode));
100: */
101:
102: //if (rcode != 0)
103: //throw new RDMException("Failed to open db, " + dbname + " err=" + rcode);
104: if (rcode == RDMDb.DB_NOTFOUND)
105: return null;
106:
107: soif = new SOIF(data.get_data(), SOIFDb.ENCODING);
108: RootDbEntrymap.put(dbname, soif);
109: } catch (Exception e) {
110: if (!(e instanceof RDMException))
111: e = new RDMException(e.getMessage());
112: throw (RDMException) e;
113: } finally {
114: if (rootdb != null)
115: rootdb.close(st);
116: }
117:
118: return soif;
119:
120: }
121:
122: /**
123: * A database factory - opens dbs according to their class in their root db entry.
124: * Use this mechanism when you don't already have a db object and don't know the db class.
125: */
126: public static RDMDb dbOpen(SToken st, String dbname, int rw)
127: throws Exception {
128: String dbdir = SearchConfig.getValue(SearchConfig.DBDIR);
129: if (dbdir == null) {
130: SearchLogger.getLogger().log(Level.WARNING,
131: "PSSH_CSPSB0083", SearchConfig.DBDIR);
132: return null;
133: }
134:
135: // Get the db class from rootdb if present
136:
137: // XXX we should pass the entire config soif to the native db constructor instead
138: // of having each db having to haul it out of rootdb again...
139:
140: RDMDb db;
141: SOIF dbsoif = null; // root db entry for this db
142: String dbClass = null;
143:
144: if (dbname.equalsIgnoreCase("taxonomy"))
145: dbClass = "com.sun.portal.search.db.SearchOnlyDb"; // XXX this will switch to TaxonomyDb at some point
146: else {
147: dbsoif = getRootDbEntry(st, dbname);
148: if (dbsoif != null)
149: dbClass = dbsoif.getValue("class");
150: if (dbClass == null)
151: dbClass = "com.sun.portal.search.db.IndexedSOIFDb"; // the default db class
152: }
153:
154: Class dbc;
155: try {
156: try {
157: // try the current class loader first
158: dbc = Class.forName(dbClass, true, Thread
159: .currentThread().getContextClassLoader());
160: } catch (ClassNotFoundException e) {
161: // try the global class loader
162: dbc = Class.forName(dbClass);
163: }
164: } catch (Exception e) {
165: SearchLogger.getLogger().log(Level.WARNING,
166: "PSSH_CSPSB0085", dbClass);
167: return null;
168: }
169: db = (RDMDb) dbc.newInstance();
170: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSB0086",
171: dbname);
172: db.open(st, dbdir, dbname, rw, 0644);
173: return db;
174: }
175:
176: }
|