mutlifile-upload in openmir
[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
10 /**
11  * Title:        Mir
12  * Description:  Class that allows access to all Mir
13  *               config values
14  * Copyright:    Copyright (c) 2001
15  * Company:      Indymedia
16  * @author       mh <heckmann@hbe.ca>
17  * @version 0.1
18  */
19
20
21 /**
22  * This class is a layer above the Configuration
23  * It manages access to config variables that are
24  * both generated on the fly and found in the config file.
25  */
26
27 public class MirConfig extends Configuration {
28  
29   private static HashMap configHash = new HashMap();
30   private static HashMap brokerHash = new HashMap();
31   private static int      instances=0;
32
33   /**
34    * Initializes Configuration hash that contains all values.
35    * loads the properties-file and any other values
36    * @param uri, the root Uri of the install
37    * @param home, The absolute path if the install root.
38    * @param name, The name of the servlet (usually "Mir")
39    * @param confName, the name of the config file to load.
40    */
41   public static void initConfig(String home, String uri, String name, String confName) {
42     initConfResource(confName);
43
44     configHash.put("Home", home);
45     configHash.put("RootUri", uri);
46     configHash.put("ServletName", name);
47    
48     Enumeration resKeys = getResourceKeys();
49     while(resKeys.hasMoreElements()) {
50       String keyNm = (String)resKeys.nextElement();
51       configHash.put(keyNm, getProperty(keyNm));
52     }
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 String getProp(String PropName) {
61     return (String)configHash.get(PropName);
62   }
63
64   public static void addBroker(String driver, String URL){
65
66     System.err.println("--trying to add broker");
67     String username,passwd,min,max,log,reset;
68
69     if(!brokerHash.containsKey("Pool.broker")){
70       username=getProp("Database.Username");
71       passwd=getProp("Database.Password");
72       min=getProp("Database.poolMin");
73       max=getProp("Database.poolMax");
74       log=getProp("Home") + configHash.get("Database.PoolLog");
75       reset=getProp("Database.poolResetTime");
76
77       try{
78         System.err.println("-- making Broker for -"
79                             +driver+" - " +URL
80                             + " log " + log + " user "
81                             + username + " pass: " + passwd);
82
83         DbConnectionBroker br = new DbConnectionBroker(driver,URL,username,passwd,(new Integer(min)).intValue(),
84                                                       (new Integer(max)).intValue(),log,(new Float(reset)).floatValue());
85         if (br!=null){
86           instances++;
87           brokerHash.put("Pool.broker",br);
88         } else {
89             throw new Exception();
90         }
91       } catch(Exception e){
92         System.err.println("Der ConnectionBroker konnte nicht initializiert werden"+ e.toString());e.printStackTrace();
93       }
94     } // end if
95   }
96
97   /**
98    * Liefert DBConnectionBroker einer Configuration zurueck
99    * @param confFilename
100    * @return DBConnectionBroker
101    */
102   public static DbConnectionBroker getBroker() {
103     DbConnectionBroker broker=null;
104     broker=(DbConnectionBroker)brokerHash.get("Pool.broker");
105     if (broker==null) {
106       System.err.println("Konnte kein ConnectionPoolBroker initialisiert werden");
107     }
108     return broker;
109   }
110
111   /**
112    * Liefert Anzahl der Instantiierten DBConnectionBroker zurueck
113    * @return
114    */
115   public static int getBrokerInstances() {
116     return instances;
117   }
118
119   public static DbConnectionBroker getBrokerInfo(){
120     return (DbConnectionBroker)brokerHash.get("Pool.broker");
121   }
122
123   /**
124    * Finalize Methode
125    */
126   public void finalize(){
127     instances --;
128     try {
129       super.finalize();
130     } catch (Throwable t) {}
131   }
132
133 }