KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > populate


1 /*
2  * @(#)populate.java 1.8 03/04/22
3  *
4  * Copyright 1997-2003 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 javax.mail.*;
40 import javax.mail.internet.*;
41
42 /*
43  * Copy folder hierarchies between different Stores. This is a useful
44  * utility to populate new (and possibly empty) mail stores. Specify
45  * both the source and destination folders as URLs.
46  *
47  * @author John Mani
48  */

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 JavaDoc argv[]) {
57         String JavaDoc srcURL = null;
58         String JavaDoc 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         // Get source folder
99
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         // Set up destination folder
107
URLName dstURLName = new URLName(dstURL);
108         Folder dstFolder;
109         // Check if the destination URL has a folder specified. If
110
// not, we use the source folder name
111
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         // Close the respective stores.
128
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         // Create it.
143
if (!dst.create(src.getType())) {
144         System.out.println("couldn't create " + dst.getFullName());
145         return;
146         }
147
148         // Copy over any messges from src to dst
149
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         // Copy over any messges from src to dst
157
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     // Copy over subfolders
165
if ((src.getType() & Folder.HOLDS_FOLDERS) != 0) {
166         Folder[] sf = src.list();
167         for (int i = 0; i < sf.length; i++) {
168             // skip SCCS directories?
169
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