Skip to content

Commit a646c99

Browse files
committed
Update the sample project
1 parent 48d0622 commit a646c99

File tree

8 files changed

+169
-32
lines changed

8 files changed

+169
-32
lines changed

samples/.classpath

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>

samples/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Samples</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3+
org.eclipse.jdt.core.compiler.compliance=1.6
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

samples/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1-
# svnClientAdapter Samples
1+
# SVNClientAdapter Sample
2+
3+
This example project shows how to use the SVNClientAdapter.
4+
It is a Maven project and demonstrates the Maven dependencies
5+
to add to your own project. Without Maven, you could just download
6+
the JAR files for the Maven artifacts and add them to your classpath.
7+
8+
This example use the SVNKit pure-Java adapter for simplicity
9+
but it would be easy to adjust the code to use a different adapter.
10+
11+
To build run this command:
12+
13+
```
14+
mvn install
15+
```
16+
17+
To run the code just execute this command:
18+
19+
```
20+
mvn exec:java -Dexec.mainClass="org.tigris.subversion.svnclientadapter.samples.Sample"
21+
```
22+
23+
224

3-
TODO: Mavenize this and update samples

samples/pom.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,33 @@
66
<version>1.0.0-SNAPSHOT</version>
77
<name>Client Adapter Sample</name>
88
<url>https://github.com/subclipse/svnclientadapter</url>
9-
<packaging>pom</packaging>
109

1110
<properties>
1211
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1312
</properties>
13+
14+
<repositories>
15+
<repository>
16+
<id>bintray-subclipse-maven</id>
17+
<name>bintray</name>
18+
<url>http://dl.bintray.com/subclipse/maven</url>
19+
</repository>
20+
</repositories>
1421

1522
<dependencies>
1623
<dependency>
1724
<groupId>org.tigris.svnclientadapter</groupId>
1825
<artifactId>adapter-base</artifactId>
1926
<version>1.9.4</version>
27+
<scope>runtime</scope>
2028
</dependency>
2129
<dependency>
2230
<groupId>org.tigris.svnclientadapter</groupId>
2331
<artifactId>adapter-svnkit</artifactId>
2432
<version>1.9.4</version>
33+
<scope>runtime</scope>
2534
</dependency>
26-
</dependencies>
35+
</dependencies>
2736

2837
<build>
2938
<plugins>
@@ -36,6 +45,11 @@
3645
<target>1.6</target>
3746
</configuration>
3847
</plugin>
48+
<plugin>
49+
<groupId>org.codehaus.mojo</groupId>
50+
<artifactId>exec-maven-plugin</artifactId>
51+
<version>1.4.0</version>
52+
</plugin>
3953
</plugins>
4054
</build>
4155

samples/src/main/java/org/tigris/subversion/svnclientadapter/samples/Sample.java

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@
2323
import java.io.InputStream;
2424

2525
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
26+
import org.tigris.subversion.svnclientadapter.ISVNDirEntry;
27+
import org.tigris.subversion.svnclientadapter.ISVNLogMessage;
2628
import org.tigris.subversion.svnclientadapter.ISVNNotifyListener;
2729
import org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory;
2830
import org.tigris.subversion.svnclientadapter.SVNClientException;
2931
import org.tigris.subversion.svnclientadapter.SVNNodeKind;
3032
import org.tigris.subversion.svnclientadapter.SVNRevision;
3133
import 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
*/
4140
public 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

Comments
 (0)