first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mir / misc / MirConfig.java
1 package  mir.misc;
2
3 import  javax.servlet.http.*;
4 import  java.net.*;
5 import  java.io.*;
6 import  java.util.*;
7 import  java.lang.*;
8 import  mir.storage.StorageObjectException;
9 import  mir.storage.DatabaseAdaptor;
10 import com.codestudio.util.*;
11
12 /**
13  * Title:        Mir
14  * Description:  Class that allows access to all Mir
15  *               config values
16  * Copyright:    Copyright (c) 2001
17  * Company:      Indymedia
18  * @author       mh <heckmann@hbe.ca>
19  * @version 0.1
20  */
21
22
23 /**
24  * This class is a layer above the Configuration
25  * It manages access to config variables that are
26  * both generated on the fly and found in the config file.
27  */
28
29 public class MirConfig extends Configuration {
30
31   private static HashMap configHash = null;
32   private static HashMap brokerHash = new HashMap();
33   private static int      instances=0;
34
35   /**
36    * Initializes Configuration hash that contains all values.
37    * loads the properties-file and any other values
38    * @param uri, the root Uri of the install
39    * @param home, The absolute path if the install root.
40    * @param name, The name of the servlet (usually "Mir")
41    * @param confName, the name of the config file to load.
42    */
43   public static synchronized void initConfig(String home, String uri, 
44                                             String name, String confName) {
45     initConfResource(confName);
46     configHash = new HashMap();
47     configHash.put("Home", home);
48     configHash.put("RootUri", uri);
49
50     Enumeration resKeys = getResourceKeys();
51     while(resKeys.hasMoreElements()) {
52       String keyNm = (String)resKeys.nextElement();
53       configHash.put(keyNm, getProperty(keyNm));
54     }
55   }
56   /**
57    * Returns the property asked for by pulling it out a HashMap
58    * @param a String containing the property name (key)
59    * @return a String containing the prop. value
60    */
61   public static void setServletName(String servletName) {
62     configHash.put("ServletName",servletName);
63   }
64
65   /**
66    * Returns the property asked for by pulling it out a HashMap
67    * @param a String containing the property name (key)
68    * @return a String containing the prop. value
69    */
70   public static String getProp(String propName) {
71     return (String)configHash.get(propName);
72   }
73
74   /**
75    * Returns the property asked for by pulling it out a HashMap and
76    * appending it to configproperty "Home"
77    * @param a String containing the property name (key)
78    * @return a String containing the prop.value
79    */
80   public static String getPropWithHome(String propName) {
81     return (String)configHash.get("Home") +
82            (String)configHash.get(propName);
83   }
84
85   public static void initDbPool () throws StorageObjectException {
86     if (configHash == null) {
87         throw new StorageObjectException("MirConfig -- Trying initialize "+
88                                         "DB pool when system not yet "+
89                                         "configured");
90     }
91     String dbUser=getProp("Database.Username");
92     String dbPassword=getProp("Database.Password");
93     String dbHost=getProp("Database.Host");
94     String dbAdapName=getProp("Database.Adaptor");
95     DatabaseAdaptor adaptor;
96     try {
97       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
98     } catch (Exception e) {
99       throw new StorageObjectException("Could not load DB adapator: "+
100                                         e.toString());
101     }
102     String dbDriver=adaptor.getDriver();
103     String dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
104     System.out.println("adding Broker with: " +dbDriver+":"+dbUrl );
105     addBroker( dbDriver, dbUrl);
106   }
107
108   public static void addBroker(String driver, String URL)
109     throws StorageObjectException {
110
111     if (configHash == null) {
112         throw new StorageObjectException("MirConfig -- Trying initialize "+
113                                         "DB pool when system not yet "+
114                                         "configured");
115     }
116     String username,passwd,min,max,log,reset,dbname,dblogfile;
117
118     if(!brokerHash.containsKey("Pool.broker")){
119       username=getProp("Database.Username");
120       passwd=getProp("Database.Password");
121       min=getProp("Database.poolMin");
122       max=getProp("Database.poolMax");
123       dbname=getProp("Database.Name");
124       log=getProp("Home")+ configHash.get("Database.PoolLog");
125       reset=getProp("Database.poolResetTime");
126       dblogfile=getPropWithHome("Database.Logfile");
127
128       System.err.println("-- making Broker for -"
129                           +driver+" - " +URL
130                           + " log " + log + " user "
131                           + username + " pass: " + passwd);
132
133       JDBCPoolMetaData meta = new JDBCPoolMetaData();
134       meta.setDbname(dbname);
135       meta.setDriver(driver);
136       meta.setURL(URL);
137       meta.setUserName(username);
138       meta.setPassword(passwd);
139       meta.setJNDIName("mir");
140       meta.setMaximumSize(Integer.parseInt(max));
141       meta.setMinimumSize(Integer.parseInt(min));
142       meta.setCacheEnabled(false);
143       meta.setCacheSize(15);
144       meta.setDebugging(false);
145       meta.setLogFile(dblogfile+".pool");
146
147       JDBCPool pool = SQLManager.getInstance().createPool(meta);
148
149       if (pool!=null){
150         instances++;
151         brokerHash.put("Pool.broker",pool);
152       }
153
154     } // end if
155   }
156
157   /**
158    * Finalize method
159    */
160   public void finalize(){
161     instances --;
162     try {
163       super.finalize();
164     } catch (Throwable t) {}
165   }
166
167 }