001: package net.sourceforge.jaxor.example.tutorial;
002:
003: import net.sourceforge.jaxor.api.ConnectionFactory;
004: import net.sourceforge.jaxor.db.ConnectionDecorator;
005: import net.sourceforge.jaxor.util.SystemException;
006: import org.hsqldb.jdbcDriver;
007:
008: import java.io.FileInputStream;
009: import java.io.FileNotFoundException;
010: import java.io.FileOutputStream;
011: import java.io.IOException;
012: import java.sql.Connection;
013: import java.sql.DriverManager;
014: import java.sql.SQLException;
015:
016: public class PretendPooledHyperDb implements ConnectionFactory {
017:
018: private static Connection pretendConnection;
019: private static Connection realConnection;
020:
021: public static final PretendPooledHyperDb INSTANCE = new PretendPooledHyperDb();
022:
023: private PretendPooledHyperDb() {
024: try {
025: DriverManager.registerDriver(new jdbcDriver());
026: } catch (SQLException e) {
027: throw new SystemException(e);
028: }
029: }
030:
031: public Connection getConnection() {
032: if (pretendConnection == null) {
033: createConnection();
034: }
035: return pretendConnection;
036: }
037:
038: private void createConnection() {
039: try {
040: realConnection = DriverManager.getConnection(
041: "jdbc:hsqldb:hyperdb/activedb", "sa", "");
042: pretendConnection = ConnectionDecorator
043: .createNonClosing(realConnection);
044: } catch (IllegalArgumentException e) {
045: throw new SystemException(e);
046: } catch (SQLException e) {
047: throw new SystemException(e);
048: }
049: }
050:
051: /**
052: * When you REALLY want to close the connection!
053: *
054: */
055: public void close() {
056: if (realConnection == null) {
057: return;
058: }
059:
060: try {
061: realConnection.close();
062: } catch (SQLException e) {
063: throw new SystemException(e);
064: }
065: realConnection = null;
066: pretendConnection = null;
067:
068: }
069:
070: /**
071: * Resets the supplied hyper db to its original state.
072: *
073: */
074: public static void resetDb(String dbName) {
075: PretendPooledHyperDb.INSTANCE.close();
076: PretendPooledHyperDb.fileCopy(dbName + ".script",
077: "hyperdb/activedb.script");
078: PretendPooledHyperDb.fileCopy(dbName + ".properties",
079: "hyperdb/activedb.properties");
080: }
081:
082: public static void fileCopy(String inFileName, String outFileName) {
083: String content = fileRead(inFileName);
084: fileWrite(outFileName, content);
085: }
086:
087: public static String fileRead(String fileName) {
088: StringBuffer buf = new StringBuffer();
089:
090: try {
091: FileInputStream in = new FileInputStream(fileName);
092:
093: int count;
094: byte[] b = new byte[512];
095: while ((count = in.read(b)) > 0) // blocking read
096: {
097: buf.append(new String(b, 0, count));
098: }
099:
100: in.close();
101: } catch (FileNotFoundException e) {
102: throw new SystemException(e);
103: } catch (IOException e) {
104: throw new SystemException(e);
105: }
106:
107: return buf.toString();
108: }
109:
110: public static void fileWrite(String fileName, String data) {
111: try {
112: FileOutputStream out = new FileOutputStream(fileName);
113: out.write(data.getBytes());
114: out.close();
115: } catch (FileNotFoundException e) {
116: throw new SystemException(e);
117: } catch (IOException e) {
118: throw new SystemException(e);
119: }
120: }
121:
122: }
|