2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
34 import java.util.Locale;
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;
43 import mir.config.MirPropertiesConfiguration;
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
45 import mir.log.LoggerWrapper;
46 import mir.storage.DatabaseAdaptor;
48 import com.codestudio.util.JDBCPool;
49 import com.codestudio.util.JDBCPoolMetaData;
50 import com.codestudio.util.SQLManager;
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 $
61 public abstract class AbstractServlet extends HttpServlet {
62 protected static String lang;
63 protected LoggerWrapper logger;
64 protected MirPropertiesConfiguration configuration;
67 * Constructor for AbstractServlet.
69 public AbstractServlet() {
71 logger = new LoggerWrapper("Servlet");
74 protected void setNoCaching(HttpServletResponse res) {
75 //nothing in Mir can or should be cached as it's all dynamic...
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");
85 * Bind the language to the session
87 protected void setLanguage(HttpSession session, String language) {
88 session.setAttribute("Language", language);
91 protected void setLocale(HttpSession session, Locale loc) {
92 session.setAttribute("Locale", loc);
96 * Get the session-bound language
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);
109 * get the locale either from the session or the accept-language header ot the request
110 * this supersedes getLanguage for the new i18n
112 public Locale getLocale(HttpServletRequest req) {
114 HttpSession session = req.getSession(false);
116 // session can be null in case of logout
117 loc = (Locale) session.getAttribute("Locale");
119 // if there is nothing in the session get it fron the accept-language
121 loc = req.getLocale();
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
131 protected String getAcceptLanguage(HttpServletRequest req) {
132 Locale loc = req.getLocale();
133 lang = loc.getLanguage();
137 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
139 public void init(ServletConfig config) throws ServletException {
141 MirPropertiesConfiguration.setContext(config.getServletContext());
143 configuration = MirPropertiesConfiguration.instance();
144 } catch (PropertiesConfigExc e) {
145 throw new ServletException(e);
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");
154 DatabaseAdaptor adaptor;
156 adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
157 } catch (Exception e) {
158 throw new ServletException("Could not load DB adapator: "+
165 dbDriver=adaptor.getDriver();
166 dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
167 } catch (Exception e) {
168 throw new ServletException(e);
171 JDBCPoolMetaData meta = new JDBCPoolMetaData();
172 meta.setDbname(dbName);
173 meta.setDriver(dbDriver);
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");
186 SQLManager manager = SQLManager.getInstance();
187 JDBCPool pool = null;
189 pool = manager.createPool(meta);