2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mir.servlet;
\r
34 import java.util.Locale;
\r
36 import javax.servlet.ServletConfig;
\r
37 import javax.servlet.ServletException;
\r
38 import javax.servlet.http.HttpServlet;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
41 import javax.servlet.http.HttpSession;
\r
43 import mir.config.MirPropertiesConfiguration;
\r
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
45 import mir.log.LoggerWrapper;
\r
46 import mir.storage.DatabaseAdaptor;
\r
48 import com.codestudio.util.JDBCPool;
\r
49 import com.codestudio.util.JDBCPoolMetaData;
\r
50 import com.codestudio.util.SQLManager;
\r
54 * Description: Abstract servlet-class
\r
55 * Copyright: Copyright (c) 2001, 2002
\r
56 * Company: Mir-coders group
\r
57 * @author idfx, the Mir-coders group
\r
58 * @version $Id: AbstractServlet.java,v 1.23 2003/03/06 05:40:39 zapata Exp $
\r
61 public abstract class AbstractServlet extends HttpServlet {
\r
62 protected static String lang;
\r
63 protected LoggerWrapper logger;
\r
64 protected MirPropertiesConfiguration configuration;
\r
67 * Constructor for AbstractServlet.
\r
69 public AbstractServlet() {
\r
71 logger = new LoggerWrapper("Servlet");
\r
74 protected void setNoCaching(HttpServletResponse aResponse) {
\r
75 //nothing in Mir can or should be cached as it's all dynamic...
\r
77 //this needs to be done here and not per page (via meta tags) as some
\r
78 //browsers have problems w/ it per-page -mh
\r
79 aResponse.setHeader("Pragma", "no-cache");
\r
80 aResponse.setDateHeader("Expires", 0);
\r
81 aResponse.setHeader("Cache-Control", "no-cache");
\r
85 * Bind the language to the session
\r
87 protected void setLanguage(HttpSession session, String language) {
\r
88 session.setAttribute("Language", language);
\r
91 protected void setLocale(HttpSession session, Locale loc) {
\r
92 session.setAttribute("Locale", loc);
\r
96 * Get the session-bound language
\r
98 protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {
\r
99 String lang = (String) session.getAttribute("Language");
\r
101 if (lang == null || lang.length()==0) {
\r
102 lang = getAcceptLanguage(aRequest);
\r
109 * get the locale either from the session or the accept-language header ot the request
\r
110 * this supersedes getLanguage for the new i18n
\r
112 public Locale getLocale(HttpServletRequest aRequest) {
\r
114 HttpSession session = aRequest.getSession(false);
\r
115 if (session != null) {
\r
116 // session can be null in case of logout
\r
117 loc = (Locale) session.getAttribute("Locale");
\r
119 // if there is nothing in the session get it fron the accept-language
\r
121 loc = aRequest.getLocale();
\r
127 * Checks the Accept-Language of the client browser.
\r
128 * If this language is available it returns its country-code,
\r
129 * else it returns the standard-language
\r
131 protected String getAcceptLanguage(HttpServletRequest aRequest) {
\r
132 Locale loc = aRequest.getLocale();
\r
133 lang = loc.getLanguage();
\r
138 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
\r
140 public void init(ServletConfig config) throws ServletException {
\r
141 super.init(config);
\r
142 MirPropertiesConfiguration.setContext(config.getServletContext());
\r
144 configuration = MirPropertiesConfiguration.instance();
\r
146 catch (PropertiesConfigExc e) {
\r
147 throw new ServletException(e);
\r
150 String dbUser = configuration.getString("Database.Username");
\r
151 String dbPassword = configuration.getString("Database.Password");
\r
152 String dbHost = configuration.getString("Database.Host");
\r
153 String dbAdapName = configuration.getString("Database.Adaptor");
\r
154 String dbName = configuration.getString("Database.Name");
\r
156 DatabaseAdaptor adaptor;
\r
158 adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();
\r
160 catch (Exception e) {
\r
161 throw new ServletException("Could not load DB adapator: " +
\r
168 dbDriver = adaptor.getDriver();
\r
169 dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);
\r
171 catch (Exception e) {
\r
172 throw new ServletException(e);
\r
175 JDBCPoolMetaData meta = new JDBCPoolMetaData();
\r
176 meta.setDbname(dbName);
\r
177 meta.setDriver(dbDriver);
\r
178 meta.setURL(dbUrl);
\r
179 meta.setUserName(dbUser);
\r
180 meta.setPassword(dbPassword);
\r
181 meta.setJNDIName("mir");
\r
182 meta.setMaximumSize(10);
\r
183 meta.setMinimumSize(1);
\r
184 meta.setPoolPreparedStatements(false);
\r
185 meta.setCacheEnabled(false);
\r
186 meta.setCacheSize(15);
\r
187 meta.setDebugging(false);
\r
188 // meta.setLogFile(dblogfile+".pool");
\r
190 SQLManager manager = SQLManager.getInstance();
\r
191 JDBCPool pool = null;
\r
192 if (manager != null) {
\r
193 pool = manager.createPool(meta);
\r