KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > EmailApp


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email;
31
32 import java.util.Properties JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import javax.mail.AuthenticationFailedException JavaDoc;
36 import javax.mail.MessagingException JavaDoc;
37 import javax.mail.Session JavaDoc;
38 import javax.mail.Store JavaDoc;
39
40 import nextapp.echo2.app.ApplicationInstance;
41 import nextapp.echo2.app.Window;
42
43 /**
44  * Email Application Instance.
45  */

46 public class EmailApp extends ApplicationInstance {
47
48     /**
49      * Flag indicating whether we should use fake e-mail data. Enabling this
50      * flag will cause the <code>FauxStore</code> e-mail store to be used and
51      * will disable sending of messages.
52      */

53     public static final boolean FAUX_MODE;
54     public static final String JavaDoc MAIL_DOMAIN, RECEIVE_MAIL_SERVER, RECEIVE_PROTOCOL, SEND_MAIL_SERVER, SEND_MAIL_PORT;
55     public static final int MESSAGES_PER_PAGE;
56
57     static {
58         // Static initializer to retrieve information from configuration properties file.
59
ResourceBundle JavaDoc config = ResourceBundle.getBundle("/echo2example/email/Configuration");
60         FAUX_MODE = "true".equals(config.getString("FauxMode"));
61         MAIL_DOMAIN = config.getString("MailDomain");
62         RECEIVE_MAIL_SERVER = config.getString("ReceiveMailServer");
63         RECEIVE_PROTOCOL = config.getString("ReceiveProtocol");
64         SEND_MAIL_SERVER = config.getString("SendMailServer");
65         SEND_MAIL_PORT = config.getString("SendMailPort");
66         MESSAGES_PER_PAGE = Integer.parseInt(config.getString("MessagesPerPage"));
67     }
68     
69     /**
70      * The user name of the currently logged in user. This property
71      * will be null when no user is logged in.
72      */

73     private String JavaDoc emailAddress;
74     
75     /**
76      * The <code>javax.mail.Store</code> used to retrieve messages from the mail server.
77      * See the Sun JavaMail API Specification for details.
78      */

79     private Store JavaDoc store;
80     
81     /**
82      * The <code>javax.mail.Session</code> used to connect to the mail server.
83      * See the Sun JavaMail API Specification for details.
84      */

85     private Session JavaDoc mailSession;
86     
87     /**
88      * Convenience method to return the active email application as a
89      * <code>EmailApp</code>.
90      *
91      * @return the active <code>EmailApp</code>
92      */

93     public static EmailApp getApp() {
94         return (EmailApp) getActive();
95     }
96     
97     /**
98      * Connects to the mail server with the given e-mail address and
99      * password. Displays the <code>MailScreen</code> on success.
100      *
101      * @param emailAddress e-mail address
102      * @param password the password
103      * @return true if the application was able to connect to the
104      * server using the specified information, false if not.
105      */

106     public boolean connect(String JavaDoc emailAddress, String JavaDoc password) {
107         Properties JavaDoc properties = System.getProperties();
108         if (!FAUX_MODE) {
109             properties.put("mail.smtp.host", SEND_MAIL_SERVER);
110             properties.put("mail.smtp.port", SEND_MAIL_PORT);
111         }
112         try {
113             mailSession = Session.getDefaultInstance(properties, null);
114             store = mailSession.getStore(RECEIVE_PROTOCOL);
115             store.connect(RECEIVE_MAIL_SERVER, emailAddress, password);
116
117             // Store user name.
118
this.emailAddress = emailAddress;
119             
120             // Display MailScreen.
121
MailScreen mailScreen = new MailScreen();
122             mailScreen.setStore(store);
123             getDefaultWindow().setContent(mailScreen);
124         } catch (AuthenticationFailedException JavaDoc ex) {
125             // Return false to indicate the user was not successfully authenticated.
126
return false;
127         } catch (MessagingException JavaDoc ex) {
128             processFatalException(ex);
129         }
130         
131         // Return indicating that the user was successfully authenticated.
132
return true;
133     }
134     
135     /**
136      * Disconnects the session with the mail server and displays
137      * the authentication screen.
138      */

139     public void disconnect() {
140         if (store != null) {
141             try {
142                 store.close();
143             } catch (MessagingException JavaDoc ex) {
144                 // Do nothing.
145
}
146             store = null;
147         }
148         
149         emailAddress = null;
150         getDefaultWindow().setContent(new LoginScreen());
151     }
152
153     /**
154      * Returns the email address of the active user, or null if none.
155      *
156      * @return the email address
157      */

158     public String JavaDoc getEmailAddress() {
159         return emailAddress;
160     }
161     
162     /**
163      * Returns the active JavaMail <code>Session</code> object.
164      *
165      * @return the <code>Session</code>
166      */

167     public Session JavaDoc getMailSession() {
168         return mailSession;
169     }
170
171     /**
172      * @see nextapp.echo2.app.ApplicationInstance#init()
173      */

174     public Window init() {
175         setStyleSheet(Styles.DEFAULT_STYLE_SHEET);
176         Window window = new Window();
177         window.setTitle(Messages.getString("Application.Title.Window"));
178         window.setContent(new LoginScreen());
179         return window;
180     }
181
182     /**
183      * Handles a fatal exception.
184      * This method is invoked when a component of the application
185      * encounters a fatal error that can not be resolved. This
186      * method will log off any currently logged in user and
187      * display an error dialog screen.
188      *
189      * @param ex the fatal exception
190      */

191     public void processFatalException(Exception JavaDoc ex) {
192         ex.printStackTrace();
193         disconnect();
194         MessageDialog messageDialog = new MessageDialog(Messages.getString("FatalException.Title"), ex.toString(),
195                 MessageDialog.TYPE_ERROR, MessageDialog.CONTROLS_OK);
196         getDefaultWindow().getContent().add(messageDialog);
197     }
198 }
199
Popular Tags