001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql.hypersonic;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.io.Reader;
030: import java.sql.SQLException;
031: import java.text.SimpleDateFormat;
032: import java.util.Date;
033:
034: /**
035: * Hypersonic temporary data source. It is similar to standalone datasource, but
036: * data files are created in system temporary directory and are scheduled to deletion
037: * on JVM exit. Use this datasource if you need to keep large amount of temporary data,
038: * for example data pumping applications can benefit from such data source.
039: * @author Pavel Vlasov
040: * @version $Revision: 1.6 $
041: */
042: public class HypersonicTmpDataSource extends HypersonicDataSource {
043:
044: /**
045: * @param initScript Fully qualified name of database initialization script to be loaded by
046: * classloader. Can be null.
047: * @throws ClassNotFoundException
048: * @throws IOException
049: * @throws SQLException
050: */
051: public HypersonicTmpDataSource(Reader scriptReader)
052: throws ClassNotFoundException, IOException, SQLException {
053: super ("jdbc:hsqldb:" + createTmpDir(), "sa", "", null);
054: initDB(scriptReader, null);
055: }
056:
057: /**
058: * @param initScript Fully qualified name of database initialization script to be loaded by
059: * classloader. Can be null.
060: * @throws ClassNotFoundException
061: * @throws IOException
062: * @throws SQLException
063: */
064: public HypersonicTmpDataSource(String initScript)
065: throws ClassNotFoundException, IOException, SQLException {
066: super ("jdbc:hsqldb:" + createTmpDir(), "sa", "", null);
067: InputStream in = getClass().getClassLoader()
068: .getResourceAsStream(initScript);
069: if (in == null) {
070: throw new IOException("Resource not found: " + initScript);
071: }
072: initDB(new InputStreamReader(in), null);
073: }
074:
075: private static String createTmpDir() throws IOException {
076: String tmpDir = System.getProperty("java.io.tmpdir");
077: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
078: String prefix = "HypersonicDB_" + sdf.format(new Date()) + "_";
079: String suffix = "_TMP";
080: final File tmpDbDir = tmpDir == null ? File.createTempFile(
081: prefix, suffix) : File.createTempFile(prefix, suffix,
082: new File(tmpDir));
083: if (tmpDbDir.delete()) {
084: if (tmpDbDir.mkdirs()) {
085: Runtime.getRuntime().addShutdownHook(new Thread() {
086: public void run() {
087:
088: File[] dbFiles = tmpDbDir.listFiles();
089: for (int i = 0; i < dbFiles.length; i++) {
090: dbFiles[i].deleteOnExit();
091: }
092:
093: tmpDbDir.deleteOnExit();
094: }
095: });
096: return tmpDbDir.getAbsolutePath() + File.separator
097: + "HypersonicTmpDB";
098: }
099: throw new IOException("Cannot create directory "
100: + tmpDbDir.getAbsolutePath());
101: }
102: throw new IOException("Cannot delete file "
103: + tmpDbDir.getAbsolutePath());
104: }
105: }
|