KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > server > web > AppServer


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.server.web;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import java.sql.Connection JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.h2.engine.Constants;
18 import org.h2.message.TraceSystem;
19 import org.h2.util.FileUtils;
20 import org.h2.util.MathUtils;
21
22 public class AppServer {
23
24     private static final String JavaDoc[] GENERIC = new String JavaDoc[] {
25         "Generic Firebird Server|org.firebirdsql.jdbc.FBDriver|jdbc:firebirdsql:localhost:c:/temp/firebird/test|sysdba",
26         "Generic OneDollarDB|in.co.daffodil.db.jdbc.DaffodilDBDriver|jdbc:daffodilDB_embedded:school;path=C:/temp;create=true|sa",
27         "Generic DB2|COM.ibm.db2.jdbc.net.DB2Driver|jdbc:db2://<host>/<db>|" ,
28         "Generic Oracle|oracle.jdbc.driver.OracleDriver|jdbc:oracle:thin:@<host>:1521:<instance>|scott" ,
29         "Generic PostgreSQL|org.postgresql.Driver|jdbc:postgresql:<db>|" ,
30         "Generic MS SQL Server|com.microsoft.jdbc.sqlserver.SQLServerDriver|jdbc:Microsoft:sqlserver://localhost:1433;DatabaseName=sqlexpress|sa",
31         "Generic MS SQL Server 2005|com.microsoft.sqlserver.jdbc.SQLServerDriver|jdbc:sqlserver://localhost;DatabaseName=test|sa",
32         "Generic MySQL|com.mysql.jdbc.Driver|jdbc:mysql://<host>:<port>/<db>|" ,
33         "Generic Derby (Embedded)|org.apache.derby.jdbc.EmbeddedDriver|jdbc:derby:test;create=true|sa",
34         "Generic Derby (Server)|org.apache.derby.jdbc.ClientDriver|jdbc:derby://localhost:1527/test;create=true|sa",
35         "Generic HSQLDB|org.hsqldb.jdbcDriver|jdbc:hsqldb:test;hsqldb.default_table_type=cached|sa" ,
36         "Generic H2|org.h2.Driver|jdbc:h2:test|sa",
37     };
38
39     // private URLClassLoader urlClassLoader;
40
private String JavaDoc driverList;
41     private static int ticker;
42     private int port;
43     private boolean allowOthers;
44     private boolean ssl;
45     private HashMap JavaDoc connectionInfos = new HashMap JavaDoc();
46
47     AppServer(String JavaDoc[] args) {
48         Properties JavaDoc prop = loadProperties();
49         driverList = prop.getProperty("drivers");
50         port = FileUtils.getIntProperty(prop, "webPort", Constants.DEFAULT_HTTP_PORT);
51         ssl = FileUtils.getBooleanProperty(prop, "webSSL", Constants.DEFAULT_HTTP_SSL);
52         allowOthers = FileUtils.getBooleanProperty(prop, "webAllowOthers", Constants.DEFAULT_HTTP_ALLOW_OTHERS);
53         for(int i=0; args != null && i<args.length; i++) {
54             if("-webPort".equals(args[i])) {
55                 port = MathUtils.decodeInt(args[++i]);
56             } else if("-webSSL".equals(args[i])) {
57                 ssl = Boolean.valueOf(args[++i]).booleanValue();
58             } else if("-webAllowOthers".equals(args[i])) {
59                 allowOthers = Boolean.valueOf(args[++i]).booleanValue();
60             }
61         }
62         // TODO gcj: don't load drivers in case of GCJ
63
// if(false) {
64
// if(driverList != null) {
65
// try {
66
// String[] drivers = StringUtils.arraySplit(driverList, ',', false);
67
// URL[] urls = new URL[drivers.length];
68
// for(int i=0; i<drivers.length; i++) {
69
// urls[i] = new URL(drivers[i]);
70
// }
71
// urlClassLoader = URLClassLoader.newInstance(urls);
72
// } catch (MalformedURLException e) {
73
// TraceSystem.traceThrowable(e);
74
// }
75
// }
76
// }
77
}
78
79     void setAllowOthers(boolean b) {
80         allowOthers = b;
81     }
82
83     void setSSL(boolean b) {
84         ssl = b;
85     }
86
87     void setPort(int port) {
88         this.port = port;
89     }
90
91     boolean getAllowOthers() {
92         return allowOthers;
93     }
94
95     boolean getSSL() {
96         return ssl;
97     }
98
99     int getPort() {
100         return port;
101     }
102
103     ConnectionInfo getSetting(String JavaDoc name) {
104         return (ConnectionInfo)connectionInfos.get(name);
105     }
106
107     void updateSetting(ConnectionInfo info) {
108         connectionInfos.put(info.name, info);
109         info.lastAccess = ticker++;
110     }
111
112     void removeSetting(String JavaDoc name) {
113         connectionInfos.remove(name);
114     }
115
116     private File JavaDoc getPropertiesFile() {
117         // store the properties in the user directory
118
return FileUtils.getFileInUserHome(Constants.SERVER_PROPERTIES_FILE);
119     }
120
121     Properties JavaDoc loadProperties() {
122         File JavaDoc file = getPropertiesFile();
123         try {
124             return FileUtils.loadProperties(file);
125         } catch(IOException JavaDoc e) {
126             // TODO log exception
127
return new Properties JavaDoc();
128         }
129     }
130
131     String JavaDoc[] getSettingNames() {
132         ArrayList JavaDoc list = getSettings();
133         String JavaDoc[] names = new String JavaDoc[list.size()];
134         for(int i=0; i<list.size(); i++) {
135             names[i] = ((ConnectionInfo)list.get(i)).name;
136         }
137         return names;
138     }
139
140     synchronized ArrayList JavaDoc getSettings() {
141         ArrayList JavaDoc settings = new ArrayList JavaDoc();
142         if(connectionInfos.size() == 0) {
143             Properties JavaDoc prop = loadProperties();
144             if(prop.size() == 0) {
145                 for(int i=0; i<AppServer.GENERIC.length; i++) {
146                     ConnectionInfo info = new ConnectionInfo(AppServer.GENERIC[i]);
147                     settings.add(info);
148                     updateSetting(info);
149                 }
150             } else {
151                 for(int i=0; ; i++) {
152                     String JavaDoc data = prop.getProperty(String.valueOf(i));
153                     if(data == null) {
154                         break;
155                     }
156                     ConnectionInfo info = new ConnectionInfo(data);
157                     settings.add(info);
158                     updateSetting(info);
159                 }
160             }
161         } else {
162             settings.addAll(connectionInfos.values());
163         }
164         sortConnectionInfo(settings);
165         return settings;
166     }
167
168     void sortConnectionInfo(ArrayList JavaDoc list) {
169           for (int i = 1, j; i < list.size(); i++) {
170               ConnectionInfo t = (ConnectionInfo) list.get(i);
171               for (j = i - 1; j >= 0 && (((ConnectionInfo)list.get(j)).lastAccess < t.lastAccess); j--) {
172                   list.set(j + 1, list.get(j));
173               }
174               list.set(j + 1, t);
175           }
176     }
177
178     synchronized void saveSettings() {
179         try {
180             Properties JavaDoc prop = new Properties JavaDoc();
181             if(driverList != null) {
182                 prop.setProperty("drivers", driverList);
183             }
184             prop.setProperty("webPort", String.valueOf(port));
185             prop.setProperty("webAllowOthers", String.valueOf(allowOthers));
186             prop.setProperty("webSSL", String.valueOf(ssl));
187             ArrayList JavaDoc settings = getSettings();
188             int len = settings.size();
189             for(int i=0; i<len; i++) {
190                 ConnectionInfo info = (ConnectionInfo) settings.get(i);
191                 if(info != null) {
192                     prop.setProperty(String.valueOf(len - i - 1), info.getString());
193                 }
194             }
195             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(getPropertiesFile());
196             prop.store(out, Constants.SERVER_PROPERTIES_TITLE);
197             out.close();
198         } catch(IOException JavaDoc e) {
199             TraceSystem.traceThrowable(e);
200         }
201     }
202
203     // TODO GCJ: if this method is synchronized, then the .exe file fails (probably does not unlock the object)
204
// and cannot go in here after a class was not found
205
Connection JavaDoc getConnection(String JavaDoc driver, String JavaDoc url, String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
206         driver = driver.trim();
207         url = url.trim();
208         user = user.trim();
209         password = password.trim();
210         org.h2.Driver.load();
211         Class.forName(driver);
212 // try {
213
// Driver dr = (Driver) urlClassLoader.loadClass(driver).newInstance();
214
// Properties p = new Properties();
215
// p.setProperty("user", user);
216
// p.setProperty("password", password);
217
// return dr.connect(url, p);
218
// } catch(ClassNotFoundException e2) {
219
// throw e2;
220
// }
221
return DriverManager.getConnection(url, user, password);
222     }
223
224 }
225
Popular Tags