0N/A/*
292N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
292N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport javax.mail.*;
0N/A
0N/A/*
0N/A * Demo app that exercises the namespace interfaces.
0N/A * Show the namespaces supported by a store.
0N/A *
0N/A * @author Bill Shannon
0N/A */
0N/A
0N/Apublic class namespace {
0N/A
0N/A static String protocol;
0N/A static String host = null;
0N/A static String user = null;
0N/A static String password = null;
0N/A static String url = null;
0N/A static int port = -1;
0N/A static boolean debug = false;
0N/A static String suser = "other";
0N/A
0N/A public static void main(String argv[]) {
0N/A int msgnum = -1;
0N/A int optind;
0N/A
0N/A for (optind = 0; optind < argv.length; optind++) {
0N/A if (argv[optind].equals("-T")) {
0N/A protocol = argv[++optind];
0N/A } else if (argv[optind].equals("-H")) {
0N/A host = argv[++optind];
0N/A } else if (argv[optind].equals("-U")) {
0N/A user = argv[++optind];
0N/A } else if (argv[optind].equals("-P")) {
0N/A password = argv[++optind];
0N/A } else if (argv[optind].equals("-D")) {
0N/A debug = true;
0N/A } else if (argv[optind].equals("-L")) {
0N/A url = argv[++optind];
0N/A } else if (argv[optind].equals("-p")) {
0N/A port = Integer.parseInt(argv[++optind]);
0N/A } else if (argv[optind].equals("-u")) {
0N/A suser = argv[++optind];
0N/A } else if (argv[optind].equals("--")) {
0N/A optind++;
0N/A break;
0N/A } else if (argv[optind].startsWith("-")) {
0N/A System.out.println(
0N/A"Usage: namespace [-L url] [-T protocol] [-H host] [-p port] [-U user]");
0N/A System.out.println(
0N/A"\t[-P password] [-u other-user] [-D]");
0N/A System.exit(1);
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A
27N/A try {
0N/A // Get a Properties object
0N/A Properties props = System.getProperties();
0N/A
0N/A // Get a Session object
0N/A Session session = Session.getInstance(props, null);
0N/A session.setDebug(debug);
0N/A
0N/A // Get a Store object
0N/A Store store = null;
0N/A if (url != null) {
0N/A URLName urln = new URLName(url);
0N/A store = session.getStore(urln);
0N/A store.connect();
0N/A } else {
0N/A if (protocol != null)
0N/A store = session.getStore(protocol);
0N/A else
0N/A store = session.getStore();
0N/A
0N/A // Connect
0N/A if (host != null || user != null || password != null)
0N/A store.connect(host, port, user, password);
0N/A else
0N/A store.connect();
0N/A }
0N/A
0N/A printFolders("Personal", store.getPersonalNamespaces());
0N/A printFolders("User \"" + suser + "\"",
0N/A store.getUserNamespaces(suser));
0N/A printFolders("Shared", store.getSharedNamespaces());
0N/A
0N/A store.close();
0N/A } catch (Exception ex) {
0N/A System.out.println("Oops, got exception! " + ex.getMessage());
0N/A ex.printStackTrace();
0N/A }
0N/A System.exit(0);
0N/A }
0N/A
0N/A private static void printFolders(String name, Folder[] folders)
0N/A throws MessagingException {
0N/A System.out.println(name + " Namespace:");
0N/A if (folders == null || folders.length == 0) {
0N/A System.out.println(" <none>");
0N/A return;
0N/A }
0N/A for (int i = 0; i < folders.length; i++) {
0N/A String fn = folders[i].getFullName();
0N/A if (fn.length() == 0)
0N/A fn = "<default folder>";
0N/A try {
0N/A System.out.println(" " + fn +
0N/A ", delimiter \"" + folders[i].getSeparator() + "\"");
0N/A Folder[] fl = folders[i].list();
0N/A if (fl.length > 0) {
0N/A System.out.println(" Subfolders:");
0N/A for (int j = 0; j < fl.length; j++)
0N/A System.out.println(" " + fl[j].getFullName());
0N/A }
0N/A } catch (FolderNotFoundException ex) { }
0N/A }
0N/A }
0N/A}