Initial revision
[mir.git] / source / mir / misc / Configuration.java
1 /*
2  * put your module comment here
3  */
4
5
6 package  mir.misc;
7
8 import  java.net.*;
9 import  java.io.*;
10 import  java.util.*;
11 import  java.lang.*;
12 import  com.javaexchange.dbConnectionBroker.*;
13
14
15 /**
16  * Diese Klasse realisert den Zugriff auf die Konfiguration.
17  *
18  */
19 public class Configuration {
20
21   private static HashMap  confs = new HashMap(); // key: conffilename, confHash
22   private static HashMap c = new HashMap();
23   private String          confFilename;
24   private static String   defaultconfFilename;
25   private static int      instances=0;
26   //static ResourceBundle conf = ResourceBundle.getBundle("config");
27   static ResourceBundle conf;
28
29   public static void initConfig(String confName) {
30     conf = ResourceBundle.getBundle(confName);
31     confs.put("confname",confName);
32   }
33
34   public static void addBroker(String driver, String URL){
35
36     System.err.println("--trying to add broker");
37     String username,passwd,min,max,log,reset;
38
39     if(!c.containsKey("Pool.broker")){
40       username=conf.getString("Database.Username");
41       passwd=conf.getString("Database.Password");
42       min=conf.getString("Database.poolMin");
43       max=conf.getString("Database.poolMax");
44       log=conf.getString("Home") + conf.getString("Database.PoolLog");
45       reset=conf.getString("Database.poolResetTime");
46
47       try{
48         System.err.println("-- making Broker for -"
49                             +driver+" - " +URL
50                             + " log " + log + " user "
51                             + username + " pass: " + passwd);
52
53         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
54                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
55         if (br!=null){
56           instances++;
57           c.put("Pool.broker",br);
58         } else {
59             throw new Exception();
60         }
61       } catch(Exception e){
62         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
63       }
64     } // end if
65   }
66
67
68         /**
69          * Fragt ab, ob das Betriebssystem Windows ist.
70          * @return true wenn ja, sonst false.
71          */
72   public static boolean isWindows() {
73     return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
74   }
75
76    /**
77          * Liefert Wert einer Property zurueck
78          * @param propName
79          * @return Wert der Property
80          */
81   public static String getProperty(String propName) {  // default
82     return conf.getString(propName);
83   }
84
85   /**
86          * Liest eine Property eines Modules aus der Konfiguration
87          * @param filename
88          * @param theModule
89          * @param propName
90          * @return Wert der Property
91          */
92   public String getProperty(String filename ,String theModule, String propName) {
93     return getProperty(filename, theModule + "." + propName);
94   }
95
96         /**
97          * Liest eine Property aus der Konfiguration
98          * @param filename
99          * @param propName
100          * @return Wert der Property
101          */
102   public static String getProperty(String filename, String propName) {
103     if (filename != null) {
104       String prop = null;
105       HashMap conf = ((HashMap)confs.get("confname"));
106
107       if (conf == null) {
108         System.err.println("Keine Konfiguration fuer " + filename);
109       } else {
110         prop = (String)conf.get(propName);
111       }
112
113       if (prop == null) {
114         System.err.println("Keine Konfiguration fuer " + filename + " " + propName);
115       }
116
117       return prop;
118
119     } else {
120         System.err.println("--- filename null!");
121     }
122
123     return null;
124   }
125
126         /**
127          * Liefert DBConnectionBroker einer Configuration zurueck
128          * @param confFilename
129          * @return DBConnectionBroker
130          */
131   public static DbConnectionBroker getBroker() {
132     DbConnectionBroker broker=null;
133     broker=(DbConnectionBroker)c.get("Pool.broker");
134     if (broker==null) {
135       System.err.println("Konnte kein ConnectionPoolBroker initialisiert werden");
136     }
137     return broker;
138   }
139
140         /**
141          * Liefert Hashtabel mit den Konfigurationen
142          * @return
143          */
144   public static HashMap getConfs(){
145     return confs;
146   }
147
148   public static DbConnectionBroker getBrokerInfo(){
149     return (DbConnectionBroker)c.get("Pool.broker");
150   }
151
152
153         /**
154          * Finalize Methode
155          */
156   public void finalize(){
157     instances --;
158     try {
159       super.finalize();
160     } catch (Throwable t) {}
161   }
162
163   /**
164          * Liefert Anzahl der Instantiierten DBConnectionBroker zurueck
165          * @return
166          */
167   public static int getBrokerInstances() {
168     return instances;
169   }
170 } //end of class