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.io.IOException;
35 import java.util.Locale;
37 import javax.servlet.ServletConfig;
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServlet;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.http.HttpSession;
44 import mir.config.MirPropertiesConfiguration;
45 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
46 import mir.log.LoggerWrapper;
47 import mir.storage.DatabaseAdaptor;
49 import com.codestudio.util.JDBCPool;
50 import com.codestudio.util.JDBCPoolMetaData;
51 import com.codestudio.util.SQLManager;
55 * Description: Abstract servlet-class
56 * Copyright: Copyright (c) 2001, 2002
57 * Company: Mir-coders group
58 * @author idfx, the Mir-coders group
59 * @version $Id: AbstractServlet.java,v 1.24 2003/03/15 02:07:36 idfx Exp $
62 public abstract class AbstractServlet extends HttpServlet {
63 protected static String lang;
64 protected LoggerWrapper logger;
65 protected MirPropertiesConfiguration configuration;
68 * Constructor for AbstractServlet.
70 public AbstractServlet() {
72 logger = new LoggerWrapper("Servlet");
75 protected void setNoCaching(HttpServletResponse aResponse) {
76 //nothing in Mir can or should be cached as it's all dynamic...
78 //this needs to be done here and not per page (via meta tags) as some
79 //browsers have problems w/ it per-page -mh
80 aResponse.setHeader("Pragma", "no-cache");
81 aResponse.setDateHeader("Expires", 0);
82 aResponse.setHeader("Cache-Control", "no-cache");
86 * Bind the language to the session
88 protected void setLanguage(HttpSession session, String language) {
89 session.setAttribute("Language", language);
92 protected void setLocale(HttpSession session, Locale loc) {
93 session.setAttribute("Locale", loc);
97 * Get the session-bound language
99 protected String getLanguage(HttpServletRequest aRequest, HttpSession session) {
100 String lang = (String) session.getAttribute("Language");
102 if (lang == null || lang.length()==0) {
103 lang = getAcceptLanguage(aRequest);
110 * get the locale either from the session or the accept-language header ot the request
111 * this supersedes getLanguage for the new i18n
113 public Locale getLocale(HttpServletRequest aRequest) {
115 HttpSession session = aRequest.getSession(false);
116 if (session != null) {
117 // session can be null in case of logout
118 loc = (Locale) session.getAttribute("Locale");
120 // if there is nothing in the session get it fron the accept-language
122 loc = aRequest.getLocale();
128 * Checks the Accept-Language of the client browser.
129 * If this language is available it returns its country-code,
130 * else it returns the standard-language
132 protected String getAcceptLanguage(HttpServletRequest aRequest) {
133 Locale loc = aRequest.getLocale();
134 lang = loc.getLanguage();
139 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
141 public void init(ServletConfig config) throws ServletException {
144 MirPropertiesConfiguration.setContext(config.getServletContext());
146 configuration = MirPropertiesConfiguration.instance();
148 catch (PropertiesConfigExc e) {
149 throw new ServletException(e);
152 String dbUser = configuration.getString("Database.Username");
153 String dbPassword = configuration.getString("Database.Password");
154 String dbHost = configuration.getString("Database.Host");
155 String dbAdapName = configuration.getString("Database.Adaptor");
156 String dbName = configuration.getString("Database.Name");
158 DatabaseAdaptor adaptor;
160 adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance();
162 catch (Exception e) {
163 throw new ServletException("Could not load DB adapator: " +
170 dbDriver = adaptor.getDriver();
171 dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost);
173 catch (Exception e) {
174 throw new ServletException(e);
177 JDBCPoolMetaData meta = new JDBCPoolMetaData();
178 meta.setDbname(dbName);
179 meta.setDriver(dbDriver);
181 meta.setUserName(dbUser);
182 meta.setPassword(dbPassword);
183 meta.setJNDIName("mir");
184 meta.setMaximumSize(10);
185 meta.setMinimumSize(1);
186 meta.setPoolPreparedStatements(false);
187 meta.setCacheEnabled(false);
188 meta.setCacheSize(15);
189 meta.setDebugging(false);
190 // meta.setLogFile(dblogfile+".pool");
192 SQLManager manager = SQLManager.getInstance();
193 JDBCPool pool = null;
194 if (manager != null) {
195 pool = manager.createPool(meta);
200 protected final void doGet(HttpServletRequest request, HttpServletResponse response)
201 throws ServletException, IOException {
202 doPost(request, response);
205 protected final void doPost(HttpServletRequest request, HttpServletResponse response)
206 throws ServletException, IOException {
207 if ((configuration.getString("RootUri") == null) ||
208 configuration.getString("RootUri").equals("")) {
209 configuration.setProperty("RootUri", request.getContextPath());
211 process(request, response);
214 abstract public void process(HttpServletRequest request, HttpServletResponse response)
215 throws ServletException, IOException;