001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import java.net.InetAddress;
034:
035: import org.hsqldb.lib.HashSet;
036: import org.hsqldb.lib.StringUtil;
037: import org.hsqldb.persist.HsqlProperties;
038:
039: //TODO: move to here from Server and WebServer the remaining extraneous code
040: // dealing primarily with reading/setting properties from files, etc.
041:
042: /**
043: * Assists with Server and WebServer configuration tasks.
044: *
045: * @author boucherb@users
046: * @version 1.7.2
047: * @since 1.7.2
048: */
049: public final class ServerConfiguration implements ServerConstants {
050:
051: private ServerConfiguration() {
052: }
053:
054: /**
055: * Retrieves the default port that a Server will try to use in the
056: * abscence of an explicitly specified one, given the specified
057: * value for whether or not to use secure sockets.
058: *
059: * @param protocol the protcol specifier code of the Server
060: * @param isTls if true, retrieve the default port when using secure
061: * sockets, else the default port when using plain sockets
062: * @return the default port used in the abscence of an explicit
063: * specification.
064: *
065: */
066: public static int getDefaultPort(int protocol, boolean isTls) {
067:
068: switch (protocol) {
069:
070: case SC_PROTOCOL_HSQL: {
071: return isTls ? SC_DEFAULT_HSQLS_SERVER_PORT
072: : SC_DEFAULT_HSQL_SERVER_PORT;
073: }
074: case SC_PROTOCOL_HTTP: {
075: return isTls ? SC_DEFAULT_HTTPS_SERVER_PORT
076: : SC_DEFAULT_HTTP_SERVER_PORT;
077: }
078: case SC_PROTOCOL_BER: {
079: return isTls ? -1 : SC_DEFAULT_BER_SERVER_PORT;
080: }
081: default: {
082: return -1;
083: }
084: }
085: }
086:
087: /**
088: * Retrieves a new HsqlProperties object, if possible, loaded from the
089: * specified file.
090: *
091: * @param path the file's path, without the .properties extention
092: * (which is added automatically)
093: * @return a new properties object loaded from the specified file
094: */
095: public static HsqlProperties getPropertiesFromFile(String path) {
096:
097: if (StringUtil.isEmpty(path)) {
098: return null;
099: }
100:
101: HsqlProperties p = new HsqlProperties(path);
102:
103: try {
104: p.load();
105: } catch (Exception e) {
106: }
107:
108: return p;
109: }
110:
111: /**
112: * Retrieves an array of Strings naming the distinct, known to be valid local
113: * InetAddress names for this machine. The process is to collect and
114: * return the union of the following sets:
115: *
116: * <ol>
117: * <li> InetAddress.getAllByName(InetAddress.getLocalHost().getHostAddress())
118: * <li> InetAddress.getAllByName(InetAddress.getLocalHost().getHostName())
119: * <li> InetAddress.getAllByName(InetAddress.getByName(null).getHostAddress())
120: * <li> InetAddress.getAllByName(InetAddress.getByName(null).getHostName())
121: * <li> InetAddress.getByName("loopback").getHostAddress()
122: * <li> InetAddress.getByName("loopback").getHostname()
123: * </ol>
124: *
125: * @return the distinct, known to be valid local
126: * InetAddress names for this machine
127: */
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:
154: try {
155: addr = InetAddress.getByName(null);
156: addrs = InetAddress.getAllByName(addr.getHostAddress());
157:
158: for (int i = 0; i < addrs.length; i++) {
159: set.add(addrs[i].getHostAddress());
160: set.add(addrs[i].getHostName());
161: }
162:
163: addrs = InetAddress.getAllByName(addr.getHostName());
164:
165: for (int i = 0; i < addrs.length; i++) {
166: set.add(addrs[i].getHostAddress());
167: set.add(addrs[i].getHostName());
168: }
169: } catch (Exception e) {
170: }
171:
172: try {
173: set.add(InetAddress.getByName("loopback").getHostAddress());
174: set.add(InetAddress.getByName("loopback").getHostName());
175: } catch (Exception e) {
176: }
177:
178: return (String[]) set.toArray(new String[set.size()]);
179: }
180:
181: /**
182: * Retrieves a new default properties object for a server of the
183: * specified protocol
184: *
185: * @return a new default properties object
186: */
187: public static HsqlProperties newDefaultProperties(int protocol) {
188:
189: HsqlProperties p = new HsqlProperties();
190:
191: p.setProperty(SC_KEY_AUTORESTART_SERVER,
192: SC_DEFAULT_SERVER_AUTORESTART);
193: p.setProperty(SC_KEY_ADDRESS, SC_DEFAULT_ADDRESS);
194: p.setProperty(SC_KEY_NO_SYSTEM_EXIT, SC_DEFAULT_NO_SYSTEM_EXIT);
195:
196: boolean isTls = SC_DEFAULT_TLS;
197:
198: try {
199: isTls = System.getProperty("javax.net.ssl.keyStore") != null;
200: } catch (Exception e) {
201: }
202:
203: p.setProperty(SC_KEY_PORT, getDefaultPort(protocol, isTls));
204: p.setProperty(SC_KEY_SILENT, SC_DEFAULT_SILENT);
205: p.setProperty(SC_KEY_TLS, isTls);
206: p.setProperty(SC_KEY_TRACE, SC_DEFAULT_TRACE);
207: p.setProperty(SC_KEY_WEB_DEFAULT_PAGE, SC_DEFAULT_WEB_PAGE);
208: p.setProperty(SC_KEY_WEB_ROOT, SC_DEFAULT_WEB_ROOT);
209:
210: return p;
211: }
212:
213: /**
214: * Translates null or zero length value for address key to the
215: * special value ServerConstants.SC_DEFAULT_ADDRESS which causes
216: * ServerSockets to be constructed without specifying an InetAddress.
217: *
218: * @param p The properties object upon which to perform the translation
219: */
220: public static void translateAddressProperty(HsqlProperties p) {
221:
222: if (p == null) {
223: return;
224: }
225:
226: String address = p.getProperty(SC_KEY_ADDRESS);
227:
228: if (StringUtil.isEmpty(address)) {
229: p.setProperty(SC_KEY_ADDRESS, SC_DEFAULT_ADDRESS);
230: }
231: }
232:
233: /**
234: * Translates the legacy default database form: database=...
235: * to the 1.7.2 form: database.0=...
236: *
237: * @param p The properties object upon which to perform the translation
238: */
239: public static void translateDefaultDatabaseProperty(HsqlProperties p) {
240:
241: if (p == null) {
242: return;
243: }
244:
245: if (!p.isPropertyTrue(SC_KEY_REMOTE_OPEN_DB)) {
246: if (p.getProperty(SC_KEY_DATABASE + "." + 0) == null) {
247: String defaultdb = p.getProperty(SC_KEY_DATABASE);
248:
249: if (defaultdb == null) {
250: defaultdb = SC_DEFAULT_DATABASE;
251: }
252:
253: p.setProperty(SC_KEY_DATABASE + ".0", defaultdb);
254: p.setProperty(SC_KEY_DBNAME + ".0", "");
255: } else if (p.getProperty(SC_KEY_DBNAME + "." + 0) == null) {
256: p.setProperty(SC_KEY_DBNAME + ".0", "");
257: }
258: }
259: }
260:
261: /**
262: * Tranlates unspecified no_system_exit property to false, the default
263: * typically required when a Server is started from the command line.
264: *
265: * @param p The properties object upon which to perform the translation
266: */
267: public static void translateDefaultNoSystemExitProperty(
268: HsqlProperties p) {
269:
270: if (p == null) {
271: return;
272: }
273:
274: p.setPropertyIfNotExists(SC_KEY_NO_SYSTEM_EXIT, "false");
275: }
276: }
|