bugfix: no classcast-exception at topics to article anymore
[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 import  mir.storage.StorageObjectException;
10
11 /**
12  * Title:        Mir
13  * Description:  Class that allows access to all Mir
14  *               config values
15  * Copyright:    Copyright (c) 2001
16  * Company:      Indymedia
17  * @author       mh <heckmann@hbe.ca>
18  * @version 0.1
19  */
20
21
22 /**
23  * This class is a layer above the Configuration
24  * It manages access to config variables that are
25  * both generated on the fly and found in the config file.
26  */
27
28 public class MirConfig extends Configuration {
29
30   private static HashMap configHash = new HashMap();
31   private static HashMap brokerHash = new HashMap();
32   private static int      instances=0;
33
34   /**
35    * Initializes Configuration hash that contains all values.
36    * loads the properties-file and any other values
37    * @param uri, the root Uri of the install
38    * @param home, The absolute path if the install root.
39    * @param name, The name of the servlet (usually "Mir")
40    * @param confName, the name of the config file to load.
41    */
42   public static void initConfig(String home, String uri, String name, String confName) {
43     initConfResource(confName);
44
45     configHash.put("Home", home);
46     configHash.put("RootUri", uri);
47
48     Enumeration resKeys = getResourceKeys();
49     while(resKeys.hasMoreElements()) {
50       String keyNm = (String)resKeys.nextElement();
51       configHash.put(keyNm, getProperty(keyNm));
52     }
53   }
54   /**
55    * Returns the property asked for by pulling it out a HashMap
56    * @param a String containing the property name (key)
57    * @return a String containing the prop. value
58    */
59   public static void setServletName(String servletName) {
60     configHash.put("ServletName",servletName);
61   }
62   
63   /**
64    * Returns the property asked for by pulling it out a HashMap
65    * @param a String containing the property name (key)
66    * @return a String containing the prop. value
67    */
68   public static String getProp(String propName) {
69     return (String)configHash.get(propName);
70   }
71
72   /**
73    * Returns the property asked for by pulling it out a HashMap and
74    * appending it to configproperty "Home"
75    * @param a String containing the property name (key)
76    * @return a String containing the prop.value
77    */
78   public static String getPropWithHome(String propName) {
79     return (String)configHash.get("Home") +
80            (String)configHash.get(propName);
81   }
82
83   public static void addBroker(String driver, String URL) throws StorageObjectException {
84
85     String username,passwd,min,max,log,reset;
86
87     if(!brokerHash.containsKey("Pool.broker")){
88       username=getProp("Database.Username");
89       passwd=getProp("Database.Password");
90       min=getProp("Database.poolMin");
91       max=getProp("Database.poolMax");
92       log=getProp("Home") + configHash.get("Database.PoolLog");
93       reset=getProp("Database.poolResetTime");
94
95       try{
96         System.err.println("-- making Broker for -"
97                             +driver+" - " +URL
98                             + " log " + log + " user "
99                             + username + " pass: " + passwd);
100
101         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
102                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
103         if (br!=null){
104           instances++;
105           brokerHash.put("Pool.broker",br);
106         }
107       } catch(IOException e){
108         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
109         throw new StorageObjectException(e.toString());
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 }