KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > client > ExceptionHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.client;
20
21 import java.security.InvalidKeyException JavaDoc;
22 import javax.swing.JButton JavaDoc;
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 /**
30  *
31  * @author Tomas Stupka
32  */

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 JavaDoc 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 JavaDoc 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 JavaDoc msg) {
106         return msg.equals(ACTION_CANCELED_BY_USER);
107     }
108     
109     private static boolean isAuthentication(String JavaDoc msg) {
110         return msg.indexOf("authentication error from server: username not found") > - 1 || // NOI18N
111
msg.indexOf("authorization failed") > - 1 || // NOI18N
112
msg.indexOf("authentication error from server: password incorrect") > -1 || // NOI18N
113
msg.indexOf("can't get password") > - 1; // NOI18N
114
// XXX we also have to check for authentication messages from proxy
115
}
116
117     private static boolean isNoCertificate(String JavaDoc msg) {
118         return msg.indexOf("server certificate verification failed") > -1; // NOI18N
119
}
120     
121     public static boolean isWrongUrl(String JavaDoc msg) {
122         msg = msg.toLowerCase();
123         return msg.indexOf("(not a valid url)") > - 1; // NOI18N
124
}
125
126     private static boolean isNoHostConnection(String JavaDoc msg) {
127         return msg.indexOf("host not found") > -1 || // NOI18N
128
msg.indexOf("could not connect to server") > -1 || // NOI18N
129
msg.indexOf("could not resolve hostname") > -1; // NOI18N
130
}
131     
132     public static boolean isUnversionedResource(String JavaDoc msg) {
133         msg = msg.toLowerCase();
134         return msg.indexOf("(not a versioned resource)") > -1 || // NOI18N
135
msg.indexOf("is not a working copy") > -1; // NOI18N
136
}
137     
138     public static boolean isWrongURLInRevision(String JavaDoc msg) {
139         msg = msg.toLowerCase();
140         if (msg.indexOf("no such revision") > -1 ) { // NOI18N
141
return true;
142         }
143         int idx = msg.indexOf("unable to find repository location for"); // NOI18N
144
if(idx > -1 && msg.indexOf("in revision", idx + 23) > -1) { // NOI18N
145
return true;
146         }
147         idx = msg.indexOf("url"); // NOI18N
148
return idx > -1 && msg.indexOf("non-existent in that revision", idx + 3) > -1; // NOI18N
149
}
150
151     private static boolean isHTTP405(String JavaDoc msg) {
152         return msg.indexOf("405") > -1; // NOI18N
153
}
154     
155     private static boolean isAlreadyAWorkingCopy(String JavaDoc msg) {
156         return msg.indexOf("is already a working copy for a different url") > -1; // NOI18N
157
}
158
159     private static boolean isClosedConnection(String JavaDoc msg) {
160         return msg.indexOf("could not read status line: an existing connection was forcibly closed by the remote host.") > -1; // NOI18N
161
}
162
163     private static boolean isCommitFailed(String JavaDoc msg) {
164         return msg.indexOf("commit failed (details follow)") > -1; // NOI18N
165
}
166
167     public static boolean isFileAlreadyExists(String JavaDoc msg) {
168         msg = msg.toLowerCase();
169         return msg.indexOf("file already exists") > -1 || // NOI18N
170
(msg.indexOf("mkcol") > -1 && isHTTP405(msg)); // NOI18N
171
}
172     
173     private static boolean isOutOfDate(String JavaDoc msg) {
174         return msg.indexOf("out of date") > -1; // NOI18N
175
}
176     
177     /** Analyzes exception and notifies user. */
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 JavaDoc msg = getCustomizedMessage(exception);
189             annotate(exception, msg);
190         } else {
191             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
192         }
193     }
194     
195     public static String JavaDoc getCustomizedMessage(SVNClientException exception) {
196         String JavaDoc msg = null;
197         if (isHTTP405(exception.getMessage())) {
198             msg = exception.getMessage() + "\n\n" + // NOI18N
199
NbBundle.getMessage(ExceptionHandler.class, "MSG_Error405"); // NOI18N
200
} else if(isOutOfDate(exception.getMessage())) {
201             msg = exception.getMessage() + "\n\n" + org.openide.util.NbBundle.getMessage(SvnClientExceptionHandler.class, "MSG_Error_OutOfDate") + "\n"; // NOI18N
202

203         }
204         return msg;
205     }
206
207     public static String JavaDoc parseExceptionMessage(SVNClientException exception) {
208         String JavaDoc msg = exception.getMessage();
209         int idx = msg.lastIndexOf("svn: "); // NOI18N
210
if(idx > -1) {
211             msg = msg.substring(idx);
212         }
213         return msg;
214     }
215
216     private static void annotate(Exception JavaDoc exception, String JavaDoc 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 JavaDoc ok = new JButton JavaDoc(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 JavaDoc [] { ok },
230                 ok);
231         DialogDisplayer.getDefault().notify(descriptor);
232         
233     }
234
235     private void cancelledAction() {
236         JButton JavaDoc ok = new JButton JavaDoc(NbBundle.getMessage(ExceptionHandler.class, "CTL_Action_OK")); // NOI18N
237
NotifyDescriptor descriptor = new NotifyDescriptor(
238                 ACTION_CANCELED_BY_USER,
239                 NbBundle.getMessage(ExceptionHandler.class, "CTL_ActionCanceled_Title"), // NOI18N
240
NotifyDescriptor.DEFAULT_OPTION,
241                 NotifyDescriptor.WARNING_MESSAGE,
242                 new Object JavaDoc [] { ok },
243                 ok);
244         DialogDisplayer.getDefault().notify(descriptor);
245         return;
246     }
247
248     static void handleInvalidKeyException(InvalidKeyException JavaDoc ike) {
249         String JavaDoc msg = NbBundle.getMessage(ExceptionHandler.class, "MSG_InvalidKeyException"); // NOI18N
250
annotate(ike, msg);
251     }
252     
253 }
254
Popular Tags