KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sendmail > SendMailService


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.sendmail;
20
21 import org.lucane.common.*;
22 import org.lucane.common.net.ObjectConnection;
23 import org.lucane.server.*;
24 import org.lucane.server.database.*;
25
26 import java.util.*;
27 import java.io.IOException JavaDoc;
28 import java.sql.*;
29
30 import javax.mail.MessagingException JavaDoc;
31
32
33 public class SendMailService extends Service
34 {
35     protected static String JavaDoc SMTP_HOST = "localhost";
36     
37     private DatabaseAbstractionLayer layer = null;
38     
39     public SendMailService()
40     {
41     }
42     
43     public void createTable()
44     {
45         try {
46             String JavaDoc dbDescription = getDirectory() + "db-sendmail.xml";
47             layer.getTableCreator().createFromXml(dbDescription);
48             
49             Connection connection = layer.getConnection();
50             PreparedStatement insert = connection.prepareStatement(
51                     "INSERT INTO smtphost VALUES (?)");
52             insert.setString(1, SMTP_HOST);
53             insert.execute();
54             insert.close();
55             connection.close();
56         } catch (Exception JavaDoc e) {
57             Logging.getLogger().severe("Unable to install SendMailService !");
58             e.printStackTrace();
59         }
60     }
61     
62     public void init(Server parent)
63     {
64         layer = parent.getDBLayer();
65         
66         initSmtpHost();
67     }
68     
69     public void install()
70     {
71         createTable();
72     }
73
74     private void initSmtpHost()
75     {
76         try {
77             if(!layer.hasTable("smtphost"))
78             {
79                 Logging.getLogger().warning("No mailhost yet, using default: " + SMTP_HOST);
80                 return;
81             }
82
83             Connection connection = layer.getConnection();
84             PreparedStatement select = connection.prepareStatement(
85                     "SELECT hostname FROM smtphost");
86             ResultSet rs = select.executeQuery();
87             if(rs.next())
88                 SMTP_HOST = rs.getString(1);
89             rs.close();
90             select.close();
91             connection.close();
92         } catch(SQLException e) {
93             Logging.getLogger().warning("Unable to fetch the mailhost, using default: " + SMTP_HOST);
94         }
95     }
96     
97     private void setSmtpHost(String JavaDoc host)
98     throws SQLException
99     {
100         SMTP_HOST = host;
101         
102         Connection connection = layer.getConnection();
103         PreparedStatement update = connection.prepareStatement(
104                 "UPDATE smtphost SET hostname=?");
105         update.setString(1, host);
106         update.execute();
107         update.close();
108         connection.close();
109     }
110
111     private String JavaDoc getSmtpHost()
112     {
113         return SMTP_HOST;
114     }
115     
116     public void process(ObjectConnection oc, Message message)
117     {
118         SendMailAction action = (SendMailAction)message.getData();
119         
120         try {
121             switch(action.getAction())
122             {
123                 case SendMailAction.GET_SMTPHOST:
124                     oc.write("OK");
125                     oc.write(getSmtpHost());
126                     break;
127
128                 case SendMailAction.SET_SMTPHOST:
129                     setSmtpHost((String JavaDoc)action.getParam());
130                     oc.write("OK");
131                     break;
132                 
133                 case SendMailAction.SEND_MAIL:
134                     sendMail((HashMap)action.getParam());
135                     oc.write("OK");
136                     break;
137             }
138         }
139         catch(Exception JavaDoc e) {
140             try {
141                 e.printStackTrace();
142                 oc.write("FAILED " + e);
143             } catch(Exception JavaDoc e2) {}
144         }
145     }
146     
147     public void sendMail(HashMap params) throws IOException JavaDoc, MessagingException JavaDoc
148     {
149         //fetch parameters
150
String JavaDoc from = (String JavaDoc)params.get("from");
151         String JavaDoc subject = (String JavaDoc)params.get("subject");
152         String JavaDoc to = (String JavaDoc)params.get("to");
153         String JavaDoc cc = (String JavaDoc)params.get("cc");
154         String JavaDoc bcc = (String JavaDoc)params.get("bcc");
155         String JavaDoc content = (String JavaDoc)params.get("content");
156         String JavaDoc contentType = (String JavaDoc)params.get("content-type");
157         HashMap attach = (HashMap)params.get("attach");
158         
159         //default values
160
if(from == null)
161             from = "SendMailService@" + SMTP_HOST;
162         if(content == null)
163             content = "";
164         if(contentType == null)
165             contentType = "text/plain";
166         
167         //build mail and send it
168
SendableMail sm = new SendableMail();
169         sm.setFrom(from);
170         sm.setContent(content, contentType);
171         if(subject != null)
172             sm.setSubject(subject);
173         if(to != null)
174             sm.addTo(to);
175         if(cc != null)
176             sm.addCc(cc);
177         if(bcc != null)
178             sm.addBcc(bcc);
179         
180         //attachments loop
181
if(attach != null)
182         {
183             Iterator i = attach.keySet().iterator();
184             while(i.hasNext())
185             {
186                 String JavaDoc key = (String JavaDoc)i.next();
187                 String JavaDoc data = (String JavaDoc)attach.get(key);
188                 if(key != null && data != null)
189                     sm.attach(key, data);
190             }
191         }
192         
193         //send
194
sm.send();
195     }
196     
197 }
198
Popular Tags