X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fservlet%2FAbstractServlet.java;h=ff059d5d836906934b3d1105ea8f3aa90add63e2;hb=aa507bfd18e723d21e63454a26af3320bb8c27f2;hp=c190cfd96761a86c390a76e515ce11254d1cdbba;hpb=1bfd964b53be329b60b4ddc3a8fec43fcfa9da88;p=mir.git diff --git a/source/mir/servlet/AbstractServlet.java b/source/mir/servlet/AbstractServlet.java index c190cfd9..ff059d5d 100755 --- a/source/mir/servlet/AbstractServlet.java +++ b/source/mir/servlet/AbstractServlet.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,23 +18,23 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - package mir.servlet; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.sql.Connection; import java.util.Locale; + import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -42,13 +42,13 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import mir.config.MirPropertiesConfiguration; +import mir.log.LoggerWrapper; +import mircoders.global.MirGlobal; + import com.codestudio.util.JDBCPool; import com.codestudio.util.JDBCPoolMetaData; import com.codestudio.util.SQLManager; -import mir.config.MirPropertiesConfiguration; -import mir.config.MirPropertiesConfiguration.PropertiesConfigExc; -import mir.log.LoggerWrapper; -import mir.storage.DatabaseAdaptor; /** * Title: Mir @@ -56,7 +56,7 @@ import mir.storage.DatabaseAdaptor; * Copyright: Copyright (c) 2001, 2002 * Company: Mir-coders group * @author idfx, the Mir-coders group - * @version $Id: AbstractServlet.java,v 1.27 2003/04/20 13:22:24 idfx Exp $ + * @version $Id: AbstractServlet.java,v 1.30.2.5 2003/10/23 14:55:26 rk Exp $ */ public abstract class AbstractServlet extends HttpServlet { @@ -69,7 +69,6 @@ public abstract class AbstractServlet extends HttpServlet { */ public AbstractServlet() { super(); - logger = new LoggerWrapper("Servlet"); } protected void setNoCaching(HttpServletResponse aResponse) { @@ -147,34 +146,30 @@ public abstract class AbstractServlet extends HttpServlet { try { configuration = MirPropertiesConfiguration.instance(); } - catch (PropertiesConfigExc e) { - throw new ServletException(e); + catch (Throwable t) { + throw new ServletException("can't read configuration: " + t.toString()); } - String dbUser = configuration.getString("Database.Username"); - String dbPassword = configuration.getString("Database.Password"); - String dbHost = configuration.getString("Database.Host"); - String dbAdapName = configuration.getString("Database.Adaptor"); - String dbName = configuration.getString("Database.Name"); + logger = new LoggerWrapper("Servlet"); - DatabaseAdaptor adaptor; try { - adaptor = (DatabaseAdaptor) Class.forName(dbAdapName).newInstance(); + MirGlobal.localizer(); } - catch (Exception e) { - throw new ServletException("Could not load DB adapator: " + - e.toString()); + catch (Throwable t) { + logger.fatal("can't get localizer: " + t.toString()); + throw new ServletException("can't get localizer: " + t.toString()); } - String dbDriver; - String dbUrl; - try { - dbDriver = adaptor.getDriver(); - dbUrl = adaptor.getURL(dbUser, dbPassword, dbHost); - } - catch (Exception e) { - throw new ServletException(e); - } + String dbUser = configuration.getString("Database.Username"); + String dbPassword = configuration.getString("Database.Password"); + String dbHost = configuration.getString("Database.Host"); + String dbPort = configuration.getString("Database.Port"); + String dbAdapName = configuration.getString("Database.Adaptor"); + String dbName = configuration.getString("Database.Name"); + String dbDriver = configuration.getString("Database.Driver"); + String dbUrl = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName; + int dbMin = configuration.getInteger("Database.poolMin", 1); + int dbMax = configuration.getInteger("Database.poolMax", 10); JDBCPoolMetaData meta = new JDBCPoolMetaData(); meta.setDbname(dbName); @@ -183,42 +178,61 @@ public abstract class AbstractServlet extends HttpServlet { meta.setUserName(dbUser); meta.setPassword(dbPassword); meta.setJNDIName("mir"); - meta.setMaximumSize(10); - meta.setMinimumSize(1); + meta.setMaximumSize(dbMax); + meta.setMinimumSize(dbMin); meta.setPoolPreparedStatements(false); meta.setCacheEnabled(false); meta.setCacheSize(15); meta.setDebugging(false); -// meta.setLogFile(dblogfile+".pool"); SQLManager manager = SQLManager.getInstance(); + JDBCPool pool = null; if (manager != null) { pool = manager.createPool(meta); } + + Connection connection; + try { + connection = pool.requestConnection(); + JDBCPool.closeConnection(connection); + } + catch (Throwable t) { + logger.fatal("Can't connect to database: " + t.toString()); + throw new ServletException("Can't connect to database: " + t.toString()); + } } - + private void setEncoding(HttpServletRequest request){ - try { - Class reqClass = request.getClass(); - Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class}); - String encoding = configuration.getString("Mir.DefaultEncoding"); - method.invoke(request, new Object[]{encoding}); - } catch (NoSuchMethodException e) { - // TODO set the encoding in a zapata-way - } catch (SecurityException e) { - logger.error(e.getMessage()); - e.printStackTrace(); - } catch (IllegalArgumentException e) { - logger.error(e.getMessage()); - e.printStackTrace(); - } catch (IllegalAccessException e) { - logger.error(e.getMessage()); - e.printStackTrace(); - } catch (InvocationTargetException e) { - logger.error(e.getMessage()); - e.printStackTrace(); - } + try { + logger.info("Request has encoding: " + request.getCharacterEncoding()); + logger.info("Config stipulates encoding: " + configuration.getString("Mir.DefaultHTMLCharset")); + Class reqClass = request.getClass(); + Method method = reqClass.getMethod("setCharacterEncoding", new Class[]{String.class}); + String encoding = configuration.getString("Mir.DefaultHTMLCharset"); + method.invoke(request, new Object[]{encoding}); + logger.info("Request now has encoding: " + request.getCharacterEncoding()); + } + catch (NoSuchMethodException e) { + // TODO set the encoding in a zapata-way +// logger.warn("set encoding not yet implemented: " + e.getMessage()); + } + catch (SecurityException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (IllegalArgumentException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (IllegalAccessException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } + catch (InvocationTargetException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + } } protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -226,8 +240,7 @@ public abstract class AbstractServlet extends HttpServlet { } protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if ( (configuration.getString("RootUri") == null) || - configuration.getString("RootUri").equals("")) { + if ((configuration.getString("RootUri") == null) || configuration.getString("RootUri").equals("")) { configuration.setProperty("RootUri", request.getContextPath()); } setEncoding(request); @@ -236,4 +249,29 @@ public abstract class AbstractServlet extends HttpServlet { abstract public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; + /** + * Selects the language for the response. + * + * @param session + * @param aRequest + */ + protected void checkLanguage(HttpSession aSession, HttpServletRequest aRequest) { + String requestLanguage = aRequest.getParameter("language"); + String sessionLanguage = (String) aSession.getAttribute("language"); + String acceptLanguage = aRequest.getLocale().getLanguage(); + String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en"); + + String language = requestLanguage; + + if (language==null) + language = sessionLanguage; + + if (language==null) + language = acceptLanguage; + + if (language==null) + language = defaultLanguage; + + setLanguage(aSession, language); + } }