KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > importcmd > ImportCommand


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.importcmd;
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 import org.netbeans.lib.cvsclient.response.WrapperSendResponse;
30 import org.netbeans.lib.cvsclient.util.*;
31
32 /**
33  * The import command imports local directory structures into the repository.
34  *
35  * @author Thomas Singer
36  */

37 public class ImportCommand extends BuildableCommand {
38     private Map wrapperMap = new HashMap();
39     private String JavaDoc logMessage;
40     private String JavaDoc module;
41     private String JavaDoc releaseTag;
42     private String JavaDoc vendorBranch;
43     private String JavaDoc vendorTag;
44     private String JavaDoc importDirectory;
45     private KeywordSubstitutionOptions keywordSubstitutionOptions;
46     private boolean useFileModifTime;
47     private List ignoreList = new LinkedList();
48
49     public ImportCommand() {
50         resetCVSCommand();
51     }
52
53     public void addWrapper(String JavaDoc filenamePattern, KeywordSubstitutionOptions keywordSubstitutionOptions) {
54         if (keywordSubstitutionOptions == null) {
55             throw new IllegalArgumentException JavaDoc("keywordSubstitutionOptions must not be null");
56         }
57
58         wrapperMap.put(new SimpleStringPattern(filenamePattern), keywordSubstitutionOptions);
59     }
60
61     public void addWrapper(StringPattern filenamePattern, KeywordSubstitutionOptions keywordSubstitutionOptions) {
62         if (keywordSubstitutionOptions == null) {
63             throw new IllegalArgumentException JavaDoc("keywordSubstitutionOptions must not be null");
64         }
65
66         wrapperMap.put(filenamePattern, keywordSubstitutionOptions);
67     }
68
69     /**
70      * Compliant method to addWrapper. It replaces the whole list of cvswrappers.
71      * The Map's structure should be following:
72      * Key: instance of StringPattern(fileName wildpattern)
73      * Value: instance of KeywordSubstitutionOptions
74      */

75     public void setWrappers(Map wrapperMap) {
76         this.wrapperMap = wrapperMap;
77     }
78
79     /**
80      * Returns a map with all wrappers.
81      * For map descriptions see setWrapper()
82      */

83     public Map getWrappers() {
84         return wrapperMap;
85     }
86
87     /**
88      * Returns the keyword substitution option.
89      */

90     public KeywordSubstitutionOptions getKeywordSubstitutionOptions() {
91         return keywordSubstitutionOptions;
92     }
93
94     /**
95      * Sets the keywords substitution option.
96      */

97     public void setKeywordSubstitutionOptions(KeywordSubstitutionOptions keywordSubstitutionOptions) {
98         this.keywordSubstitutionOptions = keywordSubstitutionOptions;
99     }
100
101     /**
102      * Returns the release tag.
103      */

104     public String JavaDoc getReleaseTag() {
105         return releaseTag;
106     }
107
108     /**
109      * Sets the necessary release tag.
110      */

111     public void setReleaseTag(String JavaDoc releaseTag) {
112         this.releaseTag = getTrimmedString(releaseTag);
113     }
114
115     /**
116      * Returns the log message.
117      */

118     public String JavaDoc getLogMessage() {
119         return logMessage;
120     }
121
122     /**
123      * Sets the log message.
124      */

125     public void setLogMessage(String JavaDoc logMessage) {
126         this.logMessage = getTrimmedString(logMessage);
127     }
128
129     /**
130      * Returns the module (the in-repository path, where the files should be
131      * stored.
132      */

133     public String JavaDoc getModule() {
134         return module;
135     }
136
137     /**
138      * Sets the module (the in-repository path, where the files should be
139      * stored).
140      */

141     public void setModule(String JavaDoc module) {
142         this.module = getTrimmedString(module);
143     }
144
145     /**
146      * Pints to directoty to import.
147      */

148     public void setImportDirectory(String JavaDoc directory) {
149         importDirectory = directory;
150     }
151
152     public String JavaDoc getImportDirectory() {
153         return importDirectory;
154     }
155
156     /**
157      * Returns the vendor branch.
158      */

159     public String JavaDoc getVendorBranch() {
160         return vendorBranch;
161     }
162
163     /**
164      * Returns the vendor branch.
165      * If not set, then 1.1.1 is returned.
166      */

167     private String JavaDoc getVendorBranchNotNull() {
168         if (vendorBranch == null) {
169             return "1.1.1"; //NOI18N
170
}
171
172         return vendorBranch;
173     }
174
175     /**
176      * Sets the vendor branch.
177      * If null is set, the default branch 1.1.1 is used automatically.
178      */

179     public void setVendorBranch(String JavaDoc vendorBranch) {
180         this.vendorBranch = getTrimmedString(vendorBranch);
181     }
182
183     /**
184      * Returns the vendor tag.
185      */

186     public String JavaDoc getVendorTag() {
187         return vendorTag;
188     }
189
190     /**
191      * Sets the necessary vendor tag.
192      */

193     public void setVendorTag(String JavaDoc vendorTag) {
194         this.vendorTag = getTrimmedString(vendorTag);
195     }
196     
197     /**
198      * Tells, whether the file modification time is to be used as the time of the import.
199      */

200     public boolean isUseFileModifTime() {
201         return useFileModifTime;
202     }
203     
204     /**
205      * Sets whether the file modification time is to be used as the time of the import.
206      */

207     public void setUseFileModifTime(boolean useFileModifTime) {
208         this.useFileModifTime = useFileModifTime;
209     }
210     
211     /**
212      * Get a list of files that are ignored by import.
213      */

214     public List getIgnoreFiles() {
215         return Collections.unmodifiableList(ignoreList);
216     }
217     
218     /**
219      * Add a file name that is to be ignored by the import.
220      */

221     public void addIgnoredFile(String JavaDoc ignoredFileName) {
222         ignoreList.add(ignoredFileName);
223     }
224
225     /**
226      * Executes thiz command using the set options.
227      */

228     public void execute(ClientServices client, EventManager eventManager)
229             throws CommandException, AuthenticationException {
230         // check necessary fields
231
if (getLogMessage() == null) {
232             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.messageEmpty"); //NOI18N
233
throw new CommandException("message may not be null nor empty", //NOI18N
234
localizedMsg);
235         }
236         if (getModule() == null) {
237             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.moduleEmpty"); //NOI18N
238
throw new CommandException("module may not be null nor empty", //NOI18N
239
localizedMsg);
240         }
241         if (getReleaseTag() == null) {
242             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.releaseTagEmpty"); //NOI18N
243
throw new CommandException("release tag may not be null nor empty", //NOI18N
244
localizedMsg);
245         }
246         if (getVendorTag() == null) {
247             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.vendorTagEmpty"); //NOI18N
248
throw new CommandException("vendor tag may not be null nor empty", //NOI18N
249
localizedMsg);
250         }
251
252         client.ensureConnection();
253
254         // get the connection wrappers here
255
Map allWrappersMap = new HashMap(client.getWrappersMap());
256         allWrappersMap.putAll(getWrappers());
257         setWrappers(allWrappersMap);
258
259         // start working
260
super.execute(client, eventManager);
261         assert getLocalDirectory() != null : "local directory may not be null";
262
263         List requestList = new ArrayList();
264
265         try {
266             // add requests
267
requestList.add(new ArgumentRequest("-b")); //NOI18N
268
requestList.add(new ArgumentRequest(getVendorBranchNotNull()));
269
270             if (getKeywordSubstitutionOptions() != null) {
271                 requestList.add(new ArgumentRequest("-k")); //NOI18N
272
requestList.add(new ArgumentRequest(getKeywordSubstitutionOptions().toString()));
273             }
274
275             addMessageRequests(requestList, getLogMessage());
276
277             addWrapperRequests(requestList, this.wrapperMap);
278             
279             if (isUseFileModifTime()) {
280                 requestList.add(new ArgumentRequest("-d")); //NOI18N
281
}
282             
283             for (int i = 0; i < ignoreList.size(); i++) {
284                 requestList.add(new ArgumentRequest("-I")); //NOI18N
285
requestList.add(new ArgumentRequest((String JavaDoc) ignoreList.get(i)));
286             }
287
288             requestList.add(new ArgumentRequest(getModule()));
289             requestList.add(new ArgumentRequest(getVendorTag()));
290             requestList.add(new ArgumentRequest(getReleaseTag()));
291
292             addFileRequests(new File(getLocalDirectory()),
293                             requestList, client);
294
295             requestList.add(new DirectoryRequest(".", getRepositoryRoot(client))); //NOI18N
296

297             requestList.add(CommandRequest.IMPORT);
298
299             // process the requests
300
client.processRequests(requestList);
301         }
302         catch (CommandException ex) {
303             throw ex;
304         }
305         catch (EOFException ex) {
306             String JavaDoc localizedMsg = CommandException.getLocalMessage("CommandException.EndOfFile", null); //NOI18N
307
throw new CommandException(ex, localizedMsg);
308         }
309         catch (Exception JavaDoc ex) {
310             throw new CommandException(ex, ex.getLocalizedMessage());
311         }
312     }
313
314     public String JavaDoc getCVSCommand() {
315         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("import "); //NOI18N
316
toReturn.append(getCVSArguments());
317         if (getModule() != null) {
318             toReturn.append(" "); //NOI18N
319
toReturn.append(getModule());
320         }
321         else {
322             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.moduleEmpty.text"); //NOI18N
323
toReturn.append(" "); //NOI18N
324
toReturn.append(localizedMsg);
325         }
326         if (getVendorTag() != null) {
327             toReturn.append(" "); //NOI18N
328
toReturn.append(getVendorTag());
329         }
330         else {
331             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.vendorTagEmpty.text"); //NOI18N
332
toReturn.append(" "); //NOI18N
333
toReturn.append(localizedMsg);
334         }
335         if (getReleaseTag() != null) {
336             toReturn.append(" "); //NOI18N
337
toReturn.append(getReleaseTag());
338         }
339         else {
340             String JavaDoc localizedMsg = CommandException.getLocalMessage("ImportCommand.releaseTagEmpty.text"); //NOI18N
341
toReturn.append(" "); //NOI18N
342
toReturn.append(localizedMsg);
343         }
344         return toReturn.toString();
345     }
346
347     public String JavaDoc getCVSArguments() {
348         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
349
if (getLogMessage() != null) {
350             toReturn.append("-m \""); //NOI18N
351
toReturn.append(getLogMessage());
352             toReturn.append("\" "); //NOI18N
353
}
354         if (getKeywordSubstitutionOptions() != null) {
355             toReturn.append("-k"); //NOI18N
356
toReturn.append(getKeywordSubstitutionOptions().toString());
357             toReturn.append(" "); //NOI18N
358
}
359         if (getVendorBranch() != null) {
360             toReturn.append("-b "); //NOI18N
361
toReturn.append(getVendorBranch());
362             toReturn.append(" "); //NOI18N
363
}
364         if (isUseFileModifTime()) {
365             toReturn.append("-d "); // NOI18N
366
}
367         if (wrapperMap.size() > 0) {
368             Iterator it = wrapperMap.keySet().iterator();
369             while (it.hasNext()) {
370                 StringPattern pattern = (StringPattern)it.next();
371                 KeywordSubstitutionOptions keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern);
372                 toReturn.append("-W "); //NOI18N
373
toReturn.append(pattern.toString());
374                 toReturn.append(" -k '"); //NOI18N
375
toReturn.append(keywordSubstitutionOptions.toString());
376                 toReturn.append("' "); //NOI18N
377
}
378         }
379         for (Iterator it = ignoreList.iterator(); it.hasNext(); ) {
380             toReturn.append("-I "); //NOI18N
381
toReturn.append((String JavaDoc) it.next());
382             toReturn.append(" "); //NOI18N
383
}
384         return toReturn.toString();
385     }
386
387     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
388         if (opt == 'b') {
389             setVendorBranch(optArg);
390         }
391         else if (opt == 'm') {
392             setLogMessage(optArg);
393         }
394         else if (opt == 'k') {
395             setKeywordSubstitutionOptions(KeywordSubstitutionOptions.findKeywordSubstOption(optArg));
396         }
397         else if (opt == 'W') {
398             Map wrappers = WrapperSendResponse.parseWrappers(optArg);
399             for (Iterator it = wrappers.keySet().iterator(); it.hasNext(); ) {
400                 StringPattern pattern = (StringPattern) it.next();
401                 KeywordSubstitutionOptions keywordOption = (KeywordSubstitutionOptions) wrappers.get(pattern);
402                 addWrapper(pattern, keywordOption);
403             }
404         }
405         else if (opt == 'd') {
406             setUseFileModifTime(true);
407         }
408         else if (opt == 'I') {
409             addIgnoredFile(optArg);
410         }
411         else {
412             return false;
413         }
414         return true;
415     }
416
417     public void resetCVSCommand() {
418         setLogMessage(null);
419         setModule(null);
420         setReleaseTag(null);
421         setVendorTag(null);
422         setVendorBranch(null);
423         setUseFileModifTime(false);
424         ignoreList.clear();
425         wrapperMap.clear();
426     }
427
428     public String JavaDoc getOptString() {
429         return "m:W:b:k:dI:"; //NOI18N
430
}
431
432     /**
433      * Adds requests for the specified logMessage to the specified requestList.
434      */

435     private void addMessageRequests(List requestList, String JavaDoc logMessage) {
436         requestList.add(new ArgumentRequest("-m")); //NOI18N
437

438         StringTokenizer token = new StringTokenizer(logMessage, "\n", false); //NOI18N
439
boolean first = true;
440         while (token.hasMoreTokens()) {
441             if (first) {
442                 requestList.add(new ArgumentRequest(token.nextToken()));
443                 first = false;
444             }
445             else {
446                 requestList.add(new ArgumentxRequest(token.nextToken()));
447             }
448         }
449     }
450
451     /**
452      * Adds requests for specified wrappers to the specified requestList.
453      */

454     private void addWrapperRequests(List requestList, Map wrapperMap) {
455         for (Iterator it = wrapperMap.keySet().iterator(); it.hasNext();) {
456             StringPattern pattern = (StringPattern) it.next();
457             KeywordSubstitutionOptions keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern);
458
459             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
460             buffer.append(pattern.toString());
461             buffer.append(" -k '"); //NOI18N
462
buffer.append(keywordSubstitutionOptions.toString());
463             buffer.append("'"); //NOI18N
464

465             requestList.add(new ArgumentRequest("-W")); //NOI18N
466
requestList.add(new ArgumentRequest(buffer.toString()));
467         }
468     }
469
470     /**
471      * Adds recursively all request for files and directories in the specified
472      * directory to the specified requestList.
473      */

474     private void addFileRequests(File directory,
475                                  List requestList,
476                                  ClientServices clientServices)
477             throws IOException {
478         String JavaDoc relativePath = getRelativeToLocalPathInUnixStyle(directory);
479         String JavaDoc repository = getRepositoryRoot(clientServices);
480         if (!relativePath.equals(".")) { //NOI18N
481
repository += '/' + relativePath;
482         }
483         requestList.add(new DirectoryRequest(relativePath, repository));
484
485         File[] files = directory.listFiles();
486         if (files == null) {
487             return;
488         }
489
490         List subdirectories = null;
491
492         for (int i = 0; i < files.length; i++) {
493             File file = files[i];
494             String JavaDoc filename = file.getName();
495
496             if (clientServices.shouldBeIgnored(directory, filename)) {
497                 continue;
498             }
499
500             if (file.isDirectory()) {
501                 if (subdirectories == null) {
502                     subdirectories = new LinkedList();
503                 }
504                 subdirectories.add(file);
505             }
506             else {
507                 boolean isBinary = isBinary(filename);
508                 requestList.add(new ModifiedRequest(file, isBinary));
509             }
510         }
511
512         if (subdirectories != null) {
513             for (Iterator it = subdirectories.iterator(); it.hasNext();) {
514                 File subdirectory = (File)it.next();
515                 addFileRequests(subdirectory, requestList, clientServices);
516             }
517         }
518     }
519
520     /**
521      * Returns the used root path in the repository.
522      * It's built from the repository stored in the clientService and the
523      * module.
524      */

525     private String JavaDoc getRepositoryRoot(ClientServices clientServices) {
526         String JavaDoc repository = clientServices.getRepository() + '/' + getModule();
527         return repository;
528     }
529
530     /**
531      * Returns true, if the file for the specified filename should be treated as
532      * a binary file.
533      *
534      * The information comes from the wrapper map and the set keywordsubstitution.
535      */

536     private boolean isBinary(String JavaDoc filename) {
537         KeywordSubstitutionOptions keywordSubstitutionOptions = getKeywordSubstitutionOptions();
538
539         for (Iterator it = wrapperMap.keySet().iterator(); it.hasNext();) {
540             StringPattern pattern = (StringPattern)it.next();
541             if (pattern.doesMatch(filename)) {
542                 keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern);
543                 break;
544             }
545         }
546
547         return keywordSubstitutionOptions == KeywordSubstitutionOptions.BINARY;
548     }
549
550     /**
551      * Creates the ImportBuilder.
552      */

553     public Builder createBuilder(EventManager eventManager) {
554         return new ImportBuilder(eventManager, this);
555     }
556 }
557
Popular Tags