001: /*
002: * $Id: Databases.java,v 1.9 2004/08/27 03:21:24 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.sql.Connection;
048: import java.sql.SQLException;
049: import java.util.HashMap;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053: import org.axiondb.AxionException;
054: import org.axiondb.Database;
055: import org.axiondb.jdbc.AxionConnection;
056: import org.axiondb.tools.BatchSqlCommandRunner;
057:
058: /**
059: * A static {@link Map} of {@link Database}s by name.
060: *
061: * @version $Revision: 1.9 $ $Date: 2004/08/27 03:21:24 $
062: * @author Rodney Waldhoff
063: * @author Ahimanikya Satapathy
064: */
065: public class Databases {
066: private Databases() {
067: }
068:
069: public static synchronized Database getOrCreateDatabase(
070: String name, File dir) throws AxionException {
071: name = name.toUpperCase();
072: Database db = (Database) (_map.get(name));
073: if (null == db) {
074: if (null == dir) {
075: db = new MemoryDatabase(name);
076: } else {
077: db = new DiskDatabase(name, dir);
078: }
079: try {
080: runOnStartup(db);
081: } catch (Exception e) {
082: _log.debug(
083: "Exception while executing startup script for \""
084: + name + "\".", e);
085: throw new AxionException(e);
086: }
087: _map.put(name, db);
088: }
089: return db;
090: }
091:
092: public static synchronized void forgetDatabase(String name) {
093: name = name.toUpperCase();
094: _map.remove(name);
095: }
096:
097: private static void runOnStartup(Database db) throws SQLException,
098: IOException {
099: String startupScriptName = null;
100: try {
101: startupScriptName = System
102: .getProperty(RUN_ON_STARTUP_PREFIX
103: + db.getName().toLowerCase()
104: + RUN_ON_STARTUP_SUFFIX);
105: } catch (Exception ignore) {
106: // ignore
107: }
108:
109: if (null != startupScriptName) {
110: InputStream scriptStream = INSTANCE.getClass()
111: .getClassLoader().getResourceAsStream(
112: startupScriptName);
113:
114: // try from absolute path
115: if (null == scriptStream) {
116: File startupScriptFile = new File(startupScriptName);
117: if (startupScriptFile.exists()) {
118: scriptStream = new FileInputStream(
119: startupScriptFile);
120: }
121: }
122:
123: if (null != scriptStream) {
124: Connection conn = null;
125: BatchSqlCommandRunner runner = null;
126:
127: try {
128: conn = new AxionConnection(db);
129: conn.setAutoCommit(false);
130: runner = new BatchSqlCommandRunner(conn);
131: runner.runCommands(scriptStream);
132: conn.commit();
133: } finally {
134: try {
135: runner.close();
136: } catch (Exception e) {
137: }
138: try {
139: conn.close();
140: } catch (Exception e) {
141: }
142: }
143: }
144: }
145: }
146:
147: public static final Databases INSTANCE = new Databases();
148:
149: private static HashMap _map = new HashMap();
150: private static final String RUN_ON_STARTUP_PREFIX = "axiondb.database.";
151: private static final String RUN_ON_STARTUP_SUFFIX = ".runonstartup";
152: private static Log _log = LogFactory.getLog(Databases.class);
153:
154: }
|