KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > MailTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.jsp.tag;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import javax.mail.*;
40 import javax.mail.internet.*;
41 import javax.activation.*;
42
43 public class MailTag extends MapTag
44 {
45     protected String JavaDoc _url;
46     protected Properties _properties;
47     protected Transport _transport;
48     protected Session _session;
49
50     public void init()
51     {
52         super.init();
53
54         _url = null;
55         _transport = null;
56         _export = null;
57         _properties = null;
58         _session = null;
59     }
60
61     public Transport getTransport()
62     {
63         return _transport;
64     }
65
66     public Session getSession()
67     {
68         return _session;
69     }
70
71     public MailTag getTransportSource()
72     {
73         MailTag transportSource = null;
74
75         if (_transport != null)
76         {
77             return this;
78         }
79
80         String JavaDoc url = _url;
81
82         if (url == null)
83         {
84             transportSource = (MailTag)getAncestorTag("com.micronova.jsp.tag.MailTag");
85
86             if (transportSource != null)
87             {
88                 return transportSource.getTransportSource();
89             }
90         }
91         else
92         {
93             try
94             {
95                 Properties properties = _properties;
96
97                 if (properties == null)
98                 {
99                     properties = new Properties();
100                 }
101
102                 URLName urlName = new URLName(url);
103
104                 Session session = MailTransport.getSession(properties, urlName);
105
106                 Transport transport = session.getTransport(urlName);
107
108                 transport.connect();
109
110                 _session = session;
111
112                 _transport = transport;
113                 
114                 transportSource = this;
115             }
116             catch (Exception JavaDoc e)
117             {
118                 e.printStackTrace();
119             }
120         }
121
122         return transportSource;
123     }
124
125     public void closeTransport()
126     {
127         Transport transport = _transport;
128
129         if (transport != null)
130         {
131             _transport = null;
132
133             try
134             {
135                 transport.close();
136             }
137             catch (Exception JavaDoc e)
138             {
139                 e.printStackTrace();
140             }
141         }
142     }
143
144     protected void cleanup()
145     {
146         closeTransport();
147
148         super.cleanup();
149     }
150
151     public void setUrl(Object JavaDoc expression) throws Exception JavaDoc
152     {
153         _url = (String JavaDoc)evaluateAttribute("url", expression, String JavaDoc.class);
154
155         closeTransport();
156     }
157
158     public void setProperties(Object JavaDoc expression) throws Exception JavaDoc
159     {
160         NestedMap propertyMap = (NestedMap)(new NestedMap(expression)).get("__param");
161
162         Properties properties = new Properties();
163
164         Iterator iterator = propertyMap.entrySet().iterator();
165
166         while (iterator.hasNext())
167         {
168             Map.Entry entry = (Map.Entry)iterator.next();
169
170             properties.put(entry.getKey().toString(), entry.getValue().toString());
171         }
172
173         _properties = properties;
174     }
175
176     public Object JavaDoc processValue(Object JavaDoc tagValue) throws Exception JavaDoc
177     {
178         if (tagValue != null)
179         {
180             NestedMap map = (NestedMap)tagValue;
181
182             if (!map.isEmpty())
183             {
184                 String JavaDoc from = map.getString("from");
185
186                 MailTag transportSource = getTransportSource();
187
188                 Transport transport = transportSource.getTransport();
189                 Session session = transportSource.getSession();
190
191                 MailTransport.send(session, transport, map);
192             }
193         }
194
195         return tagValue;
196     }
197 }
198
Popular Tags