a9e7c3e409d47813224471728764c74b502b477a
[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     // just for testing
36     configHash.put("Database.Username", "postgres");
37     configHash.put("Database.Password", "");
38   }
39
40   public static void setUsername(String user) {
41     configHash.put("Database.Username", user);
42   }
43
44   public static void setPassword(String pass) {
45     configHash.put("Database.Password", pass);
46   }
47
48   public static void setHost(String host) {
49     configHash.put("Database.Host", host);
50   }
51
52   public static void setAdaptor(String adaptor) {
53     configHash.put("Database.Adaptor", adaptor);
54   }
55
56   public static void setDefaultLimit(int limit) {
57     configHash.put("Database.Limit", Integer.toString(limit));
58   }
59
60   public static void setPoolMin(int min) {
61     configHash.put("Database.poolMin", Integer.toString(min));
62   }
63
64   public static void setPoolMax(int max) {
65     configHash.put("Database.poolMax", Integer.toString(max));
66   }
67
68   public static void setPoolResetTime(float t) {
69     configHash.put("Database.poolResetTime", Float.toString(t));
70   }
71
72   public static void setPoolLog(String l) {
73     configHash.put("Database.poolLog", l);
74   }
75
76   /**
77    * Returns the property asked for by pulling it out a HashMap
78    * @param a String containing the property name (key)
79    * @return a String containing the prop. value
80    */
81   public static String getProp(String propName) {
82     return (String)configHash.get(propName);
83   }
84
85   public static void addBroker(String driver, String URL){
86
87     String username,passwd,min,max,log,reset;
88
89     if(!brokerHash.containsKey("Pool.broker")){
90       username=getProp("Database.Username");
91       passwd=getProp("Database.Password");
92       min=getProp("Database.poolMin");
93       max=getProp("Database.poolMax");
94       log= getProp("Database.poolLog");
95       reset=getProp("Database.poolResetTime");
96
97       try{
98         System.err.println("-- making Broker for -"
99                             +driver+" - " +URL
100                             + " log " + log + " user "
101                             + username + " pass: " + passwd);
102
103         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
104                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
105         if (br!=null){
106           instances++;
107           brokerHash.put("Pool.broker",br);
108         } else {
109             throw new Exception();
110         }
111       } catch(Exception e){
112         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
113       }
114     } // end if
115   }
116
117   /**
118    * Liefert DBConnectionBroker einer Configuration zurueck
119    * @param confFilename
120    * @return DBConnectionBroker
121    */
122   public static DbConnectionBroker getBroker() {
123     DbConnectionBroker broker=null;
124     broker=(DbConnectionBroker)brokerHash.get("Pool.broker");
125     if (broker==null) {
126       System.err.println("Konnte kein ConnectionPoolBroker initialisiert werden");
127     }
128     return broker;
129   }
130
131   /**
132    * Liefert Anzahl der Instantiierten DBConnectionBroker zurueck
133    * @return
134    */
135   public static int getBrokerInstances() {
136     return instances;
137   }
138
139   public static DbConnectionBroker getBrokerInfo(){
140     return (DbConnectionBroker)brokerHash.get("Pool.broker");
141   }
142
143   /**
144    * Finalize method
145    */
146   public void finalize(){
147     instances --;
148     try {
149       super.finalize();
150     } catch (Throwable t) {}
151   }
152
153 }