1 38 39 import java.io.*; 40 import javax.mail.*; 41 import javax.mail.internet.*; 42 43 51 52 public class fpopulate { 53 54 static boolean force = false; 55 static boolean skipSCCS = false; 56 static boolean clear = false; 57 58 static Session session; 59 60 public static void main(String argv[]) { 61 String srcdir = null; 62 String dstURL = null; 63 boolean debug = false; 64 65 int optind; 66 67 for (optind = 0; optind < argv.length; optind++) { 68 if (argv[optind].equals("-s")) { 69 srcdir = argv[++optind]; 70 } else if (argv[optind].equals("-d")) { 71 dstURL = argv[++optind]; 72 } else if (argv[optind].equals("-D")) { 73 debug = true; 74 } else if (argv[optind].equals("-f")) { 75 force = true; 76 } else if (argv[optind].equals("-S")) { 77 skipSCCS = true; 78 } else if (argv[optind].equals("-c")) { 79 clear = true; 80 } else if (argv[optind].equals("--")) { 81 optind++; 82 break; 83 } else if (argv[optind].startsWith("-")) { 84 printUsage(); 85 System.exit(1); 86 } else { 87 break; 88 } 89 } 90 91 try { 92 93 if (srcdir == null || dstURL == null) { 94 printUsage(); 95 System.exit(1); 96 } 97 98 session = Session.getDefaultInstance( 99 System.getProperties(), null); 100 session.setDebug(debug); 101 102 File srcFolder = new File(srcdir); 104 if (!srcFolder.exists()) { 105 System.out.println("source folder does not exist"); 106 System.exit(1); 107 } 108 109 URLName dstURLName = new URLName(dstURL); 111 Folder dstFolder; 112 if (dstURLName.getFile() == null) { 115 Store s = session.getStore(dstURLName); 116 s.connect(); 117 dstFolder = s.getFolder(srcFolder.getName()); 118 } else 119 dstFolder = session.getFolder(new URLName(dstURL)); 120 121 if (clear && dstFolder.exists()) { 122 if (!dstFolder.delete(true)) { 123 System.out.println("couldn't delete " + 124 dstFolder.getFullName()); 125 return; 126 } 127 } 128 copy(srcFolder, dstFolder); 129 130 dstFolder.getStore().close(); 132 133 } catch (MessagingException mex) { 134 System.out.println(mex.getMessage()); 135 mex.printStackTrace(); 136 } catch (IOException ioex) { 137 System.out.println(ioex.getMessage()); 138 ioex.printStackTrace(); 139 } 140 } 141 142 private static void copy(File src, Folder dst) 143 throws MessagingException, IOException { 144 System.out.println("Populating " + dst.getFullName()); 145 146 if (!dst.exists()) { 147 int type = holdsMessages(src) ? 149 Folder.HOLDS_MESSAGES : Folder.HOLDS_FOLDERS; 150 if (!dst.create(type)) { 151 System.out.println("couldn't create " + dst.getFullName()); 152 return; 153 } 154 155 if (holdsMessages(src)) 157 copyMessages(src, dst); 158 } else { 159 System.out.println(dst.getFullName() + " already exists"); 160 if (force && holdsMessages(src)) 162 copyMessages(src, dst); 163 } 164 165 if (holdsFolders(src)) { 167 String [] sf = src.list(); 168 for (int i = 0; sf != null && i < sf.length; i++) { 169 if (skipSCCS && sf[i].equals("SCCS")) 171 continue; 172 File f = new File(src, sf[i]); 173 if (f.isDirectory()) 174 copy(f, dst.getFolder(sf[i])); 175 } 176 } 177 } 178 179 183 private static boolean holdsMessages(File f) { 184 File msg = new File(f, "1"); 185 return msg.exists(); 186 } 187 188 private static boolean holdsFolders(File f) { 189 return !holdsMessages(f); } 191 192 198 private static void copyMessages(File src, Folder dst) 199 throws MessagingException, IOException { 200 System.out.println(" Copy from " + src + " to " + dst); 201 int msgnum = 1; 202 Message[] msgs = new Message[1]; 203 for (;;) { 204 File f = new File(src, String.valueOf(msgnum)); 205 if (!f.exists()) break; 207 FileInputStream fis = new FileInputStream(f); 208 BufferedInputStream is = new BufferedInputStream(fis); 209 is.mark(1024); 210 DataInputStream dis = new DataInputStream(is); 211 String line = dis.readLine(); 212 216 if (!line.startsWith("From ")) 217 is.reset(); 218 MimeMessage msg = new MimeMessage(session, is); 219 fis.close(); 220 msgs[0] = msg; 221 dst.appendMessages(msgs); 222 msgnum++; 223 } 224 System.out.println(" Copied " + (msgnum - 1) + " messages"); 225 } 226 227 private static void printUsage() { 228 System.out.println("fpopulate [-D] [-f] [-S] [-c] " + 229 "-s source_dir -d dest_url"); 230 System.out.println("URLs are of the form: " + 231 "protocol://username:password@hostname/foldername"); 232 System.out.println("The destination URL does not need a foldername," + 233 " in which case, the source foldername is used"); 234 } 235 } 236
| Popular Tags
|