KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.internal.ccvs.core.*;
21 import org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener;
22 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
23
24 /**
25  * Abstract base class for requests that are to be sent to the server.
26  */

27 public abstract class Request {
28     public static final ExpandModules EXPAND_MODULES = new ExpandModules();
29     public static final ValidRequests VALID_REQUESTS = new ValidRequests();
30
31     /*** Response handler map ***/
32     private static final Map JavaDoc responseHandlers = new HashMap JavaDoc();
33     
34     private static void initializeHandlerCache() {
35         synchronized(responseHandlers) {
36             registerResponseHandler(new CheckedInHandler());
37             registerResponseHandler(new CopyHandler());
38             registerResponseHandler(new ModTimeHandler());
39             registerResponseHandler(new NewEntryHandler());
40             registerResponseHandler(new RemovedHandler());
41             registerResponseHandler(new RemoveEntryHandler());
42             registerResponseHandler(new StaticHandler(true));
43             registerResponseHandler(new StaticHandler(false));
44             registerResponseHandler(new StickyHandler(true));
45             registerResponseHandler(new StickyHandler(false));
46             registerResponseHandler(new UpdatedHandler(UpdatedHandler.HANDLE_UPDATED));
47             registerResponseHandler(new UpdatedHandler(UpdatedHandler.HANDLE_UPDATE_EXISTING));
48             registerResponseHandler(new UpdatedHandler(UpdatedHandler.HANDLE_CREATED));
49             registerResponseHandler(new UpdatedHandler(UpdatedHandler.HANDLE_MERGED));
50             registerResponseHandler(new ValidRequestsHandler());
51             registerResponseHandler(new ModuleExpansionHandler());
52             registerResponseHandler(new MTHandler());
53             registerResponseHandler(new NotifiedHandler());
54             registerResponseHandler(new TemplateHandler());
55         }
56     }
57     private static void registerResponseHandler(ResponseHandler handler) {
58         synchronized(responseHandlers) {
59             responseHandlers.put(handler.getResponseID(), handler);
60         }
61     }
62     
63     /**
64      * This method is invoked by Session to get a mutable copy of the
65      * global list of acceptable response handlers.
66      *
67      * @return a map of reponse handlers
68      */

69     protected static Map JavaDoc getReponseHandlerMap() {
70         synchronized(responseHandlers) {
71             if (responseHandlers.isEmpty()) {
72                 initializeHandlerCache();
73             }
74             Map JavaDoc copy = new HashMap JavaDoc();
75             for (Iterator JavaDoc iter = responseHandlers.values().iterator(); iter.hasNext();) {
76                 ResponseHandler handler = (ResponseHandler) iter.next();
77                 copy.put(handler.getResponseID(), handler.getInstance());
78                 
79             }
80             return copy;
81         }
82     }
83     /**
84      * Prevents client code from instantiating us.
85      */

86     protected Request() { }
87
88     /**
89      * Returns the string used to invoke this request on the server.
90      * [template method]
91      *
92      * @return the request identifier string
93      */

94     protected abstract String JavaDoc getRequestId();
95
96     /**
97      * Executes a request and processes the responses.
98      *
99      * @param session the open CVS session
100      * @param listener the command output listener, or null to discard all messages
101      * @param monitor the progress monitor
102      * @return a status code indicating success or failure of the operation
103      */

104     protected IStatus executeRequest(Session session, ICommandOutputListener listener,
105         IProgressMonitor monitor) throws CVSException {
106         // send request
107
session.sendRequest(getRequestId());
108
109         // This number can be tweaked if the monitor is judged to move too
110
// quickly or too slowly. After some experimentation this is a good
111
// number for both large projects (it doesn't move so quickly as to
112
// give a false sense of speed) and smaller projects (it actually does
113
// move some rather than remaining still and then jumping to 100).
114
final int TOTAL_WORK = 300;
115         monitor.beginTask(CVSMessages.Command_receivingResponses, TOTAL_WORK);
116         monitor.subTask(CVSMessages.Command_receivingResponses);
117         int halfWay = TOTAL_WORK / 2;
118         int currentIncrement = 4;
119         int nextProgress = currentIncrement;
120         int worked = 0;
121         
122         // If the session is connected to a CVSNT server (1.11.1.1), we'll need to do some special handling for
123
// some errors. Unfortunately, CVSNT 1.11.1.1 will drop the connection after so some functionality is
124
// still effected
125
boolean isCVSNT = session.isCVSNT();
126
127         session.clearErrors();
128         for (;;) {
129             // update monitor work amount
130
if (--nextProgress <= 0) {
131                 monitor.worked(1);
132                 worked++;
133                 if (worked >= halfWay) {
134                     // we have passed the current halfway point, so double the
135
// increment and reset the halfway point.
136
currentIncrement *= 2;
137                     halfWay += (TOTAL_WORK - halfWay) / 2;
138                 }
139                 // reset the progress counter to another full increment
140
nextProgress = currentIncrement;
141             }
142             Policy.checkCanceled(monitor);
143
144             // retrieve a response line
145
String JavaDoc response = session.readLine();
146             int spacePos = response.indexOf(' ');
147             String JavaDoc argument;
148             if (spacePos != -1) {
149                 argument = response.substring(spacePos + 1);
150                 response = response.substring(0, spacePos);
151             } else argument = ""; //$NON-NLS-1$
152

153             // handle completion responses
154
if (response.equals("ok")) { //$NON-NLS-1$
155
break;
156             } else if (response.equals("error") || (isCVSNT && response.equals(""))) { //$NON-NLS-1$ //$NON-NLS-2$
157
argument = argument.trim();
158                 boolean serious = false;
159                 if (argument.length() == 0) {
160                     argument = getServerErrorMessage();
161                 } else {
162                     argument = NLS.bind(CVSMessages.Command_seriousServerError, new String JavaDoc[] { argument });
163                     if (!session.hasErrors()) {
164                         session.addError(new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, argument,session.getLocalRoot()));
165                     }
166                     serious = true;
167                 }
168                     
169                 if (!session.hasErrors()) {
170                     session.addError(new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, CVSMessages.Command_noMoreInfoAvailable,session.getLocalRoot()));
171                 }
172                 IStatus status = new MultiStatus(CVSProviderPlugin.ID, CVSStatus.SERVER_ERROR,
173                         session.getErrors(),
174                     argument, null);
175                 if (serious) {
176                     throw new CVSServerException(status);
177                 } else {
178                     // look for particularly bad errors in the accumulated statii
179
IStatus[] errors = session.getErrors();
180                     for (int i = 0; i < errors.length; i++) {
181                         IStatus s = errors[i];
182                         if (s.getCode() == CVSStatus.PROTOCOL_ERROR) {
183                             throw new CVSServerException(status);
184                         }
185                     }
186                 }
187                 return status;
188             // handle message responses
189
} else if (response.equals("MT")) { //$NON-NLS-1$
190
// Handle the MT response
191
MTHandler handler = (MTHandler) session.getResponseHandler(response);
192                 if (handler != null) {
193                     handler.handle(session, argument, monitor);
194                 } else {
195                     throw new CVSException(new org.eclipse.core.runtime.Status(IStatus.ERROR,
196                         CVSProviderPlugin.ID, TeamException.IO_FAILED,
197                         NLS.bind(CVSMessages.Command_unsupportedResponse, new String JavaDoc[] { response, argument }), null));
198                 }
199                 // If a line is available, pass it on to the message listener
200
// and console as if it were an M response
201
if (handler.isLineAvailable()) {
202                     String JavaDoc line = handler.getLine();
203                     IStatus status = listener.messageLine(line, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor);
204                     session.addError(status); // The session ignores OK status
205
ConsoleListeners.getInstance().messageLineReceived(session, line, status);
206
207                 }
208             } else if (response.equals("M")) { //$NON-NLS-1$
209
IStatus status = listener.messageLine(argument, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor);
210                 session.addError(status); // The session ignores OK status
211
ConsoleListeners.getInstance().messageLineReceived(session, argument, status);
212             } else if (response.equals("E")) { //$NON-NLS-1$
213
IStatus status = listener.errorLine(argument, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor);
214                 session.addError(status); // The session ignores OK status
215
ConsoleListeners.getInstance().errorLineReceived(session, argument, status);
216             // handle other responses
217
} else {
218                 ResponseHandler handler = session.getResponseHandler(response);
219                 if (handler != null) {
220                     handler.handle(session, argument, monitor);
221                 } else {
222                     throw new CVSException(new org.eclipse.core.runtime.Status(IStatus.ERROR,
223                         CVSProviderPlugin.ID, TeamException.IO_FAILED,
224                         NLS.bind(CVSMessages.Command_unsupportedResponse, new String JavaDoc[] { response, argument }), null));
225                 }
226             }
227         }
228         if (!session.hasErrors()) {
229             return ICommandOutputListener.OK;
230         } else {
231             return new MultiStatus(CVSProviderPlugin.ID, IStatus.INFO,
232                 session.getErrors(),
233                 NLS.bind(CVSMessages.Command_warnings, new String JavaDoc[] { getDisplayText() }), null); //
234
}
235     }
236     
237     /*
238      * Provide the message that is used for the status that is generated when the server
239      * reports as error.
240      */

241     protected String JavaDoc getServerErrorMessage() {
242         return NLS.bind(CVSMessages.Command_serverError, new String JavaDoc[] { getDisplayText() }); //
243
}
244     protected String JavaDoc getDisplayText() {
245         return getRequestId();
246     }
247 }
248
Popular Tags