001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.servlet;
022:
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025:
026: import java.util.Properties;
027:
028: import javax.servlet.ServletContext;
029:
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import com.methodhead.persistable.ConnectionSingleton;
035:
036: /**
037: * <p>
038: * A servlet that initializes {@link
039: * com.methodhead.persistable.ConnectionSingleton ConnectionSingleton}. The
040: * following init-param may be specified:
041: * </p>
042: * <ul>
043: * <li>
044: * <strong>dbproperties</strong>: the path of a properties file containing
045: * the connection parameters <tt>ConnectionSingleton</tt> expects (i.e.,
046: * <tt>driver</tt>, <tt>uri</tt>, <tt>user</tt>, and <tt>password</tt>).
047: * If the path begins with a forward slash it is assumed to be an absolute
048: * path, otherwise it is assumed to be a path relative to the context's
049: * root. For example: <tt>WEB-INF/db.properties</tt> or
050: * <tt>/etc/db.properties</tt>. If this parameter is not specified, it
051: * defaults to <tt>WEB-INF/db.properties</tt>.
052: * </li>
053: * </ul>
054: */
055: public class ConnectionServlet extends HttpServlet {
056:
057: // constructors /////////////////////////////////////////////////////////////
058:
059: // constants ////////////////////////////////////////////////////////////////
060:
061: // classes //////////////////////////////////////////////////////////////////
062:
063: // methods //////////////////////////////////////////////////////////////////
064:
065: public void init() {
066: try {
067: String dbProperties = getInitParameter("dbproperties");
068:
069: if (dbProperties == null) {
070: getServletContext()
071: .log(
072: "ConnectionServlet: dbproperties init-param has not been set; "
073: + "defaulting to WEB-INF/db.properties");
074: dbProperties = getServletContext().getRealPath(
075: "WEB-INF/db.properties");
076: }
077:
078: String file = dbProperties;
079: if (!dbProperties.startsWith("/")) {
080: file = getServletContext().getRealPath(dbProperties);
081:
082: if (file == null) {
083: getServletContext()
084: .log(
085: "ConnectionServlet: Couldn't get real path for "
086: + dbProperties
087: + "; defaulting to WEB-INF/db.properties.");
088: file = getServletContext().getRealPath(
089: "WEB-INF/db.properties");
090: }
091: }
092:
093: InputStream in = new FileInputStream(file);
094:
095: Properties dbProps = new Properties();
096: dbProps.load(in);
097:
098: in.close();
099:
100: ConnectionSingleton.init(dbProps);
101: } catch (Exception e) {
102: getServletContext()
103: .log(
104: "ConnectionServlet: Unexpected exception while initializing "
105: + "ConnectionServlet with init param dbproperties = "
106: + getInitParameter("dbproperties")
107: + ": " + e);
108: }
109: }
110:
111: public void doGet(HttpServletRequest req, HttpServletResponse res) {
112: }
113:
114: // properties ///////////////////////////////////////////////////////////////
115:
116: // attributes ///////////////////////////////////////////////////////////////
117: }
|