1 19 package org.netbeans.modules.subversion.client; 20 21 import java.io.File ; 22 import java.lang.reflect.InvocationHandler ; 23 import java.lang.reflect.Proxy ; 24 import org.netbeans.modules.subversion.SvnModuleConfig; 25 import org.netbeans.modules.subversion.config.SvnConfigFiles; 26 import org.netbeans.modules.subversion.Subversion; 27 import org.openide.ErrorManager; 28 import org.openide.filesystems.FileUtil; 29 import org.tigris.subversion.svnclientadapter.ISVNClientAdapter; 30 import org.tigris.subversion.svnclientadapter.ISVNPromptUserPassword; 31 import org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory; 32 import org.tigris.subversion.svnclientadapter.SVNClientException; 33 import org.tigris.subversion.svnclientadapter.SVNUrl; 34 import org.tigris.subversion.svnclientadapter.commandline.CmdLineClientAdapterFactory; 35 import org.tigris.subversion.svnclientadapter.javahl.JhlClientAdapterFactory; 36 import org.tigris.subversion.svnclientadapter.javahl.JhlClientAdapter; 37 38 43 public class SvnClientFactory { 44 45 46 private static SvnClientFactory instance; 47 48 private ClientAdapterFactory factory; 49 50 51 private SvnClientFactory() { 52 } 53 54 59 public static SvnClientFactory getInstance() { 60 if(instance == null) { 61 instance = new SvnClientFactory(); 62 } 63 return instance; 64 } 65 66 73 public SvnClient createSvnClient() { 74 return factory.createSvnClient(); 75 } 76 77 94 public SvnClient createSvnClient(SVNUrl repositoryUrl, SvnProgressSupport support, String username, String password, int handledExceptions) { 95 return factory.createSvnClient(repositoryUrl, support, username, password, handledExceptions); 96 } 97 98 103 public void setup() throws SVNClientException { 104 try { 105 String factoryType = System.getProperty("svnClientAdapterFactory"); 106 107 if(factoryType == null || 108 factoryType.trim().equals("") || 109 factoryType.equals(CmdLineClientAdapterFactory.COMMANDLINE_CLIENT)) 110 { 111 setupCommandline(); 112 } 113 else if(factoryType.equals(JhlClientAdapterFactory.JAVAHL_CLIENT)) { 114 try { 115 setupJavaHl(); 116 } catch (Throwable t) { 117 String jhlErorrs = JhlClientAdapter.getLibraryLoadErrors(); 118 ErrorManager.getDefault().notify(ErrorManager.WARNING, t); 120 ErrorManager.getDefault().log(ErrorManager.WARNING, jhlErorrs); 121 ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not setup JavaHl. Falling back on the commandline client!"); 122 setupCommandline(); 123 } 124 } else { 134 throw new SVNClientException("Unknown factory: " + factoryType); 135 } 136 } catch (Throwable t) { 137 setupUnsupported(); 138 if(t instanceof SVNClientException) { 139 throw (SVNClientException) t; 140 } 141 throw new SVNClientException(t); 142 } 143 } 144 145 private void setupJavaHl () throws SVNClientException { 146 JhlClientAdapterFactory.setup(); 147 factory = new ClientAdapterFactory() { 148 protected ISVNClientAdapter createAdapter() { 149 return SVNClientAdapterFactory.createSVNClient(JhlClientAdapterFactory.JAVAHL_CLIENT); 150 } 151 protected SvnClientInvocationHandler getInvocationHandler(ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions) { 152 return new SvnClientInvocationHandler(adapter, desc, support, handledExceptions); 153 } 154 protected ISVNPromptUserPassword createCallback(SVNUrl repositoryUrl, int handledExceptions) { 155 return new SvnClientCallback(repositoryUrl, handledExceptions); 156 } 157 }; 158 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "svnClientAdapter running on javahl"); 159 } 160 161 178 179 private void setupUnsupported () throws SVNClientException { 180 factory = new ClientAdapterFactory() { 181 protected ISVNClientAdapter createAdapter() { 182 return new UnsupportedSvnClientAdapter(); 183 } 184 protected SvnClientInvocationHandler getInvocationHandler(ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions) { 185 return new SvnCmdLineClientInvocationHandler(adapter, desc, support, handledExceptions); 186 } 187 protected ISVNPromptUserPassword createCallback(SVNUrl repositoryUrl, int handledExceptions) { 188 return null; 189 } 190 }; 191 } 192 193 private void setupCommandline () throws SVNClientException { 194 String subversionPath = SvnModuleConfig.getDefault().getExecutableBinaryPath(); 195 CmdLineClientAdapterFactory.setup(subversionPath); 196 factory = new ClientAdapterFactory() { 197 protected ISVNClientAdapter createAdapter() { 198 return SVNClientAdapterFactory.createSVNClient(CmdLineClientAdapterFactory.COMMANDLINE_CLIENT); 199 } 200 protected SvnClientInvocationHandler getInvocationHandler(ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions) { 201 return new SvnCmdLineClientInvocationHandler(adapter, desc, support, handledExceptions); 202 } 203 protected ISVNPromptUserPassword createCallback(SVNUrl repositoryUrl, int handledExceptions) { 204 return null; 205 } 206 }; 207 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "svnClientAdapter running on commandline client"); 208 } 209 210 private abstract class ClientAdapterFactory { 211 212 abstract protected ISVNClientAdapter createAdapter(); 213 abstract protected SvnClientInvocationHandler getInvocationHandler(ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions); 214 abstract protected ISVNPromptUserPassword createCallback(SVNUrl repositoryUrl, int handledExceptions); 215 216 SvnClient createSvnClient() { 217 SvnClientInvocationHandler handler = getInvocationHandler(createAdapter(), createDescriptor(null), null, -1); 218 return createSvnClient(handler); 219 } 220 221 233 public SvnClient createSvnClient(SVNUrl repositoryUrl, SvnProgressSupport support, String username, String password, int handledExceptions) { 234 ISVNClientAdapter adapter = createAdapter(); 235 SvnClientInvocationHandler handler = getInvocationHandler(adapter, createDescriptor(repositoryUrl), support, handledExceptions); 236 setupAdapter(adapter, username, password, createCallback(repositoryUrl, handledExceptions)); 237 return createSvnClient(handler); 238 } 239 240 private SvnClientDescriptor createDescriptor(final SVNUrl repositoryUrl) { 241 return new SvnClientDescriptor() { 242 public SVNUrl getSvnUrl() { 243 return repositoryUrl; 244 } 245 }; 246 } 247 248 private SvnClient createSvnClient(SvnClientInvocationHandler handler) { 249 Class proxyClass = Proxy.getProxyClass(SvnClient.class.getClassLoader(), new Class []{ SvnClient.class } ); 250 Subversion.getInstance().cleanupFilesystem(); 251 try { 252 return (SvnClient) proxyClass.getConstructor( new Class [] { InvocationHandler .class } ).newInstance( new Object [] { handler } ); 253 } catch (Exception e) { 254 org.openide.ErrorManager.getDefault().notify(e); 255 } 256 return null; 257 } 258 259 protected void setupAdapter(ISVNClientAdapter adapter, String username, String password, ISVNPromptUserPassword callback) { 260 261 if(callback != null) { 262 adapter.addPasswordCallback(callback); 263 } 264 265 try { 266 File configDir = FileUtil.normalizeFile(new File (SvnConfigFiles.getNBConfigPath())); 267 adapter.setConfigDirectory(configDir); 268 adapter.setUsername(username); 269 adapter.setPassword(password); 270 } catch (SVNClientException ex) { 271 ErrorManager.getDefault().notify(ex); } 273 274 } 275 } 276 277 } 278 | Popular Tags |