1 5 package org.h2.server.web; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 11 import java.sql.Connection ; 12 import java.sql.DriverManager ; 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Properties ; 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 [] GENERIC = new String [] { 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 String driverList; 41 private static int ticker; 42 private int port; 43 private boolean allowOthers; 44 private boolean ssl; 45 private HashMap connectionInfos = new HashMap (); 46 47 AppServer(String [] args) { 48 Properties 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 } 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 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 name) { 113 connectionInfos.remove(name); 114 } 115 116 private File getPropertiesFile() { 117 return FileUtils.getFileInUserHome(Constants.SERVER_PROPERTIES_FILE); 119 } 120 121 Properties loadProperties() { 122 File file = getPropertiesFile(); 123 try { 124 return FileUtils.loadProperties(file); 125 } catch(IOException e) { 126 return new Properties (); 128 } 129 } 130 131 String [] getSettingNames() { 132 ArrayList list = getSettings(); 133 String [] names = new String [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 getSettings() { 141 ArrayList settings = new ArrayList (); 142 if(connectionInfos.size() == 0) { 143 Properties 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 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 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 prop = new Properties (); 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 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 out = new FileOutputStream (getPropertiesFile()); 196 prop.store(out, Constants.SERVER_PROPERTIES_TITLE); 197 out.close(); 198 } catch(IOException e) { 199 TraceSystem.traceThrowable(e); 200 } 201 } 202 203 Connection getConnection(String driver, String url, String user, String password) throws Exception { 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 return DriverManager.getConnection(url, user, password); 222 } 223 224 } 225 | Popular Tags |