1 25 29 package org.jresearch.gossip.configuration; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.sql.Connection ; 34 import java.sql.PreparedStatement ; 35 import java.sql.ResultSet ; 36 import java.sql.SQLException ; 37 import java.sql.Statement ; 38 import java.util.Locale ; 39 import java.util.Properties ; 40 41 import javax.servlet.ServletContext ; 42 import javax.sql.DataSource ; 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 56 public class Configurator implements IConst { 57 58 private static Configurator ourInstance; 59 60 private static Object lock = new Object (); 61 62 private Properties props; 63 64 private DataSource dataSource; 65 66 private DbDriver dbDriver; 67 68 private Configurator() { 69 this.props = new Properties (); 70 this.dbDriver = DbDriver.getInstance(); 71 72 } 73 74 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 103 public String get(String 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 123 public int getInt(String 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 143 public boolean getBoolean(String 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 157 public Locale getLocale(String 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 (this.props.getProperty(key)); 164 } 165 166 178 public void load(ServletContext servletContext) throws SQLException , 179 IOException , SystemException { 180 Logger log = JGossipLog.getInstance().getAppLogger(); 181 if (log.isDebugEnabled()) { 182 log.debug("try to load app.properties"); 183 } 184 185 InputStream 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 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 conn = this.dataSource.getConnection(); 205 206 PreparedStatement st = conn.prepareStatement(dbDriver.getQueries() 207 .getForumQueries().getSql_GET_CONSTANTS()); 208 Statement st2 = conn.createStatement(); 209 ResultSet rs = null; 210 211 try { 212 rs = (ResultSet ) 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 st2.executeUpdate(dbDriver.getQueries().getForumQueries() 224 .getSql_DELETE_ALL_ENTRIES()); 225 } catch (SQLException 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 252 public void reload(ServletContext servletContext) throws SystemException, 253 SQLException , IOException { 254 this.props = new Properties (); 255 load(servletContext); 256 } 257 258 263 public void setDataSource(DataSource source) { 264 dataSource = source; 265 } 266 }
| Popular Tags
|