KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > jdbc > xlConnectionMcKoi


1 /*(Header: NiLOSTEP / xlSQL)
2
3     Copyright (C) 2004 NiLOSTEP Information Sciences, all
4     rights reserved.
5     
6     This program is licensed under the terms of the GNU
7     General Public License.You should have received a copy
8     of the GNU General Public License along with this
9     program;
10 */

11 package com.nilostep.xlsql.jdbc;
12
13 import com.nilostep.xlsql.sql.*;
14
15 import java.io.*;
16 import java.sql.*;
17 import java.util.*;
18
19 import kiwi.io.*;
20 /**
21  * xlConnectionMcKoi class facilitates xlSQL with the use of a local McKoi SQL
22  * database for query processing.
23  *
24  * @author Jim Caprioli
25  */

26 public class xlConnectionMcKoi extends xlConnection implements FileAcceptor {
27     //~ Instance variables ·····················································
28

29     private String JavaDoc context;
30     private File dbconf;
31     private File data;
32     
33     //~ Constructors ···························································
34

35     /**
36      * Creates a new instance of type xlConnectionMcKoi
37      *
38      * @param url xlSQL JDBC url
39      * @param info Properties
40      *
41      * @throws SQLException
42      */

43     public xlConnectionMcKoi(String JavaDoc url,
44                              Properties info) throws SQLException {
45         try {
46             createMcKoiFiles();
47             context = "APP";
48             URL = url;
49             w = xlSqlWriterFactory.create("mckoi");
50
51             String JavaDoc sql_url = "jdbc:mckoi:local://./xlsqldb.conf?create=true";
52             dbCon = DriverManager.getConnection(sql_url, "xlsql", "xlsql");
53             query = xlSqlSelectFactory.create("mckoi", dbCon);
54             startup();
55             xlsql = xlSqlFactory.create("mckoi", datastore, context);
56         } catch (Exception JavaDoc e) {
57             throw new SQLException("xlSQL: connect to McKoi " +
58                                    e.getMessage());
59         }
60     }
61
62     //~ Methods ································································
63

64     /**
65      * As long as a McKoi connection is active for xlSQL query processing a
66      * directory is in use for (temporary) datastorage. Shutdown cleans up the
67      * garbage on disk after the connection has been closed.
68      *
69      * @throws Exception
70      */

71     public void shutdown() throws Exception JavaDoc {
72         if (!closed) {
73             logger.info("Executing McKoi clean-up...");
74             
75             dbCon.close();
76             rmDir(data);
77             dbconf.delete();
78             
79             closed = true;
80             logger.info("McKoi clean-up done");
81         }
82     }
83
84     /**
85      * When the program is interrupted unexpectedly garbage may not be cleaned
86      * up. Finalize and a separate thread in xlDriver are attempts to clean up
87      * disk garbage in as many cases as possible.
88      *
89      * @throws Throwable
90      */

91     protected void finalize() throws Throwable JavaDoc {
92         shutdown();
93     }
94     
95     private void createMcKoiFiles() throws Exception JavaDoc {
96             
97             String JavaDoc sDbconf = System.getProperty("user.dir") + File.separator + "xlsqldb.conf";
98             dbconf = new File(sDbconf);
99             if (dbconf.exists()) { dbconf.delete(); }
100             FileOutputStream fs = new FileOutputStream(dbconf);
101             PrintStream pw = new PrintStream(fs);
102             
103             pw.print("database_path=./xlsqldb\n");
104             pw.print("log_path=./xlsqldb\n");
105             pw.print("root_path=jvm\n");
106             pw.print("ignore_case_for_identifiers=disabled\n");
107             pw.print("regex_library=gnu.regexp\n");
108             pw.print("data_cache_size=16777216\n");
109             pw.print("max_cache_entry_size=8192\n");
110             pw.print("maximum_worker_threads=4\n");
111             pw.print("debug_log_file=debug.log\n");
112             pw.print("debug_level=20\n");
113             pw.close();
114
115             String JavaDoc sData = System.getProperty("user.dir") + File.separator + "xlsqldb";
116             data = new File(sData);
117             rmDir(data);
118
119     }
120
121     private void rmDir(File f) {
122         if (f.exists()) {
123             FilesystemTraverser dt = new FilesystemTraverser(f, this);
124             dt.traverse();
125             f.delete();
126         }
127     }
128     
129     public boolean accept(java.io.File JavaDoc file) {
130         file.delete();
131         return true;
132     }
133     
134     public boolean accessError(java.io.File JavaDoc file) {
135         return false;
136     }
137     
138 }
139
Popular Tags