1 19 package org.netbeans.modules.subversion.client; 20 21 import java.security.InvalidKeyException ; 22 import javax.swing.JButton ; 23 import org.openide.DialogDisplayer; 24 import org.openide.ErrorManager; 25 import org.openide.NotifyDescriptor; 26 import org.openide.util.NbBundle; 27 import org.tigris.subversion.svnclientadapter.SVNClientException; 28 29 33 public class ExceptionHandler { 34 35 public final static int EX_UNKNOWN = 0; 36 public final static int EX_ACTION_CANCELED_BY_USER = 2; 37 public final static int EX_AUTHENTICATION = 4; 38 public final static int EX_NO_CERTIFICATE = 8; 39 public final static int EX_WRONG_URL = 16; 40 public final static int EX_NO_HOST_CONNECTION = 32; 41 public final static int EX_UNVERSIONED_RESOURCE = 64; 42 public final static int EX_WRONG_URL_IN_REVISION = 128; 43 public final static int EX_URL_NON_EXISTENT = 256; 44 public final static int EX_HTTP_405 = 512; 45 public final static int EX_IS_ALREADY_WC = 1024; 46 public final static int EX_CLOSED_CONNECTION = 2048; 47 public final static int EX_COMMIT_FAILED = 4096; 48 public final static int EX_FILE_ALREADY_EXISTS = 8192; 49 public final static int EX_IS_OUT_OF_DATE = 16384; 50 51 52 public final static int EX_HANDLED_EXCEPTIONS = EX_AUTHENTICATION | EX_NO_CERTIFICATE | EX_NO_HOST_CONNECTION; 53 public final static int EX_DEFAULT_HANDLED_EXCEPTIONS = EX_HANDLED_EXCEPTIONS; 54 55 private final SVNClientException exception; 56 private final int exceptionMask; 57 58 static final String ACTION_CANCELED_BY_USER = org.openide.util.NbBundle.getMessage(ExceptionHandler.class, "MSG_ActionCanceledByUser"); 59 60 public ExceptionHandler(SVNClientException exception) { 61 this.exception = exception; 62 exceptionMask = getMask(exception.getMessage()); 63 } 64 65 66 protected int getExceptionMask() { 67 return exceptionMask; 68 } 69 70 protected SVNClientException getException() { 71 return exception; 72 } 73 74 private static int getMask(String msg) { 75 if(msg == null || msg.trim().equals("")) { 76 return EX_UNKNOWN; 77 } 78 msg = msg.toLowerCase(); 79 if(isAuthentication(msg)) { 80 return EX_AUTHENTICATION; 81 } else if (isCancelledAction(msg)) { 82 return EX_ACTION_CANCELED_BY_USER; 83 } else if (isNoCertificate(msg)) { 84 return EX_NO_CERTIFICATE; 85 } else if (isWrongUrl(msg)) { 86 return EX_WRONG_URL; 87 } else if (isNoHostConnection(msg)) { 88 return EX_NO_HOST_CONNECTION; 89 } else if(isUnversionedResource(msg)) { 90 return EX_UNVERSIONED_RESOURCE; 91 } else if(isWrongURLInRevision(msg)) { 92 return EX_WRONG_URL_IN_REVISION; 93 } else if(isHTTP405(msg)) { 94 return EX_HTTP_405; 95 } else if(isAlreadyAWorkingCopy(msg)) { 96 return EX_IS_ALREADY_WC; 97 } else if(isClosedConnection(msg)) { 98 return EX_CLOSED_CONNECTION; 99 } else if(isCommitFailed(msg)) { 100 return EX_COMMIT_FAILED; 101 } 102 return EX_UNKNOWN; 103 } 104 105 private static boolean isCancelledAction(String msg) { 106 return msg.equals(ACTION_CANCELED_BY_USER); 107 } 108 109 private static boolean isAuthentication(String msg) { 110 return msg.indexOf("authentication error from server: username not found") > - 1 || msg.indexOf("authorization failed") > - 1 || msg.indexOf("authentication error from server: password incorrect") > -1 || msg.indexOf("can't get password") > - 1; } 116 117 private static boolean isNoCertificate(String msg) { 118 return msg.indexOf("server certificate verification failed") > -1; } 120 121 public static boolean isWrongUrl(String msg) { 122 msg = msg.toLowerCase(); 123 return msg.indexOf("(not a valid url)") > - 1; } 125 126 private static boolean isNoHostConnection(String msg) { 127 return msg.indexOf("host not found") > -1 || msg.indexOf("could not connect to server") > -1 || msg.indexOf("could not resolve hostname") > -1; } 131 132 public static boolean isUnversionedResource(String msg) { 133 msg = msg.toLowerCase(); 134 return msg.indexOf("(not a versioned resource)") > -1 || msg.indexOf("is not a working copy") > -1; } 137 138 public static boolean isWrongURLInRevision(String msg) { 139 msg = msg.toLowerCase(); 140 if (msg.indexOf("no such revision") > -1 ) { return true; 142 } 143 int idx = msg.indexOf("unable to find repository location for"); if(idx > -1 && msg.indexOf("in revision", idx + 23) > -1) { return true; 146 } 147 idx = msg.indexOf("url"); return idx > -1 && msg.indexOf("non-existent in that revision", idx + 3) > -1; } 150 151 private static boolean isHTTP405(String msg) { 152 return msg.indexOf("405") > -1; } 154 155 private static boolean isAlreadyAWorkingCopy(String msg) { 156 return msg.indexOf("is already a working copy for a different url") > -1; } 158 159 private static boolean isClosedConnection(String msg) { 160 return msg.indexOf("could not read status line: an existing connection was forcibly closed by the remote host.") > -1; } 162 163 private static boolean isCommitFailed(String msg) { 164 return msg.indexOf("commit failed (details follow)") > -1; } 166 167 public static boolean isFileAlreadyExists(String msg) { 168 msg = msg.toLowerCase(); 169 return msg.indexOf("file already exists") > -1 || (msg.indexOf("mkcol") > -1 && isHTTP405(msg)); } 172 173 private static boolean isOutOfDate(String msg) { 174 return msg.indexOf("out of date") > -1; } 176 177 178 public void notifyException() { 179 notifyException(true); 180 } 181 182 void notifyException(boolean annotate) { 183 if(isCancelledAction(exception.getMessage())) { 184 cancelledAction(); 185 return; 186 } 187 if(annotate) { 188 String msg = getCustomizedMessage(exception); 189 annotate(exception, msg); 190 } else { 191 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception); 192 } 193 } 194 195 public static String getCustomizedMessage(SVNClientException exception) { 196 String msg = null; 197 if (isHTTP405(exception.getMessage())) { 198 msg = exception.getMessage() + "\n\n" + NbBundle.getMessage(ExceptionHandler.class, "MSG_Error405"); } else if(isOutOfDate(exception.getMessage())) { 201 msg = exception.getMessage() + "\n\n" + org.openide.util.NbBundle.getMessage(SvnClientExceptionHandler.class, "MSG_Error_OutOfDate") + "\n"; 203 } 204 return msg; 205 } 206 207 public static String parseExceptionMessage(SVNClientException exception) { 208 String msg = exception.getMessage(); 209 int idx = msg.lastIndexOf("svn: "); if(idx > -1) { 211 msg = msg.substring(idx); 212 } 213 return msg; 214 } 215 216 private static void annotate(Exception exception, String msg) { 217 if(msg == null) { 218 msg = exception.getMessage(); 219 } 220 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception); 221 222 CommandReport report = new CommandReport(NbBundle.getMessage(ExceptionHandler.class, "MSG_SubversionCommandError"), msg); 223 JButton ok = new JButton (NbBundle.getMessage(ExceptionHandler.class, "CTL_CommandReport_OK")); 224 NotifyDescriptor descriptor = new NotifyDescriptor( 225 report, 226 NbBundle.getMessage(ExceptionHandler.class, "MSG_CommandFailed_Title"), 227 NotifyDescriptor.DEFAULT_OPTION, 228 NotifyDescriptor.ERROR_MESSAGE, 229 new Object [] { ok }, 230 ok); 231 DialogDisplayer.getDefault().notify(descriptor); 232 233 } 234 235 private void cancelledAction() { 236 JButton ok = new JButton (NbBundle.getMessage(ExceptionHandler.class, "CTL_Action_OK")); NotifyDescriptor descriptor = new NotifyDescriptor( 238 ACTION_CANCELED_BY_USER, 239 NbBundle.getMessage(ExceptionHandler.class, "CTL_ActionCanceled_Title"), NotifyDescriptor.DEFAULT_OPTION, 241 NotifyDescriptor.WARNING_MESSAGE, 242 new Object [] { ok }, 243 ok); 244 DialogDisplayer.getDefault().notify(descriptor); 245 return; 246 } 247 248 static void handleInvalidKeyException(InvalidKeyException ike) { 249 String msg = NbBundle.getMessage(ExceptionHandler.class, "MSG_InvalidKeyException"); annotate(ike, msg); 251 } 252 253 } 254 | Popular Tags |