KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ArrayList JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.team.internal.ccvs.core.*;
22 import org.eclipse.team.internal.ccvs.core.client.listeners.*;
23 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
24
25 /**
26  * This custom update command will only update files that
27  * are either incoming changes (Update-existing) or auto-mergable
28  * (Merged with no "+=" in entry line).
29  */

30 public class UpdateMergableOnly extends Update {
31     
32     private static final String JavaDoc LOCAL_FILE_PATH_VARIABLE_NAME = "localFilePath"; //$NON-NLS-1$
33
private static ServerMessageLineMatcher MERGE_UPDATE_CONFLICTING_ADDITION_MATCHER;
34     static {
35         // TODO: temprary until proper lifecycle is defined
36
initializePatterns();
37     }
38     public static void initializePatterns() {
39         try {
40             MERGE_UPDATE_CONFLICTING_ADDITION_MATCHER = new ServerMessageLineMatcher(
41                 IMessagePatterns.MERGE_UPDATE_CONFLICTING_ADDITION, new String JavaDoc[] {LOCAL_FILE_PATH_VARIABLE_NAME});
42         } catch (CVSException e) {
43             // This is serious as the listener will not function properly
44
CVSProviderPlugin.log(e);
45         }
46     }
47     
48     List JavaDoc skippedFiles = new ArrayList JavaDoc();
49     
50     public class MergableOnlyUpdatedHandler extends UpdatedHandler {
51         
52         public MergableOnlyUpdatedHandler() {
53             // handle "Merged" responses
54
super(UpdatedHandler.HANDLE_MERGED);
55         }
56         
57         /* (non-Javadoc)
58          * @see org.eclipse.team.internal.ccvs.core.client.UpdatedHandler#getTargetFile(org.eclipse.team.internal.ccvs.core.ICVSFolder, java.lang.String, byte[])
59          */

60         protected ICVSFile getTargetFile(ICVSFolder mParent, String JavaDoc fileName, byte[] entryBytes) throws CVSException {
61             String JavaDoc adjustedFileName = fileName;
62             if (ResourceSyncInfo.isMergedWithConflicts(entryBytes)) {
63                 // for merged-with-conflict, return a temp file
64
adjustedFileName = ".##" + adjustedFileName + " " + ResourceSyncInfo.getRevision(entryBytes); //$NON-NLS-1$ //$NON-NLS-2$
65
skippedFiles.add(((IContainer)mParent.getIResource()).getFile(new Path(null, fileName)));
66             }
67             return super.getTargetFile(mParent, adjustedFileName, entryBytes);
68         }
69         
70         /* (non-Javadoc)
71          * @see org.eclipse.team.internal.ccvs.core.client.UpdatedHandler#receiveTargetFile(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.ICVSFile, java.lang.String, java.util.Date, boolean, boolean, org.eclipse.core.runtime.IProgressMonitor)
72          */

73         protected void receiveTargetFile(
74             Session session,
75             ICVSFile mFile,
76             String JavaDoc entryLine,
77             Date JavaDoc modTime,
78             boolean binary,
79             boolean readOnly,
80             boolean executable,
81             IProgressMonitor monitor)
82             throws CVSException {
83             
84             if (ResourceSyncInfo.isMergedWithConflicts(entryLine.getBytes())) {
85                 // For merged-with-conflict, just recieve the file contents.
86
// Use the Updated handler type so that the file will be created or
87
// updated.
88
session.receiveFile(mFile, binary, UpdatedHandler.HANDLE_UPDATED, monitor);
89                 // Now delete the file since it is not used
90
mFile.delete();
91             } else {
92                 super.receiveTargetFile(session, mFile, entryLine, modTime, binary, readOnly, executable, monitor);
93             }
94         }
95     }
96     
97     /**
98      * Override the general update listener to handle the following
99      * message:
100      * cvs server: file folder/file.ext exists, but has been added in revision TAG_NAME
101      * This is required because MergeSubscriber adjusts the base when an update
102      * occurs and we can end up in a situation where the update faile with the
103      * above message (see buh 58654).
104      */

105     public class MergeUpdateListener extends UpdateListener {
106         public MergeUpdateListener(IUpdateMessageListener updateMessageListener) {
107             super(updateMessageListener);
108         }
109         public IStatus errorLine(String JavaDoc line, ICVSRepositoryLocation location, ICVSFolder commandRoot, IProgressMonitor monitor) {
110             Map JavaDoc variables = MERGE_UPDATE_CONFLICTING_ADDITION_MATCHER.processServerMessage(line);
111             if (variables != null) {
112                 String JavaDoc filePath = (String JavaDoc)variables.get(LOCAL_FILE_PATH_VARIABLE_NAME);
113                 try {
114                     ICVSResource cvsResource = commandRoot.getChild(filePath);
115                     IResource resource = cvsResource.getIResource();
116                     if (resource != null && resource.getType() == IResource.FILE) {
117                         skippedFiles.add(resource);
118                         return OK;
119                     }
120                 } catch (CVSException e) {
121                     CVSProviderPlugin.log(e);
122                     // Fall through to let the superclass process the error line
123
}
124             }
125             return super.errorLine(line, location, commandRoot, monitor);
126         }
127     }
128     
129     /* (non-Javadoc)
130      * @see org.eclipse.team.internal.ccvs.core.client.Command#doExecute(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption[], org.eclipse.team.internal.ccvs.core.client.Command.LocalOption[], java.lang.String[], org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener, org.eclipse.core.runtime.IProgressMonitor)
131      */

132     protected IStatus doExecute(
133         Session session,
134         GlobalOption[] globalOptions,
135         LocalOption[] localOptions,
136         String JavaDoc[] arguments,
137         ICommandOutputListener listener,
138         IProgressMonitor monitor)
139         throws CVSException {
140         
141         MergableOnlyUpdatedHandler newHandler = new MergableOnlyUpdatedHandler();
142         ResponseHandler oldHandler = session.getResponseHandler(newHandler.getResponseID());
143         skippedFiles.clear();
144         try {
145             session.registerResponseHandler(newHandler);
146             // Don't create backup files since merges won't be overridden
147
session.setCreateBackups(false);
148             return super.doExecute(
149                 session,
150                 globalOptions,
151                 localOptions,
152                 arguments,
153                 new MergeUpdateListener(null),
154                 monitor);
155         } finally {
156             session.registerResponseHandler(oldHandler);
157             session.setCreateBackups(true);
158         }
159     }
160
161     public IFile[] getSkippedFiles() {
162         return (IFile[]) skippedFiles.toArray(new IFile[skippedFiles.size()]);
163     }
164     
165 }
166
Popular Tags