KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > response > UpdatedResponse


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 the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.response;
23
24 import java.io.*;
25 import java.util.*;
26 import java.text.*;
27
28 import org.netbeans.lib.cvsclient.admin.*;
29 import org.netbeans.lib.cvsclient.event.*;
30 import org.netbeans.lib.cvsclient.file.*;
31 import org.netbeans.lib.cvsclient.util.*;
32 import org.netbeans.lib.cvsclient.command.DefaultFileInfoContainer;
33
34 /**
35  * Sends a new copy of a particular file, indicating that the file currently
36  * checked-out needs to be updated with the one sent with this response.
37  * @author Robert Greig
38  */

39 class UpdatedResponse implements Response {
40     private static final boolean DEBUG = false;
41
42     /**
43      * The local path of the new file.
44      */

45     private String JavaDoc localPath;
46
47     /**
48      * The full repository path of the file.
49      */

50     private String JavaDoc repositoryPath;
51
52     /**
53      * The entry line.
54      */

55     private String JavaDoc entryLine;
56
57     /**
58      * The mode.
59      */

60     private String JavaDoc mode;
61
62     /**
63      * fullpath to the file being processed. Is used in Merged response when sending EnhancedMessageEvent
64      */

65     protected String JavaDoc localFile;
66
67     /**
68      * The date Formatter used to parse and format dates.
69      * Format is: "EEE MMM dd HH:mm:ss yyyy"
70      */

71     private DateFormat dateFormatter;
72     
73     /**
74      * Process the data for the response.
75      * @param r the buffered reader allowing the client to read the server's
76      * response. Note that the actual response name has already been read
77      * and the reader is positioned just before the first argument, if any.
78      * @param services various services that are useful to response handlers
79      * @throws ResponseException if something goes wrong handling this response
80      */

81     public void process(LoggedDataInputStream dis, ResponseServices services)
82             throws ResponseException {
83         try {
84             localPath = dis.readLine();
85             repositoryPath = dis.readLine();
86             entryLine = dis.readLine();
87             mode = dis.readLine();
88
89             String JavaDoc nextLine = dis.readLine();
90
91             boolean useGzip = (nextLine.charAt(0) == 'z');
92
93             int length = Integer.parseInt(useGzip ? nextLine.substring(1)
94                                           : nextLine);
95
96             if (DEBUG) {
97                 System.err.println("Got update response."); //NOI18N
98
System.err.println("LocalPath is : " + localPath); //NOI18N
99
System.err.println("Repository path is : " + repositoryPath); //NOI18N
100
System.err.println("Entries line is : " + entryLine); //NOI18N
101
System.err.println("Mode is : " + mode); //NOI18N
102
System.err.println("Next line (length) is : " + nextLine); //NOI18N
103
System.err.println("File length is : " + length); //NOI18N
104
}
105
106             // now read in the file
107
final String JavaDoc filePath = services.convertPathname(localPath,
108                                                              repositoryPath);
109
110             final File newFile = new File(filePath);
111
112             if (services.getGlobalOptions().isExcluded(newFile)) {
113                 skip(dis, length);
114                 return;
115             }
116             
117             if (this instanceof CreatedResponse) {
118                 if (newFile.exists()) {
119                     skip(dis, length);
120                     // Fire "C file.txt" type of event. This event is caught directly by clients of the library
121
DefaultFileInfoContainer fic = new DefaultFileInfoContainer();
122                     fic.setType("C");
123                     fic.setFile(newFile);
124                     services.getEventManager().fireCVSEvent(new FileInfoEvent(this, fic));
125                     return;
126                 }
127             }
128             
129             localFile = newFile.getAbsolutePath();
130             final Entry entry = new Entry(entryLine);
131
132             FileHandler fileHandler = useGzip ? services.getGzipFileHandler()
133                     : services.getUncompressedFileHandler();
134             fileHandler.setNextFileDate(services.getNextFileDate());
135
136             // check if the file is binary
137
if (entry.isBinary()) {
138                 fileHandler.writeBinaryFile(filePath, mode, dis, length);
139             }
140             else {
141                 fileHandler.writeTextFile(filePath, mode, dis, length);
142             }
143
144             // we set the date the file was last modified in the Entry line
145
// so that we can easily determine whether the file has been
146
// untouched
147
// for files with conflicts skip the setting of the conflict field.
148
String JavaDoc conflictString = null;
149             if ((entry.getConflict() != null) &&
150                     (entry.getConflict().charAt(0) == Entry.HAD_CONFLICTS)) {
151                 if (entry.getConflict().charAt(1) ==
152                         Entry.TIMESTAMP_MATCHES_FILE) {
153                     final Date d = new Date(newFile.lastModified());
154                     conflictString = getEntryConflict(d, true);
155                 }
156                 else {
157                     conflictString = entry.getConflict().substring(1);
158                 }
159             }
160             else {
161                 final Date d = new Date(newFile.lastModified());
162                 conflictString = getEntryConflict(d, false);
163             }
164             entry.setConflict(conflictString);
165             // fix for correctly merging files that are added in other branches.
166
if (entry.isNewUserFile()) {
167                 entry.setConflict(Entry.DUMMY_TIMESTAMP);
168             }
169             // update the admin files (i.e. within the CVS directory)
170
services.updateAdminData(localPath, repositoryPath, entry);
171
172             // now fire the appropriate event
173
if (newFile.exists()) {
174                 FileAddedEvent e = new FileAddedEvent(this, filePath);
175                 services.getEventManager().fireCVSEvent(e);
176             }
177             else {
178                 FileUpdatedEvent e = new FileUpdatedEvent(this, filePath);
179                 services.getEventManager().fireCVSEvent(e);
180             }
181             //System.err.println("Finished writing file");
182
}
183         catch (IOException e) {
184             throw new ResponseException(e);
185         }
186     }
187
188     private void skip(LoggedDataInputStream dis, int length) throws IOException {
189         while (length > 0) {
190             length -= dis.skip(length);
191         
192         }
193     }
194
195     /**
196      * Returns the Conflict field for the file's entry.
197      * Can be overriden by subclasses.
198      * (For example the MergedResponse that sets the "result of merge" there.)
199      * @param date the date to put in
200      * @param hadConflicts if there were conflicts (e.g after merge)
201      * @return the conflict field
202      */

203     protected String JavaDoc getEntryConflict(Date date, boolean hadConflicts) {
204         return getDateFormatter().format(date);
205     }
206     
207     /**
208      * Returns the DateFormatter instance that parses and formats date Strings.
209      * The exact format matches the one in Entry.getLastModifiedDateFormatter() method.
210      *
211      */

212     protected DateFormat getDateFormatter() {
213         if (dateFormatter == null) {
214             dateFormatter = new SimpleDateFormat(Entry.getLastModifiedDateFormatter().toPattern(), Locale.US);
215             dateFormatter.setTimeZone(Entry.getTimeZone());
216
217         }
218         return dateFormatter;
219     }
220
221     /**
222      * Is this a terminal response, i.e. should reading of responses stop
223      * after this response. This is true for responses such as OK or
224      * an error response
225      */

226     public boolean isTerminalResponse() {
227         return false;
228     }
229 }
230
Popular Tags