KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > namespace


1 /*
2  * @(#)namespace.java 1.5 04/02/27
3  *
4  * Copyright 1997-2004 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.*;
40 import java.io.*;
41 import javax.mail.*;
42
43 /*
44  * Demo app that exercises the namespace interfaces.
45  * Show the namespaces supported by a store.
46  *
47  * @author Bill Shannon
48  */

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