contains only admin templates which should not be changed by users
[mir.git] / source / mir / misc / MirConfig.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package  mir.misc;
33
34 import  java.net.*;
35 import  java.io.*;
36 import  java.util.*;
37 import  java.lang.*;
38
39 import  javax.servlet.ServletContext;
40 import  javax.servlet.http.*;
41
42 import  mir.storage.StorageObjectException;
43 import  mir.storage.DatabaseAdaptor;
44 import com.codestudio.util.*;
45
46 /**
47  * Title:        Mir
48  * Description:  Class that allows access to all Mir
49  *               config values
50  * Copyright:    Copyright (c) 2001
51  * Company:      Indymedia
52  * @author       mh <heckmann@hbe.ca>
53  * @version 0.1
54  */
55
56
57 /**
58  * This class is a layer above the Configuration
59  * It manages access to config variables that are
60  * both generated on the fly and found in the config file.
61  */
62
63 public class MirConfig extends Configuration {
64
65   private static HashMap configHash = null;
66   private static HashMap brokerHash = new HashMap();
67   private static int      instances=0;
68
69   /**
70    * Initializes Configuration hash that contains all values.
71    * loads the properties-file and any other values
72    * @param uri, the root Uri of the install
73    * @param home, The absolute path if the install root.
74    * @param name, The name of the servlet (usually "Mir")
75    * @param confName, the name of the config file to load.
76    */
77   public static synchronized void initConfig(ServletContext ctx, String uri,
78                                             String name, String confName) {
79
80     initConfResource(ctx.getRealPath("/WEB-INF/")+"/"+confName);
81
82     configHash = new HashMap();
83
84     configHash.put("Home", ctx.getRealPath("/WEB-INF/")+"/");
85     configHash.put("ServletContext", ctx);
86     configHash.put("RootUri", uri);
87
88     Enumeration resKeys = getResourceKeys();
89     while(resKeys.hasMoreElements()) {
90       String keyNm = (String)resKeys.nextElement();
91       configHash.put(keyNm, getProperty(keyNm));
92     }
93   }
94   /**
95    * Returns the property asked for by pulling it out a HashMap
96    * @param a String containing the property name (key)
97    * @return a String containing the prop. value
98    */
99   public static void setServletName(String servletName) {
100     configHash.put("ServletName",servletName);
101   }
102
103   /**
104    * Returns the property asked for by pulling it out a HashMap
105    * @param a String containing the property name (key)
106    * @return a String containing the prop. value
107    */
108   public static String getProp(String propName) {
109     String result = (String)configHash.get(propName);
110
111     if (result==null)
112       throw new ConfigException("config property '"+propName+"' not available!");
113
114     return result;
115   }
116
117   /**
118    * Returns the property asked for by pulling it out a HashMap and
119    * appending it to configproperty "Home"
120    * @param a String containing the property name (key)
121    * @return a String containing the prop.value
122    */
123   public static String getPropWithHome(String propName) {
124     return getProp("Home") + getProp(propName);
125   }
126
127   /**
128    * Returns the property asked for iin raw Object form by
129    * pulling it out a HashMap
130    * @param a String containing the property name (key)
131    * @return an Object containing the prop.value
132    */
133   public static Object getPropAsObject(String propName) {
134     return configHash.get(propName);
135   }
136
137   public static void initDbPool () throws StorageObjectException {
138     if (configHash == null) {
139         throw new StorageObjectException("MirConfig -- Trying initialize "+
140                                         "DB pool when system not yet "+
141                                         "configured");
142     }
143     String dbUser=getProp("Database.Username");
144     String dbPassword=getProp("Database.Password");
145     String dbHost=getProp("Database.Host");
146     String dbAdapName=getProp("Database.Adaptor");
147     DatabaseAdaptor adaptor;
148     try {
149       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
150     } catch (Exception e) {
151       throw new StorageObjectException("Could not load DB adapator: "+
152                                         e.toString());
153     }
154     String dbDriver=adaptor.getDriver();
155     String dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
156     System.out.println("adding Broker with: " +dbDriver+":"+dbUrl );
157     addBroker( dbDriver, dbUrl);
158   }
159
160   public static void addBroker(String driver, String URL)
161     throws StorageObjectException {
162
163     if (configHash == null) {
164         throw new StorageObjectException("MirConfig -- Trying initialize "+
165                                         "DB pool when system not yet "+
166                                         "configured");
167     }
168     String username,passwd,min,max,log,reset,dbname,dblogfile;
169
170     if(!brokerHash.containsKey("Pool.broker")){
171       username=getProp("Database.Username");
172       passwd=getProp("Database.Password");
173       min=getProp("Database.poolMin");
174       max=getProp("Database.poolMax");
175       dbname=getProp("Database.Name");
176       log=getProp("Home")+ configHash.get("Database.PoolLog");
177       reset=getProp("Database.poolResetTime");
178       dblogfile=getPropWithHome("Database.Logfile");
179
180       System.err.println("-- making Broker for -"
181                           +driver+" - " +URL
182                           + " log " + log + " user "
183                           + username + " pass: " + passwd);
184
185       JDBCPoolMetaData meta = new JDBCPoolMetaData();
186       meta.setDbname(dbname);
187       meta.setDriver(driver);
188       meta.setURL(URL);
189       meta.setUserName(username);
190       meta.setPassword(passwd);
191       meta.setJNDIName("mir");
192       meta.setMaximumSize(Integer.parseInt(max));
193       meta.setMinimumSize(Integer.parseInt(min));
194       meta.setPoolPreparedStatements(false);
195       meta.setCacheEnabled(false);
196       meta.setCacheSize(15);
197       meta.setDebugging(false);
198       meta.setLogFile(dblogfile+".pool");
199
200       JDBCPool pool = SQLManager.getInstance().createPool(meta);
201
202       if (pool!=null){
203         instances++;
204         brokerHash.put("Pool.broker",pool);
205       }
206
207     } // end if
208   }
209
210   /**
211    * Finalize method
212    */
213   public void finalize(){
214     instances --;
215     try {
216       super.finalize();
217     } catch (Throwable t) {}
218   }
219
220   public static Map allSettings() {
221     return configHash;
222   }
223
224 }