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