1 11 package org.eclipse.team.internal.ccvs.core.client; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 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 32 private static final Map responseHandlers = new HashMap (); 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 69 protected static Map getReponseHandlerMap() { 70 synchronized(responseHandlers) { 71 if (responseHandlers.isEmpty()) { 72 initializeHandlerCache(); 73 } 74 Map copy = new HashMap (); 75 for (Iterator 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 86 protected Request() { } 87 88 94 protected abstract String getRequestId(); 95 96 104 protected IStatus executeRequest(Session session, ICommandOutputListener listener, 105 IProgressMonitor monitor) throws CVSException { 106 session.sendRequest(getRequestId()); 108 109 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 boolean isCVSNT = session.isCVSNT(); 126 127 session.clearErrors(); 128 for (;;) { 129 if (--nextProgress <= 0) { 131 monitor.worked(1); 132 worked++; 133 if (worked >= halfWay) { 134 currentIncrement *= 2; 137 halfWay += (TOTAL_WORK - halfWay) / 2; 138 } 139 nextProgress = currentIncrement; 141 } 142 Policy.checkCanceled(monitor); 143 144 String response = session.readLine(); 146 int spacePos = response.indexOf(' '); 147 String argument; 148 if (spacePos != -1) { 149 argument = response.substring(spacePos + 1); 150 response = response.substring(0, spacePos); 151 } else argument = ""; 153 if (response.equals("ok")) { break; 156 } else if (response.equals("error") || (isCVSNT && response.equals(""))) { 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 [] { 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 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 } else if (response.equals("MT")) { 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 [] { response, argument }), null)); 198 } 199 if (handler.isLineAvailable()) { 202 String line = handler.getLine(); 203 IStatus status = listener.messageLine(line, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor); 204 session.addError(status); ConsoleListeners.getInstance().messageLineReceived(session, line, status); 206 207 } 208 } else if (response.equals("M")) { IStatus status = listener.messageLine(argument, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor); 210 session.addError(status); ConsoleListeners.getInstance().messageLineReceived(session, argument, status); 212 } else if (response.equals("E")) { IStatus status = listener.errorLine(argument, session.getCVSRepositoryLocation(), session.getLocalRoot(), monitor); 214 session.addError(status); ConsoleListeners.getInstance().errorLineReceived(session, argument, status); 216 } 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 [] { 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 [] { getDisplayText() }), null); } 235 } 236 237 241 protected String getServerErrorMessage() { 242 return NLS.bind(CVSMessages.Command_serverError, new String [] { getDisplayText() }); } 244 protected String getDisplayText() { 245 return getRequestId(); 246 } 247 } 248 | Popular Tags |