KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.InvocationHandler JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.security.InvalidKeyException JavaDoc;
25 import java.util.*;
26 import javax.net.ssl.SSLKeyException;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.modules.subversion.Subversion;
29 import org.netbeans.modules.subversion.config.SvnConfigFiles;
30 import org.openide.ErrorManager;
31 import org.openide.util.Cancellable;
32 import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
33 import org.tigris.subversion.svnclientadapter.SVNClientException;
34
35 /**
36  *
37  *
38  * @author Tomas Stupka
39  */

40 public class SvnClientInvocationHandler implements InvocationHandler JavaDoc {
41     
42     private static Set<String JavaDoc> remoteMethods = new HashSet<String JavaDoc>();
43     static {
44         remoteMethods.add("checkout"); // NOI18N
45
remoteMethods.add("commit"); // NOI18N
46
remoteMethods.add("commitAcrossWC"); // NOI18N
47
remoteMethods.add("getList"); // NOI18N
48
remoteMethods.add("getDirEntry"); // NOI18N
49
remoteMethods.add("copy"); // NOI18N
50
remoteMethods.add("remove"); // NOI18N
51
remoteMethods.add("doExport"); // NOI18N
52
remoteMethods.add("doImport"); // NOI18N
53
remoteMethods.add("mkdir"); // NOI18N
54
remoteMethods.add("move"); // NOI18N
55
remoteMethods.add("update"); // NOI18N
56
remoteMethods.add("getLogMessages"); // NOI18N
57
remoteMethods.add("getContent"); // NOI18N
58
remoteMethods.add("setRevProperty"); // NOI18N
59
remoteMethods.add("diff"); // NOI18N
60
remoteMethods.add("annotate"); // NOI18N
61
remoteMethods.add("getInfo"); // NOI18N
62
remoteMethods.add("switchToUrl"); // NOI18N
63
remoteMethods.add("merge"); // NOI18N
64
remoteMethods.add("lock"); // NOI18N
65
remoteMethods.add("unlock"); // NOI18N
66
}
67     
68     private static Object JavaDoc semaphor = new Object JavaDoc();
69
70     private final ISVNClientAdapter adapter;
71     private final SvnClientDescriptor desc;
72     private Cancellable cancellable;
73     private SvnProgressSupport support;
74     private final int handledExceptions;
75     
76    /**
77      *
78      */

79     public SvnClientInvocationHandler (ISVNClientAdapter adapter, SvnClientDescriptor desc, int handledExceptions) {
80         
81         assert adapter != null;
82         assert desc != null;
83         
84         this.adapter = adapter;
85         this.desc = desc;
86         this.handledExceptions = handledExceptions;
87     }
88
89     public SvnClientInvocationHandler (ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions) {
90         
91         assert adapter != null;
92         assert desc != null;
93         
94         this.adapter = adapter;
95         this.desc = desc;
96         this.support = support;
97         this.handledExceptions = handledExceptions;
98         this.cancellable = new Cancellable() {
99             public boolean cancel() {
100                 try {
101                     SvnClientInvocationHandler.this.adapter.cancelOperation();
102                 } catch (SVNClientException ex) {
103                     ErrorManager.getDefault().notify(ex);
104                     return false;
105                 }
106                 return true;
107             }
108         };
109     }
110
111     /**
112      * @see InvocationHandler#invoke(Object proxy, Method method, Object[] args)
113      */

114     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
115         
116         String JavaDoc methodName = method.getName();
117         assert noRemoteCallinAWT(methodName, args) : "noRemoteCallinAWT(): " + methodName; // NOI18N
118

119         try {
120             Object JavaDoc ret = null;
121             if(SwingUtilities.isEventDispatchThread()) {
122                 ret = invokeMethod(method, args);
123             } else {
124                 synchronized (semaphor) {
125                     ret = invokeMethod(method, args);
126                 }
127             }
128             Subversion.getInstance().getStatusCache().refreshDirtyFileSystems();
129             return ret;
130         } catch (Exception JavaDoc e) {
131             try {
132                 if(handleException((SvnClient) proxy, e) ) {
133                     return invoke(proxy, method, args);
134                 } else {
135                     // some action canceled by user message
136
throw new SVNClientException(ExceptionHandler.ACTION_CANCELED_BY_USER);
137                 }
138             } catch (InvocationTargetException JavaDoc ite) {
139                 Throwable JavaDoc t = ite.getTargetException();
140                 if(t instanceof SVNClientException) {
141                     throw t;
142                 }
143                 throw ite;
144             } catch (SSLKeyException ex) {
145                 if(ex.getCause() instanceof InvalidKeyException JavaDoc) {
146                     InvalidKeyException JavaDoc ike = (InvalidKeyException JavaDoc) ex.getCause();
147                     if(ike.getMessage().toLowerCase().equals("illegal key size or default parameters")) { // NOI18N
148
ExceptionHandler.handleInvalidKeyException(ike);
149                     }
150                     return null;
151                 }
152                 throw ex;
153             }
154         }
155     }
156     
157     protected Object JavaDoc invokeMethod(Method JavaDoc proxyMethod, Object JavaDoc[] args)
158     throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
159     {
160         return handle(proxyMethod, args);
161     }
162
163     protected Object JavaDoc handle(final Method JavaDoc proxyMethod, final Object JavaDoc[] args)
164     throws SecurityException JavaDoc, InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, IllegalArgumentException JavaDoc
165     {
166         Object JavaDoc ret;
167
168         Class JavaDoc[] parameters = proxyMethod.getParameterTypes();
169         Class JavaDoc declaringClass = proxyMethod.getDeclaringClass();
170
171         if( ISVNClientAdapter.class.isAssignableFrom(declaringClass) ) {
172             // Cliet Adapter
173
if(support != null) {
174                 support.setCancellableDelegate(cancellable);
175             }
176             if (remoteMethods.contains(proxyMethod.getName())) {
177                 // save the proxy settings into the svn servers file
178
SvnConfigFiles.getInstance().setProxy(desc.getSvnUrl().toString());
179             }
180             ret = adapter.getClass().getMethod(proxyMethod.getName(), parameters).invoke(adapter, args);
181             if(support != null) {
182                 support.setCancellableDelegate(null);
183             }
184         } else if( Cancellable.class.isAssignableFrom(declaringClass) ) {
185             // Cancellable
186
ret = cancellable.getClass().getMethod(proxyMethod.getName(), parameters).invoke(cancellable, args);
187         } else if( SvnClientDescriptor.class.isAssignableFrom(declaringClass) ) {
188             // Client Descriptor
189
if(desc != null) {
190                 ret = desc.getClass().getMethod(proxyMethod.getName(), parameters).invoke(desc, args);
191             } else {
192                 // when there is no descriptor, then why has the method been called
193
throw new NoSuchMethodException JavaDoc(proxyMethod.getName());
194             }
195         } else {
196             // try to take care for hashCode, equals & co. -> fallback to clientadapter
197
ret = adapter.getClass().getMethod(proxyMethod.getName(), parameters).invoke(adapter, args);
198         }
199         
200         return ret;
201     }
202
203     private boolean handleException(SvnClient client, Throwable JavaDoc t) throws Throwable JavaDoc {
204         SVNClientException svnException = null;
205         if( t instanceof InvocationTargetException JavaDoc ) {
206             t = ((InvocationTargetException JavaDoc) t).getCause();
207         }
208         if( !(t instanceof SVNClientException) ) {
209             throw t;
210         }
211
212         SvnClientExceptionHandler eh = new SvnClientExceptionHandler((SVNClientException) t, adapter, client, handledExceptions);
213         return eh.handleException();
214     }
215     
216    /**
217      * @return false for methods that perform calls over network
218      */

219     protected boolean noRemoteCallinAWT(String JavaDoc methodName, Object JavaDoc[] args) {
220         if(!SwingUtilities.isEventDispatchThread()) {
221             return true;
222         }
223
224         if (remoteMethods.contains(methodName)) {
225             return false;
226         }
227         return true;
228     }
229 }
230
231
Popular Tags