KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > ServerConfiguration


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.net.InetAddress JavaDoc;
35
36 import org.hsqldb.lib.HashSet;
37 import org.hsqldb.lib.StringUtil;
38 import org.hsqldb.persist.HsqlProperties;
39
40 //TODO: move to here from Server and WebServer the remaining extraneous code
41
// dealing primarily with reading/setting properties from files, etc.
42

43 /**
44  * Assists with Server and WebServer configuration tasks.
45  *
46  * @author boucherb@users
47  * @version 1.7.2
48  * @since 1.7.2
49  */

50 public final class ServerConfiguration implements ServerConstants {
51
52     private ServerConfiguration() {}
53
54     /**
55      * Retrieves the default port that a Server will try to use in the
56      * abscence of an explicitly specified one, given the specified
57      * value for whether or not to use secure sockets.
58      *
59      * @param protocol the protcol specifier code of the Server
60      * @param isTls if true, retrieve the default port when using secure
61      * sockets, else the default port when using plain sockets
62      * @return the default port used in the abscence of an explicit
63      * specification.
64      *
65      */

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     /**
89      * Retrieves a new HsqlProperties object, if possible, loaded from the
90      * specified file.
91      *
92      * @param path the file's path, without the .properties extention
93      * (which is added automatically)
94      * @return a new properties object loaded from the specified file
95      */

96     public static HsqlProperties getPropertiesFromFile(String JavaDoc 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 JavaDoc e) {}
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 JavaDoc[] listLocalInetAddressNames() {
129
130         InetAddress JavaDoc addr;
131         InetAddress JavaDoc[] 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 JavaDoc 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 JavaDoc e) {}
169
170         try {
171             set.add(InetAddress.getByName("loopback").getHostAddress());
172             set.add(InetAddress.getByName("loopback").getHostName());
173         } catch (Exception JavaDoc e) {}
174
175         return (String JavaDoc[]) set.toArray(new String JavaDoc[set.size()]);
176     }
177
178     /**
179      * Retrieves a new default properties object for a server of the
180      * specified protocol
181      *
182      * @return a new default properties object
183      */

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 JavaDoc 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     /**
210      * Translates null or zero length value for address key to the
211      * special value ServerConstants.SC_DEFAULT_ADDRESS which causes
212      * ServerSockets to be constructed without specifying an InetAddress.
213      *
214      * @param p The properties object upon which to perform the translation
215      */

216     public static void translateAddressProperty(HsqlProperties p) {
217
218         if (p == null) {
219             return;
220         }
221
222         String JavaDoc 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     /**
230      * Translates the legacy default database form: database=...
231      * to the 1.7.2 form: database.0=...
232      *
233      * @param p The properties object upon which to perform the translation
234      */

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 JavaDoc 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     /**
258      * Tranlates unspecified no_system_exit property to false, the default
259      * typically required when a Server is started from the command line.
260      *
261      * @param p The properties object upon which to perform the translation
262      */

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