1 38 39 import javax.mail.*; 40 import javax.mail.internet.*; 41 42 49 50 public class populate { 51 52 static boolean force = false; 53 static boolean skipSCCS = false; 54 static boolean clear = false; 55 56 public static void main(String argv[]) { 57 String srcURL = null; 58 String dstURL = null; 59 boolean debug = false; 60 61 int optind; 62 63 for (optind = 0; optind < argv.length; optind++) { 64 if (argv[optind].equals("-s")) { 65 srcURL = argv[++optind]; 66 } else if (argv[optind].equals("-d")) { 67 dstURL = argv[++optind]; 68 } else if (argv[optind].equals("-D")) { 69 debug = true; 70 } else if (argv[optind].equals("-f")) { 71 force = true; 72 } else if (argv[optind].equals("-S")) { 73 skipSCCS = true; 74 } else if (argv[optind].equals("-c")) { 75 clear = true; 76 } else if (argv[optind].equals("--")) { 77 optind++; 78 break; 79 } else if (argv[optind].startsWith("-")) { 80 printUsage(); 81 System.exit(1); 82 } else { 83 break; 84 } 85 } 86 87 try { 88 89 if (srcURL == null || dstURL == null) { 90 printUsage(); 91 System.exit(1); 92 } 93 94 Session session = Session.getInstance( 95 System.getProperties(), null); 96 session.setDebug(debug); 97 98 Folder srcFolder = session.getFolder(new URLName(srcURL)); 100 if (!srcFolder.exists()) { 101 System.out.println("source folder does not exist"); 102 srcFolder.getStore().close(); 103 System.exit(1); 104 } 105 106 URLName dstURLName = new URLName(dstURL); 108 Folder dstFolder; 109 if (dstURLName.getFile() == null) { 112 Store s = session.getStore(dstURLName); 113 s.connect(); 114 dstFolder = s.getFolder(srcFolder.getName()); 115 } else 116 dstFolder = session.getFolder(new URLName(dstURL)); 117 118 if (clear && dstFolder.exists()) { 119 if (!dstFolder.delete(true)) { 120 System.out.println("couldn't delete " + 121 dstFolder.getFullName()); 122 return; 123 } 124 } 125 copy(srcFolder, dstFolder); 126 127 srcFolder.getStore().close(); 129 dstFolder.getStore().close(); 130 131 } catch (MessagingException mex) { 132 System.out.println(mex.getMessage()); 133 mex.printStackTrace(); 134 } 135 } 136 137 private static void copy(Folder src, Folder dst) 138 throws MessagingException { 139 System.out.println("Populating " + dst.getFullName()); 140 141 if (!dst.exists()) { 142 if (!dst.create(src.getType())) { 144 System.out.println("couldn't create " + dst.getFullName()); 145 return; 146 } 147 148 if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) { 150 src.open(Folder.READ_ONLY); 151 src.copyMessages(src.getMessages(), dst); 152 src.close(false); 153 } 154 } else { 155 System.out.println(dst.getFullName() + " already exists"); 156 if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) { 158 src.open(Folder.READ_ONLY); 159 src.copyMessages(src.getMessages(), dst); 160 src.close(false); 161 } 162 } 163 164 if ((src.getType() & Folder.HOLDS_FOLDERS) != 0) { 166 Folder[] sf = src.list(); 167 for (int i = 0; i < sf.length; i++) { 168 if (skipSCCS && sf[i].getName().equals("SCCS")) 170 continue; 171 copy(sf[i], dst.getFolder(sf[i].getName())); 172 } 173 } 174 } 175 176 private static void printUsage() { 177 System.out.println("populate [-D] [-f] [-S] [-c] " + 178 "-s source_url -d dest_url"); 179 System.out.println("URLs are of the form: " + 180 "protocol://username:password@hostname/foldername"); 181 System.out.println("The destination URL does not need a foldername," + 182 " in which case, the source foldername is used"); 183 } 184 } 185 | Popular Tags |