a64f4cfb4bf14e476b2f46281e50c1f812b22d0e
[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     return (String)configHash.get(propName);
77   }
78
79   /**
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
84    */
85   public static String getPropWithHome(String propName) {
86     return (String)configHash.get("Home") +
87            (String)configHash.get(propName);
88   }
89
90   /**
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
95    */
96   public static Object getPropAsObject(String propName) {
97     return configHash.get(propName);
98   }
99
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 "+
104                                         "configured");
105     }
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;
111     try {
112       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
113     } catch (Exception e) {
114       throw new StorageObjectException("Could not load DB adapator: "+
115                                         e.toString());
116     }
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);
121   }
122
123   public static void addBroker(String driver, String URL)
124     throws StorageObjectException {
125
126     if (configHash == null) {
127         throw new StorageObjectException("MirConfig -- Trying initialize "+
128                                         "DB pool when system not yet "+
129                                         "configured");
130     }
131     String username,passwd,min,max,log,reset,dbname,dblogfile;
132
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");
142
143       System.err.println("-- making Broker for -"
144                           +driver+" - " +URL
145                           + " log " + log + " user "
146                           + username + " pass: " + passwd);
147
148       JDBCPoolMetaData meta = new JDBCPoolMetaData();
149       meta.setDbname(dbname);
150       meta.setDriver(driver);
151       meta.setURL(URL);
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");
162
163       JDBCPool pool = SQLManager.getInstance().createPool(meta);
164
165       if (pool!=null){
166         instances++;
167         brokerHash.put("Pool.broker",pool);
168       }
169
170     } // end if
171   }
172
173   /**
174    * Finalize method
175    */
176   public void finalize(){
177     instances --;
178     try {
179       super.finalize();
180     } catch (Throwable t) {}
181   }
182
183 }