8 import javax.servlet.ServletContext;
9 import javax.servlet.http.*;
11 import mir.storage.StorageObjectException;
12 import mir.storage.DatabaseAdaptor;
13 import com.codestudio.util.*;
17 * Description: Class that allows access to all Mir
19 * Copyright: Copyright (c) 2001
21 * @author mh <heckmann@hbe.ca>
27 * This class is a layer above the Configuration
28 * It manages access to config variables that are
29 * both generated on the fly and found in the config file.
32 public class MirConfig extends Configuration {
34 private static HashMap configHash = null;
35 private static HashMap brokerHash = new HashMap();
36 private static int instances=0;
39 * Initializes Configuration hash that contains all values.
40 * loads the properties-file and any other values
41 * @param uri, the root Uri of the install
42 * @param home, The absolute path if the install root.
43 * @param name, The name of the servlet (usually "Mir")
44 * @param confName, the name of the config file to load.
46 public static synchronized void initConfig(ServletContext ctx, String uri,
47 String name, String confName) {
48 initConfResource(confName);
49 configHash = new HashMap();
51 configHash.put("Home", ctx.getRealPath("/"));
52 configHash.put("ServletContext", ctx);
53 configHash.put("RootUri", uri);
55 Enumeration resKeys = getResourceKeys();
56 while(resKeys.hasMoreElements()) {
57 String keyNm = (String)resKeys.nextElement();
58 configHash.put(keyNm, getProperty(keyNm));
62 * Returns the property asked for by pulling it out a HashMap
63 * @param a String containing the property name (key)
64 * @return a String containing the prop. value
66 public static void setServletName(String servletName) {
67 configHash.put("ServletName",servletName);
71 * Returns the property asked for by pulling it out a HashMap
72 * @param a String containing the property name (key)
73 * @return a String containing the prop. value
75 public static String getProp(String propName) {
76 String result = (String)configHash.get(propName);
79 throw new ConfigException("config property '"+propName+"' not available!");
87 * Returns the property asked for by pulling it out a HashMap and
88 * appending it to configproperty "Home"
89 * @param a String containing the property name (key)
90 * @return a String containing the prop.value
92 public static String getPropWithHome(String propName) {
93 return getProp("Home") + getProp(propName);
97 * Returns the property asked for iin raw Object form by
98 * pulling it out a HashMap
99 * @param a String containing the property name (key)
100 * @return an Object containing the prop.value
102 public static Object getPropAsObject(String propName) {
103 return configHash.get(propName);
106 public static void initDbPool () throws StorageObjectException {
107 if (configHash == null) {
108 throw new StorageObjectException("MirConfig -- Trying initialize "+
109 "DB pool when system not yet "+
112 String dbUser=getProp("Database.Username");
113 String dbPassword=getProp("Database.Password");
114 String dbHost=getProp("Database.Host");
115 String dbAdapName=getProp("Database.Adaptor");
116 DatabaseAdaptor adaptor;
118 adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
119 } catch (Exception e) {
120 throw new StorageObjectException("Could not load DB adapator: "+
123 String dbDriver=adaptor.getDriver();
124 String dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
125 System.out.println("adding Broker with: " +dbDriver+":"+dbUrl );
126 addBroker( dbDriver, dbUrl);
129 public static void addBroker(String driver, String URL)
130 throws StorageObjectException {
132 if (configHash == null) {
133 throw new StorageObjectException("MirConfig -- Trying initialize "+
134 "DB pool when system not yet "+
137 String username,passwd,min,max,log,reset,dbname,dblogfile;
139 if(!brokerHash.containsKey("Pool.broker")){
140 username=getProp("Database.Username");
141 passwd=getProp("Database.Password");
142 min=getProp("Database.poolMin");
143 max=getProp("Database.poolMax");
144 dbname=getProp("Database.Name");
145 log=getProp("Home")+ configHash.get("Database.PoolLog");
146 reset=getProp("Database.poolResetTime");
147 dblogfile=getPropWithHome("Database.Logfile");
149 System.err.println("-- making Broker for -"
151 + " log " + log + " user "
152 + username + " pass: " + passwd);
154 JDBCPoolMetaData meta = new JDBCPoolMetaData();
155 meta.setDbname(dbname);
156 meta.setDriver(driver);
158 meta.setUserName(username);
159 meta.setPassword(passwd);
160 meta.setJNDIName("mir");
161 meta.setMaximumSize(Integer.parseInt(max));
162 meta.setMinimumSize(Integer.parseInt(min));
163 meta.setPoolPreparedStatements(false);
164 meta.setCacheEnabled(false);
165 meta.setCacheSize(15);
166 meta.setDebugging(false);
167 meta.setLogFile(dblogfile+".pool");
169 JDBCPool pool = SQLManager.getInstance().createPool(meta);
173 brokerHash.put("Pool.broker",pool);
182 public void finalize(){
186 } catch (Throwable t) {}