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 javax.mail.*;
  33. import javax.mail.internet.*;
  34.  
  35. /*
  36.  * Copy folder hierarchies between different Stores. This is a useful
  37.  * utility to populate new (and possibly empty) mail stores. Specify
  38.  * both the source and destination folders as URLs.
  39.  *     
  40.  * @author John Mani
  41.  * @author Bill Shannon
  42.  */
  43.  
  44. public class populate {
  45.  
  46.     static boolean force = false;
  47.     static boolean skipSpecial = false;
  48.     static boolean clear = false;
  49.     static boolean dontPreserveFlags = false;
  50.  
  51.     public static void main(String argv[]) {
  52.         String srcURL = null;
  53.         String dstURL = null;
  54.         boolean debug = false;
  55.  
  56.         int optind;
  57.  
  58.         for (optind = 0; optind < argv.length; optind++) {
  59.             if (argv[optind].equals("-s")) {
  60.                 srcURL = argv[++optind];
  61.             } else if (argv[optind].equals("-d")) {
  62.                 dstURL = argv[++optind];
  63.             } else if (argv[optind].equals("-D")) {
  64.                 debug = true;
  65.             } else if (argv[optind].equals("-f")) {
  66.                 force = true;
  67.             } else if (argv[optind].equals("-S")) {
  68.                 skipSpecial = true;
  69.             } else if (argv[optind].equals("-c")) {
  70.                 clear = true;
  71.             } else if (argv[optind].equals("-P")) {
  72.                 dontPreserveFlags = true;
  73.             } else if (argv[optind].equals("--")) {
  74.                 optind++;
  75.                 break;
  76.             } else if (argv[optind].startsWith("-")) {
  77.                 printUsage();
  78.                 System.exit(1);
  79.             } else {
  80.                 break;
  81.             }
  82.         }
  83.  
  84.         try {
  85.  
  86.             if (srcURL == null || dstURL == null) {
  87.                 printUsage();
  88.                 System.exit(1);
  89.             }
  90.  
  91.             Session session = Session.getInstance(
  92.                                 System.getProperties(), null);
  93.             session.setDebug(debug);
  94.  
  95.             // Get source folder
  96.             URLName srcURLName = new URLName(srcURL);
  97.             Folder srcFolder;
  98.             // Check if the source URL has a folder specified. If
  99.             // not, we use the default folder
  100.             if (srcURLName.getFile() == null) {
  101.                 Store s = session.getStore(srcURLName);
  102.                 s.connect();
  103.                 srcFolder = s.getDefaultFolder();
  104.             } else {
  105.                 srcFolder = session.getFolder(new URLName(srcURL));
  106.                 if (!srcFolder.exists()) {
  107.                     System.out.println("source folder does not exist");
  108.                     srcFolder.getStore().close();
  109.                     System.exit(1);
  110.                 }
  111.             }
  112.  
  113.             // Set up destination folder
  114.             URLName dstURLName = new URLName(dstURL);
  115.             Folder dstFolder;
  116.             // Check if the destination URL has a folder specified. If
  117.             // not, we use the source folder name
  118.             if (dstURLName.getFile() == null) {
  119.                 Store s = session.getStore(dstURLName);
  120.                 s.connect();
  121.                 dstFolder = s.getFolder(srcFolder.getName());
  122.             } else
  123.                 dstFolder = session.getFolder(dstURLName);
  124.  
  125.             if (clear && dstFolder.exists()) {
  126.                 if (!dstFolder.delete(true)) {
  127.                     System.out.println("couldn't delete " +
  128.                                                 dstFolder.getFullName());
  129.                     return;
  130.                 }
  131.             }
  132.             copy(srcFolder, dstFolder);
  133.  
  134.             // Close the respective stores.
  135.             srcFolder.getStore().close();
  136.             dstFolder.getStore().close();
  137.  
  138.         } catch (MessagingException mex) {
  139.             System.out.println(mex.getMessage());
  140.             mex.printStackTrace();
  141.         }
  142.     }
  143.  
  144.     private static void copy(Folder src, Folder dst)
  145.                 throws MessagingException {
  146.         System.out.println("Populating " + dst.getFullName());
  147.  
  148.         Folder ddst = dst;
  149.         Folder[] srcFolders = null;
  150.         if ((src.getType() & Folder.HOLDS_FOLDERS) != 0)
  151.             srcFolders = src.list();
  152.         boolean srcHasChildren = srcFolders != null && srcFolders.length > 0;
  153.  
  154.         if (!dst.exists()) {
  155.             // Create it.
  156.             boolean dstHoldsOnlyFolders = false;
  157.             try {
  158.                 if (!dst.create(src.getType())) {
  159.                     System.out.println("couldn't create " + dst.getFullName());
  160.                     return;
  161.                 }
  162.             } catch (MessagingException mex) {
  163.                 // might not be able to create a folder that holds both
  164.                 if (src.getType() !=
  165.                         (Folder.HOLDS_MESSAGES|Folder.HOLDS_FOLDERS))
  166.                     throw mex;
  167.                 if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS :
  168.                                                 Folder.HOLDS_MESSAGES)) {
  169.                     System.out.println("couldn't create " + dst.getFullName());
  170.                     return;
  171.                 }
  172.                 dstHoldsOnlyFolders = srcHasChildren;
  173.             }
  174.  
  175.             // Copy over any messges from src to dst
  176.             if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) {
  177.                 src.open(Folder.READ_ONLY);
  178.                 if (dstHoldsOnlyFolders) {
  179.                     if (src.getMessageCount() > 0) {
  180.                         System.out.println("Unable to copy messages from " +
  181.                             src.getFullName() + " to " + dst.getFullName() +
  182.                             " because destination holds only folders");
  183.                     }
  184.                 } else
  185.                     copyMessages(src, dst);
  186.                 src.close(false);
  187.             }
  188.         } else  {
  189.             System.out.println(dst.getFullName() + " already exists");
  190.             // Copy over any messges from src to dst
  191.             if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) {
  192.                 src.open(Folder.READ_ONLY);
  193.                 copyMessages(src, dst);
  194.                 src.close(false);
  195.             }
  196.         }
  197.  
  198.         // Copy over subfolders
  199.         if (srcHasChildren) {
  200.             for (int i = 0; i < srcFolders.length; i++) {
  201.                 // skip special directories?
  202.                 if (skipSpecial) {
  203.                     if (srcFolders[i].getName().equals("SCCS") ||
  204.                         srcFolders[i].getName().equals("Drafts") ||
  205.                         srcFolders[i].getName().equals("Trash") ||
  206.                         srcFolders[i].getName().equals("Shared Folders"))
  207.                         continue;
  208.                 }
  209.                 copy(srcFolders[i], dst.getFolder(srcFolders[i].getName()));
  210.             }
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * Copy message from src to dst.  If dontPreserveFlags is set
  216.      * we first copy the messages to memory, clear all the flags,
  217.      * and then copy to the destination.
  218.      */
  219.     private static void copyMessages(Folder src, Folder dst)
  220.                                 throws MessagingException {
  221.         Message[] msgs = src.getMessages();
  222.         if (dontPreserveFlags) {
  223.             for (int i = 0; i < msgs.length; i++) {
  224.                 MimeMessage m = new MimeMessage((MimeMessage)msgs[i]);
  225.                 m.setFlags(m.getFlags(), false);
  226.                 msgs[i] = m;
  227.             }
  228.         }
  229.         src.copyMessages(msgs, dst);
  230.     }
  231.  
  232.     private static void printUsage() {
  233.         System.out.println("populate [-D] [-f] [-S] [-c] " +
  234.                            "-s source_url -d dest_url");
  235.         System.out.println("URLs are of the form: " +
  236.                            "protocol://username:password@hostname/foldername");
  237.         System.out.println("The destination URL does not need a foldername," +
  238.                            " in which case, the source foldername is used");
  239.     }
  240. }
  241.