001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: ClientMailer.java 4618 2004-04-19 06:39:30Z benoitf $
024: * --------------------------------------------------------------------------
025: */package mailsb;
026:
027: import java.io.BufferedReader;
028: import java.io.InputStreamReader;
029: import java.io.IOException;
030: import javax.naming.InitialContext;
031: import javax.naming.Context;
032: import javax.rmi.PortableRemoteObject;
033:
034: /**
035: * Sample for Mailer Session Bean. Usage: java mailsb.ClientMailer
036: * @author Florent Benoit
037: * @author Ludovic Bert
038: */
039: public class ClientMailer {
040:
041: /**
042: * Session case
043: */
044: private static final int SESSION_CASE = 1;
045:
046: /**
047: * MimePartDatasource case
048: */
049: private static final int MIMEPARTDS_CASE = 2;
050:
051: /**
052: * Content of the mail
053: */
054: private static String content = null;
055:
056: /**
057: * Constructor. Hide constructor as it is an utility class
058: */
059: private ClientMailer() {
060:
061: }
062:
063: /**
064: * Read from bufferedReader the text which is given by the user after the
065: * given prompt.
066: * @param bufferedReader the buffer to read input.
067: * @param prompt the prompt asking the user.
068: * @return the text given by the user.
069: * @throws IOException if it can't the bufferedReader.
070: */
071: private static String getTextFromUser(
072: BufferedReader bufferedReader, String prompt)
073: throws IOException {
074:
075: boolean isSet = false;
076: String txt = null;
077: while (!isSet) {
078: System.out.print(prompt + " :");
079: txt = bufferedReader.readLine();
080: if (!txt.equals("")) {
081: isSet = true;
082: } else {
083: System.out.println("'" + prompt + "' can't be empty.");
084: }
085: }
086: return txt;
087: }
088:
089: /**
090: * Main method of the class
091: * @param args the arguments of the program
092: */
093: public static void main(String[] args) {
094:
095: //Check if there are valid args
096: if (args.length < 1) {
097: System.err
098: .println("Syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>");
099: System.exit(2);
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
109: .println("Invalid type '"
110: + args[0]
111: + "', valid syntax is : java mailsb.ClientMailer <SessionMailer | MimePartDSMailer>");
112: System.exit(2);
113: }
114:
115: //Get the initial context
116: Context initialContext = null;
117: try {
118: initialContext = new InitialContext();
119: } catch (Exception e) {
120: System.err.println("Cannot get initial context for JNDI: "
121: + e.getMessage());
122: System.exit(2);
123: }
124:
125: switch (argType) {
126: case SESSION_CASE:
127: sessionMailer(initialContext);
128: break;
129: case MIMEPARTDS_CASE:
130: if (args.length > 1) {
131: content = "Content of mail :";
132: for (int i = 1; i < args.length; i++) {
133: content += args[i];
134: }
135: }
136: mimePartDSMailer(initialContext);
137: break;
138: default:
139:
140: }
141:
142: }
143:
144: /**
145: * Deal with the MimePartDSMailer bean
146: * @param initialContext the initial context
147: */
148: private static void mimePartDSMailer(Context initialContext) {
149:
150: // Connecting to the mailer bean MailerHome thru JNDI
151: MimePartDSMailerHome mimePartDSMailerHome = null;
152: try {
153: System.out.print("Looking up the Session mailer home...");
154: mimePartDSMailerHome = (MimePartDSMailerHome) PortableRemoteObject
155: .narrow(initialContext
156: .lookup("MimePartDSMailerHome"),
157: MimePartDSMailerHome.class);
158: System.out.println("OK !");
159: } catch (Exception e) {
160: System.out.println("failed !");
161: System.err.println("Cannot lookup MimePartDSMailerHome: "
162: + e.getMessage());
163: System.exit(2);
164: }
165:
166: // MimePartDSMailerBean creation
167: MimePartDSMailer mimePartDSMailer = null;
168: try {
169: System.out.print("Creating a MimePartDS mailer bean ...");
170: mimePartDSMailer = mimePartDSMailerHome
171: .create("MimePartDS mailer");
172: System.out.println("OK !");
173: } catch (Exception e) {
174: System.out.println("failed !");
175: System.err.println("Cannot create MimePartDSMailerBean: "
176: + e.getMessage());
177: System.exit(2);
178: }
179:
180: if (content == null) {
181: //Read the value from the user
182: BufferedReader bufferedReader = new BufferedReader(
183: new InputStreamReader(System.in));
184:
185: try {
186: System.out
187: .println("Content of the mail (Only one line)");
188: content = getTextFromUser(bufferedReader, "Content");
189: } catch (IOException e) {
190: System.err
191: .println("Cannot read line from the bufferedReader : "
192: + e.getMessage());
193: System.exit(2);
194: }
195: }
196:
197: //Set the message with a recipient, a subject and a content
198: try {
199: System.out.print("Setting the message with given content '"
200: + content + "'...");
201: mimePartDSMailer.setMessage(content);
202: System.out.println("OK !");
203: } catch (Exception e) {
204: System.out.println("failed !");
205: System.err.println("Cannot set args of the message : " + e);
206: System.exit(2);
207: }
208:
209: //And send the message
210: try {
211: System.out.print("Sending the message...");
212: mimePartDSMailer.send();
213: System.out.println("OK !");
214: } catch (Exception e) {
215: System.out.println("Failed !");
216: System.err
217: .println("Cannot send the message with MailerBean:"
218: + e);
219: System.exit(2);
220: }
221: }
222:
223: /**
224: * Deal with the SessionMailer bean
225: * @param initialContext the initial context
226: */
227: private static void sessionMailer(Context initialContext) {
228: // Connecting to the mailer bean MailerHome thru JNDI
229: SessionMailerHome sessionMailerHome = null;
230: try {
231: System.out.print("Looking up the Session mailer home...");
232: sessionMailerHome = (SessionMailerHome) PortableRemoteObject
233: .narrow(initialContext.lookup("SessionMailerHome"),
234: SessionMailerHome.class);
235: System.out.println("OK !");
236: } catch (Exception e) {
237: System.out.println("failed !");
238: System.err.println("Cannot lookup MailerHome: "
239: + e.getMessage());
240: System.exit(2);
241: }
242:
243: // MailerBean creation
244: SessionMailer sessionMailer = null;
245: try {
246: System.out.print("Creating a session mailer bean...");
247: sessionMailer = sessionMailerHome.create("mailer");
248: System.out.println("OK !");
249: } catch (Exception e) {
250: System.out.println("failed !");
251: System.err.println("Cannot create MailerBean: "
252: + e.getMessage());
253: System.exit(2);
254: }
255:
256: String toRecipient = null;
257: String subject = null;
258: String content = null;
259:
260: //Read the value from the user
261: BufferedReader bufferedReader = new BufferedReader(
262: new InputStreamReader(System.in));
263:
264: try {
265: System.out.println("Recipient is the 'to' field of a mail");
266: toRecipient = getTextFromUser(bufferedReader, "TO");
267:
268: System.out.println("Subject of the mail");
269: subject = getTextFromUser(bufferedReader, "Subject");
270:
271: System.out.println("Content of the mail (Only one line)");
272: content = getTextFromUser(bufferedReader, "Content");
273:
274: } catch (IOException e) {
275: System.err
276: .println("Cannot read line from the bufferedReader : "
277: + e.getMessage());
278: System.exit(2);
279: }
280:
281: //Set the message with a recipient, a subject and a content
282: try {
283: System.out.print("Setting the message with given args...");
284: sessionMailer.setMessage(toRecipient, subject, content);
285: System.out.println("OK !");
286: } catch (Exception e) {
287: System.out.println("failed !");
288: System.err.println("Cannot set args of the message : " + e);
289: System.exit(2);
290: }
291:
292: //And send the message
293: try {
294: System.out.print("Sending the message...");
295: sessionMailer.send();
296: System.out.println("OK !");
297: } catch (Exception e) {
298: System.out.println("Failed !");
299: System.err
300: .println("Cannot send the message with MailerBean:"
301: + e);
302: System.exit(2);
303: }
304: }
305:
306: }
|