a3e9199168cd644828e0a17e8611261a96b573f6
[mir.git] / source / mir / misc / MirConfig.java
1 package  mir.misc;
2
3 import  java.net.*;
4 import  java.io.*;
5 import  java.util.*;
6 import  java.lang.*;
7
8 import  javax.servlet.ServletContext;
9 import  javax.servlet.http.*;
10
11 import  mir.storage.StorageObjectException;
12 import  mir.storage.DatabaseAdaptor;
13 import com.codestudio.util.*;
14
15 /**
16  * Title:        Mir
17  * Description:  Class that allows access to all Mir
18  *               config values
19  * Copyright:    Copyright (c) 2001
20  * Company:      Indymedia
21  * @author       mh <heckmann@hbe.ca>
22  * @version 0.1
23  */
24
25
26 /**
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.
30  */
31
32 public class MirConfig extends Configuration {
33
34   private static HashMap configHash = null;
35   private static HashMap brokerHash = new HashMap();
36   private static int      instances=0;
37
38   /**
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.
45    */
46   public static synchronized void initConfig(ServletContext ctx, String uri,
47                                             String name, String confName) {
48     initConfResource(confName);
49     configHash = new HashMap();
50
51     configHash.put("Home", ctx.getRealPath("/"));
52     configHash.put("ServletContext", ctx);
53     configHash.put("RootUri", uri);
54
55     Enumeration resKeys = getResourceKeys();
56     while(resKeys.hasMoreElements()) {
57       String keyNm = (String)resKeys.nextElement();
58       configHash.put(keyNm, getProperty(keyNm));
59     }
60   }
61   /**
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
65    */
66   public static void setServletName(String servletName) {
67     configHash.put("ServletName",servletName);
68   }
69
70   /**
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
74    */
75   public static String getProp(String propName) {
76     String result = (String)configHash.get(propName);
77
78     if (result==null)
79       throw new ConfigException("config property '"+propName+"' not available!");
80
81
82
83     return result;
84   }
85
86   /**
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
91    */
92   public static String getPropWithHome(String propName) {
93     return getProp("Home") + getProp(propName);
94   }
95
96   /**
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
101    */
102   public static Object getPropAsObject(String propName) {
103     return configHash.get(propName);
104   }
105
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 "+
110                                         "configured");
111     }
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;
117     try {
118       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
119     } catch (Exception e) {
120       throw new StorageObjectException("Could not load DB adapator: "+
121                                         e.toString());
122     }
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);
127   }
128
129   public static void addBroker(String driver, String URL)
130     throws StorageObjectException {
131
132     if (configHash == null) {
133         throw new StorageObjectException("MirConfig -- Trying initialize "+
134                                         "DB pool when system not yet "+
135                                         "configured");
136     }
137     String username,passwd,min,max,log,reset,dbname,dblogfile;
138
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");
148
149       System.err.println("-- making Broker for -"
150                           +driver+" - " +URL
151                           + " log " + log + " user "
152                           + username + " pass: " + passwd);
153
154       JDBCPoolMetaData meta = new JDBCPoolMetaData();
155       meta.setDbname(dbname);
156       meta.setDriver(driver);
157       meta.setURL(URL);
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");
168
169       JDBCPool pool = SQLManager.getInstance().createPool(meta);
170
171       if (pool!=null){
172         instances++;
173         brokerHash.put("Pool.broker",pool);
174       }
175
176     } // end if
177   }
178
179   /**
180    * Finalize method
181    */
182   public void finalize(){
183     instances --;
184     try {
185       super.finalize();
186     } catch (Throwable t) {}
187   }
188
189 }