KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fpopulate


1 /*
2  * @(#)fpopulate.java 1.1 02/04/05
3  *
4  * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.io.*;
40 import javax.mail.*;
41 import javax.mail.internet.*;
42
43 /*
44  * Copy folder hierarchies between files and a Store. This is a useful
45  * utility to populate new (and possibly empty) mail stores. Specify
46  * the source as a directory name and the destination folders as a URL.
47  *
48  * @author John Mani
49  * @author Bill Shannon
50  */

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 JavaDoc argv[]) {
61         String JavaDoc srcdir = null;
62         String JavaDoc 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         // Get source folder
103
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         // Set up destination folder
110
URLName dstURLName = new URLName(dstURL);
111         Folder dstFolder;
112         // Check if the destination URL has a folder specified. If
113
// not, we use the source folder name
114
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         // Close the respective stores.
131
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         // Create it.
148
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         // Copy over any messages from src to dst
156
if (holdsMessages(src))
157         copyMessages(src, dst);
158     } else {
159         System.out.println(dst.getFullName() + " already exists");
160         // Copy over any messges from src to dst
161
if (force && holdsMessages(src))
162         copyMessages(src, dst);
163     }
164
165     // Copy over subfolders
166
if (holdsFolders(src)) {
167         String JavaDoc[] sf = src.list();
168         for (int i = 0; sf != null && i < sf.length; i++) {
169         // skip SCCS directories?
170
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     /**
180      * Does this directory hold messages?
181      * Return true if there's at least one message.
182      */

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); // XXX - hack for now
190
}
191
192     /**
193      * Copy message files from the source directory to the
194      * destination folder. Message files must be named "1",
195      * "2", etc. The first missing number terminates the
196      * copy.
197      */

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 when we find a message missing
206
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 JavaDoc line = dis.readLine();
212         /*
213          * If it's in UNIX mbox format, we skip the first line,
214          * otherwise we start reading at the beginning.
215          */

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