2323import java .io .InputStream ;
2424
2525import org .tigris .subversion .svnclientadapter .ISVNClientAdapter ;
26+ import org .tigris .subversion .svnclientadapter .ISVNDirEntry ;
27+ import org .tigris .subversion .svnclientadapter .ISVNLogMessage ;
2628import org .tigris .subversion .svnclientadapter .ISVNNotifyListener ;
2729import org .tigris .subversion .svnclientadapter .SVNClientAdapterFactory ;
2830import org .tigris .subversion .svnclientadapter .SVNClientException ;
2931import org .tigris .subversion .svnclientadapter .SVNNodeKind ;
3032import org .tigris .subversion .svnclientadapter .SVNRevision ;
3133import org .tigris .subversion .svnclientadapter .SVNUrl ;
32- import org .tigris .subversion .svnclientadapter .commandline .CmdLineClientAdapterFactory ;
33- import org .tigris .subversion .svnclientadapter .javahl .JhlClientAdapterFactory ;
34+ import org .tigris .subversion .svnclientadapter .svnkit .SvnKitClientAdapterFactory ;;;
3435
3536/**
3637 * A very simple sample
37- * see svnant task for more samples and unit tests
3838 *
39- * @author Cédric Chabanois (cchabanois at no-log.org)
4039 */
4140public class Sample {
4241
@@ -51,6 +50,9 @@ public void logMessage(String message) {
5150
5251 public void logCommandLine (String message ) {
5352 // the command line used
53+ // Note that the notification handler suppresses these for
54+ // many of the simple SVN commands that might be run a lot
55+ // in order to generate less noise.
5456 System .out .println (message );
5557 }
5658
@@ -74,70 +76,109 @@ public void onNotify(File path, SVNNodeKind nodeKind) {
7476 // nodeKind is SVNNodeKind.FILE or SVNNodeKind.DIR
7577
7678 // this is the function we use in subclipse to know which files need to be refreshed
79+ // So commands like checkout/update/commit will generate these messages but
80+ // command like ls/log/status/diff will not
7781
7882 System .out .println ("Status of " +path .toString ()+" has changed" );
7983 }
8084 }
8185
82- public void setup () {
83- try {
84- JhlClientAdapterFactory .setup ();
85- } catch (SVNClientException e ) {
86- // can't register this factory
87- }
88- try {
89- CmdLineClientAdapterFactory .setup ();
90- } catch (SVNClientException e1 ) {
91- // can't register this factory
92- }
93-
86+ public void setup () throws SVNClientException {
87+ // You could register JavaHL or CommandLine
88+ // or all of them and then choose which one
89+ // to use later. For ease of demo just
90+ // using the SVNKit pure-Java adapter
91+
92+ SvnKitClientAdapterFactory .setup ();
9493 }
9594
9695 public void run () {
9796 // register the factories
98- setup ();
97+ try {
98+ setup ();
99+ } catch (SVNClientException e ) {
100+ System .out .println ("Failed to initialize adapter" );
101+ System .out .println (e .getMessage ());
102+ return ;
103+ }
99104
100- // first create the SVNClient from factory
101- // SVNClientAdapterFactory.JAVAHL_CLIENT to use JNI client (recommanded)
102- // SVNClientAdapterFactory.COMMANDLINE_CLIENT to use command line client
103- // You can also get the best client type interface using getBestSVNClientType
104105 ISVNClientAdapter svnClient ;
106+
105107 try {
108+ // If you registered multiple factories, this would return
109+ // the best one available - JavaHL > SVNKit > CmdLine
106110 String bestClientType = SVNClientAdapterFactory .getPreferredSVNClientType ();
107- System .out .println ("Using " +bestClientType +" factory" );
108- svnClient = SVNClientAdapterFactory .createSVNClient (bestClientType );
111+ System .out .println ("Using " + bestClientType +" factory" );
112+
113+ // This code uses the returned value, but you could just use
114+ // a String constant here too
115+ svnClient = SVNClientAdapterFactory .createSVNClient (bestClientType );
109116 } catch (SVNClientException e ) {
110117 System .out .println (e .getMessage ());
111118 return ;
112119 }
113120
114121
115- // set username and password if necessary
122+ // set username and password if necessary based on repository
116123 svnClient .setUsername ("guest" );
117124 svnClient .setPassword (" " );
118125
119- // add a listener if you wish
126+
127+ // add a listener if you wish. This is almost always necessary
128+ // if you are doing anything interesting as the listener receives
129+ // all the feedback from the API
130+
120131 NotifyListener listener = new Sample .NotifyListener ();
121132 svnClient .addNotifyListener (listener );
122133
123134 try {
124135 // use the svn commands
125- InputStream is = svnClient .getContent (new SVNUrl ("http://subclipse.tigris.org/svn/subclipse/trunk/svnClientAdapter/readme.txt" ),SVNRevision .HEAD );
136+ String SVNROOT = "http://svn.apache.org/repos/asf/subversion/trunk" ;
137+ System .out .println ("Getting list of files in: " + SVNROOT );
138+ boolean RECURSE = false ;
139+
140+ // This does equivalent of svn ls command.
141+ ISVNDirEntry [] list = svnClient .getList (new SVNUrl (SVNROOT ), SVNRevision .HEAD , RECURSE );
142+ for (int i = 0 ; i < list .length ; i ++) {
143+ System .out .println (list [i ].getPath ());
144+ }
145+
146+ String SVNFILE = SVNROOT + "/COMMITTERS" ;
147+ System .out .println ("Retrieving file: " + SVNFILE );
148+
149+ // This does equivalent of svn cat command to read the file from repository
150+ InputStream is = svnClient .getContent (new SVNUrl (SVNFILE ),SVNRevision .HEAD );
126151
127152 System .out .println ("The beginning of the file is :" );
128153 byte [] bytes = new byte [100 ];
129154 is .read (bytes );
130155 System .out .println (new String (bytes ));
156+
157+ // This does equivalent of svn log command to fetch history
158+ System .out .println ("Fetching info for most recent commit to: " + SVNROOT );
159+ ISVNLogMessage [] logMessages = svnClient .getLogMessages (new SVNUrl (SVNROOT ), SVNRevision .HEAD ,
160+ SVNRevision .HEAD , new SVNRevision .Number (0 ), false , false , 1 , false );
161+ for (int i = 0 ; i < logMessages .length ; i ++) {
162+ System .out .println ("Revision: " + logMessages [i ].getRevision ());
163+ System .out .println ("Author: " + logMessages [i ].getAuthor ());
164+ System .out .println ("Date: " + logMessages [i ].getDate ());
165+ System .out .println ("Message: " + logMessages [i ].getMessage ());
166+ }
167+
131168 } catch (IOException e ) {
132- System .out .println ("An exception occured while getting remote file :" + e .getMessage ());
169+ System .out .println (e .getMessage ());
133170 } catch (SVNClientException e ) {
134- System .out .println ("An exception occured while getting remote file :" +e .getMessage ());
171+ System .out .println (e .getMessage ());
172+ } finally {
173+ // You should dispose svnClient when done with it to free native handles
174+ svnClient .dispose ();
135175 }
136176 }
137177
138178
139179 public static void main (String [] args ) {
140180 Sample sample = new Sample ();
141181 sample .run ();
182+ System .exit (0 );
142183 }
143184}
0 commit comments