some small changes delting unneeded imports. two new exceptions in mir.storage. usage...
[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.20 2003/01/25 17:45:19 idfx Exp $
59  */
60
61 public abstract class AbstractServlet extends HttpServlet {
62     protected static String lang;
63     //protected static Logfile theLog;
64         protected LoggerWrapper logger;
65                 protected MirPropertiesConfiguration configuration;     
66         
67   /**
68    * Constructor for AbstractServlet.
69    */
70   public AbstractServlet() {
71     super();
72     logger = new LoggerWrapper("Servlet");
73   }
74
75     protected void setNoCaching(HttpServletResponse res) {
76       //nothing in Mir can or should be cached as it's all dynamic...
77       //
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       res.setHeader("Pragma", "no-cache");
81       res.setDateHeader("Expires", 0);
82       res.setHeader("Cache-Control", "no-cache");
83     }
84
85     /**
86      * Bind the language to the session
87      */
88     protected void setLanguage(HttpSession session, String language) {
89         session.setAttribute("Language", language);
90     }
91
92     protected void setLocale(HttpSession session, Locale loc) {
93         session.setAttribute("Locale", loc);
94     }
95
96     /**
97      * Get the session-bound language
98      */
99     protected String getLanguage(HttpServletRequest req, HttpSession session) {
100         String lang = (String) session.getAttribute("Language");
101         if (lang == null || lang.equals("")) {
102             return getAcceptLanguage(req);
103         }
104         else {
105             return lang;
106         }
107     }
108
109     /**
110      * get the locale either from the session or the accept-language header ot the request
111      * this supersedes getLanguage for the new i18n
112      */
113     public Locale getLocale(HttpServletRequest req) {
114         Locale loc=null;
115         HttpSession session = req.getSession(false);
116         if (session!=null) {
117             // session can be null in case of logout
118             loc = (Locale) session.getAttribute("Locale");
119         }
120         // if there is nothing in the session get it fron the accept-language
121         if (loc == null) {
122             loc = req.getLocale();
123         }
124         return loc;
125     }
126
127     /**
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
131      */
132     protected String getAcceptLanguage(HttpServletRequest req) {
133         Locale loc = req.getLocale();
134         lang = loc.getLanguage();
135         return lang;
136     }
137   /**
138    * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
139    */
140   public void init(ServletConfig arg0) throws ServletException {
141     super.init(arg0);
142     MirPropertiesConfiguration.setContext(arg0.getServletContext());
143     MirPropertiesConfiguration.setContextPath("/"+arg0.getServletName());
144     try {
145       configuration = MirPropertiesConfiguration.instance();
146     } catch (PropertiesConfigExc e) {
147       throw new ServletException(e);
148     }
149     
150     String dbUser=configuration.getString("Database.Username");
151     String dbPassword=configuration.getString("Database.Password");
152     String dbHost=configuration.getString("Database.Host");
153     String dbAdapName=configuration.getString("Database.Adaptor");
154     
155     DatabaseAdaptor adaptor;
156     try {
157       adaptor = (DatabaseAdaptor)Class.forName(dbAdapName).newInstance();
158     } catch (Exception e) {
159       throw new ServletException("Could not load DB adapator: "+
160                                         e.toString());
161     }
162     
163     String min,max,log,reset,dbname,dblogfile;
164
165     min=configuration.getString("Database.poolMin");
166     System.out.println(min);
167     max=configuration.getString("Database.poolMax");
168     System.out.println(max);
169     dbname=configuration.getString("Database.Name");
170     System.out.println(dbname);
171     log=configuration.getStringWithHome("Database.PoolLog");
172     System.out.println(log);
173     reset=configuration.getString("Database.poolResetTime");
174     System.out.println(reset);
175     dblogfile=configuration.getStringWithHome("Database.Logfile");
176     System.out.println(dblogfile);
177     
178     String dbDriver;
179     String dbUrl;
180     try{
181       dbDriver=adaptor.getDriver();
182       dbUrl=adaptor.getURL(dbUser,dbPassword, dbHost);
183     } catch (Exception e) {
184       throw new ServletException(e);
185     }
186
187     JDBCPoolMetaData meta = new JDBCPoolMetaData();
188     meta.setDbname(dbname);
189     meta.setDriver(dbDriver);
190     meta.setURL(dbUrl);
191     meta.setUserName(dbUser);
192     meta.setPassword(dbPassword);
193     meta.setJNDIName("mir");
194     meta.setMaximumSize(10);
195     meta.setMinimumSize(1);
196     meta.setPoolPreparedStatements(false);
197     meta.setCacheEnabled(false);
198     meta.setCacheSize(15);
199     meta.setDebugging(false);
200     meta.setLogFile(dblogfile+".pool");
201
202     SQLManager manager = SQLManager.getInstance();
203     JDBCPool pool = null;
204     if(manager != null){
205       pool = manager.createPool(meta);
206     }
207   }
208
209 }