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 return (String)configHash.get(propName);
80 * Returns the property asked for by pulling it out a HashMap and
81 * appending it to configproperty "Home"
82 * @param a String containing the property name (key)
83 * @return a String containing the prop.value
85 public static String getPropWithHome(String propName) {
86 return (String)configHash.get("Home") +
87 (String)configHash.get(propName);
91 * Returns the property asked for iin raw Object form by
92 * pulling it out a HashMap
93 * @param a String containing the property name (key)
94 * @return an Object containing the prop.value
96 public static Object getPropAsObject(String propName) {
97 return configHash.get(propName);
100 public static void initDbPool () throws StorageObjectException {
101 if (configHash == null) {
102 throw new StorageObjectException("MirConfig -- Trying initialize "+
103 "DB pool when system not yet "+
106 String dbUser=getProp("Database.Username");
107 String dbPassword=getProp("Database.Password");
108 String dbHost=getProp("Database.Host");
109 String dbAdapName=getProp("Database.Adaptor");
110 DatabaseAdaptor adaptor;
112 adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
113 } catch (Exception e) {
114 throw new StorageObjectException("Could not load DB adapator: "+
117 String dbDriver=adaptor.getDriver();
118 String dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
119 System.out.println("adding Broker with: " +dbDriver+":"+dbUrl );
120 addBroker( dbDriver, dbUrl);
123 public static void addBroker(String driver, String URL)
124 throws StorageObjectException {
126 if (configHash == null) {
127 throw new StorageObjectException("MirConfig -- Trying initialize "+
128 "DB pool when system not yet "+
131 String username,passwd,min,max,log,reset,dbname,dblogfile;
133 if(!brokerHash.containsKey("Pool.broker")){
134 username=getProp("Database.Username");
135 passwd=getProp("Database.Password");
136 min=getProp("Database.poolMin");
137 max=getProp("Database.poolMax");
138 dbname=getProp("Database.Name");
139 log=getProp("Home")+ configHash.get("Database.PoolLog");
140 reset=getProp("Database.poolResetTime");
141 dblogfile=getPropWithHome("Database.Logfile");
143 System.err.println("-- making Broker for -"
145 + " log " + log + " user "
146 + username + " pass: " + passwd);
148 JDBCPoolMetaData meta = new JDBCPoolMetaData();
149 meta.setDbname(dbname);
150 meta.setDriver(driver);
152 meta.setUserName(username);
153 meta.setPassword(passwd);
154 meta.setJNDIName("mir");
155 meta.setMaximumSize(Integer.parseInt(max));
156 meta.setMinimumSize(Integer.parseInt(min));
157 meta.setPoolPreparedStatements(false);
158 meta.setCacheEnabled(false);
159 meta.setCacheSize(15);
160 meta.setDebugging(false);
161 meta.setLogFile(dblogfile+".pool");
163 JDBCPool pool = SQLManager.getInstance().createPool(meta);
167 brokerHash.put("Pool.broker",pool);
176 public void finalize(){
180 } catch (Throwable t) {}