KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client;
12
13
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.team.internal.ccvs.core.*;
18 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
19 import org.eclipse.team.internal.ccvs.core.util.Util;
20
21 /**
22  * Handles server responses that arise as a result of issuing a request
23  * (usually a command) to a CVS server. The processing of each such
24  * response is deferred to subclasses.
25  */

26 public abstract class ResponseHandler {
27     /**
28      * Returns the text string of the server response handled by this object.
29      * @return the id
30      */

31     public abstract String JavaDoc getResponseID();
32
33     /**
34      * Handles a server response.
35      * <p>
36      * Suppose as a result of performing a command the CVS server responds
37      * as follows:<br>
38      * <pre>
39      * [...]
40      * Clear-sticky myDirectory \n
41      * /u/cvsroot/myDirectory \n
42      * [...]
43      * </pre>
44      * Then the <code>handle</code> method of the <code>ResponseHandler</code>
45      * for <em>Clear-sticky</em> will be invoked with <code>argument</code>
46      * set to <em>"myDirectory"</em>. It must then read the remaining
47      * response data from the connection (<em>"/u/cvsroot/myDirectory"</em>
48      * including the newline) and take any necessary action.
49      * </p><p>
50      * Note: The type and quantity of additional data that must be read
51      * from the connection varies on a per-response basis.
52      * </p>
53      * @param session the Session used for CVS communication
54      * @param argument the argument supplied with the response
55      * @param monitor the progress monitor for the current CVS command
56      */

57     public abstract void handle(Session session, String JavaDoc argument,
58         IProgressMonitor monitor) throws CVSException;
59     
60     /**
61      * Creates a new CVS folder.
62      * @param localDir the local path of the folder relative to root
63      * @param repositoryDir the remote path of the folder relative to the repository
64      * @return the new folder
65      */

66     protected static ICVSFolder createFolder(
67             Session session,
68             String JavaDoc localDir,
69             String JavaDoc repositoryDir) throws CVSException {
70         
71         ICVSFolder folder = session.getLocalRoot().getFolder(localDir);
72         if (!folder.exists()
73                 && (!CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()
74                         || !folder.getParent().isCVSFolder())) {
75             // Only create the folder if pruning is disabled or the
76
// folder's parent is not a CVS folder (which occurs on checkout).
77
// When pruning is enabled, the folder will be lazily created
78
// when it contains a file (see getExistingFolder)
79
folder.mkdir();
80         }
81         if (! folder.isCVSFolder()) {
82             String JavaDoc repositoryRoot = session.getRepositoryRoot();
83             String JavaDoc relativePath;
84             if (repositoryDir.startsWith(repositoryRoot)) {
85                 // The repositoryDir is an absolute path
86
relativePath = Util.getRelativePath(repositoryRoot, repositoryDir);
87             } else {
88                 // The repositoryDir is already a relative path
89
relativePath = repositoryDir;
90             }
91             IResource resource = folder.getIResource();
92             if (resource != null) {
93                 IProject project = resource.getProject();
94                 if (project != null && project.isAccessible() && !CVSTeamProvider.isSharedWithCVS(project)) {
95                     // The project isn't shared but we are about to perform an operation on it.
96
// we need to flag the project as shared so that the sync info management works
97
CVSTeamProvider.markAsTempShare(project);
98                 }
99             }
100             try{
101             folder.setFolderSyncInfo(new FolderSyncInfo(
102                 relativePath,
103                 session.getCVSRepositoryLocation().getLocation(false),
104                 null, false));
105             } catch (CVSException ex){
106                 IStatus status = ex.getStatus();
107                 if (status != null){
108                     if (status.getCode() == IResourceStatus.INVALID_VALUE){
109                         //if it's an invalid value, just ignore the exception (see Bug# 152053),
110
//else throw it again
111
} else {
112                         throw ex;
113                     }
114                 }
115             }
116         }
117         return folder;
118     }
119
120     protected ICVSFolder getExistingFolder(Session session, String JavaDoc localDir) throws CVSException {
121             ICVSFolder mParent = session.getLocalRoot().getFolder(localDir);
122             if (! mParent.exists()) {
123                 // First, check if the parent is a phantom
124
IContainer container = (IContainer)mParent.getIResource();
125                 if (container != null) {
126                     try {
127                         // Create all the parents as need
128
recreatePhantomFolders(mParent);
129                     } catch (CVSException e) {
130                         if (!handleInvalidResourceName(session, mParent, e)) {
131                             throw e;
132                         }
133                     }
134                 }
135             }
136             return mParent;
137         }
138
139     /**
140      * Method recreatePhantomFolders.
141      * @param mParent
142      */

143     private void recreatePhantomFolders(ICVSFolder folder) throws CVSException {
144         ICVSFolder parent = folder.getParent();
145         if (!parent.exists()) {
146             recreatePhantomFolders(parent);
147         }
148         folder.mkdir();
149     }
150
151     /**
152      * Return as instance that can be used by an open session. Subclasses that contain
153      * session related state must override this message to return a copy of themselves.
154      */

155     /* package */ ResponseHandler getInstance() {
156         return this;
157     }
158     
159     protected boolean handleInvalidResourceName(Session session, ICVSResource resource, CVSException e) {
160         int code = e.getStatus().getCode();
161         if (code == IResourceStatus.INVALID_VALUE
162                 || code == IResourceStatus.INVALID_RESOURCE_NAME
163                 || code == IResourceStatus.RESOURCE_NOT_FOUND
164                 || code == IResourceStatus.RESOURCE_EXISTS
165                 || code == IResourceStatus.RESOURCE_WRONG_TYPE
166                 || code == IResourceStatus.CASE_VARIANT_EXISTS
167                 || code == IResourceStatus.PATH_OCCUPIED) {
168             
169             try {
170                 IResource local = resource.getIResource();
171                 String JavaDoc path;
172                 if (local == null) {
173                     path = resource.getRepositoryRelativePath();
174                 } else {
175                     path = local.getFullPath().toString();
176                 }
177                 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.INVALID_LOCAL_RESOURCE_PATH, NLS.bind(CVSMessages.ResponseHandler_0, new String JavaDoc[] { path, e.getMessage() }), e, session.getLocalRoot());
178                 session.handleResponseError(status);
179             } catch (CVSException e1) {
180                 CVSProviderPlugin.log(e1);
181             }
182             return true;
183         }
184         return false;
185     }
186 }
187
188
Popular Tags