KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > mail > session > MailSession


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: MailSession.java,v $
31  * Revision 1.2 2005/04/09 17:20:01 colinmacleod
32  * Changed copyright text to GPL v2 explicitly.
33  *
34  * Revision 1.1.1.1 2005/03/10 17:51:16 colinmacleod
35  * Restructured ivata op around Hibernate/PicoContainer.
36  * Renamed ivata groupware.
37  *
38  * Revision 1.6 2004/11/12 18:16:08 colinmacleod
39  * Ordered imports.
40  *
41  * Revision 1.5 2004/11/12 15:57:25 colinmacleod
42  * Removed dependencies on SSLEXT.
43  * Moved Persistence classes to ivata masks.
44  *
45  * Revision 1.4 2004/07/13 19:48:12 colinmacleod
46  * Moved project to POJOs from EJBs.
47  * Applied PicoContainer to services layer (replacing session EJBs).
48  * Applied Hibernate to persistence layer (replacing entity EJBs).
49  *
50  * Revision 1.3 2004/03/21 21:16:39 colinmacleod
51  * Shortened name to ivata op.
52  *
53  * Revision 1.2 2004/02/01 22:07:32 colinmacleod
54  * Added full names to author tags
55  *
56  * Revision 1.1.1.1 2004/01/27 20:59:58 colinmacleod
57  * Moved ivata openportal to SourceForge..
58  *
59  * Revision 1.2 2003/10/15 14:11:33 colin
60  * fixing for XDoclet
61  *
62  * Revision 1.1 2003/02/24 19:09:24 colin
63  * moved to business
64  *
65  * Revision 1.3 2003/02/04 17:39:22 colin
66  * copyright notice
67  *
68  * Revision 1.2 2002/09/09 11:00:38 colin
69  * made session classes serializable :-)
70  *
71  * Revision 1.3 2002/09/09 08:44:52 colin
72  * made new mail session class to abstract the non-serializable JavaMail session
73  *
74  * Revision 1.1 2002/09/09 08:26:37 colin
75  * first version of MailSession logs into session everytime
76  * -----------------------------------------------------------------------------
77  */

78 package com.ivata.groupware.business.mail.session;
79
80 import java.security.NoSuchProviderException JavaDoc;
81 import java.util.Properties JavaDoc;
82
83 import javax.mail.MessagingException JavaDoc;
84 import javax.mail.Session JavaDoc;
85 import javax.mail.Store JavaDoc;
86 import javax.naming.AuthenticationException JavaDoc;
87
88 import org.picocontainer.PicoContainer;
89
90 import com.ivata.groupware.admin.security.server.AbstractSecuritySession;
91 import com.ivata.groupware.admin.security.server.SecurityServerException;
92 import com.ivata.groupware.admin.security.user.UserDO;
93 import com.ivata.mask.util.SystemException;
94
95
96 /**
97  * <p><code>javax.mail.Session</code> is not serializable and cannot
98  * be passed from client to server. This class stores the login details
99  * an provides a new session instance when required, by logging in
100  * again.</p>
101  *
102  * @since 2002-09-08
103  * @author Colin MacLeod
104  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
105  * @version $Revision: 1.2 $
106  */

107 public class MailSession extends AbstractSecuritySession {
108     /**
109      * <p>Contains all of the properties necessary to log into the
110      * <code>javax.mail.Session</code>.</p>
111      */

112     private Properties JavaDoc mailProperties = null;
113
114     /**
115      * <p>Used to autheticate this mail session, whenever the
116      * <code>javax.mail.Session</code> is recreated.</p>
117      */

118     public MailAuthenticator authenticator;
119
120     /**
121      * Construct a new mail session.
122      */

123     public MailSession(PicoContainer container, UserDO user)
124             throws SystemException {
125         super(container, user);
126     }
127
128     /**
129      * <p>Logs into the mail session for the first time. This stores the
130      * user name and password so that the session can be continued again
131      * later.</p>
132      *
133      * @userName name of the user to log in.
134      * @param password clear-text user's password to log into the mail
135      * server.
136      * @param mailProperties all of the mail properties necessary to login
137      * to the system.
138      * @throws AuthenticationException thrown by JavaMail if the user cannot
139      * login.
140      * @return newly created mail session.
141      */

142     public Session JavaDoc login(final String JavaDoc password,
143             final Properties JavaDoc mailProperties)
144         throws SecurityServerException, NoSuchProviderException JavaDoc,
145             MessagingException JavaDoc {
146         authenticator = new MailAuthenticator(
147                 mailProperties.getProperty("mail.user"), password);
148         this.mailProperties = mailProperties;
149
150         return getJavaMailSession();
151     }
152
153     /**
154      * <p>Return the current mail session as a
155      * <code>javax.mail.Session</code> instance. This involves logging in
156      * again.</p>
157      *
158      * @throws AuthenticationException thrown by JavaMail if the user cannot login.
159      * @throws NoSuchProviderException thrown by JavaMail if the user cannot login.
160      * @throws MessagingException thrown by JavaMail if the user cannot login.
161      * @return the current mail session as a
162      * <code>javax.mail.Session</code> instance.
163      */

164     public Session JavaDoc getJavaMailSession()
165         throws SecurityServerException, NoSuchProviderException JavaDoc,
166             MessagingException JavaDoc {
167         Session JavaDoc javaSession = Session.getInstance(mailProperties, authenticator);
168         Store JavaDoc store = javaSession.getStore("imap");
169         if (!store.isConnected()) {
170             store.connect();
171         }
172         store.close();
173         return javaSession;
174     }
175 }
176
Popular Tags