1 30 31 32 package org.hsqldb; 33 34 import java.net.InetAddress ; 35 36 import org.hsqldb.lib.HashSet; 37 import org.hsqldb.lib.StringUtil; 38 import org.hsqldb.persist.HsqlProperties; 39 40 43 50 public final class ServerConfiguration implements ServerConstants { 51 52 private ServerConfiguration() {} 53 54 66 public static int getDefaultPort(int protocol, boolean isTls) { 67 68 switch (protocol) { 69 70 case SC_PROTOCOL_HSQL : { 71 return isTls ? SC_DEFAULT_HSQLS_SERVER_PORT 72 : SC_DEFAULT_HSQL_SERVER_PORT; 73 } 74 case SC_PROTOCOL_HTTP : { 75 return isTls ? SC_DEFAULT_HTTPS_SERVER_PORT 76 : SC_DEFAULT_HTTP_SERVER_PORT; 77 } 78 case SC_PROTOCOL_BER : { 79 return isTls ? -1 80 : SC_DEFAULT_BER_SERVER_PORT; 81 } 82 default : { 83 return -1; 84 } 85 } 86 } 87 88 96 public static HsqlProperties getPropertiesFromFile(String path) { 97 98 if (StringUtil.isEmpty(path)) { 99 return null; 100 } 101 102 HsqlProperties p = new HsqlProperties(path); 103 104 try { 105 p.load(); 106 } catch (Exception e) {} 107 108 return p; 109 } 110 111 128 public static String [] listLocalInetAddressNames() { 129 130 InetAddress addr; 131 InetAddress [] addrs; 132 HashSet set; 133 134 set = new HashSet(); 135 136 try { 137 addr = InetAddress.getLocalHost(); 138 addrs = InetAddress.getAllByName(addr.getHostAddress()); 139 140 for (int i = 0; i < addrs.length; i++) { 141 set.add(addrs[i].getHostAddress()); 142 set.add(addrs[i].getHostName()); 143 } 144 145 addrs = InetAddress.getAllByName(addr.getHostName()); 146 147 for (int i = 0; i < addrs.length; i++) { 148 set.add(addrs[i].getHostAddress()); 149 set.add(addrs[i].getHostName()); 150 } 151 } catch (Exception e) {} 152 153 try { 154 addr = InetAddress.getByName(null); 155 addrs = InetAddress.getAllByName(addr.getHostAddress()); 156 157 for (int i = 0; i < addrs.length; i++) { 158 set.add(addrs[i].getHostAddress()); 159 set.add(addrs[i].getHostName()); 160 } 161 162 addrs = InetAddress.getAllByName(addr.getHostName()); 163 164 for (int i = 0; i < addrs.length; i++) { 165 set.add(addrs[i].getHostAddress()); 166 set.add(addrs[i].getHostName()); 167 } 168 } catch (Exception e) {} 169 170 try { 171 set.add(InetAddress.getByName("loopback").getHostAddress()); 172 set.add(InetAddress.getByName("loopback").getHostName()); 173 } catch (Exception e) {} 174 175 return (String []) set.toArray(new String [set.size()]); 176 } 177 178 184 public static HsqlProperties newDefaultProperties(int protocol) { 185 186 HsqlProperties p = new HsqlProperties(); 187 188 p.setProperty(SC_KEY_AUTORESTART_SERVER, 189 SC_DEFAULT_SERVER_AUTORESTART); 190 p.setProperty(SC_KEY_ADDRESS, SC_DEFAULT_ADDRESS); 191 p.setProperty(SC_KEY_NO_SYSTEM_EXIT, SC_DEFAULT_NO_SYSTEM_EXIT); 192 193 boolean isTls = SC_DEFAULT_TLS; 194 195 try { 196 isTls = System.getProperty("javax.net.ssl.keyStore") != null; 197 } catch (Exception e) {} 198 199 p.setProperty(SC_KEY_PORT, getDefaultPort(protocol, isTls)); 200 p.setProperty(SC_KEY_SILENT, SC_DEFAULT_SILENT); 201 p.setProperty(SC_KEY_TLS, isTls); 202 p.setProperty(SC_KEY_TRACE, SC_DEFAULT_TRACE); 203 p.setProperty(SC_KEY_WEB_DEFAULT_PAGE, SC_DEFAULT_WEB_PAGE); 204 p.setProperty(SC_KEY_WEB_ROOT, SC_DEFAULT_WEB_ROOT); 205 206 return p; 207 } 208 209 216 public static void translateAddressProperty(HsqlProperties p) { 217 218 if (p == null) { 219 return; 220 } 221 222 String address = p.getProperty(SC_KEY_ADDRESS); 223 224 if (StringUtil.isEmpty(address)) { 225 p.setProperty(SC_KEY_ADDRESS, SC_DEFAULT_ADDRESS); 226 } 227 } 228 229 235 public static void translateDefaultDatabaseProperty(HsqlProperties p) { 236 237 if (p == null) { 238 return; 239 } 240 241 if (!p.isPropertyTrue(SC_KEY_REMOTE_OPEN_DB)) { 242 if (p.getProperty(SC_KEY_DATABASE + "." + 0) == null) { 243 String defaultdb = p.getProperty(SC_KEY_DATABASE); 244 245 if (defaultdb == null) { 246 defaultdb = SC_DEFAULT_DATABASE; 247 } 248 249 p.setProperty(SC_KEY_DATABASE + ".0", defaultdb); 250 p.setProperty(SC_KEY_DBNAME + ".0", ""); 251 } else if (p.getProperty(SC_KEY_DBNAME + "." + 0) == null) { 252 p.setProperty(SC_KEY_DBNAME + ".0", ""); 253 } 254 } 255 } 256 257 263 public static void translateDefaultNoSystemExitProperty( 264 HsqlProperties p) { 265 266 if (p == null) { 267 return; 268 } 269 270 p.setPropertyIfNotExists(SC_KEY_NO_SYSTEM_EXIT, "false"); 271 } 272 } 273 | Popular Tags |