KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mailsb > ClientMailer


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: ClientMailer.java,v 1.3 2004/04/19 06:39:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package mailsb;
28
29 import java.io.BufferedReader;
30 import java.io.InputStreamReader;
31 import java.io.IOException;
32 import javax.naming.InitialContext;
33 import javax.naming.Context;
34 import javax.rmi.PortableRemoteObject;
35
36 /**
37  * Sample for Mailer Session Bean. Usage: java mailsb.ClientMailer
38  * @author Florent Benoit
39  * @author Ludovic Bert
40  */

41 public class ClientMailer {
42
43     /**
44      * Session case
45      */

46     private static final int SESSION_CASE = 1;
47
48     /**
49      * MimePartDatasource case
50      */

51     private static final int MIMEPARTDS_CASE = 2;
52
53     /**
54      * Content of the mail
55      */

56     private static String content = null;
57
58     /**
59      * Constructor. Hide constructor as it is an utility class
60      */

61     private ClientMailer() {
62
63     }
64
65     /**
66      * Read from bufferedReader the text which is given by the user after the
67      * given prompt.
68      * @param bufferedReader the buffer to read input.
69      * @param prompt the prompt asking the user.
70      * @return the text given by the user.
71      * @throws IOException if it can't the bufferedReader.
72      */

73     private static String getTextFromUser(BufferedReader bufferedReader, String prompt) throws IOException {
74
75         boolean isSet = false;
76         String txt = null;
77         while (!isSet) {
78             System.out.print(prompt + " :");
79             txt = bufferedReader.readLine();
80             if (!txt.equals("")) {
81                 isSet = true;
82             } else {
83                 System.out.println("'" + prompt + "' can't be empty.");
84             }
85         }
86         return txt;
87     }
88
89     /**
90      * Main method of the class
91      * @param args the arguments of the program
92      */

93 public static void main(String[] args) {
94
95         //Check if there are valid args
96
if (args.length < 1) {
97             System.err.println("Syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>");
98             System.exit(2);
99         }
100
101
102         int argType = 0;
103         if (args[0].equalsIgnoreCase("SessionMailer")) {
104             argType = SESSION_CASE;
105         } else if (args[0].equalsIgnoreCase("MimePartDSMailer")) {
106             argType = MIMEPARTDS_CASE;
107         } else {
108             System.err.println("Invalid type '" + args[0] + "', valid syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>");
109             System.exit(2);
110         }
111
112         //Get the initial context
113
Context initialContext = null;
114         try {
115             initialContext = new InitialContext();
116         } catch (Exception e) {
117             System.err.println("Cannot get initial context for JNDI: " + e.getMessage());
118             System.exit(2);
119         }
120
121         switch (argType) {
122         case SESSION_CASE :
123             sessionMailer(initialContext);
124             break;
125         case MIMEPARTDS_CASE :
126             if (args.length > 1) {
127                 content = "Content of mail :";
128                 for (int i = 1; i < args.length; i++) {
129                     content += args[i];
130                 }
131             }
132             mimePartDSMailer(initialContext);
133             break;
134         default :
135
136         }
137
138
139     }
140     /**
141      * Deal with the MimePartDSMailer bean
142      * @param initialContext the initial context
143      */

144     private static void mimePartDSMailer(Context initialContext) {
145
146         // Connecting to the mailer bean MailerHome thru JNDI
147
MimePartDSMailerHome mimePartDSMailerHome = null;
148         try {
149             System.out.print("Looking up the Session mailer home...");
150             mimePartDSMailerHome = (MimePartDSMailerHome) PortableRemoteObject.narrow(initialContext
151                     .lookup("MimePartDSMailerHome"), MimePartDSMailerHome.class);
152             System.out.println("OK !");
153         } catch (Exception e) {
154             System.out.println("failed !");
155             System.err.println("Cannot lookup MimePartDSMailerHome: " + e.getMessage());
156             System.exit(2);
157         }
158
159         // MimePartDSMailerBean creation
160
MimePartDSMailer mimePartDSMailer = null;
161         try {
162             System.out.print("Creating a MimePartDS mailer bean ...");
163             mimePartDSMailer = mimePartDSMailerHome.create("MimePartDS mailer");
164             System.out.println("OK !");
165         } catch (Exception e) {
166             System.out.println("failed !");
167             System.err.println("Cannot create MimePartDSMailerBean: " + e.getMessage());
168             System.exit(2);
169         }
170
171         if (content == null) {
172             //Read the value from the user
173
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
174
175             try {
176                 System.out.println("Content of the mail (Only one line)");
177                 content = getTextFromUser(bufferedReader, "Content");
178             } catch (IOException e) {
179                 System.err.println("Cannot read line from the bufferedReader : " + e.getMessage());
180                 System.exit(2);
181             }
182         }
183
184         //Set the message with a recipient, a subject and a content
185
try {
186             System.out.print("Setting the message with given content '" + content + "'...");
187             mimePartDSMailer.setMessage(content);
188             System.out.println("OK !");
189         } catch (Exception e) {
190             System.out.println("failed !");
191             System.err.println("Cannot set args of the message : " + e);
192             System.exit(2);
193         }
194
195         //And send the message
196
try {
197             System.out.print("Sending the message...");
198             mimePartDSMailer.send();
199             System.out.println("OK !");
200         } catch (Exception e) {
201             System.out.println("Failed !");
202             System.err.println("Cannot send the message with MailerBean:" + e);
203             System.exit(2);
204         }
205     }
206
207     /**
208      * Deal with the SessionMailer bean
209      * @param initialContext the initial context
210      */

211     private static void sessionMailer(Context initialContext) {
212         // Connecting to the mailer bean MailerHome thru JNDI
213
SessionMailerHome sessionMailerHome = null;
214         try {
215             System.out.print("Looking up the Session mailer home...");
216             sessionMailerHome = (SessionMailerHome) PortableRemoteObject.narrow(initialContext
217                     .lookup("SessionMailerHome"), SessionMailerHome.class);
218             System.out.println("OK !");
219         } catch (Exception e) {
220             System.out.println("failed !");
221             System.err.println("Cannot lookup MailerHome: " + e.getMessage());
222             System.exit(2);
223         }
224
225         // MailerBean creation
226
SessionMailer sessionMailer = null;
227         try {
228             System.out.print("Creating a session mailer bean...");
229             sessionMailer = sessionMailerHome.create("mailer");
230             System.out.println("OK !");
231         } catch (Exception e) {
232             System.out.println("failed !");
233             System.err.println("Cannot create MailerBean: " + e.getMessage());
234             System.exit(2);
235         }
236
237         String toRecipient = null;
238         String subject = null;
239         String content = null;
240
241         //Read the value from the user
242
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
243
244         try {
245             System.out.println("Recipient is the 'to' field of a mail");
246             toRecipient = getTextFromUser(bufferedReader, "TO");
247
248             System.out.println("Subject of the mail");
249             subject = getTextFromUser(bufferedReader, "Subject");
250
251             System.out.println("Content of the mail (Only one line)");
252             content = getTextFromUser(bufferedReader, "Content");
253
254         } catch (IOException e) {
255             System.err.println("Cannot read line from the bufferedReader : " + e.getMessage());
256             System.exit(2);
257         }
258
259         //Set the message with a recipient, a subject and a content
260
try {
261             System.out.print("Setting the message with given args...");
262             sessionMailer.setMessage(toRecipient, subject, content);
263             System.out.println("OK !");
264         } catch (Exception e) {
265             System.out.println("failed !");
266             System.err.println("Cannot set args of the message : " + e);
267             System.exit(2);
268         }
269
270         //And send the message
271
try {
272             System.out.print("Sending the message...");
273             sessionMailer.send();
274             System.out.println("OK !");
275         } catch (Exception e) {
276             System.out.println("Failed !");
277             System.err.println("Cannot send the message with MailerBean:" + e);
278             System.exit(2);
279         }
280     }
281
282 }
Popular Tags