KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > export > ExportCommand


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.cvsclient.command.export;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.netbeans.lib.cvsclient.*;
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.connection.*;
27 import org.netbeans.lib.cvsclient.event.*;
28 import org.netbeans.lib.cvsclient.request.*;
29
30 /**
31  * The export command exports the projects (modules in the repository)
32  * to the local directory structure.
33  *
34  * @author MIlos Kleint
35  */

36 public class ExportCommand extends RepositoryCommand {
37
38     /**
39      * A store of potentially empty directories. When a directory has a file
40      * in it, it is removed from this set. This set allows the prune option
41      * to be implemented.
42      */

43     private final Set emptyDirectories = new HashSet();
44     private boolean pruneDirectories;
45     private KeywordSubstitutionOptions keywordSubstitutionOptions;
46
47     /** Holds value of property exportByDate. */
48     private String JavaDoc exportByDate;
49
50     /** Holds value of property exportByRevision. */
51     private String JavaDoc exportByRevision;
52
53     /** Holds value of property exportDirectory. */
54     private String JavaDoc exportDirectory;
55
56     /** Holds value of property useHeadIfNotFound. */
57     private boolean useHeadIfNotFound;
58
59     /** Don't shorten module paths if -d specified. */
60     private boolean notShortenPaths;
61     
62     /** Do not run module program (if any). */
63     private boolean notRunModuleProgram;
64     
65     public ExportCommand() {
66         resetCVSCommand();
67     }
68
69     /**
70      * Returns the keyword substitution option.
71      */

72     public KeywordSubstitutionOptions getKeywordSubstitutionOptions() {
73         return keywordSubstitutionOptions;
74     }
75
76     /**
77      * Sets the keywords substitution option.
78      */

79     public void setKeywordSubstitutionOptions(KeywordSubstitutionOptions keywordSubstitutionOptions) {
80         this.keywordSubstitutionOptions = keywordSubstitutionOptions;
81     }
82
83     /**
84      * Set whether to prune directories.
85      * This is the -P option in the command-line CVS.
86      */

87     public void setPruneDirectories(boolean pruneDirectories) {
88         this.pruneDirectories = pruneDirectories;
89     }
90
91     /**
92      * Get whether to prune directories.
93      * @return true if directories should be removed if they contain no files,
94      * false otherwise.
95      */

96     public boolean isPruneDirectories() {
97         return pruneDirectories;
98     }
99
100     /**
101      * Execute this command
102      * @param client the client services object that provides any necessary
103      * services to this command, including the ability to actually process
104      * all the requests
105      */

106     protected void postExpansionExecute(ClientServices client, EventManager em)
107             throws CommandException, AuthenticationException {
108         //
109
// moved modules code to the end of the other arguments --GAR
110
//
111
final int FIRST_INDEX = 0;
112         final int SECOND_INDEX = 1;
113         if (!isRecursive()) {
114             requests.add(FIRST_INDEX, new ArgumentRequest("-l")); //NOI18N
115
}
116         if (useHeadIfNotFound) {
117             requests.add(FIRST_INDEX, new ArgumentRequest("-f")); //NOI18N
118
}
119         if (exportDirectory != null && (!exportDirectory.equals(""))) {
120             requests.add(FIRST_INDEX, new ArgumentRequest("-d")); //NOI18N
121
requests.add(SECOND_INDEX, new ArgumentRequest(getExportDirectory()));
122         }
123         if (exportByDate != null && exportByDate.length() > 0) {
124             requests.add(FIRST_INDEX, new ArgumentRequest("-D")); //NOI18N
125
requests.add(SECOND_INDEX, new ArgumentRequest(getExportByDate()));
126         }
127         if (exportByRevision != null && exportByRevision.length() > 0) {
128             requests.add(FIRST_INDEX, new ArgumentRequest("-r")); //NOI18N
129
requests.add(SECOND_INDEX, new ArgumentRequest(getExportByRevision()));
130         }
131         if (notShortenPaths) {
132             requests.add(FIRST_INDEX, new ArgumentRequest("-N")); //NOI18N
133
}
134         if (notRunModuleProgram) {
135             requests.add(FIRST_INDEX, new ArgumentRequest("-n")); //NOI18N
136
}
137         if (getKeywordSubstitutionOptions() != null) {
138             requests.add(new ArgumentRequest("-k" + getKeywordSubstitutionOptions())); //NOI18N
139
}
140
141         addArgumentRequests();
142
143         requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
144
requests.add(CommandRequest.EXPORT);
145         try {
146             client.processRequests(requests);
147             if (pruneDirectories) {
148                 pruneEmptyDirectories();
149             }
150             requests.clear();
151
152         }
153         catch (CommandException ex) {
154             throw ex;
155         }
156         catch (Exception JavaDoc ex) {
157             throw new CommandException(ex, ex.getLocalizedMessage());
158         } finally {
159             removeAllCVSAdminFiles();
160         }
161     }
162
163     private void removeAllCVSAdminFiles() {
164         File rootDirect = null;
165         if (getExportDirectory() != null) {
166             rootDirect = new File(getLocalDirectory(), getExportDirectory());
167             deleteCVSSubDirs(rootDirect);
168         }
169         else {
170             rootDirect = new File(getLocalDirectory());
171             Iterator mods = expandedModules.iterator();
172             while (mods.hasNext()) {
173                 String JavaDoc mod = mods.next().toString();
174                 File modRoot = new File(rootDirect.getAbsolutePath(), mod);
175                 deleteCVSSubDirs(modRoot);
176             }
177         }
178     }
179
180     private void deleteCVSSubDirs(File root) {
181         if (root.isDirectory()) {
182             File[] subDirs = root.listFiles();
183             if (subDirs == null) {
184                 return;
185             }
186
187             for (int i = 0; i < subDirs.length; i++) {
188                 if (subDirs[i].isDirectory()) {
189                     if (subDirs[i].getName().equalsIgnoreCase("CVS")) { //NOI18N
190
final File[] adminFiles = subDirs[i].listFiles();
191                         for (int j = 0; j < adminFiles.length; j++) {
192                             adminFiles[j].delete();
193                         }
194                         subDirs[i].delete();
195                     }
196                     else {
197                         deleteCVSSubDirs(subDirs[i]);
198                     }
199                 }
200             }
201         }
202     }
203
204     public String JavaDoc getCVSCommand() {
205         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("export "); //NOI18N
206
toReturn.append(getCVSArguments());
207         if (modules != null && modules.size() > 0) {
208             for (Iterator it = modules.iterator(); it.hasNext();) {
209                 String JavaDoc module = (String JavaDoc)it.next();
210                 toReturn.append(module);
211                 toReturn.append(' ');
212             }
213         }
214         else {
215             String JavaDoc localizedMsg = CommandException.getLocalMessage("ExportCommand.moduleEmpty.text"); //NOI18N
216
toReturn.append(" "); //NOI18N
217
toReturn.append(localizedMsg);
218         }
219         return toReturn.toString();
220     }
221
222     public String JavaDoc getCVSArguments() {
223         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
224
if (!isRecursive()) {
225             toReturn.append("-l "); //NOI18N
226
}
227         if (isUseHeadIfNotFound()) {
228             toReturn.append("-f "); //NOI18N
229
}
230         if (getExportByDate() != null) {
231             toReturn.append("-D "); //NOI18N
232
toReturn.append(getExportByDate());
233             toReturn.append(" "); //NOI18N
234
}
235         if (getExportByRevision() != null) {
236             toReturn.append("-r "); //NOI18N
237
toReturn.append(getExportByRevision());
238             toReturn.append(" "); //NOI18N
239
}
240         if (isPruneDirectories()) {
241             toReturn.append("-P "); //NOI18N
242
}
243         if (isNotShortenPaths()) {
244             toReturn.append("-N "); // NOI18N
245
}
246         if (isNotRunModuleProgram()) {
247             toReturn.append("-n "); // NOI18N
248
}
249         if (getExportDirectory() != null) {
250             toReturn.append("-d "); //NOI18N
251
toReturn.append(getExportDirectory());
252             toReturn.append(" "); //NOI18N
253
}
254         if (getKeywordSubstitutionOptions() != null) {
255             toReturn.append("-k"); //NOI18N
256
toReturn.append(getKeywordSubstitutionOptions().toString());
257             toReturn.append(" "); //NOI18N
258
}
259         return toReturn.toString();
260     }
261
262     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
263         if (opt == 'k') {
264             setKeywordSubstitutionOptions(KeywordSubstitutionOptions.findKeywordSubstOption(optArg));
265         }
266         else if (opt == 'r') {
267             setExportByRevision(optArg);
268         }
269         else if (opt == 'f') {
270             setUseHeadIfNotFound(true);
271         }
272         else if (opt == 'D') {
273             setExportByDate(optArg);
274         }
275         else if (opt == 'd') {
276             setExportDirectory(optArg);
277         }
278         else if (opt == 'P') {
279             setPruneDirectories(true);
280         }
281         else if (opt == 'N') {
282             setNotShortenPaths(true);
283         }
284         else if (opt == 'n') {
285             setNotRunModuleProgram(true);
286         }
287         else if (opt == 'l') {
288             setRecursive(false);
289         }
290         else if (opt == 'R') {
291             setRecursive(true);
292         }
293         else {
294             return false;
295         }
296         return true;
297     }
298
299     public void resetCVSCommand() {
300         setModules(null);
301         setKeywordSubstitutionOptions(null);
302         setPruneDirectories(false);
303         setRecursive(true);
304         setExportByDate(null);
305         setExportByRevision(null);
306         setExportDirectory(null);
307         setUseHeadIfNotFound(false);
308         setNotShortenPaths(false);
309         setNotRunModuleProgram(false);
310     }
311
312     public String JavaDoc getOptString() {
313         return "k:r:D:NPlRnd:f"; //NOI18N
314
}
315
316     /**
317      * Creates the ExportBuilder.
318      */

319     public Builder createBuilder(EventManager eventManager) {
320         return new ExportBuilder(eventManager, this);
321     }
322
323     /**
324      * Called when the server wants to send a message to be displayed to
325      * the user. The message is only for information purposes and clients
326      * can choose to ignore these messages if they wish.
327      * @param e the event
328      */

329     public void messageSent(MessageEvent e) {
330         super.messageSent(e);
331         // we use this event to determine which directories need to be checked
332
// for updating
333
if (pruneDirectories &&
334                 e.getMessage().indexOf(": Exporting ") > 0) { //NOI18N
335
File file = new File(getLocalDirectory(), e.getMessage().substring(21));
336             emptyDirectories.add(file);
337         }
338     }
339
340     /**
341      * Prunes a directory, recursively pruning its subdirectories
342      * @param directory the directory to prune
343      */

344     private boolean pruneEmptyDirectory(File directory) throws IOException {
345         boolean empty = true;
346
347         final File[] contents = directory.listFiles();
348
349         // should never be null, but just in case...
350
if (contents != null) {
351             for (int i = 0; i < contents.length; i++) {
352                 if (contents[i].isFile()) {
353                     empty = false;
354                 }
355                 else {
356                     if (!contents[i].getName().equals("CVS")) { //NOI18N
357
empty = pruneEmptyDirectory(contents[i]);
358                     }
359                 }
360
361                 if (!empty) {
362                     break;
363                 }
364             }
365
366             if (empty) {
367                 // check this is a CVS directory and not some directory the user
368
// has stupidly called CVS...
369
final File entriesFile = new File(directory, "CVS/Entries"); //NOI18N
370
if (entriesFile.exists()) {
371                     final File adminDir = new File(directory, "CVS"); //NOI18N
372
final File[] adminFiles = adminDir.listFiles();
373                     for (int i = 0; i < adminFiles.length; i++) {
374                         adminFiles[i].delete();
375                     }
376                     adminDir.delete();
377                     directory.delete();
378                 }
379             }
380         }
381
382         return empty;
383     }
384
385     /**
386      * Remove any directories that don't contain any files
387      */

388     private void pruneEmptyDirectories() throws IOException {
389         final Iterator it = emptyDirectories.iterator();
390         while (it.hasNext()) {
391             final File dir = (File)it.next();
392             // we might have deleted it already (due to recursive delete)
393
// so we need to check existence
394
if (dir.exists()) {
395                 pruneEmptyDirectory(dir);
396             }
397         }
398         emptyDirectories.clear();
399     }
400
401     /** Getter for property exportByDate.
402      * @return Value of property exportByDate.
403      */

404     public String JavaDoc getExportByDate() {
405         return this.exportByDate;
406     }
407
408     /** Setter for property exportByDate.
409      * @param exportByDate New value of property exportByDate.
410      */

411     public void setExportByDate(String JavaDoc exportByDate) {
412         this.exportByDate = exportByDate;
413     }
414
415     /** Getter for property exportByRevision.
416      * @return Value of property exportByRevision.
417      */

418     public String JavaDoc getExportByRevision() {
419         return this.exportByRevision;
420     }
421
422     /** Setter for property exportByRevision.
423      * @param exportByRevision New value of property exportByRevision.
424      */

425     public void setExportByRevision(String JavaDoc exportByRevision) {
426         this.exportByRevision = exportByRevision;
427     }
428
429     /** Getter for property exportDirectory.
430      * @return Value of property exportDirectory.
431      */

432     public String JavaDoc getExportDirectory() {
433         return this.exportDirectory;
434     }
435
436     /** Setter for property exportDirectory.
437      * @param exportDirectory New value of property exportDirectory.
438      */

439     public void setExportDirectory(String JavaDoc exportDirectory) {
440         this.exportDirectory = exportDirectory;
441     }
442
443     /** Getter for property useHeadIfNotFound.
444      * @return Value of property useHeadIfNotFound.
445      */

446     public boolean isUseHeadIfNotFound() {
447         return this.useHeadIfNotFound;
448     }
449
450     /** Setter for property useHeadIfNotFound.
451      * @param useHeadIfNotFound New value of property useHeadIfNotFound.
452      */

453     public void setUseHeadIfNotFound(boolean useHeadIfNotFound) {
454         this.useHeadIfNotFound = useHeadIfNotFound;
455     }
456
457     /**
458      * Getter for property notShortenPaths.
459      * @return Value of property notShortenPaths.
460      */

461     public boolean isNotShortenPaths() {
462         return notShortenPaths;
463     }
464     
465     /**
466      * Setter for property notShortenPaths.
467      * @param notShortenPaths New value of property notShortenPaths.
468      */

469     public void setNotShortenPaths(boolean notShortenPaths) {
470         this.notShortenPaths = notShortenPaths;
471     }
472     
473     /**
474      * Getter for property notRunModuleProgram.
475      * @return Value of property notRunModuleProgram.
476      */

477     public boolean isNotRunModuleProgram() {
478         return notRunModuleProgram;
479     }
480     
481     /**
482      * Setter for property notRunModuleProgram.
483      * @param notRunModuleProgram New value of property notRunModuleProgram.
484      */

485     public void setNotRunModuleProgram(boolean notRunModuleProgram) {
486         this.notRunModuleProgram = notRunModuleProgram;
487     }
488     
489 }
490
Popular Tags