Merge branch 'master' of https://github.com/vicmosin/tutorials into vicmosin-master
This commit is contained in:
@@ -27,3 +27,4 @@ target/
|
||||
|
||||
spring-openid/src/main/resources/application.properties
|
||||
.recommenders/
|
||||
/spring-hibernate4/nbproject/
|
||||
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>JGitSnippets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jgit-repository</id>
|
||||
<url>https://repo.eclipse.org/content/groups/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<!-- Core Library -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit.archive</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.jgit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to create a new repository
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CreateNewRepository {
|
||||
|
||||
public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
|
||||
// prepare a new folder
|
||||
File localPath = File.createTempFile("TestGitRepository", "");
|
||||
if(!localPath.delete()) {
|
||||
throw new IOException("Could not delete temporary file " + localPath);
|
||||
}
|
||||
|
||||
// create the directory
|
||||
try (Git git = Git.init().setDirectory(localPath).call()) {
|
||||
System.out.println("Having repository: " + git.getRepository().getDirectory());
|
||||
}
|
||||
|
||||
FileUtils.deleteDirectory(localPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.jgit;
|
||||
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to open an existing repository
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class OpenRepository {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// first create a test-repository, the return is including the .get directory here!
|
||||
File repoDir = createSampleGitRepo();
|
||||
|
||||
// now open the resulting repository with a FileRepositoryBuilder
|
||||
FileRepositoryBuilder builder = new FileRepositoryBuilder();
|
||||
try (Repository repository = builder.setGitDir(repoDir)
|
||||
.readEnvironment() // scan environment GIT_* variables
|
||||
.findGitDir() // scan up the file system tree
|
||||
.build()) {
|
||||
System.out.println("Having repository: " + repository.getDirectory());
|
||||
|
||||
// the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
|
||||
Ref head = repository.exactRef("refs/heads/master");
|
||||
System.out.println("Ref of refs/heads/master: " + head);
|
||||
}
|
||||
}
|
||||
|
||||
private static File createSampleGitRepo() throws IOException, GitAPIException {
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
System.out.println("Temporary repository at " + repository.getDirectory());
|
||||
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// run the add-call
|
||||
try (Git git = new Git(repository)) {
|
||||
git.add()
|
||||
.addFilepattern("testfile")
|
||||
.call();
|
||||
|
||||
|
||||
// and then commit the changes
|
||||
git.commit()
|
||||
.setMessage("Added testfile")
|
||||
.call();
|
||||
}
|
||||
|
||||
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
|
||||
|
||||
return repository.getDirectory();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
package com.baeldung.jgit.helper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
|
||||
public class Helper {
|
||||
|
||||
public static Repository openJGitRepository() throws IOException {
|
||||
FileRepositoryBuilder builder = new FileRepositoryBuilder();
|
||||
return builder
|
||||
.readEnvironment() // scan environment GIT_* variables
|
||||
.findGitDir() // scan up the file system tree
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Repository createNewRepository() throws IOException {
|
||||
// prepare a new folder
|
||||
File localPath = File.createTempFile("TestGitRepository", "");
|
||||
if(!localPath.delete()) {
|
||||
throw new IOException("Could not delete temporary file " + localPath);
|
||||
}
|
||||
|
||||
// create the directory
|
||||
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
|
||||
repository.create();
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to add a file to the index
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class AddFile {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare a new test-repository
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// run the add-call
|
||||
git.add()
|
||||
.addFilepattern("testfile")
|
||||
.call();
|
||||
|
||||
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to commit all files
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CommitAll {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare a new test-repository
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// Stage all files in the repo including new files
|
||||
git.add().addFilepattern(".").call();
|
||||
|
||||
// and then commit the changes.
|
||||
git.commit()
|
||||
.setMessage("Commit all changes including additions")
|
||||
.call();
|
||||
|
||||
try(PrintWriter writer = new PrintWriter(myfile)) {
|
||||
writer.append("Hello, world!");
|
||||
}
|
||||
|
||||
// Stage all changed files, omitting new files, and commit with one command
|
||||
git.commit()
|
||||
.setAll(true)
|
||||
.setMessage("Commit changes to all files")
|
||||
.call();
|
||||
|
||||
|
||||
System.out.println("Committed all changes to repository at " + repository.getDirectory());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to create a tag
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CreateAndDeleteTag {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare test-repository
|
||||
try (Repository repository = Helper.openJGitRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// remove the tag before creating it
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// set it on the current HEAD
|
||||
Ref tag = git.tag().setName("tag_for_testing").call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// read some other commit and set the tag on it
|
||||
ObjectId id = repository.resolve("HEAD^");
|
||||
try (RevWalk walk = new RevWalk(repository)) {
|
||||
RevCommit commit = walk.parseCommit(id);
|
||||
tag = git.tag().setObjectId(commit).setName("tag_for_testing").call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// create an annotated tag
|
||||
tag = git.tag().setName("tag_for_testing").setAnnotated(true).call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
walk.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to get the commit-ids for a file to provide log information.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Log {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
try (Repository repository = Helper.openJGitRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
Iterable<RevCommit> logs = git.log()
|
||||
.call();
|
||||
int count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall on current branch");
|
||||
|
||||
logs = git.log()
|
||||
.add(repository.resolve("remotes/origin/testbranch"))
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall on test-branch");
|
||||
|
||||
logs = git.log()
|
||||
.all()
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall in repository");
|
||||
|
||||
logs = git.log()
|
||||
// for all log.all()
|
||||
.addPath("README.md")
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits on README.md");
|
||||
|
||||
logs = git.log()
|
||||
// for all log.all()
|
||||
.addPath("pom.xml")
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits on pom.xml");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.ObjectReader;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Tests which show issues with JGit that we reported upstream.
|
||||
*/
|
||||
public class JGitBugTest {
|
||||
@Test
|
||||
public void testRevWalkDisposeClosesReader() throws IOException {
|
||||
try (Repository repo = Helper.openJGitRepository()) {
|
||||
try (ObjectReader reader = repo.newObjectReader()) {
|
||||
try (RevWalk walk = new RevWalk(reader)) {
|
||||
walk.dispose();
|
||||
|
||||
Ref head = repo.exactRef("refs/heads/master");
|
||||
System.out.println("Found head: " + head);
|
||||
|
||||
ObjectLoader loader = reader.open(head.getObjectId());
|
||||
assertNotNull(loader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PorcelainTest {
|
||||
@Test
|
||||
public void runSamples() throws Exception {
|
||||
// simply call all the samples to see any severe problems with the samples
|
||||
AddFile.main(null);
|
||||
|
||||
CommitAll.main(null);
|
||||
|
||||
CreateAndDeleteTag.main(null);
|
||||
|
||||
Log.main(null);
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,7 @@ public class Dijkstra {
|
||||
while (unsettledNodes.size() != 0) {
|
||||
Node currentNode = getLowestDistanceNode(unsettledNodes);
|
||||
unsettledNodes.remove(currentNode);
|
||||
for (Entry<Node, Integer> adjacencyPair : currentNode
|
||||
.getAdjacentNodes()
|
||||
.entrySet()) {
|
||||
for (Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) {
|
||||
Node adjacentNode = adjacencyPair.getKey();
|
||||
Integer edgeWeigh = adjacencyPair.getValue();
|
||||
|
||||
|
||||
@@ -55,29 +55,19 @@ public class DijkstraAlgorithmTest {
|
||||
for (Node node : graph.getNodes()) {
|
||||
switch (node.getName()) {
|
||||
case "B":
|
||||
assertTrue(node
|
||||
.getShortestPath()
|
||||
.equals(shortestPathForNodeB));
|
||||
assertTrue(node.getShortestPath().equals(shortestPathForNodeB));
|
||||
break;
|
||||
case "C":
|
||||
assertTrue(node
|
||||
.getShortestPath()
|
||||
.equals(shortestPathForNodeC));
|
||||
assertTrue(node.getShortestPath().equals(shortestPathForNodeC));
|
||||
break;
|
||||
case "D":
|
||||
assertTrue(node
|
||||
.getShortestPath()
|
||||
.equals(shortestPathForNodeD));
|
||||
assertTrue(node.getShortestPath().equals(shortestPathForNodeD));
|
||||
break;
|
||||
case "E":
|
||||
assertTrue(node
|
||||
.getShortestPath()
|
||||
.equals(shortestPathForNodeE));
|
||||
assertTrue(node.getShortestPath().equals(shortestPathForNodeE));
|
||||
break;
|
||||
case "F":
|
||||
assertTrue(node
|
||||
.getShortestPath()
|
||||
.equals(shortestPathForNodeF));
|
||||
assertTrue(node.getShortestPath().equals(shortestPathForNodeF));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-12
@@ -27,17 +27,12 @@ public class BuilderProcessor extends AbstractProcessor {
|
||||
|
||||
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
|
||||
|
||||
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream()
|
||||
.collect(Collectors.partitioningBy(element ->
|
||||
((ExecutableType) element.asType()).getParameterTypes().size() == 1
|
||||
&& element.getSimpleName().toString().startsWith("set")));
|
||||
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(element -> ((ExecutableType) element.asType()).getParameterTypes().size() == 1 && element.getSimpleName().toString().startsWith("set")));
|
||||
|
||||
List<Element> setters = annotatedMethods.get(true);
|
||||
List<Element> otherMethods = annotatedMethods.get(false);
|
||||
|
||||
otherMethods.forEach(element ->
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
|
||||
"@BuilderProperty must be applied to a setXxx method with a single argument", element));
|
||||
otherMethods.forEach(element -> processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@BuilderProperty must be applied to a setXxx method with a single argument", element));
|
||||
|
||||
if (setters.isEmpty()) {
|
||||
continue;
|
||||
@@ -45,11 +40,7 @@ public class BuilderProcessor extends AbstractProcessor {
|
||||
|
||||
String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString();
|
||||
|
||||
Map<String, String> setterMap = setters.stream().collect(Collectors.toMap(
|
||||
setter -> setter.getSimpleName().toString(),
|
||||
setter -> ((ExecutableType) setter.asType())
|
||||
.getParameterTypes().get(0).toString()
|
||||
));
|
||||
Map<String, String> setterMap = setters.stream().collect(Collectors.toMap(setter -> setter.getSimpleName().toString(), setter -> ((ExecutableType) setter.asType()).getParameterTypes().get(0).toString()));
|
||||
|
||||
try {
|
||||
writeBuilderFile(className, setterMap);
|
||||
|
||||
+1
-4
@@ -9,10 +9,7 @@ public class PersonBuilderTest {
|
||||
@Test
|
||||
public void whenBuildPersonWithBuilder_thenObjectHasPropertyValues() {
|
||||
|
||||
Person person = new PersonBuilder()
|
||||
.setAge(25)
|
||||
.setName("John")
|
||||
.build();
|
||||
Person person = new PersonBuilder().setAge(25).setName("John").build();
|
||||
|
||||
assertEquals(25, person.getAge());
|
||||
assertEquals("John", person.getName());
|
||||
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
@@ -8,7 +8,7 @@ public class Server {
|
||||
String address = "http://localhost:8080/baeldung";
|
||||
Endpoint.publish(address, implementor);
|
||||
System.out.println("Server ready...");
|
||||
Thread.sleep(60 * 1000);
|
||||
Thread.sleep(60 * 1000);
|
||||
System.out.println("Server exiting");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ public class RestfulServer {
|
||||
factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
|
||||
factoryBean.setAddress("http://localhost:8080/");
|
||||
Server server = factoryBean.create();
|
||||
|
||||
|
||||
System.out.println("Server ready...");
|
||||
Thread.sleep(60 * 1000);
|
||||
System.out.println("Server exiting");
|
||||
|
||||
+4
-4
@@ -5,20 +5,20 @@
|
||||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<modules>
|
||||
<module>cxf-introduction</module>
|
||||
<module>cxf-spring</module>
|
||||
<module>cxf-jaxrs-implementation</module>
|
||||
<module>cxf-aegis</module>
|
||||
</modules>
|
||||
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@@ -27,7 +27,7 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<defaultGoal>install</defaultGoal>
|
||||
<pluginManagement>
|
||||
|
||||
+148
-147
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,55 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-thrift</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<java.versin>1.8</java.versin>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<thrift.version>0.10.0</thrift.version>
|
||||
<maven-thrift.version>0.1.11</maven-thrift.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.thrift</groupId>
|
||||
<artifactId>libthrift</artifactId>
|
||||
<version>${thrift.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>install</defaultGoal>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.apache.thrift.protocol.TProtocol;
|
||||
import org.apache.thrift.transport.TSocket;
|
||||
import org.apache.thrift.transport.TTransport;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
|
||||
public class CrossPlatformServiceClient {
|
||||
|
||||
public boolean ping() {
|
||||
try {
|
||||
TTransport transport;
|
||||
|
||||
transport = new TSocket("localhost", 9090);
|
||||
transport.open();
|
||||
|
||||
TProtocol protocol = new TBinaryProtocol(transport);
|
||||
CrossPlatformService.Client client = new CrossPlatformService.Client(protocol);
|
||||
|
||||
System.out.print("Calling remote method...");
|
||||
|
||||
boolean result = client.ping();
|
||||
|
||||
System.out.println("done.");
|
||||
|
||||
transport.close();
|
||||
|
||||
return result;
|
||||
} catch (TTransportException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TException x) {
|
||||
x.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformResource;
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
import com.baeldung.thrift.impl.InvalidOperationException;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CrossPlatformServiceImpl implements CrossPlatformService.Iface {
|
||||
|
||||
@Override
|
||||
public CrossPlatformResource get(final int id) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return new CrossPlatformResource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(final CrossPlatformResource resource) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrossPlatformResource> getList() throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ping() throws InvalidOperationException, TException {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.server.TServer;
|
||||
import org.apache.thrift.server.TSimpleServer;
|
||||
import org.apache.thrift.transport.TServerSocket;
|
||||
import org.apache.thrift.transport.TServerTransport;
|
||||
|
||||
public class CrossPlatformServiceServer {
|
||||
|
||||
private TServer server;
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
TServerTransport serverTransport = new TServerSocket(9090);
|
||||
server = new TSimpleServer(new TServer.Args(serverTransport)
|
||||
.processor(new CrossPlatformService.Processor<>(new CrossPlatformServiceImpl())));
|
||||
|
||||
System.out.print("Starting the server... ");
|
||||
|
||||
server.serve();
|
||||
|
||||
System.out.println("done.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (server != null && server.isServing()) {
|
||||
System.out.print("Stopping the server... ");
|
||||
|
||||
server.stop();
|
||||
|
||||
System.out.println("done.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace cpp com.baeldung.thrift.impl
|
||||
namespace java com.baeldung.thrift.impl
|
||||
|
||||
exception InvalidOperationException {
|
||||
1: i32 code,
|
||||
2: string description
|
||||
}
|
||||
|
||||
struct CrossPlatformResource {
|
||||
1: i32 id,
|
||||
2: string name,
|
||||
3: optional string salutation
|
||||
}
|
||||
|
||||
service CrossPlatformService {
|
||||
|
||||
CrossPlatformResource get(1:i32 id) throws (1:InvalidOperationException e),
|
||||
|
||||
void save(1:CrossPlatformResource resource) throws (1:InvalidOperationException e),
|
||||
|
||||
list <CrossPlatformResource> getList() throws (1:InvalidOperationException e),
|
||||
|
||||
bool ping() throws (1:InvalidOperationException e)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CrossPlatformServiceTest {
|
||||
|
||||
private CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
new Thread(() -> server.start()).start();
|
||||
try {
|
||||
// wait for the server start up
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ping() {
|
||||
CrossPlatformServiceClient client = new CrossPlatformServiceClient();
|
||||
Assert.assertTrue(client.ping());
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user