KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > UpdatedHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Red Hat Incorporated - is/setExecutable() code
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.core.client;
13
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.team.internal.ccvs.core.*;
18 import org.eclipse.team.internal.ccvs.core.syncinfo.MutableResourceSyncInfo;
19 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
20
21 /**
22  * Handles any "Updated" and "Merged" responses
23  * from the CVS server.
24  * <p>
25  * Suppose as a result of performing a command the CVS server responds
26  * as follows:<br>
27  * <pre>
28  * [...]
29  * Updated ???\n
30  * [...]
31  * </pre>
32  * Then
33  * </p>
34  */

35
36 /**
37  * Does get information about the file that is updated
38  * and the file-content itself and puts it on the fileSystem.
39  *
40  * The difference beetween the "Updated" and the "Merged" is, that
41  * an "Merged" file is not going to be up-to-date after the operation.
42  *
43  * Requiers a exisiting parent-folder.
44  */

45 public class UpdatedHandler extends ResponseHandler {
46     
47     private int handlerType;
48     
49     public static final int HANDLE_UPDATED = ICVSFile.UPDATED;
50     public static final int HANDLE_MERGED = ICVSFile.MERGED;
51     public static final int HANDLE_UPDATE_EXISTING = ICVSFile.UPDATE_EXISTING;
52     public static final int HANDLE_CREATED = ICVSFile.CREATED;
53     
54     private static final String JavaDoc READ_ONLY_FLAG = "u=rw"; //$NON-NLS-1$
55
private static final String JavaDoc EXECUTE_FLAG = "x"; //$NON-NLS-1$
56

57     public UpdatedHandler(int handlerType) {
58         this.handlerType = handlerType;
59     }
60     
61     public String JavaDoc getResponseID() {
62         switch (handlerType) {
63             case HANDLE_UPDATED: return "Updated"; //$NON-NLS-1$
64
case HANDLE_MERGED: return "Merged"; //$NON-NLS-1$
65
case HANDLE_UPDATE_EXISTING: return "Update-existing"; //$NON-NLS-1$
66
case HANDLE_CREATED: return "Created"; //$NON-NLS-1$
67
}
68         return null;
69     }
70
71     public void handle(Session session, String JavaDoc localDir, IProgressMonitor monitor) throws CVSException {
72         // read additional data for the response
73
String JavaDoc repositoryFile = session.readLine();
74         String JavaDoc entryLine = session.readLine();
75         byte[] entryBytes = entryLine.getBytes();
76         String JavaDoc permissionsLine = session.readLine();
77
78         // clear file update modifiers
79
Date JavaDoc modTime = session.getModTime();
80         session.setModTime(null);
81         
82         // Get the local file
83
String JavaDoc fileName = repositoryFile.substring(repositoryFile.lastIndexOf("/") + 1); //$NON-NLS-1$
84
ICVSFolder mParent = getExistingFolder(session, localDir);
85         ICVSFile mFile = getTargetFile(mParent, fileName, entryBytes);
86         
87         boolean binary = ResourceSyncInfo.isBinary(entryBytes);
88         boolean readOnly = permissionsLine.indexOf(READ_ONLY_FLAG) == -1;
89         boolean executable = permissionsLine.indexOf(EXECUTE_FLAG) != -1;
90         
91         try {
92             // The file may have been set as read-only by a previous checkout/update
93
if (mFile.isReadOnly()) mFile.setReadOnly(false);
94         } catch (CVSException e) {
95             // Just log and keep going
96
CVSProviderPlugin.log(e);
97         }
98         
99         try {
100             receiveTargetFile(session, mFile, entryLine, modTime, binary, readOnly, executable, monitor);
101         } catch (CVSException e) {
102             // An error occurred while recieving the file.
103
// If it is due to an invalid file name,
104
// accumulate the error and continue.
105
// Otherwise, exit
106
if (!handleInvalidResourceName(session, mFile, e)) {
107                 throw e;
108             }
109         }
110     }
111
112     protected ICVSFile getTargetFile(ICVSFolder mParent, String JavaDoc fileName, byte[] entryBytes) throws CVSException {
113         return mParent.getFile(fileName);
114     }
115     
116     protected void receiveTargetFile(Session session, ICVSFile mFile, String JavaDoc entryLine, Date JavaDoc modTime, boolean binary, boolean readOnly, boolean executable, IProgressMonitor monitor) throws CVSException {
117         
118         // receive the file contents from the server
119
session.receiveFile(mFile, binary, handlerType, monitor);
120         
121         // Set the timestamp in the file and get it again so that we use the *real* timestamp
122
// in the sync info. The os may not actually set the time we provided :)
123
mFile.setTimeStamp(modTime);
124         modTime = mFile.getTimeStamp();
125         ResourceSyncInfo info = new ResourceSyncInfo(entryLine, null);
126         MutableResourceSyncInfo newInfoWithTimestamp = info.cloneMutable();
127         newInfoWithTimestamp.setTimeStamp(modTime);
128         
129         //see bug 106876
130
CVSTag tag = newInfoWithTimestamp.getTag();
131         if(tag != null && CVSTag.BASE.getName().equals(tag.getName())){
132             newInfoWithTimestamp.setTag(mFile.getSyncInfo().getTag());
133         }
134         
135         int modificationState = ICVSFile.UNKNOWN;
136         if(handlerType==HANDLE_MERGED) {
137             newInfoWithTimestamp.setMerged();
138         } else if (!session.isIgnoringLocalChanges()
139             && !info.isAdded() /* could be an added entry during a merge in which case it is dirty */
140             && (handlerType==HANDLE_UPDATE_EXISTING || handlerType==HANDLE_CREATED)) {
141             // both these cases result in an unmodified file.
142
// reporting is handled by the FileModificationManager
143
modificationState = ICVSFile.CLEAN;
144             CVSProviderPlugin.getPlugin().getFileModificationManager().updated(mFile);
145         }
146         mFile.setSyncInfo(newInfoWithTimestamp, modificationState);
147         try {
148             if (readOnly) mFile.setReadOnly(true);
149             if (executable) mFile.setExecutable(true);
150         } catch (CVSException e) {
151             // Just log and keep going
152
CVSProviderPlugin.log(e);
153         }
154     }
155
156     public int getHandlerType() {
157         return handlerType;
158     }
159     
160 }
161
Popular Tags