KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > ServerConfig


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server;
20
21 import javax.xml.parsers.*;
22
23 import org.lucane.common.Logging;
24 import org.w3c.dom.*;
25
26
27 /**
28  * Server configuration
29  */

30 public class ServerConfig
31 {
32     //-- attributes
33
private int port = 9115;
34     
35     private boolean sslEnabled = false;
36     private int sslPort = 9116;
37     private String JavaDoc sslPassword = "password";
38     
39     private String JavaDoc dbDriver = null;
40     private String JavaDoc dbUrl = null;
41     private String JavaDoc dbLogin = null;
42     private String JavaDoc dbPassword = "";
43     private String JavaDoc dbLayer = null;
44     
45     private int dbPoolInitialSize = 0;
46     private int dbPoolMaxActive = 8;
47     private int dbPoolMaxIdle = 8;
48     private int dbPoolMinIdle = 0;
49     private int dbPoolMaxWait = -1;
50     
51     private String JavaDoc storeBackend = "database";
52     private String JavaDoc authenticatorClass = "org.lucane.server.auth.DefaultAuthenticator";
53     
54     /**
55      * Constructor
56      *
57      * @param filename the XML config file
58      */

59     public ServerConfig(String JavaDoc filename)
60     throws Exception JavaDoc
61     {
62           DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
63           
64           Document document = builder.parse(filename);
65                                                                   
66           //-- root element
67
Node node = document.getFirstChild();
68           while(node != null && node.getNodeType() != Node.ELEMENT_NODE)
69             node = node.getNextSibling();
70           
71           if(node == null || !node.getNodeName().equals("lucane-server"))
72             throw new Exception JavaDoc("root element is different from 'lucane-server'");
73           
74           this.port = Integer.parseInt(node.getAttributes().getNamedItem("port").getNodeValue());
75           
76           node = node.getFirstChild();
77           while(node != null)
78           {
79             if(node.getNodeType() == Node.ELEMENT_NODE)
80             {
81                 if(node.getNodeName().equals("ssl"))
82                     handleSSL(node);
83                 else if(node.getNodeName().equals("database"))
84                     handleDatabase(node);
85                 else if(node.getNodeName().equals("store"))
86                     handleStore(node);
87                 else if(node.getNodeName().equals("authenticator"))
88                     handleAuthenticator(node);
89                 else
90                   Logging.getLogger().warning("unexepected node : " + node.getNodeName());
91             }
92             node = node.getNextSibling();
93           }
94     }
95     
96     /**
97      * Parse ssl node
98      *
99      * @param node the ssl node
100      */

101     private void handleSSL(Node node)
102     {
103         NamedNodeMap attrs = node.getAttributes();
104         this.sslEnabled = Boolean.valueOf(attrs.getNamedItem("enabled").getNodeValue()).booleanValue();
105         this.sslPort = Integer.parseInt(attrs.getNamedItem("port").getNodeValue());
106         this.sslPassword = attrs.getNamedItem("password").getNodeValue();
107     }
108     
109     /**
110      * Parse database node
111      *
112      * @param node the database node
113      */

114     private void handleDatabase(Node node)
115     {
116         node = node.getFirstChild();
117         while(node != null)
118         {
119           if(node.getNodeType() == Node.ELEMENT_NODE)
120           {
121               if(node.getNodeName().equals("jdbc"))
122               {
123                 this.dbDriver = node.getAttributes().getNamedItem("driver").getNodeValue();
124                 this.dbUrl = node.getAttributes().getNamedItem("url").getNodeValue();
125                 this.dbLogin = node.getAttributes().getNamedItem("login").getNodeValue();
126                 this.dbPassword = node.getAttributes().getNamedItem("password").getNodeValue();
127               }
128               else if(node.getNodeName().equals("dblayer"))
129                 this.dbLayer = node.getAttributes().getNamedItem("class").getNodeValue();
130               else if(node.getNodeName().equals("pool"))
131               {
132                 this.dbPoolInitialSize = Integer.parseInt(node.getAttributes().getNamedItem("initialSize").getNodeValue());
133                 this.dbPoolMaxActive = Integer.parseInt(node.getAttributes().getNamedItem("maxActive").getNodeValue());
134                 this.dbPoolMaxIdle = Integer.parseInt(node.getAttributes().getNamedItem("maxIdle").getNodeValue());
135                 this.dbPoolMinIdle = Integer.parseInt(node.getAttributes().getNamedItem("minIdle").getNodeValue());
136                 this.dbPoolMaxWait = Integer.parseInt(node.getAttributes().getNamedItem("maxWait").getNodeValue());
137               }
138               else
139                 Logging.getLogger().warning("unexepected node : " + node.getNodeName());
140           }
141           node = node.getNextSibling();
142         }
143     }
144
145     /**
146      * Parse store node
147      *
148      * @param node the store node
149      */

150     private void handleStore(Node node)
151     {
152         this.storeBackend = node.getAttributes().getNamedItem("backend").getNodeValue();
153     }
154     
155     /**
156      * Parse authenticator node
157      *
158      * @param node the authenticator node
159      */

160     private void handleAuthenticator(Node node)
161     {
162         this.authenticatorClass = node.getAttributes().getNamedItem("class").getNodeValue();
163     }
164     
165     //-- getters
166

167     /**
168      * Get server port
169      *
170      * @return the server port
171      */

172     public int getPort()
173     {
174         return this.port;
175     }
176     
177     /**
178      * Get ssl parameters
179      *
180      * @return true if SSL is enabled
181      */

182     public boolean isSslEnabled()
183     {
184         return this.sslEnabled;
185     }
186     
187     /**
188      * Get SSL port
189      *
190      * @return the SSL port
191      */

192     public int getSslPort()
193     {
194         return this.sslPort;
195     }
196     
197     /**
198      * Get SSL password
199      *
200      * @return the SSL password
201      */

202     protected String JavaDoc getSslPassword()
203     {
204         return this.sslPassword;
205     }
206     
207     /**
208      * Get JDBC driver
209      *
210      * @return the JDBC driver
211      */

212     public String JavaDoc getDbDriver()
213     {
214         return this.dbDriver;
215     }
216     
217     /**
218      * Get JDBC url
219      *
220      * @return the JDBC url
221      */

222     public String JavaDoc getDbUrl()
223     {
224         return this.dbUrl;
225     }
226
227     /**
228      * Get JDBC login
229      *
230      * @return the JDBC login
231      */

232     public String JavaDoc getDbLogin()
233     {
234         return this.dbLogin;
235     }
236
237     /**
238      * Get JDBC password
239      *
240      * @return the JDBC password
241      */

242     public String JavaDoc getDbPassword()
243     {
244         return this.dbPassword;
245     }
246     
247     /**
248      * Get the DatabaseAbstractionLayer used
249      *
250      * @return the concrete DatabaseLayer
251      */

252     public String JavaDoc getDbLayer()
253     {
254         return this.dbLayer;
255     }
256     
257     /**
258      * Get the pool initial size
259      *
260      * @return the initial size
261      */

262     public int getDbPoolInitialSize()
263     {
264         return this.dbPoolInitialSize;
265     }
266
267     /**
268      * Get the pool maximum number of active connections
269      *
270      * @return maxActive
271      */

272     public int getDbPoolMaxActive()
273     {
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     {
284         return this.dbPoolMaxIdle;
285     }
286
287     /**
288      * Get the pool minimun number of idle connections
289      *
290      * @return minIdle
291      */

292     public int getDbPoolMinIdle()
293     {
294         return this.dbPoolMinIdle;
295     }
296
297     /**
298      * Get the maximum time (in ms) to wait before throwing an error
299      * if no connection is free in the pool
300      *
301      * @return maxWait
302      */

303     public long getDbPoolMaxWait()
304     {
305         return this.dbPoolMaxWait;
306     }
307     
308     /**
309      * Get the backend used for Store
310      * (should be database, or ldap later)
311      *
312      * @return the backend for store
313      */

314     public String JavaDoc getStoreBackend()
315     {
316         return this.storeBackend;
317     }
318     
319     /**
320      * Get the authenticator class to use
321      *
322      * @return the class
323      */

324     public String JavaDoc getAuthenticatorClass()
325     {
326         return this.authenticatorClass;
327     }
328 }
Popular Tags