KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > configuration > Configurator


1 /*
2  * $$Id: Configurator.java,v 1.3 2005/06/07 12:32:29 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Oct 29, 2003
27  *
28  */

29 package org.jresearch.gossip.configuration;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Statement JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 import javax.servlet.ServletContext JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 import org.apache.log.Logger;
45 import org.jresearch.gossip.IConst;
46 import org.jresearch.gossip.dao.drivers.DbDriver;
47 import org.jresearch.gossip.exception.ConfiguratorException;
48 import org.jresearch.gossip.exception.SystemException;
49 import org.jresearch.gossip.log.avalon.JGossipLog;
50
51 /**
52  * DOCUMENT ME!
53  *
54  * @author dbelov
55  */

56 public class Configurator implements IConst {
57
58     private static Configurator ourInstance;
59
60     private static Object JavaDoc lock = new Object JavaDoc();
61
62     private Properties JavaDoc props;
63
64     private DataSource JavaDoc dataSource;
65
66     private DbDriver dbDriver;
67
68     private Configurator() {
69         this.props = new Properties JavaDoc();
70         this.dbDriver = DbDriver.getInstance();
71
72     }
73
74     /**
75      * DOCUMENT ME!
76      *
77      * @return DOCUMENT ME!
78      * @throws SystemException
79      */

80     public static Configurator getInstance() {
81         if (ourInstance == null) {
82             synchronized (lock) {
83                 if (ourInstance == null) {
84                     ourInstance = new Configurator();
85                 }
86             }
87         }
88
89         return ourInstance;
90     }
91
92     /**
93      * DOCUMENT ME!
94      *
95      * @param key
96      * DOCUMENT ME!
97      *
98      * @return DOCUMENT ME!
99      *
100      * @throws ConfiguratorException
101      * DOCUMENT ME!
102      */

103     public String JavaDoc get(String JavaDoc key) throws ConfiguratorException {
104         if (this.props.getProperty(key) == null) {
105             throw new ConfiguratorException("Parameter by key=" + key
106                     + " not found");
107         }
108
109         return this.props.getProperty(key);
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @param key
116      * DOCUMENT ME!
117      *
118      * @return DOCUMENT ME!
119      *
120      * @throws ConfiguratorException
121      * DOCUMENT ME!
122      */

123     public int getInt(String JavaDoc key) throws ConfiguratorException {
124         if (this.props.getProperty(key) == null) {
125             throw new ConfiguratorException("Parameter by key=" + key
126                     + " not found");
127         }
128
129         return Integer.parseInt(this.props.getProperty(key));
130     }
131
132     /**
133      *
134      *
135      * @param key
136      *
137      *
138      * @return true if by key stored String 'Y' or 'y', otherwise return false
139      *
140      * @throws ConfiguratorException
141      *
142      */

143     public boolean getBoolean(String JavaDoc key) throws ConfiguratorException {
144         if (this.props.getProperty(key) == null) {
145             throw new ConfiguratorException("Parameter by key=" + key
146                     + " not found");
147         }
148
149         return this.props.getProperty(key).equalsIgnoreCase(VALUES.TRUE);
150     }
151
152     /**
153      * @param key
154      * @return
155      * @throws ConfiguratorException
156      */

157     public Locale JavaDoc getLocale(String JavaDoc key) throws ConfiguratorException {
158         if (this.props.getProperty(key) == null) {
159             throw new ConfiguratorException("Parameter by key=" + key
160                     + " not found");
161         }
162
163         return new Locale JavaDoc(this.props.getProperty(key));
164     }
165
166     /**
167      * DOCUMENT ME!
168      *
169      * @param servletContext
170      * DOCUMENT ME!
171      *
172      * @throws SQLException
173      * DOCUMENT ME!
174      * @throws IOException
175      * DOCUMENT ME!
176      * @throws SystemException
177      */

178     public void load(ServletContext JavaDoc servletContext) throws SQLException JavaDoc,
179             IOException JavaDoc, SystemException {
180         Logger log = JGossipLog.getInstance().getAppLogger();
181         if (log.isDebugEnabled()) {
182             log.debug("try to load app.properties");
183         }
184
185         InputStream JavaDoc is = servletContext
186                 .getResourceAsStream("/WEB-INF/classes/org/jresearch/gossip/resources/app.properties");
187
188         try {
189             this.props.load(is);
190             is.close();
191         } catch (IOException JavaDoc e1) {
192             if (log.isErrorEnabled()) {
193                 log.error("Loading of jGossip's configuration failed "
194                         + e1.getMessage());
195             }
196
197             throw e1;
198         }
199
200         if (log.isDebugEnabled()) {
201             log.debug("try to load configuration from Data Base");
202         }
203
204         Connection JavaDoc conn = this.dataSource.getConnection();
205
206         PreparedStatement JavaDoc st = conn.prepareStatement(dbDriver.getQueries()
207                 .getForumQueries().getSql_GET_CONSTANTS());
208         Statement JavaDoc st2 = conn.createStatement();
209         ResultSet JavaDoc rs = null;
210
211         try {
212             rs = (ResultSet JavaDoc) st.executeQuery();
213
214             while (rs.next()) {
215                 if (log.isDebugEnabled()) {
216                     log.debug(rs.getString("c_name") + " is loaded");
217                 }
218
219                 this.props.put(rs.getString("c_name"), rs.getString("c_value"));
220             }
221
222             // refresh all sessions(drop all entries)
223
st2.executeUpdate(dbDriver.getQueries().getForumQueries()
224                     .getSql_DELETE_ALL_ENTRIES());
225         } catch (SQLException JavaDoc e) {
226             if (log.isErrorEnabled()) {
227                 log.error("Loading of jGossip's configuration failed "
228                         + e.getMessage());
229             }
230
231             throw e;
232         } finally {
233             if (rs != null) {
234                 rs.close();
235             }
236
237             st2.close();
238             st.close();
239             conn.close();
240         }
241     }
242
243     /**
244      * DOCUMENT ME!
245      *
246      * @param servletContext
247      * DOCUMENT ME!
248      * @throws SQLException
249      * @throws IOException
250      * @throws SystemException
251      */

252     public void reload(ServletContext JavaDoc servletContext) throws SystemException,
253             SQLException JavaDoc, IOException JavaDoc {
254         this.props = new Properties JavaDoc();
255         load(servletContext);
256     }
257
258     /**
259      * DOCUMENT ME!
260      *
261      * @param source
262      */

263     public void setDataSource(DataSource JavaDoc source) {
264         dataSource = source;
265     }
266 }
Popular Tags