Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-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. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35.  
  36. /*
  37.  * Demo app that exercises the namespace interfaces.
  38.  * Show the namespaces supported by a store.
  39.  *
  40.  * @author Bill Shannon
  41.  */
  42.  
  43. public class namespace {
  44.  
  45.     static String protocol;
  46.     static String host = null;
  47.     static String user = null;
  48.     static String password = null;
  49.     static String url = null;
  50.     static int port = -1;
  51.     static boolean debug = false;
  52.     static String suser = "other";
  53.  
  54.     public static void main(String argv[]) {
  55.         int msgnum = -1;
  56.         int optind;
  57.  
  58.         for (optind = 0; optind < argv.length; optind++) {
  59.             if (argv[optind].equals("-T")) {
  60.                 protocol = argv[++optind];
  61.             } else if (argv[optind].equals("-H")) {
  62.                 host = argv[++optind];
  63.             } else if (argv[optind].equals("-U")) {
  64.                 user = argv[++optind];
  65.             } else if (argv[optind].equals("-P")) {
  66.                 password = argv[++optind];
  67.             } else if (argv[optind].equals("-D")) {
  68.                 debug = true;
  69.             } else if (argv[optind].equals("-L")) {
  70.                 url = argv[++optind];
  71.             } else if (argv[optind].equals("-p")) {
  72.                 port = Integer.parseInt(argv[++optind]);
  73.             } else if (argv[optind].equals("-u")) {
  74.                 suser = argv[++optind];
  75.             } else if (argv[optind].equals("--")) {
  76.                 optind++;
  77.                 break;
  78.             } else if (argv[optind].startsWith("-")) {
  79.                 System.out.println(
  80. "Usage: namespace [-L url] [-T protocol] [-H host] [-p port] [-U user]");
  81.                 System.out.println(
  82. "\t[-P password] [-u other-user] [-D]");
  83.                 System.exit(1);
  84.             } else {
  85.                 break;
  86.             }
  87.         }
  88.  
  89.         try {
  90.             // Get a Properties object
  91.             Properties props = System.getProperties();
  92.  
  93.             // Get a Session object
  94.             Session session = Session.getInstance(props, null);
  95.             session.setDebug(debug);
  96.  
  97.             // Get a Store object
  98.             Store store = null;
  99.             if (url != null) {
  100.                 URLName urln = new URLName(url);
  101.                 store = session.getStore(urln);
  102.                 store.connect();
  103.             } else {
  104.                 if (protocol != null)          
  105.                     store = session.getStore(protocol);
  106.                 else
  107.                     store = session.getStore();
  108.  
  109.                 // Connect
  110.                 if (host != null || user != null || password != null)
  111.                     store.connect(host, port, user, password);
  112.                 else
  113.                     store.connect();
  114.             }
  115.  
  116.             printFolders("Personal", store.getPersonalNamespaces());
  117.             printFolders("User \"" + suser + "\"",
  118.                                 store.getUserNamespaces(suser));
  119.             printFolders("Shared", store.getSharedNamespaces());
  120.  
  121.             store.close();
  122.         } catch (Exception ex) {
  123.             System.out.println("Oops, got exception! " + ex.getMessage());
  124.             ex.printStackTrace();
  125.         }
  126.         System.exit(0);
  127.     }
  128.  
  129.     private static void printFolders(String name, Folder[] folders)
  130.                                 throws MessagingException {
  131.         System.out.println(name + " Namespace:");
  132.         if (folders == null || folders.length == 0) {
  133.             System.out.println("  <none>");
  134.             return;
  135.         }
  136.         for (int i = 0; i < folders.length; i++) {
  137.             String fn = folders[i].getFullName();
  138.             if (fn.length() == 0)
  139.                 fn = "<default folder>";
  140.             try {
  141.                 System.out.println("  " + fn +
  142.                         ", delimiter \"" + folders[i].getSeparator() + "\"");
  143.                 Folder[] fl = folders[i].list();
  144.                 if (fl.length > 0) {
  145.                     System.out.println("  Subfolders:");
  146.                     for (int j = 0; j < fl.length; j++)
  147.                         System.out.println("    " + fl[j].getFullName());
  148.                 }
  149.             } catch (FolderNotFoundException ex) { }
  150.         }
  151.     }
  152. }
  153.