Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. package demo;
  33.  
  34. import java.util.*;
  35. import javax.mail.*;
  36. import javax.naming.*;
  37.  
  38. /**
  39.  * This JavaBean is used to store mail user information.
  40.  */
  41. public class MailUserBean {
  42.     private Folder folder;
  43.     private String hostname;
  44.     private String username;
  45.     private String password;
  46.     private Session session;
  47.     private Store store;
  48.     private URLName url;
  49.     private String protocol = "imap";
  50.     private String mbox = "INBOX";     
  51.  
  52.     public MailUserBean(){}
  53.  
  54.     /**
  55.      * Returns the javax.mail.Folder object.
  56.      */
  57.     public Folder getFolder() {
  58.         return folder;
  59.     }
  60.    
  61.     /**
  62.      * Returns the number of messages in the folder.
  63.      */
  64.     public int getMessageCount() throws MessagingException {
  65.         return folder.getMessageCount();
  66.     }
  67.  
  68.     /**
  69.      * hostname getter method.
  70.      */
  71.     public String getHostname() {
  72.         return hostname;
  73.     }
  74.    
  75.     /**
  76.      * hostname setter method.
  77.      */
  78.     public void setHostname(String hostname) {
  79.         this.hostname = hostname;
  80.     }
  81.        
  82.     /**
  83.      * username getter method.
  84.      */
  85.     public String getUsername() {
  86.         return username;
  87.     }
  88.  
  89.     /**
  90.      * username setter method.
  91.      */
  92.     public void setUsername(String username) {
  93.         this.username = username;
  94.     }
  95.  
  96.     /**
  97.      * password getter method.
  98.      */
  99.     public String getPassword() {
  100.         return password;
  101.     }
  102.  
  103.     /**
  104.      * password setter method.
  105.      */
  106.     public void setPassword(String password) {
  107.         this.password = password;
  108.     }
  109.  
  110.     /**
  111.      * session getter method.
  112.      */
  113.     public Session getSession() {
  114.         return session;
  115.     }
  116.  
  117.     /**
  118.      * session setter method.
  119.      */
  120.     public void setSession(Session session) {
  121.         this.session = session;
  122.     }
  123.  
  124.     /**
  125.      * store getter method.
  126.      */
  127.     public Store getStore() {
  128.         return store;
  129.     }
  130.  
  131.     /**
  132.      * store setter method.
  133.      */
  134.     public void setStore(Store store) {
  135.         this.store = store;
  136.     }
  137.  
  138.     /**
  139.      * url getter method.
  140.      */
  141.     public URLName getUrl() {
  142.         return url;
  143.     }
  144.  
  145.     /**
  146.      * Method for checking if the user is logged in.
  147.      */
  148.     public boolean isLoggedIn() {
  149.         return store.isConnected();
  150.     }
  151.      
  152.     /**
  153.      * Method used to login to the mail host.
  154.      */
  155.     public void login() throws Exception {
  156.         url = new URLName(protocol, getHostname(), -1, mbox,
  157.                           getUsername(), getPassword());
  158.         /*
  159.          * First, try to get the session from JNDI,
  160.          * as would be done under J2EE.
  161.          */
  162.         try {
  163.             InitialContext ic = new InitialContext();
  164.             Context ctx = (Context)ic.lookup("java:comp/env");
  165.             session = (Session)ctx.lookup("MySession");
  166.         } catch (Exception ex) {
  167.             // ignore it
  168.         }
  169.  
  170.         // if JNDI fails, try the old way that should work everywhere
  171.         if (session == null) {
  172.             Properties props = null;
  173.             try {
  174.                 props = System.getProperties();
  175.             } catch (SecurityException sex) {
  176.                 props = new Properties();
  177.             }
  178.             session = Session.getInstance(props, null);
  179.         }
  180.         store = session.getStore(url);
  181.         store.connect();
  182.         folder = store.getFolder(url);
  183.        
  184.         folder.open(Folder.READ_WRITE);
  185.     }
  186.  
  187.     /**
  188.      * Method used to login to the mail host.
  189.      */
  190.     public void login(String hostname, String username, String password)
  191.         throws Exception {
  192.            
  193.         this.hostname = hostname;
  194.         this.username = username;
  195.         this.password = password;
  196.            
  197.         login();
  198.     }
  199.  
  200.     /**
  201.      * Method used to logout from the mail host.
  202.      */
  203.     public void logout() throws MessagingException {
  204.         folder.close(false);
  205.         store.close();
  206.         store = null;
  207.         session = null;
  208.     }
  209. }
  210.  
  211.