Ok, big merge. here's the new xml-config stuff in action. There's a few
[mir.git] / source / mir / storage / DatabaseConfig.java
1
2 package  mir.storage;
3
4 import java.util.*;
5 import java.lang.*;
6 import  com.javaexchange.dbConnectionBroker.*;
7
8 /**
9  * Title:        Mir
10  * Description:  Class that allows access to all Database Config values
11  * Copyright:    Copyright (c) 2001
12  * Company:      Indymedia
13  * @author       mh <heckmann@hbe.ca>
14  * @version 0.1
15  */
16
17
18 public class DatabaseConfig {
19
20   private static HashMap configHash;
21   private static HashMap brokerHash;
22   private static int      instances=0;
23
24   static {
25     configHash = new HashMap();
26     brokerHash = new HashMap();
27     configHash.put("Database.Host", "localhost");
28     configHash.put("Database.Adaptor", "mir.storage.DatabaseAdaptorPostgresql");
29     configHash.put("Database.Limit", "20");
30     configHash.put("Database.poolMin", "1");
31     configHash.put("Database.poolMax", "10");
32     configHash.put("Database.poolLog", "/tmp/pool.log");
33     configHash.put("Database.poolResetTime", "1.0");
34   }
35
36   public static void setUsername(String user) {
37     configHash.put("Database.Username", user);
38   }
39
40   public static void setPassword(String pass) {
41     configHash.put("Database.Password", pass);
42   }
43
44   public static void setHost(String host) {
45     configHash.put("Database.Host", host);
46   }
47
48   public static void setAdaptor(String adaptor) {
49     configHash.put("Database.Adaptor", adaptor);
50   }
51
52   public static void setDefaultLimit(int limit) {
53     configHash.put("Database.Limit", Integer.toString(limit));
54   }
55   
56   public static void setPoolMin(int min) {
57     configHash.put("Database.poolMin", Integer.toString(min));
58   }
59
60   public static void setPoolMax(int max) {
61     configHash.put("Database.poolMax", Integer.toString(max));
62   }
63
64   public static void setPoolResetTime(float t) {
65     configHash.put("Database.poolResetTime", Float.toString(t));
66   }
67
68   public static void setPoolLog(String l) {
69     configHash.put("Database.poolLog", l);
70   }
71
72   /**
73    * Returns the property asked for by pulling it out a HashMap
74    * @param a String containing the property name (key)
75    * @return a String containing the prop. value
76    */
77   public static String getProp(String propName) {
78     return (String)configHash.get(propName);
79   }
80
81   public static void addBroker(String driver, String URL){
82
83     String username,passwd,min,max,log,reset;
84
85     if(!brokerHash.containsKey("Pool.broker")){
86       username=getProp("Database.Username");
87       passwd=getProp("Database.Password");
88       min=getProp("Database.poolMin");
89       max=getProp("Database.poolMax");
90       log= getProp("Database.poolLog");
91       reset=getProp("Database.poolResetTime");
92
93       try{
94         System.err.println("-- making Broker for -"
95                             +driver+" - " +URL
96                             + " log " + log + " user "
97                             + username + " pass: " + passwd);
98
99         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
100                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
101         if (br!=null){
102           instances++;
103           brokerHash.put("Pool.broker",br);
104         } else {
105             throw new Exception();
106         }
107       } catch(Exception e){
108         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
109       }
110     } // end if
111   }
112
113   /**
114    * Liefert DBConnectionBroker einer Configuration zurueck
115    * @param confFilename
116    * @return DBConnectionBroker
117    */
118   public static DbConnectionBroker getBroker() {
119     DbConnectionBroker broker=null;
120     broker=(DbConnectionBroker)brokerHash.get("Pool.broker");
121     if (broker==null) {
122       System.err.println("Konnte kein ConnectionPoolBroker initialisiert werden");
123     }
124     return broker;
125   }
126
127   /**
128    * Liefert Anzahl der Instantiierten DBConnectionBroker zurueck
129    * @return
130    */
131   public static int getBrokerInstances() {
132     return instances;
133   }
134
135   public static DbConnectionBroker getBrokerInfo(){
136     return (DbConnectionBroker)brokerHash.get("Pool.broker");
137   }
138
139   /**
140    * Finalize method
141    */
142   public void finalize(){
143     instances --;
144     try {
145       super.finalize();
146     } catch (Throwable t) {}
147   }
148
149 }