KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.Proxy JavaDoc;
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 /**
39  * A SvnClient factory
40 *
41  * @author Tomas Stupka
42  */

43 public class SvnClientFactory {
44
45     /** the only existing SvnClientFactory instance */
46     private static SvnClientFactory instance;
47     
48     private ClientAdapterFactory factory;
49         
50     /** Creates a new instance of SvnClientFactory */
51     private SvnClientFactory() {
52     }
53
54     /**
55      * Returns the only existing SvnClientFactory instance
56      *
57      * @return the SvnClientFactory instance
58      */

59     public static SvnClientFactory getInstance() {
60         if(instance == null) {
61             instance = new SvnClientFactory();
62         }
63         return instance;
64     }
65
66     /**
67      * Returns a SvnClient, which isn't configured in any way.
68      * Knows no username, password, has no SvnProgressSupport<br/>
69      * Such an instance isn't supposed to work properly when calling remote svn commands.
70      *
71      * @return the SvnClient
72      */

73     public SvnClient createSvnClient() {
74         return factory.createSvnClient();
75     }
76
77     /**
78      *
79      * Returns a SvnClient which is configured with the given <tt>username</tt>,
80      * <tt>password</tt>, <tt>repositoryUrl</tt> and the <tt>support</tt>.<br>
81      * In case a http proxy was given via <tt>pd</tt> an according entry for the <tt>repositoryUrl</tt>
82      * will be created in the svn config file.
83      * The mask <tt>handledExceptions</tt> specifies which exceptions are to be handled.
84      *
85      * @param repositoryUrl
86      * @param support
87      * @param username
88      * @param password
89      * @param handledExceptions
90      *
91      * @return the configured SvnClient
92      *
93      */

94     public SvnClient createSvnClient(SVNUrl repositoryUrl, SvnProgressSupport support, String JavaDoc username, String JavaDoc password, int handledExceptions) {
95         return factory.createSvnClient(repositoryUrl, support, username, password, handledExceptions);
96     }
97     
98     /**
99      * A SVNClientAdapterFactory will be setup, according to the svnClientAdapterFactory property.<br>
100      * The CommandlineClientAdapterFactory is default as long no value is set for svnClientAdapterFactory.
101      *
102      */

103     public void setup() throws SVNClientException {
104         try {
105             String JavaDoc 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 JavaDoc t) {
117                     String JavaDoc jhlErorrs = JhlClientAdapter.getLibraryLoadErrors();
118                     // something went wrong - fallback on the commandline
119
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 if(factoryType.equals(JavaSvnClientAdapterFactory.JAVASVN_CLIENT)) {
125                 try {
126                     setupJavaSvn();
127                 } catch (Throwable t) {
128                     // something went wrong - fallback on the commandline
129                     ErrorManager.getDefault().notify(ErrorManager.WARNING, t);
130                     ErrorManager.getDefault().log(ErrorManager.WARNING, "Could not setup javasvn. Falling back on the commandline client!");
131                     setupCommandline();
132                 }
133             } */
else {
134                 throw new SVNClientException("Unknown factory: " + factoryType);
135             }
136         } catch (Throwable JavaDoc 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     /*
162     private void setupJavaSvn () throws SVNClientException {
163         JavaSvnClientAdapterFactory.setup();
164         factory = new ClientAdapterFactory() {
165             protected ISVNClientAdapter createAdapter() {
166                 return SVNClientAdapterFactory.createSVNClient(JavaSvnClientAdapterFactory.JAVASVN_CLIENT);
167             }
168             protected SvnClientInvocationHandler getInvocationHandler(ISVNClientAdapter adapter, SvnClientDescriptor desc, SvnProgressSupport support, int handledExceptions) {
169                 return new SvnClientInvocationHandler(adapter, desc, support, handledExceptions);
170             }
171             protected ISVNPromptUserPassword createCallback(SVNUrl repositoryUrl, int handledExceptions) {
172                 return new SvnClientCallback(repositoryUrl, handledExceptions);
173             }
174         };
175         ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "svnClientAdapter running on javasvn");
176     }
177     */

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 JavaDoc 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         /**
222          *
223          * Returns a SvnClientInvocationHandler instance which is configured with the given <tt>adapter</tt>,
224          * <tt>support</tt> and a SvnClientDescriptor for <tt>repository</tt>.
225          *
226          * @param adapter
227          * @param support
228          * @param repository
229          *
230          * @return the created SvnClientInvocationHandler instance
231          *
232          */

233         public SvnClient createSvnClient(SVNUrl repositoryUrl, SvnProgressSupport support, String JavaDoc username, String JavaDoc 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 JavaDoc proxyClass = Proxy.getProxyClass(SvnClient.class.getClassLoader(), new Class JavaDoc[]{ SvnClient.class } );
250             Subversion.getInstance().cleanupFilesystem();
251             try {
252                return (SvnClient) proxyClass.getConstructor( new Class JavaDoc[] { InvocationHandler JavaDoc.class } ).newInstance( new Object JavaDoc[] { handler } );
253             } catch (Exception JavaDoc e) {
254                 org.openide.ErrorManager.getDefault().notify(e);
255             }
256             return null;
257         }
258                   
259         protected void setupAdapter(ISVNClientAdapter adapter, String JavaDoc username, String JavaDoc password, ISVNPromptUserPassword callback) {
260             
261             if(callback != null) {
262                 adapter.addPasswordCallback(callback);
263             }
264             
265             try {
266                 File JavaDoc configDir = FileUtil.normalizeFile(new File JavaDoc(SvnConfigFiles.getNBConfigPath()));
267                 adapter.setConfigDirectory(configDir);
268                 adapter.setUsername(username);
269                 adapter.setPassword(password);
270             } catch (SVNClientException ex) {
271                 ErrorManager.getDefault().notify(ex); // should not happen
272
}
273             
274         }
275     }
276     
277 }
278
Popular Tags