KickJava   Java API By Example, From Geeks To Geeks.

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


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.mail.Mail;
35
36 /**
37  * <p>
38  * A servlet that initializes {@link
39  * com.methodhead.mail.Mail Mail} singleton. The
40  * following init-param may be specified:
41  * </p>
42  * <ul>
43  * <li>
44  * <strong>mailproperties</strong>: the path of a properties file containing
45  * the properties <tt>Mail</tt> expects.
46  * If the path begins with a forward slash it is assumed to be an absolute
47  * path, otherwise it is assumed to be a path relative to the context's
48  * root. For example: <tt>WEB-INF/mail.properties</tt> or
49  * <tt>/etc/mail.properties</tt>. If this parameter is not specified, it
50  * defaults to <tt>WEB-INF/mail.properties</tt>.
51  * </li>
52  * </ul>
53  */

54 public class MailServlet extends HttpServlet JavaDoc {
55
56   // constructors /////////////////////////////////////////////////////////////
57

58   // constants ////////////////////////////////////////////////////////////////
59

60   // classes //////////////////////////////////////////////////////////////////
61

62   // methods //////////////////////////////////////////////////////////////////
63

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

109   // attributes ///////////////////////////////////////////////////////////////
110
}
111
Popular Tags