1 19 package org.netbeans.modules.subversion.client; 20 21 import java.lang.reflect.InvocationHandler ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.security.InvalidKeyException ; 25 import java.util.*; 26 import javax.net.ssl.SSLKeyException; 27 import javax.swing.SwingUtilities ; 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 40 public class SvnClientInvocationHandler implements InvocationHandler { 41 42 private static Set<String > remoteMethods = new HashSet<String >(); 43 static { 44 remoteMethods.add("checkout"); remoteMethods.add("commit"); remoteMethods.add("commitAcrossWC"); remoteMethods.add("getList"); remoteMethods.add("getDirEntry"); remoteMethods.add("copy"); remoteMethods.add("remove"); remoteMethods.add("doExport"); remoteMethods.add("doImport"); remoteMethods.add("mkdir"); remoteMethods.add("move"); remoteMethods.add("update"); remoteMethods.add("getLogMessages"); remoteMethods.add("getContent"); remoteMethods.add("setRevProperty"); remoteMethods.add("diff"); remoteMethods.add("annotate"); remoteMethods.add("getInfo"); remoteMethods.add("switchToUrl"); remoteMethods.add("merge"); remoteMethods.add("lock"); remoteMethods.add("unlock"); } 67 68 private static Object semaphor = new Object (); 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 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 114 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 115 116 String methodName = method.getName(); 117 assert noRemoteCallinAWT(methodName, args) : "noRemoteCallinAWT(): " + methodName; 119 try { 120 Object 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 e) { 131 try { 132 if(handleException((SvnClient) proxy, e) ) { 133 return invoke(proxy, method, args); 134 } else { 135 throw new SVNClientException(ExceptionHandler.ACTION_CANCELED_BY_USER); 137 } 138 } catch (InvocationTargetException ite) { 139 Throwable 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 ) { 146 InvalidKeyException ike = (InvalidKeyException ) ex.getCause(); 147 if(ike.getMessage().toLowerCase().equals("illegal key size or default parameters")) { ExceptionHandler.handleInvalidKeyException(ike); 149 } 150 return null; 151 } 152 throw ex; 153 } 154 } 155 } 156 157 protected Object invokeMethod(Method proxyMethod, Object [] args) 158 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException 159 { 160 return handle(proxyMethod, args); 161 } 162 163 protected Object handle(final Method proxyMethod, final Object [] args) 164 throws SecurityException , InvocationTargetException , IllegalAccessException , NoSuchMethodException , IllegalArgumentException 165 { 166 Object ret; 167 168 Class [] parameters = proxyMethod.getParameterTypes(); 169 Class declaringClass = proxyMethod.getDeclaringClass(); 170 171 if( ISVNClientAdapter.class.isAssignableFrom(declaringClass) ) { 172 if(support != null) { 174 support.setCancellableDelegate(cancellable); 175 } 176 if (remoteMethods.contains(proxyMethod.getName())) { 177 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 ret = cancellable.getClass().getMethod(proxyMethod.getName(), parameters).invoke(cancellable, args); 187 } else if( SvnClientDescriptor.class.isAssignableFrom(declaringClass) ) { 188 if(desc != null) { 190 ret = desc.getClass().getMethod(proxyMethod.getName(), parameters).invoke(desc, args); 191 } else { 192 throw new NoSuchMethodException (proxyMethod.getName()); 194 } 195 } else { 196 ret = adapter.getClass().getMethod(proxyMethod.getName(), parameters).invoke(adapter, args); 198 } 199 200 return ret; 201 } 202 203 private boolean handleException(SvnClient client, Throwable t) throws Throwable { 204 SVNClientException svnException = null; 205 if( t instanceof InvocationTargetException ) { 206 t = ((InvocationTargetException ) 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 219 protected boolean noRemoteCallinAWT(String methodName, Object [] 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 |