4435521b53135d27b4454a4751282b59c28844ea
[mir.git] / source / mir / servlet / AbstractServlet.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.servlet;
33
34 import java.util.Locale;
35
36 import javax.servlet.ServletConfig;
37 import javax.servlet.ServletException;
38 import javax.servlet.http.HttpServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
42
43 import mir.config.MirPropertiesConfiguration;
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
45 import mir.log.LoggerWrapper;
46 import mir.storage.DatabaseAdaptor;
47
48 import com.codestudio.util.JDBCPool;
49 import com.codestudio.util.JDBCPoolMetaData;
50 import com.codestudio.util.SQLManager;
51
52 /**
53  * Title:        Mir
54  * Description:  Abstract servlet-class
55  * Copyright:    Copyright (c) 2001, 2002
56  * Company:      Mir-coders group
57  * @author       idfx, the Mir-coders group
58  * @version      $Id: AbstractServlet.java,v 1.22 2003/02/20 16:05:33 zapata Exp $
59  */
60
61 public abstract class AbstractServlet extends HttpServlet {
62     protected static String lang;
63     protected LoggerWrapper logger;
64     protected MirPropertiesConfiguration configuration;
65
66   /**
67    * Constructor for AbstractServlet.
68    */
69   public AbstractServlet() {
70     super();
71     logger = new LoggerWrapper("Servlet");
72   }
73
74     protected void setNoCaching(HttpServletResponse res) {
75       //nothing in Mir can or should be cached as it's all dynamic...
76       //
77       //this needs to be done here and not per page (via meta tags) as some
78       //browsers have problems w/ it per-page -mh
79       res.setHeader("Pragma", "no-cache");
80       res.setDateHeader("Expires", 0);
81       res.setHeader("Cache-Control", "no-cache");
82     }
83
84     /**
85      * Bind the language to the session
86      */
87     protected void setLanguage(HttpSession session, String language) {
88         session.setAttribute("Language", language);
89     }
90
91     protected void setLocale(HttpSession session, Locale loc) {
92         session.setAttribute("Locale", loc);
93     }
94
95     /**
96      * Get the session-bound language
97      */
98     protected String getLanguage(HttpServletRequest req, HttpSession session) {
99         String lang = (String) session.getAttribute("Language");
100         if (lang == null || lang.equals("")) {
101             return getAcceptLanguage(req);
102         }
103         else {
104             return lang;
105         }
106     }
107
108     /**
109      * get the locale either from the session or the accept-language header ot the request
110      * this supersedes getLanguage for the new i18n
111      */
112     public Locale getLocale(HttpServletRequest req) {
113         Locale loc=null;
114         HttpSession session = req.getSession(false);
115         if (session!=null) {
116             // session can be null in case of logout
117             loc = (Locale) session.getAttribute("Locale");
118         }
119         // if there is nothing in the session get it fron the accept-language
120         if (loc == null) {
121             loc = req.getLocale();
122         }
123         return loc;
124     }
125
126     /**
127      * Checks the Accept-Language of the client browser.
128      * If this language is available it returns its country-code,
129      * else it returns the standard-language
130      */
131     protected String getAcceptLanguage(HttpServletRequest req) {
132         Locale loc = req.getLocale();
133         lang = loc.getLanguage();
134         return lang;
135     }
136   /**
137    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
138    */
139   public void init(ServletConfig config) throws ServletException {
140     super.init(config);
141     MirPropertiesConfiguration.setContext(config.getServletContext());
142     try {
143       configuration = MirPropertiesConfiguration.instance();
144     } catch (PropertiesConfigExc e) {
145       throw new ServletException(e);
146     }
147
148     String dbUser=configuration.getString("Database.Username");
149     String dbPassword=configuration.getString("Database.Password");
150     String dbHost=configuration.getString("Database.Host");
151     String dbAdapName=configuration.getString("Database.Adaptor");
152     String dbName=configuration.getString("Database.Name");
153
154     DatabaseAdaptor adaptor;
155     try {
156       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
157     } catch (Exception e) {
158       throw new ServletException("Could not load DB adapator: "+
159                                         e.toString());
160     }
161
162     String dbDriver;
163     String dbUrl;
164     try{
165       dbDriver=adaptor.getDriver();
166       dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
167     } catch (Exception e) {
168       throw new ServletException(e);
169     }
170
171     JDBCPoolMetaData meta = new JDBCPoolMetaData();
172     meta.setDbname(dbName);
173     meta.setDriver(dbDriver);
174     meta.setURL(dbUrl);
175     meta.setUserName(dbUser);
176     meta.setPassword(dbPassword);
177     meta.setJNDIName("mir");
178     meta.setMaximumSize(10);
179     meta.setMinimumSize(1);
180     meta.setPoolPreparedStatements(false);
181     meta.setCacheEnabled(false);
182     meta.setCacheSize(15);
183     meta.setDebugging(false);
184 //    meta.setLogFile(dblogfile+".pool");
185
186     SQLManager manager = SQLManager.getInstance();
187     JDBCPool pool = null;
188     if(manager != null){
189       pool = manager.createPool(meta);
190     }
191   }
192
193 }