KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > folderlist


1 /*
2  * @(#)folderlist.java 1.29 03/04/22
3  *
4  * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.util.Properties JavaDoc;
40 import javax.mail.*;
41
42 /**
43  * Demo app that exercises the Message interfaces.
44  * List information about folders.
45  *
46  * @author John Mani
47  * @author Bill Shannon
48  */

49
50 public class folderlist {
51     static String JavaDoc protocol = null;
52     static String JavaDoc host = null;
53     static String JavaDoc user = null;
54     static String JavaDoc password = null;
55     static String JavaDoc url = null;
56     static String JavaDoc root = null;
57     static String JavaDoc pattern = "%";
58     static boolean recursive = false;
59     static boolean verbose = false;
60     static boolean debug = false;
61
62     public static void main(String JavaDoc argv[]) throws Exception JavaDoc {
63     int optind;
64     for (optind = 0; optind < argv.length; optind++) {
65         if (argv[optind].equals("-T")) {
66         protocol = argv[++optind];
67         } else if (argv[optind].equals("-H")) {
68         host = argv[++optind];
69         } else if (argv[optind].equals("-U")) {
70         user = argv[++optind];
71         } else if (argv[optind].equals("-P")) {
72         password = argv[++optind];
73         } else if (argv[optind].equals("-L")) {
74         url = argv[++optind];
75         } else if (argv[optind].equals("-R")) {
76         root = argv[++optind];
77         } else if (argv[optind].equals("-r")) {
78         recursive = true;
79         } else if (argv[optind].equals("-v")) {
80         verbose = true;
81         } else if (argv[optind].equals("-D")) {
82         debug = true;
83         } else if (argv[optind].equals("--")) {
84         optind++;
85         break;
86         } else if (argv[optind].startsWith("-")) {
87         System.out.println(
88 "Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]");
89         System.out.println(
90 "\t[-R root] [-r] [-v] [-D] [pattern]");
91         System.exit(1);
92         } else {
93         break;
94         }
95     }
96     if (optind < argv.length)
97         pattern = argv[optind];
98
99     // Get a Properties object
100
Properties JavaDoc props = System.getProperties();
101
102     // Get a Session object
103
Session session = Session.getInstance(props, null);
104     session.setDebug(debug);
105
106     // Get a Store object
107
Store store = null;
108     Folder rf = null;
109     if (url != null) {
110         URLName urln = new URLName(url);
111         store = session.getStore(urln);
112         store.connect();
113     } else {
114         if (protocol != null)
115         store = session.getStore(protocol);
116         else
117         store = session.getStore();
118
119         // Connect
120
if (host != null || user != null || password != null)
121         store.connect(host, user, password);
122         else
123         store.connect();
124     }
125
126     // List namespace
127
if (root != null)
128         rf = store.getFolder(root);
129     else
130         rf = store.getDefaultFolder();
131
132     dumpFolder(rf, false, "");
133     if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
134         Folder[] f = rf.list(pattern);
135         for (int i = 0; i < f.length; i++)
136         dumpFolder(f[i], recursive, " ");
137     }
138
139     store.close();
140     }
141
142     static void dumpFolder(Folder folder, boolean recurse, String JavaDoc tab)
143                     throws Exception JavaDoc {
144         System.out.println(tab + "Name: " + folder.getName());
145     System.out.println(tab + "Full Name: " + folder.getFullName());
146     System.out.println(tab + "URL: " + folder.getURLName());
147
148     if (verbose) {
149         if (!folder.isSubscribed())
150         System.out.println(tab + "Not Subscribed");
151
152         if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
153         if (folder.hasNewMessages())
154             System.out.println(tab + "Has New Messages");
155         System.out.println(tab + "Total Messages: " +
156                         folder.getMessageCount());
157         System.out.println(tab + "New Messages: " +
158                         folder.getNewMessageCount());
159         System.out.println(tab + "Unread Messages: " +
160                         folder.getUnreadMessageCount());
161         }
162         if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
163         System.out.println(tab + "Is Directory");
164     }
165
166     System.out.println();
167
168     if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
169         if (recurse) {
170         Folder[] f = folder.list();
171         for (int i = 0; i < f.length; i++)
172             dumpFolder(f[i], recurse, tab + " ");
173         }
174     }
175     }
176 }
177
Popular Tags