KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > servlet > ConnectionServlet


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.servlet;
22
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 import java.util.Properties JavaDoc;
27
28 import javax.servlet.ServletContext JavaDoc;
29
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import com.methodhead.persistable.ConnectionSingleton;
35
36 /**
37  * <p>
38  * A servlet that initializes {@link
39  * com.methodhead.persistable.ConnectionSingleton ConnectionSingleton}. The
40  * following init-param may be specified:
41  * </p>
42  * <ul>
43  * <li>
44  * <strong>dbproperties</strong>: the path of a properties file containing
45  * the connection parameters <tt>ConnectionSingleton</tt> expects (i.e.,
46  * <tt>driver</tt>, <tt>uri</tt>, <tt>user</tt>, and <tt>password</tt>).
47  * If the path begins with a forward slash it is assumed to be an absolute
48  * path, otherwise it is assumed to be a path relative to the context's
49  * root. For example: <tt>WEB-INF/db.properties</tt> or
50  * <tt>/etc/db.properties</tt>. If this parameter is not specified, it
51  * defaults to <tt>WEB-INF/db.properties</tt>.
52  * </li>
53  * </ul>
54  */

55 public class ConnectionServlet extends HttpServlet JavaDoc {
56
57   // constructors /////////////////////////////////////////////////////////////
58

59   // constants ////////////////////////////////////////////////////////////////
60

61   // classes //////////////////////////////////////////////////////////////////
62

63   // methods //////////////////////////////////////////////////////////////////
64

65   public void init() {
66     try {
67       String JavaDoc dbProperties = getInitParameter( "dbproperties" );
68
69       if ( dbProperties == null ) {
70         getServletContext().log(
71           "ConnectionServlet: dbproperties init-param has not been set; " +
72           "defaulting to WEB-INF/db.properties" );
73         dbProperties =
74           getServletContext().getRealPath( "WEB-INF/db.properties" );
75       }
76
77       String JavaDoc file = dbProperties;
78       if ( !dbProperties.startsWith( "/" ) ) {
79         file = getServletContext().getRealPath( dbProperties );
80
81         if ( file == null ) {
82           getServletContext().log(
83             "ConnectionServlet: Couldn't get real path for " + dbProperties +
84             "; defaulting to WEB-INF/db.properties." );
85           file = getServletContext().getRealPath( "WEB-INF/db.properties" );
86         }
87       }
88
89       InputStream JavaDoc in =
90         new FileInputStream JavaDoc( file );
91
92       Properties JavaDoc dbProps = new Properties JavaDoc();
93       dbProps.load( in );
94
95       in.close();
96
97       ConnectionSingleton.init( dbProps );
98     }
99     catch ( Exception JavaDoc e ) {
100       getServletContext().log(
101         "ConnectionServlet: Unexpected exception while initializing " +
102         "ConnectionServlet with init param dbproperties = " +
103         getInitParameter( "dbproperties" ) + ": " + e );
104     }
105   }
106
107   public void doGet(
108     HttpServletRequest JavaDoc req,
109     HttpServletResponse JavaDoc res) {
110   }
111
112   // properties ///////////////////////////////////////////////////////////////
113

114   // attributes ///////////////////////////////////////////////////////////////
115
}
116
Popular Tags