dbpoolman instead of connectionbroker
[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 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 = new HashMap();
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 void initConfig(String home, String uri, String name, String confName) {
44     initConfResource(confName);
45
46     configHash.put("Home", home);
47     configHash.put("RootUri", uri);
48
49     Enumeration resKeys = getResourceKeys();
50     while(resKeys.hasMoreElements()) {
51       String keyNm = (String)resKeys.nextElement();
52       configHash.put(keyNm, getProperty(keyNm));
53     }
54   }
55   /**
56    * Returns the property asked for by pulling it out a HashMap
57    * @param a String containing the property name (key)
58    * @return a String containing the prop. value
59    */
60   public static void setServletName(String servletName) {
61     configHash.put("ServletName",servletName);
62   }
63   
64   /**
65    * Returns the property asked for by pulling it out a HashMap
66    * @param a String containing the property name (key)
67    * @return a String containing the prop. value
68    */
69   public static String getProp(String propName) {
70     return (String)configHash.get(propName);
71   }
72
73   /**
74    * Returns the property asked for by pulling it out a HashMap and
75    * appending it to configproperty "Home"
76    * @param a String containing the property name (key)
77    * @return a String containing the prop.value
78    */
79   public static String getPropWithHome(String propName) {
80     return (String)configHash.get("Home") +
81            (String)configHash.get(propName);
82   }
83   
84   public static void addBroker(String driver, String URL) throws StorageObjectException {
85
86     String username,passwd,min,max,log,reset;
87
88     if(!brokerHash.containsKey("Pool.broker")){
89       username=getProp("Database.Username");
90       passwd=getProp("Database.Password");
91       min=getProp("Database.poolMin");
92       max=getProp("Database.poolMax");
93       log=getProp("Home") + configHash.get("Database.PoolLog");
94       reset=getProp("Database.poolResetTime");
95
96       System.err.println("-- making Broker for -"
97                           +driver+" - " +URL
98                           + " log " + log + " user "
99                           + username + " pass: " + passwd);
100
101       JDBCPoolMetaData meta = new JDBCPoolMetaData();
102       meta.setDbname("mir");
103       meta.setDriver(driver);
104       meta.setURL(URL);
105       meta.setUserName(username);
106       meta.setPassword(passwd);
107       meta.setJNDIName("mir");
108       meta.setMaximumSize(Integer.parseInt(max));
109       meta.setMinimumSize(Integer.parseInt(min));
110       
111       JDBCPool pool = SQLManager.getInstance().createPool(meta);
112       SQLManager.getInstance().addPool("mir",pool);
113           
114       if (pool!=null){
115         instances++;
116         brokerHash.put("Pool.broker",pool);
117       }
118
119     } // end if
120   }
121
122   public static void addBroker2(String driver, String URL) throws StorageObjectException {
123
124     String username,passwd,min,max,log,reset;
125
126     if(!brokerHash.containsKey("Pool.broker")){
127       username=getProp("Database.Username");
128       passwd=getProp("Database.Password");
129       min=getProp("Database.poolMin");
130       max=getProp("Database.poolMax");
131       log=getProp("Home") + configHash.get("Database.PoolLog");
132       reset=getProp("Database.poolResetTime");
133
134       try{
135         System.err.println("-- making Broker for -"
136                             +driver+" - " +URL
137                             + " log " + log + " user "
138                             + username + " pass: " + passwd);
139
140         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
141                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
142         if (br!=null){
143           instances++;
144           brokerHash.put("Pool.broker",br);
145         }
146       } catch(IOException e){
147         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
148         throw new StorageObjectException(e.toString());
149       }
150     } // end if
151   }
152
153   /**
154    * Finalize method
155    */
156   public void finalize(){
157     instances --;
158     try {
159       super.finalize();
160     } catch (Throwable t) {}
161   }
162
163 }