2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
34 import java.util.Enumeration;
35 import java.util.HashMap;
38 import javax.servlet.ServletContext;
40 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
41 import mir.storage.DatabaseAdaptor;
42 import mir.storage.StorageObjectExc;
43 import mir.storage.StorageObjectFailure;
45 import com.codestudio.util.JDBCPool;
46 import com.codestudio.util.JDBCPoolMetaData;
47 import com.codestudio.util.SQLManager;
51 * Description: Class that allows access to all Mir
53 * Copyright: Copyright (c) 2001
55 * @author mh <heckmann@hbe.ca>
61 * This class is a layer above the Configuration
62 * It manages access to config variables that are
63 * both generated on the fly and found in the config file.
66 public class MirConfig extends Configuration {
68 private static HashMap configHash = null;
69 private static HashMap brokerHash = new HashMap();
70 private static int instances=0;
73 * Initializes Configuration hash that contains all values.
74 * loads the properties-file and any other values
75 * @param uri, the root Uri of the install
76 * @param home, The absolute path if the install root.
77 * @param name, The name of the servlet (usually "Mir")
78 * @param confName, the name of the config file to load.
80 public static synchronized void initConfig(ServletContext ctx, String ctxPath,
81 String name, String confName) {
83 initConfResource(ctx.getRealPath("/WEB-INF/")+"/"+confName);
85 configHash = new HashMap();
87 configHash.put("Home", ctx.getRealPath("/WEB-INF/")+"/");
88 configHash.put("ServletContext", ctx);
89 configHash.put("RootUri", ctxPath);
91 Enumeration resKeys = getResourceKeys();
92 while(resKeys.hasMoreElements()) {
93 String keyNm = (String)resKeys.nextElement();
94 configHash.put(keyNm, getProperty(keyNm));
98 * Returns the property asked for by pulling it out a HashMap
99 * @param a String containing the property name (key)
100 * @return a String containing the prop. value
102 public static void setServletName(String servletName) {
103 configHash.put("ServletName",servletName);
107 * Returns the property asked for by pulling it out a HashMap
108 * @param a String containing the property name (key)
109 * @return a String containing the prop. value
111 public static String getProp(String propName) {
112 String result = (String)configHash.get(propName);
115 throw new ConfigException("config property '"+propName+"' not available!");
121 * Returns the property asked for by pulling it out a HashMap and
122 * appending it to configproperty "Home"
123 * @param a String containing the property name (key)
124 * @return a String containing the prop.value
126 public static String getPropWithHome(String propName) {
127 return getProp("Home") + getProp(propName);
131 * Returns the property asked for iin raw Object form by
132 * pulling it out a HashMap
133 * @param a String containing the property name (key)
134 * @return an Object containing the prop.value
136 public static Object getPropAsObject(String propName) {
137 return configHash.get(propName);
140 public static void initDbPool () throws StorageObjectExc, StorageObjectFailure {
141 if (configHash == null) {
142 throw new StorageObjectExc("MirConfig -- Trying initialize "+
143 "DB pool when system not yet "+
146 String dbUser=getProp("Database.Username");
147 String dbPassword=getProp("Database.Password");
148 String dbHost=getProp("Database.Host");
149 String dbAdapName=getProp("Database.Adaptor");
150 DatabaseAdaptor adaptor;
152 adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
153 } catch (Exception e) {
154 throw new StorageObjectFailure("Could not load DB adapator.",e);
159 dbDriver = adaptor.getDriver();
160 dbUrl =adaptor.getURL(dbUser,dbPassword, dbHost);
161 } catch (PropertiesConfigExc e) {
162 throw new StorageObjectFailure(e);
165 System.out.println("adding Broker with: " +dbDriver+":"+dbUrl );
166 addBroker( dbDriver, dbUrl);
169 public static void addBroker(String driver, String URL)
170 throws StorageObjectFailure, StorageObjectExc {
172 if (configHash == null) {
173 throw new StorageObjectExc("MirConfig -- Trying initialize "+
174 "DB pool when system not yet "+
177 String username,passwd,min,max,log,reset,dbname,dblogfile;
179 if(!brokerHash.containsKey("Pool.broker")){
180 username=getProp("Database.Username");
181 passwd=getProp("Database.Password");
182 min=getProp("Database.poolMin");
183 max=getProp("Database.poolMax");
184 dbname=getProp("Database.Name");
185 log=getProp("Home")+ configHash.get("Database.PoolLog");
186 reset=getProp("Database.poolResetTime");
187 dblogfile=getPropWithHome("Database.Logfile");
189 System.err.println("-- making Broker for -"
191 + " log " + log + " user "
192 + username + " pass: " + passwd);
194 JDBCPoolMetaData meta = new JDBCPoolMetaData();
195 meta.setDbname(dbname);
196 meta.setDriver(driver);
198 meta.setUserName(username);
199 meta.setPassword(passwd);
200 meta.setJNDIName("mir");
201 meta.setMaximumSize(Integer.parseInt(max));
202 meta.setMinimumSize(Integer.parseInt(min));
203 meta.setPoolPreparedStatements(false);
204 meta.setCacheEnabled(false);
205 meta.setCacheSize(15);
206 meta.setDebugging(false);
207 meta.setLogFile(dblogfile+".pool");
209 JDBCPool pool = SQLManager.getInstance().createPool(meta);
213 brokerHash.put("Pool.broker",pool);
222 public void finalize(){
226 } catch (Throwable t) {}
229 public static Map allSettings() {