d6cd48599a120e6e44d5685e366f5f0dad2537f3
[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  com.javaexchange.dbConnectionBroker.*;
9
10 /**
11  * Title:        Mir
12  * Description:  Class that allows access to all Mir
13  *               config values
14  * Copyright:    Copyright (c) 2001
15  * Company:      Indymedia
16  * @author       mh <heckmann@hbe.ca>
17  * @version 0.1
18  */
19
20
21 /**
22  * This class is a layer above the Configuration
23  * It manages access to config variables that are
24  * both generated on the fly and found in the config file.
25  */
26
27 public class MirConfig extends Configuration {
28
29   private static HashMap configHash = new HashMap();
30   private static HashMap brokerHash = new HashMap();
31   private static int      instances=0;
32
33   /**
34    * Initializes Configuration hash that contains all values.
35    * loads the properties-file and any other values
36    * @param uri, the root Uri of the install
37    * @param home, The absolute path if the install root.
38    * @param name, The name of the servlet (usually "Mir")
39    * @param confName, the name of the config file to load.
40    */
41   public static void initConfig(String home, String uri, String name, String confName) {
42     initConfResource(confName);
43
44     configHash.put("Home", home);
45     configHash.put("RootUri", uri);
46
47     Enumeration resKeys = getResourceKeys();
48     while(resKeys.hasMoreElements()) {
49       String keyNm = (String)resKeys.nextElement();
50       configHash.put(keyNm, getProperty(keyNm));
51     }
52   }
53   /**
54    * Returns the property asked for by pulling it out a HashMap
55    * @param a String containing the property name (key)
56    * @return a String containing the prop. value
57    */
58   public static void setServletName(String servletName) {
59     configHash.put("ServletName",servletName);
60   }
61   
62   /**
63    * Returns the property asked for by pulling it out a HashMap
64    * @param a String containing the property name (key)
65    * @return a String containing the prop. value
66    */
67   public static String getProp(String propName) {
68     return (String)configHash.get(propName);
69   }
70
71   /**
72    * Returns the property asked for by pulling it out a HashMap and
73    * appending it to configproperty "Home"
74    * @param a String containing the property name (key)
75    * @return a String containing the prop.value
76    */
77   public static String getPropWithHome(String propName) {
78     return (String)configHash.get("Home") +
79            (String)configHash.get(propName);
80   }
81
82   public static void addBroker(String driver, String URL){
83
84     String username,passwd,min,max,log,reset;
85
86     if(!brokerHash.containsKey("Pool.broker")){
87       username=getProp("Database.Username");
88       passwd=getProp("Database.Password");
89       min=getProp("Database.poolMin");
90       max=getProp("Database.poolMax");
91       log=getProp("Home") + configHash.get("Database.PoolLog");
92       reset=getProp("Database.poolResetTime");
93
94       try{
95         System.err.println("-- making Broker for -"
96                             +driver+" - " +URL
97                             + " log " + log + " user "
98                             + username + " pass: " + passwd);
99
100         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
101                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
102         if (br!=null){
103           instances++;
104           brokerHash.put("Pool.broker",br);
105         } else {
106             throw new Exception();
107         }
108       } catch(Exception e){
109         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
110       }
111     } // end if
112   }
113
114   /**
115    * Liefert DBConnectionBroker einer Configuration zurueck
116    * @param confFilename
117    * @return DBConnectionBroker
118    */
119   public static DbConnectionBroker getBroker() {
120     DbConnectionBroker broker=null;
121     broker=(DbConnectionBroker)brokerHash.get("Pool.broker");
122     if (broker==null) {
123       System.err.println("Konnte kein ConnectionPoolBroker initialisiert werden");
124     }
125     return broker;
126   }
127
128   /**
129    * Liefert Anzahl der Instantiierten DBConnectionBroker zurueck
130    * @return
131    */
132   public static int getBrokerInstances() {
133     return instances;
134   }
135
136   public static DbConnectionBroker getBrokerInfo(){
137     return (DbConnectionBroker)brokerHash.get("Pool.broker");
138   }
139
140   /**
141    * Finalize method
142    */
143   public void finalize(){
144     instances --;
145     try {
146       super.finalize();
147     } catch (Throwable t) {}
148   }
149
150 }