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.io.*;
0N/Aimport javax.mail.*;
0N/Aimport javax.mail.internet.*;
0N/A
0N/A/*
0N/A * Copy folder hierarchies between files and a Store. This is a useful
0N/A * utility to populate new (and possibly empty) mail stores. Specify
0N/A * the source as a directory name and the destination folders as a URL.
0N/A *
0N/A * @author John Mani
0N/A * @author Bill Shannon
0N/A */
0N/A
0N/Apublic class fpopulate {
0N/A
0N/A static boolean force = false;
0N/A static boolean skipSCCS = false;
0N/A static boolean clear = false;
0N/A
0N/A static Session session;
0N/A
0N/A public static void main(String argv[]) {
0N/A String srcdir = null;
0N/A String dstURL = null;
0N/A boolean debug = false;
0N/A
0N/A int optind;
0N/A
0N/A for (optind = 0; optind < argv.length; optind++) {
0N/A if (argv[optind].equals("-s")) {
0N/A srcdir = argv[++optind];
0N/A } else if (argv[optind].equals("-d")) {
0N/A dstURL = argv[++optind];
0N/A } else if (argv[optind].equals("-D")) {
0N/A debug = true;
0N/A } else if (argv[optind].equals("-f")) {
0N/A force = true;
0N/A } else if (argv[optind].equals("-S")) {
0N/A skipSCCS = true;
0N/A } else if (argv[optind].equals("-c")) {
0N/A clear = true;
0N/A } else if (argv[optind].equals("--")) {
0N/A optind++;
0N/A break;
0N/A } else if (argv[optind].startsWith("-")) {
0N/A printUsage();
0N/A System.exit(1);
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A try {
0N/A
0N/A if (srcdir == null || dstURL == null) {
0N/A printUsage();
0N/A System.exit(1);
0N/A }
0N/A
0N/A session = Session.getDefaultInstance(
0N/A System.getProperties(), null);
0N/A session.setDebug(debug);
0N/A
0N/A // Get source folder
0N/A File srcFolder = new File(srcdir);
0N/A if (!srcFolder.exists()) {
0N/A System.out.println("source folder does not exist");
0N/A System.exit(1);
0N/A }
0N/A
0N/A // Set up destination folder
0N/A URLName dstURLName = new URLName(dstURL);
0N/A Folder dstFolder;
0N/A // Check if the destination URL has a folder specified. If
0N/A // not, we use the source folder name
0N/A if (dstURLName.getFile() == null) {
0N/A Store s = session.getStore(dstURLName);
0N/A s.connect();
0N/A dstFolder = s.getFolder(srcFolder.getName());
0N/A } else
0N/A dstFolder = session.getFolder(new URLName(dstURL));
0N/A
0N/A if (clear && dstFolder.exists()) {
0N/A if (!dstFolder.delete(true)) {
0N/A System.out.println("couldn't delete " +
0N/A dstFolder.getFullName());
0N/A return;
0N/A }
0N/A }
0N/A copy(srcFolder, dstFolder);
0N/A
0N/A // Close the respective stores.
0N/A dstFolder.getStore().close();
0N/A
0N/A } catch (MessagingException mex) {
0N/A System.out.println(mex.getMessage());
0N/A mex.printStackTrace();
0N/A } catch (IOException ioex) {
0N/A System.out.println(ioex.getMessage());
0N/A ioex.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A private static void copy(File src, Folder dst)
0N/A throws MessagingException, IOException {
0N/A System.out.println("Populating " + dst.getFullName());
0N/A
0N/A if (!dst.exists()) {
0N/A // Create it.
0N/A int type = holdsMessages(src) ?
0N/A Folder.HOLDS_MESSAGES : Folder.HOLDS_FOLDERS;
0N/A if (!dst.create(type)) {
0N/A System.out.println("couldn't create " + dst.getFullName());
0N/A return;
0N/A }
0N/A
0N/A // Copy over any messages from src to dst
0N/A if (holdsMessages(src))
0N/A copyMessages(src, dst);
0N/A } else {
0N/A System.out.println(dst.getFullName() + " already exists");
0N/A // Copy over any messges from src to dst
0N/A if (force && holdsMessages(src))
0N/A copyMessages(src, dst);
0N/A }
0N/A
0N/A // Copy over subfolders
0N/A if (holdsFolders(src)) {
0N/A String[] sf = src.list();
0N/A for (int i = 0; sf != null && i < sf.length; i++) {
0N/A // skip SCCS directories?
0N/A if (skipSCCS && sf[i].equals("SCCS"))
0N/A continue;
0N/A File f = new File(src, sf[i]);
0N/A if (f.isDirectory())
0N/A copy(f, dst.getFolder(sf[i]));
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Does this directory hold messages?
0N/A * Return true if there's at least one message.
0N/A */
0N/A private static boolean holdsMessages(File f) {
0N/A File msg = new File(f, "1");
0N/A return msg.exists();
0N/A }
0N/A
0N/A private static boolean holdsFolders(File f) {
0N/A return !holdsMessages(f); // XXX - hack for now
0N/A }
0N/A
0N/A /**
0N/A * Copy message files from the source directory to the
0N/A * destination folder. Message files must be named "1",
0N/A * "2", etc. The first missing number terminates the
0N/A * copy.
0N/A */
0N/A private static void copyMessages(File src, Folder dst)
0N/A throws MessagingException, IOException {
0N/A System.out.println(" Copy from " + src + " to " + dst);
0N/A int msgnum = 1;
0N/A Message[] msgs = new Message[1];
0N/A for (;;) {
0N/A File f = new File(src, String.valueOf(msgnum));
0N/A if (!f.exists()) // break when we find a message missing
0N/A break;
0N/A FileInputStream fis = new FileInputStream(f);
0N/A BufferedInputStream is = new BufferedInputStream(fis);
0N/A is.mark(1024);
0N/A DataInputStream dis = new DataInputStream(is);
0N/A String line = dis.readLine();
0N/A /*
0N/A * If it's in UNIX mbox format, we skip the first line,
0N/A * otherwise we start reading at the beginning.
0N/A */
0N/A if (!line.startsWith("From "))
0N/A is.reset();
0N/A MimeMessage msg = new MimeMessage(session, is);
0N/A fis.close();
0N/A msgs[0] = msg;
0N/A dst.appendMessages(msgs);
0N/A msgnum++;
0N/A }
0N/A System.out.println(" Copied " + (msgnum - 1) + " messages");
0N/A }
0N/A
0N/A private static void printUsage() {
0N/A System.out.println("fpopulate [-D] [-f] [-S] [-c] " +
0N/A "-s source_dir -d dest_url");
0N/A System.out.println("URLs are of the form: " +
0N/A "protocol://username:password@hostname/foldername");
0N/A System.out.println("The destination URL does not need a foldername," +
0N/A " in which case, the source foldername is used");
0N/A }
0N/A}