001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail;
009:
010: import java.io.*;
011: import java.util.*;
012: import java.text.MessageFormat;
013: import javax.mail.*;
014: import javax.mail.event.*;
015: import javax.mail.internet.*;
016: import javax.servlet.http.*;
017:
018: import org.apache.log4j.Logger;
019:
020: import dtw.webmail.model.*;
021: import dtw.webmail.util.*;
022: import dtw.webmail.util.config.JwmaConfiguration;
023: import dtw.webmail.util.config.PostOffice;
024: import dtw.webmail.util.config.MailTransportAgent;
025:
026: /**
027: * Class that models the state of a jwma session.
028: * <p>
029: * Each instance stores the state and provides accessor and
030: * mutator operations for retrieving and changing.
031: * <p>
032: * The reference to the instance itself is stored in the
033: * <tt>HttpSession</tt> as session attribute.
034: * <p>
035: * Note that this class is actually the bridge between the web
036: * session and the mail client session, and that instances have
037: * the responsibility to manage the state of both.
038: *
039: * @author Dieter Wimberger
040: * @version 0.9.7 07/02/2003
041: */
042: public class JwmaSession implements Serializable {
043:
044: //logging
045: private static Logger log = Logger.getLogger(JwmaSession.class);
046:
047: //instance attributes
048: private boolean m_Authenticated;
049: private boolean m_LanguageToggled = true;
050:
051: //http session state related
052: private List m_BeanNames;
053: transient private HttpServletRequest m_Request;
054: transient private HttpServletResponse m_Response;
055: transient private HttpSession m_WebSession;
056: private String[] m_ViewHistory;
057:
058: //mail session state related
059: transient private PostOffice m_PostOffice;
060: transient private MailTransportAgent m_MTA;
061: private boolean m_PostOfficeConnected;
062: transient private Session m_MailSession;
063: transient private JwmaStoreImpl m_JwmaStore;
064: transient private JwmaTransportImpl m_JwmaTransport;
065: transient private JwmaAuthenticator m_Authenticator;
066:
067: //utility attributes
068: transient private JwmaConfiguration m_Configuration;
069: transient private JwmaPreferencesImpl m_Preferences;
070: transient private JwmaHtmlHelper m_HtmlHelper = new JwmaHtmlHelperImpl();
071: private String m_Username;
072: private String m_LastLogin;
073: private boolean m_NewUser = false;
074:
075: /**
076: * Constructs a <tt>JwmaSession</tt> instance.
077: *
078: * @param session reference to the actual <tt>HttpSession</tt> instance.
079: */
080: public JwmaSession(HttpSession session) {
081: m_Configuration = JwmaKernel.getReference().getConfiguration();
082: m_MTA = m_Configuration.getMTA();
083: m_PostOffice = m_Configuration.getDefaultPostOffice();
084: m_Authenticated = false;
085: m_WebSession = session;
086: m_BeanNames = new ArrayList(15);
087: m_ViewHistory = new String[2];
088: m_ViewHistory[0] = JwmaKernel.LOGIN_VIEW;
089: storeDefaultBeans();
090: }//constructor
091:
092: /*** Web session related part ***********************************************************/
093:
094: /**
095: * Sets the reference to the actual <tt>HttpServletRequest</tt>
096: * instance.
097: *
098: * @param request reference to the actual incoming
099: * <tt>HttpServletRequest</tt> instance.
100: */
101: public void setRequest(HttpServletRequest request) {
102: m_Request = request;
103: }//setRequest
104:
105: /**
106: * Returns the reference to the actual <tt>HttpServletRequest</tt>
107: * instance.
108: *
109: * @return reference to the actual <tt>HttpServletRequest</tt> instance.
110: */
111: public HttpServletRequest getRequest() {
112: return m_Request;
113: }//getRequest
114:
115: /**
116: * Sets the reference to the actual <tt>HttpServletResponse</tt>
117: * instance.
118: *
119: * @param request reference to the actual <tt>HttpServletResponse</tt>
120: * instance.
121: */
122: public void setResponse(HttpServletResponse response) {
123: m_Response = response;
124: }//setResponse
125:
126: /**
127: * Returns the reference to the actual <tt>HttpServletResponse</tt>
128: * instance.
129: *
130: * @return reference to the actual <tt>HttpServletResponse</tt>
131: * instance.
132: */
133: public HttpServletResponse getResponse() {
134: return m_Response;
135: }//getResponse
136:
137: /**
138: * Returns the reference to the actual <tt>HttpSession</tt>
139: * instance.
140: *
141: * @return reference to the actual <tt>HttpSession</tt> instance.
142: */
143: public HttpSession getWebSession() {
144: return m_WebSession;
145: }//getWebSession
146:
147: /**
148: * Tests if the associated web session is valid.
149: * This is the case if it is not new, and if it has
150: * not been invalidated.
151: *
152: * @return true if valid, false otherwise.
153: */
154: public boolean isValidWebSession() {
155: try {
156: if (m_WebSession == null || m_WebSession.isNew()) {
157: return false;
158: }
159: } catch (IllegalStateException illex) {
160: log.error("isValidWebSession()", illex);
161: return false;
162: }
163: return true;
164: }//isValidWebSession
165:
166: /**
167: * Sets the reference to the actual <tt>HttpSession</tt>
168: * instance.
169: *
170: * @param reference to the actual <tt>HttpSession</tt> instance.
171: */
172: public void setWebSession(HttpSession websession) {
173: m_WebSession = websession;
174: }//setWebSession
175:
176: private String getRequestAddress() {
177: return getRequest().getRemoteHost();
178: }//getRequestAddress
179:
180: /**
181: * Returns the value of a request parameter from this
182: * session's <tt>HttpServletRequest</tt>.
183: * This is a convenience method that trims the Strings.
184: *
185: * @return the value of the request parameter or null
186: * if it doesn't have a value.
187: */
188: public String getRequestParameter(String name) {
189: String value = getRequest().getParameter(name);
190:
191: if (value != null && value.length() > 0) {
192: return value.trim();
193: } else {
194: return null;
195: }
196: }//getRequestParameter
197:
198: /**
199: * Returns the values of a request parameter from this
200: * session's <tt>HttpServletRequest</tt>.
201: * This is a convenience method that trims the Strings.
202: *
203: * @return the value of the request parameters or null
204: * if it doesn't have any value.
205: */
206: public String[] getRequestParameters(String name) {
207: String[] value = getRequest().getParameterValues(name);
208:
209: if (value != null && value.length > 0) {
210: for (int i = 0; i < value.length; i++) {
211: value[i] = value[i].trim();
212: }
213: }
214: return value;
215: }//getRequestParameter
216:
217: /**
218: * Redirects the the actual <tt>HttpServletRequest</tt>
219: * to an abstract view.
220: * <p>
221: * The <tt>String</tt> parameter passed in <b>has to be</b>
222: * one of the defined abstract view constants.
223: *
224: * @param view representing one of the constants defining an abstract
225: * view.
226: *
227: * @see dtw.webmail.JwmaKernel#LOGIN_VIEW
228: * @see dtw.webmail.JwmaKernel#ERROR_VIEW
229: * @see dtw.webmail.JwmaKernel#LOGOUT_VIEW
230: * @see dtw.webmail.JwmaKernel#FOLDER_VIEW
231: * @see dtw.webmail.JwmaKernel#MESSAGE_VIEW
232: * @see dtw.webmail.JwmaKernel#PREFERENCES_VIEW
233: * @see dtw.webmail.JwmaKernel#COMPOSE_VIEW
234: * @see dtw.webmail.JwmaKernel#FIRSTTIME_VIEW
235: * @see dtw.webmail.JwmaKernel#CONTACTS_VIEW
236: */
237: public void redirect(String view) {
238: //update history
239: m_ViewHistory[1] = m_ViewHistory[0];
240: m_ViewHistory[0] = view;
241:
242: //retrieve URL from Kernel
243: view = JwmaKernel.getReference().getViewUrl(view);
244:
245: try {
246: m_Response.sendRedirect(m_Response.encodeRedirectUrl(view));
247: } catch (IOException ex) {
248: log.error(JwmaKernel.getReference().getLogMessage(
249: "jwma.session.redirectfailed"), ex);
250: }
251: }//redirect
252:
253: /**
254: * Redirects the the actual <tt>HttpServletRequest</tt>
255: * to the last view.
256: * <p>
257: * The last view is traced by a simple history mechanism
258: * implemented in the <tt>redirect()</tt> method.
259: */
260: public void redirectToLast() {
261: redirect(m_ViewHistory[1]);
262: }//redirectToLast
263:
264: /**
265: * Redirects the the actual <tt>HttpServletRequest</tt>
266: * to the present view.
267: * <p>
268: * The actual view is traced by a simple history mechanism
269: * implemented in the <tt>redirect()</tt> method.
270: * This method helps to acquire an updated view of the actual view.
271: */
272: public void redirectToActual() {
273: redirect(m_ViewHistory[0]);
274: }//redirectToLast
275:
276: /**
277: * Stores a reference to a bean(<i>model</i>) in the
278: * actual <tt>HttpSession</tt>.
279: * <p>
280: * All stored beans are traced, so that they can be properly
281: * removed.
282: *
283: * @param name the unique identifier of bean to be stored as
284: * session attribute.
285: * @param bean the reference to the bean to be stored as session
286: * attribute.
287: */
288: public void storeBean(String name, Object bean) {
289: m_BeanNames.add(name);
290: m_WebSession.putValue(name, bean);
291: }//storeBean
292:
293: /**
294: * Stores the default beans of any session.
295: * This is done directly, to bypass the mechanism
296: * that traces the other stored beans.
297: * On cleanup, the default beans remain referenceable
298: * within the session.
299: */
300: private void storeDefaultBeans() {
301: //the session bean
302: m_WebSession.putValue("jwma.session", this );
303: //the HtmlHelper bean
304: m_WebSession.putValue("jwma.htmlhelper", m_HtmlHelper);
305: //a reference to the i18n classloader
306: m_WebSession.putValue("jwma.resourceloader", JwmaKernel
307: .getReference().getResourceClassLoader());
308: }//storeDefaultBeans
309:
310: /**
311: * Returns a reference to a bean(<i>model</i>) in the
312: * actual <tt>HttpSession</tt>.
313: * <p>
314: * If no bean with the given name/identifyer is stored
315: * in the actual session, then it returns null.
316: *
317: * @param name the unique identifier of bean to be stored as
318: * session attribute.
319: * @return the reference to the bean stored as session
320: * attribute or null.
321: */
322: public Object retrieveBean(String name) {
323: return m_WebSession.getValue(name);
324: }//retrieveBean
325:
326: /**
327: * Removes a reference to a bean (<i>model</i>) in the
328: * actual <tt>HttpSession</tt>.
329: *
330: * @param name the unique identifier of bean to be stored as
331: * session attribute.
332: */
333: public void removeBean(String name) {
334: m_WebSession.removeValue(name);
335: }//removeBean
336:
337: /**
338: * Removes all traced bean references from the
339: * actual <tt>HttpSession</tt>.
340: */
341: private void removeBeans() {
342: try {
343: for (int i = 0; i < m_BeanNames.size(); i++) {
344: m_WebSession.removeValue((String) m_BeanNames.get(i));
345: }
346: m_BeanNames.clear();
347: } catch (IllegalStateException illex) {
348: return;
349: } catch (Exception ex) {
350: log.error(JwmaKernel.getReference().getLogMessage(
351: "jwma.session.beanreferences"), ex);
352: }
353: }//removeBeans
354:
355: /**
356: * Fetches the last login from the user's
357: * preferences (to have it available for the
358: * rest of the session) and then overwrites
359: * the value in the preferences.
360: */
361: private void setLastLogin() {
362: //hold the last one for this session
363: m_LastLogin = m_Preferences.getLastLogin();
364: //overwrite the persisted one
365: m_Preferences.setLastLogin(StringUtil.getFormattedDate()
366: + " from " + getRequest().getRemoteHost());
367: }//setLastLogin
368:
369: /**
370: * Returns a <tt>String</tt> representing the
371: * user's last login date and originating host.
372: *
373: * @return the last login with date and originating
374: * host as <tt>String</tt>.
375: */
376: public String getLastLogin() {
377: return m_LastLogin;
378: }//getLastLogin
379:
380: /*** End web session related part *************************************************/
381:
382: /*** Mail session related part ****************************************************/
383:
384: /**
385: * Initializes the mail session and prepares the store wrapper instance.
386: *
387: * @param password completing the login for the post office of
388: * the jwma user.
389: * @throws JwmaException when something goes wrong while opening or preparing
390: * the mail session.
391: */
392: private Store initMailSession(String password) throws JwmaException {
393:
394: log.debug("initMailSession()");
395: //prepare for formatted log message
396: Object[] args = { m_Username + "@" + m_PostOffice.getAddress(),
397: getRequestAddress() };
398: try {
399:
400: //JwmaSettings settings = JwmaKernel.getReference().getSettings();
401: //log.debug("Retrieved Settings from Kernel");
402:
403: //prepare authenticator
404: m_Authenticator = new JwmaAuthenticator(m_Username,
405: password);
406: log.debug("Prepared Authenticator");
407:
408: //get session instance
409: m_MailSession = Session.getInstance(System.getProperties(),
410: m_Authenticator);
411: log.debug("Retrieved Session instance.");
412: //setup store and connect to it
413: Store store = m_MailSession.getStore(m_PostOffice
414: .getProtocol());
415: log.debug("Retrieved store instance");
416: store.connect(m_PostOffice.getAddress(), m_PostOffice
417: .getPort(), m_Username, password);
418: log.debug("connected to store");
419: m_PostOfficeConnected = true;
420:
421: //1. setup MTA transport
422: Transport transport = null;
423: if (m_MTA.isAuthenticated()) {
424: transport = m_MailSession
425: .getTransport(new URLName(m_MTA.getProtocol(),
426: m_MTA.getAddress(), m_MTA.getPort(),
427: null, m_Username, password));
428: log.debug("Prepared authenticated transport.");
429: } else {
430: transport = m_MailSession.getTransport(new URLName(
431: m_MTA.getProtocol(), m_MTA.getAddress(), m_MTA
432: .getPort(), null, null, null));
433: log.debug("Prepared non authenticated transport.");
434: }
435:
436: //create JwmaTransport wrapper instance
437: m_JwmaTransport = JwmaTransportImpl
438: .createJwmaTransportImpl(transport, m_Authenticator);
439: log.debug("Created JwmaTransport wrapper instance.");
440:
441: //log to syslog
442: log
443: .info(MessageFormat
444: .format(
445: JwmaKernel
446: .getReference()
447: .getLogMessage(
448: "jwma.session.login.success"),
449: args));
450: return store;
451: } catch (AuthenticationFailedException afe) {
452: //cleanup
453: m_MailSession = null;
454: //log failure to syslog
455: log
456: .info(MessageFormat
457: .format(
458: JwmaKernel
459: .getReference()
460: .getLogMessage(
461: "jwma.session.login.failure"),
462: args));
463:
464: //throw JwmaException
465: throw new JwmaException("session.login.authentication",
466: true).setException(afe);
467: } catch (MessagingException e) {
468: //cleanup
469: m_MailSession = null;
470: //throw JwmaException
471: throw new JwmaException("jwma.session.initmail", true)
472: .setException(e);
473: }
474: }//initMailSession
475:
476: /**
477: * Ends a jwma user's mail session.
478: */
479: private void endMailSession() {
480:
481: //prepare for formatted log message
482: Object[] args = { m_Username + "@" + m_PostOffice.getAddress(),
483: getRequestAddress() };
484:
485: try {
486: //remove authentication flag
487: setAuthenticated(false);
488: //cleanup all mailsession references
489: m_MailSession = null;
490: m_JwmaStore.close();
491: m_JwmaStore = null;
492:
493: log.info(MessageFormat
494: .format(JwmaKernel.getReference().getLogMessage(
495: "jwma.session.logout.success"), args));
496:
497: } catch (Exception ex) {
498: log
499: .error(
500: MessageFormat
501: .format(
502: JwmaKernel
503: .getReference()
504: .getLogMessage(
505: "jwma.session.logout.failed"),
506: args), ex);
507: //JwmaKernel.getReference().debugLog().writeStackTrace(ex);
508: }
509: }//endMailSession
510:
511: /**
512: * Returns this instances mailsession.
513: *
514: * @return this instances mailsession as <tt>javax.mail.Session</tt>.
515: *
516: * @see javax.mail.Session
517: */
518: public Session getMailSession() {
519: return m_MailSession;
520: }//getSession;
521:
522: /**
523: * Returns the actual mailsession's <tt>JwmaStoreImpl</tt>.
524: *
525: * @return the actual mailsession's store as <tt>JwmaStoreImpl</tt>
526: *
527: * @see dtw.webmail.model.JwmaStoreImpl
528: */
529: public JwmaStoreImpl getJwmaStore() {
530: return m_JwmaStore;
531: }//getJwmaStore;
532:
533: /**
534: * Returns the actual mailsession's <tt>JwmaTransportImpl</tt>.
535: *
536: * @return the actual mailsession's transport as <tt>JwmaTransportImpl</tt>
537: *
538: * @see dtw.webmail.model.JwmaTransportImpl
539: */
540: public JwmaTransportImpl getJwmaTransport() {
541: return m_JwmaTransport;
542: }//getJwmaTransport
543:
544: public MailTransportAgent getMTA() {
545: return m_MTA;
546: }//getMTA
547:
548: public void setMTA(MailTransportAgent MTA) {
549: m_MTA = MTA;
550: }//setMTA
551:
552: public PostOffice getPostOffice() {
553: return m_PostOffice;
554: }//getPostOffice
555:
556: public void setPostOffice(PostOffice office) {
557: if (office != null) {
558: m_PostOffice = office;
559: }
560: }//setPostOffice
561:
562: /**
563: * Tests wheter a connection to the post office
564: * exists, throwing a <tt>JwmaException</tt> if
565: * not, thus it ensures that a connection to the
566: * post office exists.
567: *
568: * @throws JwmaException if there is no connection to
569: * the post office.
570: */
571: public void ensurePostOfficeConnection() throws JwmaException {
572:
573: if (!isPostOfficeConnected()) {
574: //throw JwmaException
575: throw new JwmaException("jwma.session.mailconnection");
576: }
577: }//ensurePostOfficeConnection
578:
579: /**
580: * Tests if this session has a connection to the post office.
581: *
582: * @return true if there is a connection, false otherwise.
583: */
584: public boolean isPostOfficeConnected() {
585: return (m_PostOfficeConnected);
586: }//isPostOfficeConnected
587:
588: /*** End mail session related part ********************************************/
589:
590: /**
591: * Sets the <tt>Locale</tt> of this <tt>Session</tt> to the one
592: * that is stored in the user's preferences.
593: */
594: public void setLocale() {
595: storeBean("jwma.locale", m_Preferences.getLocale());
596: }//setLocale
597:
598: /**
599: * Returns the assembled user's identity as <tt>String</tt>.
600: *
601: * @param username the user's name as <tt>String</tt>.
602: *
603: * @return the identifier is assembled like a standard email address
604: * using the set postoffice host.
605: */
606: public String getUserIdentity(String username) {
607: return new StringBuffer(username).append('@').append(
608: m_PostOffice.getAddress()).toString();
609: }//getUserIdentity
610:
611: /**
612: * Returns the assembled user's identity as <tt>String</tt>.
613: *
614: * @return the identifier assembled like a standard email address
615: * using the set username and hostname.
616: */
617: public String getUserIdentity() {
618: return new StringBuffer(m_Username).append('@').append(
619: m_PostOffice.getAddress()).toString();
620: }//getUserIdentity
621:
622: /**
623: * Returns the username associated with this <tt>JwmaSession</tt>.
624: *
625: * @return the name as <tt>String</tt>.
626: */
627: public String getUsername() {
628: return m_Username;
629: }//getUsername
630:
631: /**
632: * Returns the <tt>JwmaPreferencesImpl</tt> instance
633: * of this session instance's user.
634: *
635: * @return this session instance's user's preferences as
636: * <tt>JwmaPreferencesImpl</tt>.
637: */
638: public JwmaPreferencesImpl getPreferences() {
639: return m_Preferences;
640: }//getPreferences
641:
642: /**
643: * Saves the <tt>JwmaPreferencesImpl</tt> instance
644: * of this session instance's user.
645: * Note that the actual process of saving is delegated
646: * to the active <tt>PreferencesPersistencePlugin</tt>
647: * instance obtained from the kernel.
648: *
649: * @see dtw.webmail.plugin.PreferencesPersistencePlugin#savePreferences(JwmaPreferencesImpl)
650: *
651: * @return true if saved properly, false otherwise.
652: */
653: public boolean savePreferences() throws JwmaException {
654:
655: try {
656: JwmaKernel.getReference().getPrefsPersistencePlugin()
657: .savePreferences(m_Preferences);
658: } catch (Exception e) {
659: throw new JwmaException("jwma.session.preferences.save");
660: }
661: return true;
662: }//savePreferences
663:
664: /**
665: * Tests if this session is authenticated.
666: * If the session is flagged as authenticated, the
667: * initialization of the mail session worked successfully,
668: * and the user associated with this session could be
669: * authenticated against the post office.
670: *
671: * @return true if authenticated, false otherwise.
672: */
673: public boolean isAuthenticated() {
674: return m_Authenticated;
675: }//isAuthenticated
676:
677: /**
678: * Sets wheter this session is authenticated or not.
679: * Private a really good reason: security.
680: *
681: * @param b true if authenticated, false otherwise.
682: */
683: private void setAuthenticated(boolean b) {
684: m_Authenticated = b;
685: storeBean("jwma.session.authenticated", "true");
686: }//setAuthenticated
687:
688: /**
689: * Tests wheter this session is authenticated, throwing
690: * a <tt>JwmaException</tt> if not, thus ensuring an
691: * authenticated session.
692: *
693: * @throws JwmaException if the session is not authenticated.
694: */
695: public void ensureAuthenticated() throws JwmaException {
696:
697: if (!isAuthenticated()) {
698: //throw JwmaException
699: throw new JwmaException("jwma.session.authentication");
700: }
701: }//ensureAuthenticated
702:
703: /**
704: * Tests if this session's associated user is an
705: * administrator.
706: *
707: * @return true if associated username represents an admin,
708: * false otherwise.
709: */
710: public boolean isAdministrator() {
711: return m_Configuration.getAdministration().isAdmin(m_Username);
712: }//isAdministrator
713:
714: /**
715: * Tests whether the user associated with this session
716: * is allowed to administrate jwma and throws
717: * a <tt>JwmaException</tt> if not, thus ensuring an
718: * administrative user.
719: *
720: * @throws JwmaException if the associated username is not an
721: * admin.
722: */
723: public void ensureAdministrator() throws JwmaException {
724:
725: if (!isAdministrator()) {
726: //throw JwmaException
727: throw new JwmaException("jwma.session.administration");
728: }
729: }//ensureAdministrator
730:
731: /**
732: * Authenticates the given user by initializing the mail session,
733: * thus authenticating against the post office of this session.
734: * Note that the credentials (i.e. password) are not stored
735: * anywhere.
736: *
737: * @param username the name of this session's user as
738: * <tt>String</tt>.
739: * @param password the related credentials as <tt>String</tt>.
740: * @param newAccount a flag that denotes whether a jwma account
741: * already exists (false) or not (true).
742: *
743: * @returns true if the session was successfully authenticated,
744: * false if not.
745: *
746: * @throws JwmaException if initializing a mailsession or
747: * authenticating against the post office fails.
748: *
749: */
750: public boolean authenticate(String username, String password,
751: boolean newAccount) throws JwmaException {
752: log.debug("authenticate()");
753: if (isAuthenticated()) {
754: //we already have a mail session
755: throw new JwmaException("jwma.session.multiplelogin", true);
756: } else {
757: //store username and hostname
758: m_Username = username;
759:
760: //initialize mail session
761: Store store = initMailSession(password);
762: store.addConnectionListener(new StoreConnectionHandler(
763: store));
764: log.debug("Added store connection listener.");
765:
766: //load the preferences
767: if (newAccount) {
768: log.debug("newAccount=true");
769: m_Preferences = JwmaKernel.getReference()
770: .getPrefsPersistencePlugin()
771: .getPreferencesTemplate(this );
772: log.debug("retrieved template");
773: m_Preferences.setUserIdentity(getUserIdentity());
774: } else {
775: log.debug("newAccount=false");
776: m_Preferences = JwmaKernel.getReference()
777: .getPrefsPersistencePlugin().loadPreferences(
778: getUserIdentity());
779: log.debug("loaded prefs.");
780: }
781: //store jwmapreferences information
782: if (m_Preferences != null) {
783: storeBean("jwma.preferences", m_Preferences);
784: log.debug("Stored prefs bean");
785: setLocale();
786: log.debug("Stored locale bean");
787: }
788:
789: //prepare store wrapper instance
790: m_JwmaStore = JwmaStoreImpl
791: .createJwmaStoreImpl(this , store);
792: log.debug("Created JwmaStore wrapper instance.");
793: //worked, remember that
794: setAuthenticated(true);
795:
796: //and store the last login
797: setLastLogin();
798: }
799: return isAuthenticated();
800: }//authenticate
801:
802: /**
803: * Ends this session, by cleaning up user related data
804: * and resources.
805: * If the session is authenticated, then the new last
806: * login <tt>String</tt> is stored in the users
807: * preferences, before they are saved. Consequently the
808: * mail session is closed, and the bean references stored
809: * in this session are cleaned up.
810: *
811: * @see #isAuthenticated()
812: * @see #savePreferences()
813: */
814: public void end() {
815: if (isAuthenticated()) {
816: //store new last login
817: m_Preferences.setLastLogin(m_LastLogin);
818:
819: //save actual preferences
820: try {
821: savePreferences();
822: } catch (JwmaException ex) {
823:
824: }
825: //end mailsession
826: endMailSession();
827:
828: //removes the traced beans
829: removeBeans();
830: }
831: }//end
832:
833: /**
834: * Overrides the superclass method to call <tt>end()</tt>.
835: *
836: * @see #end()
837: */
838: public void finalize() {
839: end();
840: }//finalize
841:
842: class StoreConnectionHandler extends ConnectionAdapter {
843:
844: private Store m_Store;
845:
846: public StoreConnectionHandler(Store store) {
847: m_Store = store;
848: }//constructor
849:
850: /**
851: * Invoked when a service connection is disconnected.
852: * This probably means the user exceeded idle timeout,
853: * and might want to be reconnected.
854: *
855: * @param e <tt>ConnectionEvent</tt> the event that was fired.
856: */
857: public void disconnected(ConnectionEvent e) {
858: try {
859: if (isAuthenticated()) {
860: m_Store.connect(m_PostOffice.getAddress(),
861: m_PostOffice.getPort(), m_Authenticator
862: .getUserName(), m_Authenticator
863: .getPassword());
864: log.debug("Reconnected " + m_Store.getURLName());
865: }
866: } catch (Exception ex) {
867: log.debug("StoreConnectionHandler.disconnected()", ex);
868: m_PostOfficeConnected = false;
869: end();
870: }
871: }//disconnected
872:
873: }//inner class StoreConnectionHandler
874:
875: }//class JwmaSession
|