001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server;
020:
021: import javax.xml.parsers.*;
022:
023: import org.lucane.common.Logging;
024: import org.w3c.dom.*;
025:
026: /**
027: * Server configuration
028: */
029: public class ServerConfig {
030: //-- attributes
031: private int port = 9115;
032:
033: private boolean sslEnabled = false;
034: private int sslPort = 9116;
035: private String sslPassword = "password";
036:
037: private String dbDriver = null;
038: private String dbUrl = null;
039: private String dbLogin = null;
040: private String dbPassword = "";
041: private String dbLayer = null;
042:
043: private int dbPoolInitialSize = 0;
044: private int dbPoolMaxActive = 8;
045: private int dbPoolMaxIdle = 8;
046: private int dbPoolMinIdle = 0;
047: private int dbPoolMaxWait = -1;
048:
049: private String storeBackend = "database";
050: private String authenticatorClass = "org.lucane.server.auth.DefaultAuthenticator";
051:
052: /**
053: * Constructor
054: *
055: * @param filename the XML config file
056: */
057: public ServerConfig(String filename) throws Exception {
058: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
059: .newDocumentBuilder();
060:
061: Document document = builder.parse(filename);
062:
063: //-- root element
064: Node node = document.getFirstChild();
065: while (node != null && node.getNodeType() != Node.ELEMENT_NODE)
066: node = node.getNextSibling();
067:
068: if (node == null || !node.getNodeName().equals("lucane-server"))
069: throw new Exception(
070: "root element is different from 'lucane-server'");
071:
072: this .port = Integer.parseInt(node.getAttributes().getNamedItem(
073: "port").getNodeValue());
074:
075: node = node.getFirstChild();
076: while (node != null) {
077: if (node.getNodeType() == Node.ELEMENT_NODE) {
078: if (node.getNodeName().equals("ssl"))
079: handleSSL(node);
080: else if (node.getNodeName().equals("database"))
081: handleDatabase(node);
082: else if (node.getNodeName().equals("store"))
083: handleStore(node);
084: else if (node.getNodeName().equals("authenticator"))
085: handleAuthenticator(node);
086: else
087: Logging.getLogger().warning(
088: "unexepected node : " + node.getNodeName());
089: }
090: node = node.getNextSibling();
091: }
092: }
093:
094: /**
095: * Parse ssl node
096: *
097: * @param node the ssl node
098: */
099: private void handleSSL(Node node) {
100: NamedNodeMap attrs = node.getAttributes();
101: this .sslEnabled = Boolean.valueOf(
102: attrs.getNamedItem("enabled").getNodeValue())
103: .booleanValue();
104: this .sslPort = Integer.parseInt(attrs.getNamedItem("port")
105: .getNodeValue());
106: this .sslPassword = attrs.getNamedItem("password")
107: .getNodeValue();
108: }
109:
110: /**
111: * Parse database node
112: *
113: * @param node the database node
114: */
115: private void handleDatabase(Node node) {
116: node = node.getFirstChild();
117: while (node != null) {
118: if (node.getNodeType() == Node.ELEMENT_NODE) {
119: if (node.getNodeName().equals("jdbc")) {
120: this .dbDriver = node.getAttributes().getNamedItem(
121: "driver").getNodeValue();
122: this .dbUrl = node.getAttributes().getNamedItem(
123: "url").getNodeValue();
124: this .dbLogin = node.getAttributes().getNamedItem(
125: "login").getNodeValue();
126: this .dbPassword = node.getAttributes()
127: .getNamedItem("password").getNodeValue();
128: } else if (node.getNodeName().equals("dblayer"))
129: this .dbLayer = node.getAttributes().getNamedItem(
130: "class").getNodeValue();
131: else if (node.getNodeName().equals("pool")) {
132: this .dbPoolInitialSize = Integer
133: .parseInt(node.getAttributes()
134: .getNamedItem("initialSize")
135: .getNodeValue());
136: this .dbPoolMaxActive = Integer.parseInt(node
137: .getAttributes().getNamedItem("maxActive")
138: .getNodeValue());
139: this .dbPoolMaxIdle = Integer.parseInt(node
140: .getAttributes().getNamedItem("maxIdle")
141: .getNodeValue());
142: this .dbPoolMinIdle = Integer.parseInt(node
143: .getAttributes().getNamedItem("minIdle")
144: .getNodeValue());
145: this .dbPoolMaxWait = Integer.parseInt(node
146: .getAttributes().getNamedItem("maxWait")
147: .getNodeValue());
148: } else
149: Logging.getLogger().warning(
150: "unexepected node : " + node.getNodeName());
151: }
152: node = node.getNextSibling();
153: }
154: }
155:
156: /**
157: * Parse store node
158: *
159: * @param node the store node
160: */
161: private void handleStore(Node node) {
162: this .storeBackend = node.getAttributes()
163: .getNamedItem("backend").getNodeValue();
164: }
165:
166: /**
167: * Parse authenticator node
168: *
169: * @param node the authenticator node
170: */
171: private void handleAuthenticator(Node node) {
172: this .authenticatorClass = node.getAttributes().getNamedItem(
173: "class").getNodeValue();
174: }
175:
176: //-- getters
177:
178: /**
179: * Get server port
180: *
181: * @return the server port
182: */
183: public int getPort() {
184: return this .port;
185: }
186:
187: /**
188: * Get ssl parameters
189: *
190: * @return true if SSL is enabled
191: */
192: public boolean isSslEnabled() {
193: return this .sslEnabled;
194: }
195:
196: /**
197: * Get SSL port
198: *
199: * @return the SSL port
200: */
201: public int getSslPort() {
202: return this .sslPort;
203: }
204:
205: /**
206: * Get SSL password
207: *
208: * @return the SSL password
209: */
210: protected String getSslPassword() {
211: return this .sslPassword;
212: }
213:
214: /**
215: * Get JDBC driver
216: *
217: * @return the JDBC driver
218: */
219: public String getDbDriver() {
220: return this .dbDriver;
221: }
222:
223: /**
224: * Get JDBC url
225: *
226: * @return the JDBC url
227: */
228: public String getDbUrl() {
229: return this .dbUrl;
230: }
231:
232: /**
233: * Get JDBC login
234: *
235: * @return the JDBC login
236: */
237: public String getDbLogin() {
238: return this .dbLogin;
239: }
240:
241: /**
242: * Get JDBC password
243: *
244: * @return the JDBC password
245: */
246: public String getDbPassword() {
247: return this .dbPassword;
248: }
249:
250: /**
251: * Get the DatabaseAbstractionLayer used
252: *
253: * @return the concrete DatabaseLayer
254: */
255: public String getDbLayer() {
256: return this .dbLayer;
257: }
258:
259: /**
260: * Get the pool initial size
261: *
262: * @return the initial size
263: */
264: public int getDbPoolInitialSize() {
265: return this .dbPoolInitialSize;
266: }
267:
268: /**
269: * Get the pool maximum number of active connections
270: *
271: * @return maxActive
272: */
273: public int getDbPoolMaxActive() {
274: return this .dbPoolMaxActive;
275: }
276:
277: /**
278: * Get the pool maximum number of idle connections
279: *
280: * @return maxIdle
281: */
282: public int getDbPoolMaxIdle() {
283: return this .dbPoolMaxIdle;
284: }
285:
286: /**
287: * Get the pool minimun number of idle connections
288: *
289: * @return minIdle
290: */
291: public int getDbPoolMinIdle() {
292: return this .dbPoolMinIdle;
293: }
294:
295: /**
296: * Get the maximum time (in ms) to wait before throwing an error
297: * if no connection is free in the pool
298: *
299: * @return maxWait
300: */
301: public long getDbPoolMaxWait() {
302: return this .dbPoolMaxWait;
303: }
304:
305: /**
306: * Get the backend used for Store
307: * (should be database, or ldap later)
308: *
309: * @return the backend for store
310: */
311: public String getStoreBackend() {
312: return this .storeBackend;
313: }
314:
315: /**
316: * Get the authenticator class to use
317: *
318: * @return the class
319: */
320: public String getAuthenticatorClass() {
321: return this.authenticatorClass;
322: }
323: }
|