apache.commons.config
[mir.git] / source / mir / servlet / AbstractServlet.java
index f81b4d8..13f674b 100755 (executable)
@@ -1,61 +1,85 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
 package mir.servlet;
 
-import mir.misc.Logfile;
-import mir.misc.MirConfig;
-import mir.misc.StringUtil;
-import mir.storage.StorageObjectException;
+import java.util.Locale;
 
-import javax.servlet.UnavailableException;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
-import java.util.Locale;
-import java.util.Random;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.log.LoggerWrapper;
+import mir.storage.DatabaseAdaptor;
+
+import com.codestudio.util.JDBCPool;
+import com.codestudio.util.JDBCPoolMetaData;
+import com.codestudio.util.SQLManager;
 
 /**
  * Title:        Mir
  * Description:  Abstract servlet-class
- * Copyright:    Copyright (c) 2001
- * Company:      Indymedia
- * @author       idfx
- * @version 1.0
+ * Copyright:    Copyright (c) 2001, 2002
+ * Company:      Mir-coders group
+ * @author       idfx, the Mir-coders group
+ * @version      $Id: AbstractServlet.java,v 1.20 2003/01/25 17:45:19 idfx Exp $
  */
 
 public abstract class AbstractServlet extends HttpServlet {
     protected static String lang;
-    protected static Logfile theLog;
+    //protected static Logfile theLog;
+       protected LoggerWrapper logger;
+               protected MirPropertiesConfiguration configuration;     
+       
+  /**
+   * Constructor for AbstractServlet.
+   */
+  public AbstractServlet() {
+    super();
+    logger = new LoggerWrapper("Servlet");
+  }
 
-    /**
-     * the configration
-     */
-    protected boolean getConfig(HttpServletRequest req)
-            throws UnavailableException {
-
-        //String RealPath = super.getServletContext().getRealPath("/");
-        String Uri = req.getRequestURI();
-        String Name = super.getServletName();
-        String RootUri = StringUtil.replace(Uri, "/servlet/" + Name, "");
-
-        // init config
-        //MirConfig.initConfig(RealPath, RootUri, Name, getInitParameter("Config"));
-        MirConfig.initConfig(super.getServletContext(), RootUri, Name,
-                              getInitParameter("Config"));
-
-        theLog = Logfile.getInstance(MirConfig.getPropWithHome(Name + ".Logfile"));
-        theLog.printInfo(Name + " started.");
-        theLog.printInfo("Path is: " + MirConfig.getProp("Home"));
-        theLog.printInfo("Root URI is: " + MirConfig.getProp("RootUri"));
-        theLog.printInfo("StandardLanguage is: " + MirConfig.getProp("StandardLanguage"));
-        try {
-            MirConfig.initDbPool();
-        }
-        catch (StorageObjectException e) {
-            throw new UnavailableException(
-                    "Could not initialize database pool. -- "
-                    + e.toString(), 0);
-        }
-        super.getServletContext().setAttribute("mir.confed", new Boolean(true));
-        return true;
+    protected void setNoCaching(HttpServletResponse res) {
+      //nothing in Mir can or should be cached as it's all dynamic...
+      //
+      //this needs to be done here and not per page (via meta tags) as some
+      //browsers have problems w/ it per-page -mh
+      res.setHeader("Pragma", "no-cache");
+      res.setDateHeader("Expires", 0);
+      res.setHeader("Cache-Control", "no-cache");
     }
 
     /**
@@ -108,16 +132,78 @@ public abstract class AbstractServlet extends HttpServlet {
     protected String getAcceptLanguage(HttpServletRequest req) {
         Locale loc = req.getLocale();
         lang = loc.getLanguage();
-        /* not needed anymore due to new i18n
-          File f = new File(HTMLTemplateProcessor.templateDir+"/"+lang);
-        //is there an existing template-path?
-        if(!f.isDirectory()){
-          //no there isn't. we use standard-language
-          lang = MirConfig.getProp("StandardLanguage");
-          theLog.printDebugInfo("language not existing");
-        }
-        theLog.printDebugInfo("Language: " + lang);
-        */
         return lang;
     }
+  /**
+   * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
+   */
+  public void init(ServletConfig arg0) throws ServletException {
+    super.init(arg0);
+    MirPropertiesConfiguration.setContext(arg0.getServletContext());
+    MirPropertiesConfiguration.setContextPath("/"+arg0.getServletName());
+    try {
+      configuration = MirPropertiesConfiguration.instance();
+    } catch (PropertiesConfigExc e) {
+      throw new ServletException(e);
+    }
+    
+    String dbUser=configuration.getString("Database.Username");
+    String dbPassword=configuration.getString("Database.Password");
+    String dbHost=configuration.getString("Database.Host");
+    String dbAdapName=configuration.getString("Database.Adaptor");
+    
+    DatabaseAdaptor adaptor;
+    try {
+      adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
+    } catch (Exception e) {
+      throw new ServletException("Could not load DB adapator: "+
+                                        e.toString());
+    }
+    
+    String min,max,log,reset,dbname,dblogfile;
+
+    min=configuration.getString("Database.poolMin");
+    System.out.println(min);
+    max=configuration.getString("Database.poolMax");
+    System.out.println(max);
+    dbname=configuration.getString("Database.Name");
+    System.out.println(dbname);
+    log=configuration.getStringWithHome("Database.PoolLog");
+    System.out.println(log);
+    reset=configuration.getString("Database.poolResetTime");
+    System.out.println(reset);
+    dblogfile=configuration.getStringWithHome("Database.Logfile");
+    System.out.println(dblogfile);
+    
+    String dbDriver;
+    String dbUrl;
+    try{
+      dbDriver=adaptor.getDriver();
+      dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
+    } catch (Exception e) {
+      throw new ServletException(e);
+    }
+
+    JDBCPoolMetaData meta = new JDBCPoolMetaData();
+    meta.setDbname(dbname);
+    meta.setDriver(dbDriver);
+    meta.setURL(dbUrl);
+    meta.setUserName(dbUser);
+    meta.setPassword(dbPassword);
+    meta.setJNDIName("mir");
+    meta.setMaximumSize(10);
+    meta.setMinimumSize(1);
+    meta.setPoolPreparedStatements(false);
+    meta.setCacheEnabled(false);
+    meta.setCacheSize(15);
+    meta.setDebugging(false);
+    meta.setLogFile(dblogfile+".pool");
+
+    SQLManager manager = SQLManager.getInstance();
+    JDBCPool pool = null;
+    if(manager != null){
+      pool = manager.createPool(meta);
+    }
+  }
+
 }