Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15393
This commit is contained in:
commit
e7aa80b8a2
@ -0,0 +1,70 @@
|
|||||||
|
package com.baeldung.bucketsort;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class IntegerBucketSorter implements Sorter<Integer> {
|
||||||
|
|
||||||
|
private final Comparator<Integer> comparator;
|
||||||
|
|
||||||
|
public IntegerBucketSorter(Comparator<Integer> comparator) {
|
||||||
|
this.comparator = comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntegerBucketSorter() {
|
||||||
|
comparator = Comparator.naturalOrder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Integer> sort(List<Integer> arrayToSort) {
|
||||||
|
|
||||||
|
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
|
||||||
|
|
||||||
|
for(List<Integer> bucket : buckets){
|
||||||
|
bucket.sort(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
return concatenateSortedBuckets(buckets);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
|
||||||
|
List<Integer> sortedArray = new ArrayList<>();
|
||||||
|
int index = 0;
|
||||||
|
for(List<Integer> bucket : buckets){
|
||||||
|
for(int number : bucket){
|
||||||
|
sortedArray.add(index++, number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sortedArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
|
||||||
|
|
||||||
|
final int[] codes = createHashes(initialList);
|
||||||
|
|
||||||
|
List<List<Integer>> buckets = new ArrayList<>(codes[1]);
|
||||||
|
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
|
||||||
|
|
||||||
|
//distribute the data
|
||||||
|
for (int i : initialList) {
|
||||||
|
buckets.get(hash(i, codes)).add(i);
|
||||||
|
}
|
||||||
|
return buckets;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] createHashes(List<Integer> input){
|
||||||
|
int m = input.get(0);
|
||||||
|
for (int i = 1; i < input.size(); i++) {
|
||||||
|
if (m < input.get(i)) {
|
||||||
|
m = input.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new int[]{m, (int) Math.sqrt(input.size())};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int hash(int i, int[] code) {
|
||||||
|
return (int) ((double) i / code[0] * (code[1] - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.bucketsort;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface Sorter<T> {
|
||||||
|
|
||||||
|
List<T> sort(List<T> arrayToSort);
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.bucketsort;
|
||||||
|
|
||||||
|
import com.baeldung.bucketsort.IntegerBucketSorter;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class IntegerBucketSorterUnitTest {
|
||||||
|
|
||||||
|
private IntegerBucketSorter sorter;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
sorter = new IntegerBucketSorter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
|
||||||
|
|
||||||
|
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
|
||||||
|
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
|
||||||
|
|
||||||
|
List<Integer> actual = sorter.sort(unsorted);
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -48,6 +48,11 @@
|
|||||||
<version>${spock-core.version}</version>
|
<version>${spock-core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.groovy-wslite</groupId>
|
||||||
|
<artifactId>groovy-wslite</artifactId>
|
||||||
|
<version>${groovy-wslite.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -175,6 +180,7 @@
|
|||||||
<junit.platform.version>1.0.0</junit.platform.version>
|
<junit.platform.version>1.0.0</junit.platform.version>
|
||||||
<hsqldb.version>2.4.0</hsqldb.version>
|
<hsqldb.version>2.4.0</hsqldb.version>
|
||||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||||
|
<groovy-wslite.version>1.1.3</groovy-wslite.version>
|
||||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||||
<logback.version>1.2.3</logback.version>
|
<logback.version>1.2.3</logback.version>
|
||||||
<groovy.version>2.5.7</groovy.version>
|
<groovy.version>2.5.7</groovy.version>
|
||||||
|
@ -0,0 +1,152 @@
|
|||||||
|
package com.baeldung.webservice
|
||||||
|
|
||||||
|
import groovy.json.JsonSlurper
|
||||||
|
import wslite.rest.ContentType
|
||||||
|
import wslite.rest.RESTClient
|
||||||
|
import wslite.rest.RESTClientException
|
||||||
|
import wslite.soap.SOAPClient
|
||||||
|
import wslite.soap.SOAPMessageBuilder
|
||||||
|
import wslite.http.auth.HTTPBasicAuthorization
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class WebserviceUnitTest extends GroovyTestCase {
|
||||||
|
|
||||||
|
JsonSlurper jsonSlurper = new JsonSlurper()
|
||||||
|
|
||||||
|
static RESTClient client = new RESTClient("https://postman-echo.com")
|
||||||
|
|
||||||
|
static {
|
||||||
|
client.defaultAcceptHeader = ContentType.JSON
|
||||||
|
client.httpClient.sslTrustAllCerts = true
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenSendingGet_thenRespose200() {
|
||||||
|
def postmanGet = new URL('https://postman-echo.com/get')
|
||||||
|
def getConnection = postmanGet.openConnection()
|
||||||
|
getConnection.requestMethod = 'GET'
|
||||||
|
assert getConnection.responseCode == 200
|
||||||
|
if (getConnection.responseCode == 200) {
|
||||||
|
assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenSendingPost_thenRespose200() {
|
||||||
|
def postmanPost = new URL('https://postman-echo.com/post')
|
||||||
|
def query = "q=This is post request form parameter."
|
||||||
|
def postConnection = postmanPost.openConnection()
|
||||||
|
postConnection.requestMethod = 'POST'
|
||||||
|
assert postConnection.responseCode == 200
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenSendingPostWithParams_thenRespose200() {
|
||||||
|
def postmanPost = new URL('https://postman-echo.com/post')
|
||||||
|
def form = "param1=This is request parameter."
|
||||||
|
def postConnection = postmanPost.openConnection()
|
||||||
|
postConnection.requestMethod = 'POST'
|
||||||
|
postConnection.doOutput = true
|
||||||
|
def text
|
||||||
|
postConnection.with {
|
||||||
|
outputStream.withWriter { outputStreamWriter ->
|
||||||
|
outputStreamWriter << form
|
||||||
|
}
|
||||||
|
text = content.text
|
||||||
|
}
|
||||||
|
assert postConnection.responseCode == 200
|
||||||
|
assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter."
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenReadingRSS_thenReceiveFeed() {
|
||||||
|
def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en")
|
||||||
|
def stories = []
|
||||||
|
(0..4).each {
|
||||||
|
def item = rssFeed.channel.item.get(it)
|
||||||
|
stories << item.title.text()
|
||||||
|
}
|
||||||
|
assert stories.size() == 5
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenReadingAtom_thenReceiveFeed() {
|
||||||
|
def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en")
|
||||||
|
def stories = []
|
||||||
|
(0..4).each {
|
||||||
|
def entry = atomFeed.entry.get(it)
|
||||||
|
stories << entry.title.text()
|
||||||
|
}
|
||||||
|
assert stories.size() == 5
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenConsumingSoap_thenReceiveResponse() {
|
||||||
|
def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso"
|
||||||
|
def soapClient = new SOAPClient(url)
|
||||||
|
def message = new SOAPMessageBuilder().build({
|
||||||
|
body {
|
||||||
|
NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") {
|
||||||
|
ubiNum(1234)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
def response = soapClient.send(message.toString());
|
||||||
|
def words = response.NumberToWordsResponse
|
||||||
|
assert words == "one thousand two hundred and thirty four "
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenConsumingRestGet_thenReceiveResponse() {
|
||||||
|
def path = "/get"
|
||||||
|
def response
|
||||||
|
try {
|
||||||
|
response = client.get(path: path)
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.json?.headers?.host == "postman-echo.com"
|
||||||
|
} catch (RESTClientException e) {
|
||||||
|
assert e?.response?.statusCode != 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenConsumingRestPost_thenReceiveResponse() {
|
||||||
|
def path = "/post"
|
||||||
|
def params = ["foo":1,"bar":2]
|
||||||
|
def response
|
||||||
|
try {
|
||||||
|
response = client.post(path: path) {
|
||||||
|
type ContentType.JSON
|
||||||
|
json params
|
||||||
|
}
|
||||||
|
assert response.json?.data == params
|
||||||
|
} catch (RESTClientException e) {
|
||||||
|
e.printStackTrace()
|
||||||
|
assert e?.response?.statusCode != 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenBasicAuthentication_thenReceive200() {
|
||||||
|
def path = "/basic-auth"
|
||||||
|
def response
|
||||||
|
try {
|
||||||
|
client.authorization = new HTTPBasicAuthorization("postman", "password")
|
||||||
|
response = client.get(path: path)
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.json?.authenticated == true
|
||||||
|
} catch (RESTClientException e) {
|
||||||
|
e.printStackTrace()
|
||||||
|
assert e?.response?.statusCode != 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_whenOAuth_thenReceive200() {
|
||||||
|
RESTClient oAuthClient = new RESTClient("https://postman-echo.com")
|
||||||
|
oAuthClient.defaultAcceptHeader = ContentType.JSON
|
||||||
|
oAuthClient.httpClient.sslTrustAllCerts = true
|
||||||
|
|
||||||
|
def path = "/oauth1"
|
||||||
|
def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A=']
|
||||||
|
def response
|
||||||
|
try {
|
||||||
|
response = oAuthClient.get(path: path, query: params)
|
||||||
|
assert response.statusCode == 200
|
||||||
|
assert response.statusMessage == "OK"
|
||||||
|
assert response.json.status == "pass"
|
||||||
|
} catch (RESTClientException e) {
|
||||||
|
assert e?.response?.statusCode != 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.java.list;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class RemoveFromList {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> sports = new ArrayList<>();
|
||||||
|
sports.add("Football");
|
||||||
|
sports.add("Basketball");
|
||||||
|
sports.add("Baseball");
|
||||||
|
sports.add("Boxing");
|
||||||
|
sports.add("Cycling");
|
||||||
|
|
||||||
|
System.out.println("List before removing: " + sports);
|
||||||
|
|
||||||
|
// Remove with index
|
||||||
|
sports.remove(1);
|
||||||
|
|
||||||
|
// Remove with an element
|
||||||
|
sports.remove("Baseball");
|
||||||
|
|
||||||
|
// Iterator remove method
|
||||||
|
Iterator<String> iterator = sports.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (iterator.next().equals("Boxing")) {
|
||||||
|
iterator.remove();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArrayList removeIf method (Java 8)
|
||||||
|
sports.removeIf(p -> p.equals("Cycling"));
|
||||||
|
|
||||||
|
System.out.println("List after removing: " + sports);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -17,15 +17,9 @@ public class FileReaderExampleUnitTest {
|
|||||||
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
|
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
|
||||||
String expectedText = "Hello, World!";
|
String expectedText = "Hello, World!";
|
||||||
File file = new File(FILE_PATH);
|
File file = new File(FILE_PATH);
|
||||||
FileReader fileReader = null;
|
try (FileReader fileReader = new FileReader(file)) {
|
||||||
try {
|
|
||||||
fileReader = new FileReader(file);
|
|
||||||
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
|
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
|
||||||
Assert.assertEquals(expectedText, content);
|
Assert.assertEquals(expectedText, content);
|
||||||
} finally {
|
|
||||||
if (fileReader != null) {
|
|
||||||
fileReader.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,15 +27,9 @@ public class FileReaderExampleUnitTest {
|
|||||||
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
|
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
|
||||||
String expectedText = "Hello";
|
String expectedText = "Hello";
|
||||||
File file = new File(FILE_PATH);
|
File file = new File(FILE_PATH);
|
||||||
FileReader fileReader = null;
|
try (FileReader fileReader = new FileReader(file)) {
|
||||||
try {
|
|
||||||
fileReader = new FileReader(file);
|
|
||||||
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
|
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
|
||||||
Assert.assertEquals(expectedText, content);
|
Assert.assertEquals(expectedText, content);
|
||||||
} finally {
|
|
||||||
if (fileReader != null) {
|
|
||||||
fileReader.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,6 +207,21 @@
|
|||||||
<target>${maven.compiler.target}</target>
|
<target>${maven.compiler.target}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<!-- Build an executable JAR -->
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>${maven-jar-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<mainClass>com.baeldung.resource.MyResourceLoader</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@ -274,6 +289,8 @@
|
|||||||
<!-- Mime Type Libraries -->
|
<!-- Mime Type Libraries -->
|
||||||
<tika.version>1.18</tika.version>
|
<tika.version>1.18</tika.version>
|
||||||
<jmime-magic.version>0.1.5</jmime-magic.version>
|
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||||
|
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.resource;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class MyResourceLoader {
|
||||||
|
|
||||||
|
private void loadFileWithReader() throws IOException {
|
||||||
|
|
||||||
|
try (FileReader fileReader = new FileReader("src/main/resources/input.txt");
|
||||||
|
BufferedReader reader = new BufferedReader(fileReader)) {
|
||||||
|
String contents = reader.lines()
|
||||||
|
.collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
System.out.println(contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFileAsResource() throws IOException {
|
||||||
|
|
||||||
|
try (InputStream inputStream = getClass().getResourceAsStream("/input.txt");
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||||
|
String contents = reader.lines()
|
||||||
|
.collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
System.out.println(contents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
|
MyResourceLoader resourceLoader = new MyResourceLoader();
|
||||||
|
|
||||||
|
resourceLoader.loadFileAsResource();
|
||||||
|
resourceLoader.loadFileWithReader();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.copyconstructor;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
protected int id;
|
||||||
|
protected String name;
|
||||||
|
protected Date startDate;
|
||||||
|
|
||||||
|
public Employee(int id, String name, Date startDate) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.startDate = startDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(Employee employee) {
|
||||||
|
this.id = employee.id;
|
||||||
|
this.name = employee.name;
|
||||||
|
this.startDate = new Date(employee.startDate.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
Date getStartDate() {
|
||||||
|
return startDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee copy() {
|
||||||
|
return new Employee(this);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.copyconstructor;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class Manager extends Employee {
|
||||||
|
|
||||||
|
private List<Employee> directReports;
|
||||||
|
|
||||||
|
public Manager(int id, String name, Date startDate, List<Employee> directReports) {
|
||||||
|
super(id, name, startDate);
|
||||||
|
this.directReports = directReports;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Manager(Manager manager) {
|
||||||
|
super(manager.id, manager.name, manager.startDate);
|
||||||
|
this.directReports = manager.directReports.stream()
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Employee copy() {
|
||||||
|
return new Manager(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Employee> getDirectReport() {
|
||||||
|
return this.directReports;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.copyconstructor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class EmployeeUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||||
|
Date d1 = new Date(123);
|
||||||
|
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||||
|
Employee e2 = new Employee(e1);
|
||||||
|
assertEquals(d1, e1.getStartDate());
|
||||||
|
assertEquals(d1, e2.getStartDate());
|
||||||
|
|
||||||
|
d1.setTime(456);
|
||||||
|
assertEquals(d1, e1.getStartDate());
|
||||||
|
assertNotEquals(d1, e2.getStartDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||||
|
Date d1 = new Date(123);
|
||||||
|
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||||
|
Employee e2 = e1.copy();
|
||||||
|
assertEquals(d1, e1.getStartDate());
|
||||||
|
assertEquals(d1, e2.getStartDate());
|
||||||
|
|
||||||
|
d1.setTime(456);
|
||||||
|
assertEquals(d1, e1.getStartDate());
|
||||||
|
assertNotEquals(d1, e2.getStartDate());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.copyconstructor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ManagerUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||||
|
Date startDate = new Date(123);
|
||||||
|
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||||
|
Employee e2 = new Employee(e1);
|
||||||
|
List<Employee> directReports = new ArrayList<Employee>();
|
||||||
|
directReports.add(e1);
|
||||||
|
directReports.add(e2);
|
||||||
|
|
||||||
|
Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||||
|
Manager m2 = new Manager(m1);
|
||||||
|
List<Employee> directReports1 = m1.getDirectReport();
|
||||||
|
List<Employee> directReports2 = m2.getDirectReport();
|
||||||
|
assertEquals(directReports1.size(), directReports2.size());
|
||||||
|
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||||
|
|
||||||
|
// clear m1's direct reports list. m2's list should not be affected
|
||||||
|
directReports.clear();
|
||||||
|
directReports1 = m1.getDirectReport();
|
||||||
|
directReports2 = m2.getDirectReport();
|
||||||
|
assertEquals(0, directReports1.size());
|
||||||
|
assertEquals(2, directReports2.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||||
|
Date startDate = new Date(123);
|
||||||
|
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||||
|
Employee e2 = new Employee(e1);
|
||||||
|
List<Employee> directReports = new ArrayList<Employee>();
|
||||||
|
directReports.add(e1);
|
||||||
|
directReports.add(e2);
|
||||||
|
|
||||||
|
// a Manager object whose declaration type is Employee.
|
||||||
|
Employee source = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||||
|
Employee clone = source.copy();
|
||||||
|
|
||||||
|
// after copy, clone should be still a Manager object.
|
||||||
|
assertTrue(clone instanceof Manager);
|
||||||
|
List<Employee> directReports1 = ((Manager) source).getDirectReport();
|
||||||
|
List<Employee> directReports2 = ((Manager) clone).getDirectReport();
|
||||||
|
assertEquals(directReports1.size(), directReports2.size());
|
||||||
|
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||||
|
|
||||||
|
// clear source's direct reports list. clone's list should not be affected
|
||||||
|
directReports.clear();
|
||||||
|
directReports1 = ((Manager) source).getDirectReport();
|
||||||
|
directReports2 = ((Manager) clone).getDirectReport();
|
||||||
|
assertEquals(0, directReports1.size());
|
||||||
|
assertEquals(2, directReports2.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.booleanoperators;
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
|
||||||
|
private boolean diesel;
|
||||||
|
private boolean manual;
|
||||||
|
|
||||||
|
public Car(boolean diesel, boolean manual) {
|
||||||
|
this.diesel = diesel;
|
||||||
|
this.manual = manual;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDiesel() {
|
||||||
|
return diesel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isManual() {
|
||||||
|
return manual;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car dieselAndManualCar() {
|
||||||
|
return new Car(true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car dieselAndAutomaticCar() {
|
||||||
|
return new Car(true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car oilAndManualCar() {
|
||||||
|
return new Car(false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Car oilAndAutomaticCar() {
|
||||||
|
return new Car(false, false);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.booleanoperators;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class XorUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDieselManualCar_whenXorOldSchool_thenFalse() {
|
||||||
|
Car car = Car.dieselAndManualCar();
|
||||||
|
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||||
|
assertThat(dieselXorManual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() {
|
||||||
|
Car car = Car.dieselAndAutomaticCar();
|
||||||
|
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||||
|
assertThat(dieselXorManual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNonDieselManualCar_whenXorOldSchool_thenTrue() {
|
||||||
|
Car car = Car.oilAndManualCar();
|
||||||
|
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||||
|
assertThat(dieselXorManual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() {
|
||||||
|
Car car = Car.oilAndAutomaticCar();
|
||||||
|
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||||
|
assertThat(dieselXorManual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDieselManualCar_whenXor_thenFalse() {
|
||||||
|
Car car = Car.dieselAndManualCar();
|
||||||
|
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||||
|
assertThat(dieselXorManual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenDieselAutomaticCar_whenXor_thenTrue() {
|
||||||
|
Car car = Car.dieselAndAutomaticCar();
|
||||||
|
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||||
|
assertThat(dieselXorManual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNonDieselManualCar_whenXor_thenTrue() {
|
||||||
|
Car car = Car.oilAndManualCar();
|
||||||
|
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||||
|
assertThat(dieselXorManual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNonDieselAutomaticCar_whenXor_thenFalse() {
|
||||||
|
Car car = Car.oilAndAutomaticCar();
|
||||||
|
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||||
|
assertThat(dieselXorManual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNumbersOneAndThree_whenXor_thenTwo() {
|
||||||
|
assertThat(1 ^ 3).isEqualTo(2);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.baeldung.uuid;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class UUIDGenerator {
|
public class UUIDGenerator {
|
||||||
@ -65,9 +66,9 @@ public class UUIDGenerator {
|
|||||||
try {
|
try {
|
||||||
md = MessageDigest.getInstance("SHA-1");
|
md = MessageDigest.getInstance("SHA-1");
|
||||||
} catch (NoSuchAlgorithmException nsae) {
|
} catch (NoSuchAlgorithmException nsae) {
|
||||||
throw new InternalError("MD5 not supported", nsae);
|
throw new InternalError("SHA-1 not supported", nsae);
|
||||||
}
|
}
|
||||||
byte[] bytes = md.digest(name);
|
byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16);
|
||||||
bytes[6] &= 0x0f; /* clear version */
|
bytes[6] &= 0x0f; /* clear version */
|
||||||
bytes[6] |= 0x50; /* set to version 5 */
|
bytes[6] |= 0x50; /* set to version 5 */
|
||||||
bytes[8] &= 0x3f; /* clear variant */
|
bytes[8] &= 0x3f; /* clear variant */
|
||||||
|
@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
## Java Bean Validation Examples
|
## Java Bean Validation Examples
|
||||||
|
|
||||||
###The Course
|
|
||||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Java Bean Validation Basics](http://www.baeldung.com/javax-validation)
|
- [Java Bean Validation Basics](http://www.baeldung.com/javax-validation)
|
||||||
- [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements)
|
- [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements)
|
||||||
|
25
mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java
Normal file
25
mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.dto;
|
||||||
|
|
||||||
|
public class CustomerDto {
|
||||||
|
|
||||||
|
private String forename;
|
||||||
|
private String surname;
|
||||||
|
|
||||||
|
public String getForename() {
|
||||||
|
return forename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomerDto setForename(String forename) {
|
||||||
|
this.forename = forename;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomerDto setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
35
mapstruct/src/main/java/com/baeldung/entity/Address.java
Normal file
35
mapstruct/src/main/java/com/baeldung/entity/Address.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.entity;
|
||||||
|
|
||||||
|
public class Address {
|
||||||
|
|
||||||
|
private String street;
|
||||||
|
private String postalcode;
|
||||||
|
private String county;
|
||||||
|
|
||||||
|
public String getStreet() {
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address setStreet(String street) {
|
||||||
|
this.street = street;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostalcode() {
|
||||||
|
return postalcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address setPostalcode(String postalcode) {
|
||||||
|
this.postalcode = postalcode;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCounty() {
|
||||||
|
return county;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address setCounty(String county) {
|
||||||
|
this.county = county;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
25
mapstruct/src/main/java/com/baeldung/entity/Customer.java
Normal file
25
mapstruct/src/main/java/com/baeldung/entity/Customer.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.entity;
|
||||||
|
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.entity;
|
||||||
|
|
||||||
|
public class DeliveryAddress {
|
||||||
|
|
||||||
|
private String forename;
|
||||||
|
private String surname;
|
||||||
|
private String street;
|
||||||
|
private String postalcode;
|
||||||
|
private String county;
|
||||||
|
|
||||||
|
public String getForename() {
|
||||||
|
return forename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeliveryAddress setForename(String forename) {
|
||||||
|
this.forename = forename;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeliveryAddress setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getStreet() {
|
||||||
|
return street;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeliveryAddress setStreet(String street) {
|
||||||
|
this.street = street;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostalcode() {
|
||||||
|
return postalcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeliveryAddress setPostalcode(String postalcode) {
|
||||||
|
this.postalcode = postalcode;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCounty() {
|
||||||
|
return county;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeliveryAddress setCounty(String county) {
|
||||||
|
this.county = county;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
|
import com.baeldung.dto.CustomerDto;
|
||||||
|
import com.baeldung.entity.Customer;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface CustomerDtoMapper {
|
||||||
|
|
||||||
|
@Mapping(source = "firstName", target = "forename")
|
||||||
|
@Mapping(source = "lastName", target = "surname")
|
||||||
|
CustomerDto from(Customer customer);
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
|
||||||
|
import com.baeldung.entity.Address;
|
||||||
|
import com.baeldung.entity.Customer;
|
||||||
|
import com.baeldung.entity.DeliveryAddress;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface DeliveryAddressMapper {
|
||||||
|
|
||||||
|
@Mapping(source = "customer.firstName", target = "forename")
|
||||||
|
@Mapping(source = "customer.lastName", target = "surname")
|
||||||
|
@Mapping(source = "address.street", target = "street")
|
||||||
|
@Mapping(source = "address.postalcode", target = "postalcode")
|
||||||
|
@Mapping(source = "address.county", target = "county")
|
||||||
|
DeliveryAddress from(Customer customer, Address address);
|
||||||
|
|
||||||
|
@Mapping(source = "address.postalcode", target = "postalcode")
|
||||||
|
@Mapping(source = "address.county", target = "county")
|
||||||
|
DeliveryAddress updateAddress(@MappingTarget DeliveryAddress deliveryAddress, Address address);
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
import com.baeldung.dto.CustomerDto;
|
||||||
|
import com.baeldung.entity.Customer;
|
||||||
|
|
||||||
|
public class CustomerDtoMapperUnitTest {
|
||||||
|
|
||||||
|
private CustomerDtoMapper customerDtoMapper = Mappers.getMapper(CustomerDtoMapper.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGivenCustomer_mapsToCustomerDto() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
Customer customer = new Customer().setFirstName("Max")
|
||||||
|
.setLastName("Powers");
|
||||||
|
|
||||||
|
// when
|
||||||
|
CustomerDto customerDto = customerDtoMapper.from(customer);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(customerDto.getForename(), customer.getFirstName());
|
||||||
|
assertEquals(customerDto.getSurname(), customer.getLastName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.baeldung.mapper;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
import com.baeldung.entity.Address;
|
||||||
|
import com.baeldung.entity.Customer;
|
||||||
|
import com.baeldung.entity.DeliveryAddress;
|
||||||
|
|
||||||
|
public class DeliveryAddressMapperUnitTest {
|
||||||
|
|
||||||
|
private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGivenCustomerAndAddress_mapsToDeliveryAddress() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
Customer customer = new Customer().setFirstName("Max")
|
||||||
|
.setLastName("Powers");
|
||||||
|
|
||||||
|
Address homeAddress = new Address().setStreet("123 Some Street")
|
||||||
|
.setCounty("Nevada")
|
||||||
|
.setPostalcode("89123");
|
||||||
|
|
||||||
|
// when
|
||||||
|
DeliveryAddress deliveryAddress = deliveryAddressMapper.from(customer, homeAddress);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertEquals(deliveryAddress.getForename(), customer.getFirstName());
|
||||||
|
assertEquals(deliveryAddress.getSurname(), customer.getLastName());
|
||||||
|
assertEquals(deliveryAddress.getStreet(), homeAddress.getStreet());
|
||||||
|
assertEquals(deliveryAddress.getCounty(), homeAddress.getCounty());
|
||||||
|
assertEquals(deliveryAddress.getPostalcode(), homeAddress.getPostalcode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGivenDeliveryAddressAndSomeOtherAddress_updatesDeliveryAddress() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
Customer customer = new Customer().setFirstName("Max")
|
||||||
|
.setLastName("Powers");
|
||||||
|
|
||||||
|
DeliveryAddress deliveryAddress = new DeliveryAddress().setStreet("123 Some Street")
|
||||||
|
.setCounty("Nevada")
|
||||||
|
.setPostalcode("89123");
|
||||||
|
|
||||||
|
Address otherAddress = new Address().setStreet("456 Some other street")
|
||||||
|
.setCounty("Arizona")
|
||||||
|
.setPostalcode("12345");
|
||||||
|
|
||||||
|
// when
|
||||||
|
DeliveryAddress updatedDeliveryAddress = deliveryAddressMapper.updateAddress(deliveryAddress, otherAddress);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertSame(deliveryAddress, updatedDeliveryAddress);
|
||||||
|
|
||||||
|
assertEquals(deliveryAddress.getStreet(), otherAddress.getStreet());
|
||||||
|
assertEquals(deliveryAddress.getCounty(), otherAddress.getCounty());
|
||||||
|
assertEquals(deliveryAddress.getPostalcode(), otherAddress.getPostalcode());
|
||||||
|
}
|
||||||
|
}
|
5
ml/README.md
Normal file
5
ml/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
### Logistic Regression in Java
|
||||||
|
This is a soft introduction to ML using [deeplearning4j](https://deeplearning4j.org) library
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Logistic Regression in Java](http://www.baeldung.com/)
|
52
ml/pom.xml
Normal file
52
ml/pom.xml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<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/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.deeplearning4j</groupId>
|
||||||
|
<artifactId>ml</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>Machine Learning</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.nd4j</groupId>
|
||||||
|
<artifactId>nd4j-native-platform</artifactId>
|
||||||
|
<version>${dl4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.deeplearning4j</groupId>
|
||||||
|
<artifactId>deeplearning4j-core</artifactId>
|
||||||
|
<version>${dl4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.deeplearning4j</groupId>
|
||||||
|
<artifactId>deeplearning4j-nn</artifactId>
|
||||||
|
<version>${dl4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.datavec/datavec-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.datavec</groupId>
|
||||||
|
<artifactId>datavec-api</artifactId>
|
||||||
|
<version>${dl4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.3.5</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<dl4j.version>1.0.0-beta4</dl4j.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
168
ml/src/main/java/com/baeldung/logreg/MnistClassifier.java
Normal file
168
ml/src/main/java/com/baeldung/logreg/MnistClassifier.java
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
package com.baeldung.logreg;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.datavec.api.io.labels.ParentPathLabelGenerator;
|
||||||
|
import org.datavec.api.split.FileSplit;
|
||||||
|
import org.datavec.image.loader.NativeImageLoader;
|
||||||
|
import org.datavec.image.recordreader.ImageRecordReader;
|
||||||
|
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
|
||||||
|
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
|
||||||
|
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
|
||||||
|
import org.deeplearning4j.nn.conf.inputs.InputType;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.DenseLayer;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.OutputLayer;
|
||||||
|
import org.deeplearning4j.nn.conf.layers.SubsamplingLayer;
|
||||||
|
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||||
|
import org.deeplearning4j.nn.weights.WeightInit;
|
||||||
|
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
|
||||||
|
import org.deeplearning4j.util.ModelSerializer;
|
||||||
|
import org.nd4j.evaluation.classification.Evaluation;
|
||||||
|
import org.nd4j.linalg.activations.Activation;
|
||||||
|
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
|
||||||
|
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
|
||||||
|
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
|
||||||
|
import org.nd4j.linalg.learning.config.Nesterovs;
|
||||||
|
import org.nd4j.linalg.lossfunctions.LossFunctions;
|
||||||
|
import org.nd4j.linalg.schedule.MapSchedule;
|
||||||
|
import org.nd4j.linalg.schedule.ScheduleType;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handwritten digit image classification based on LeNet-5 architecture by Yann LeCun.
|
||||||
|
*
|
||||||
|
* This code accompanies the article "Logistic regression in Java" and is heavily based on
|
||||||
|
* <a href="https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/mnist/MnistClassifier.java">MnistClassifier</a>.
|
||||||
|
* Some minor changes have been made in order to make article's flow smoother.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class MnistClassifier {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MnistClassifier.class);
|
||||||
|
private static final String basePath = System.getProperty("java.io.tmpdir") + "mnist" + File.separator;
|
||||||
|
private static final File modelPath = new File(basePath + "mnist-model.zip");
|
||||||
|
private static final String dataUrl = "http://github.com/myleott/mnist_png/raw/master/mnist_png.tar.gz";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// input image sizes in pixels
|
||||||
|
int height = 28;
|
||||||
|
int width = 28;
|
||||||
|
// input image colour depth (1 for gray scale images)
|
||||||
|
int channels = 1;
|
||||||
|
// the number of output classes
|
||||||
|
int outputClasses = 10;
|
||||||
|
// number of samples that will be propagated through the network in each iteration
|
||||||
|
int batchSize = 54;
|
||||||
|
// total number of training epochs
|
||||||
|
int epochs = 1;
|
||||||
|
|
||||||
|
// initialize a pseudorandom number generator
|
||||||
|
int seed = 1234;
|
||||||
|
Random randNumGen = new Random(seed);
|
||||||
|
|
||||||
|
final String path = basePath + "mnist_png" + File.separator;
|
||||||
|
if (!new File(path).exists()) {
|
||||||
|
logger.info("Downloading data {}", dataUrl);
|
||||||
|
String localFilePath = basePath + "mnist_png.tar.gz";
|
||||||
|
File file = new File(localFilePath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.getParentFile()
|
||||||
|
.mkdirs();
|
||||||
|
Utils.downloadAndSave(dataUrl, file);
|
||||||
|
Utils.extractTarArchive(file, basePath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.info("Using the local data from folder {}", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Vectorizing the data from folder {}", path);
|
||||||
|
// vectorization of train data
|
||||||
|
File trainData = new File(path + "training");
|
||||||
|
FileSplit trainSplit = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
|
||||||
|
// use parent directory name as the image label
|
||||||
|
ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();
|
||||||
|
ImageRecordReader trainRR = new ImageRecordReader(height, width, channels, labelMaker);
|
||||||
|
trainRR.initialize(trainSplit);
|
||||||
|
DataSetIterator train = new RecordReaderDataSetIterator(trainRR, batchSize, 1, outputClasses);
|
||||||
|
|
||||||
|
// pixel values from 0-255 to 0-1 (min-max scaling)
|
||||||
|
DataNormalization imageScaler = new ImagePreProcessingScaler();
|
||||||
|
imageScaler.fit(train);
|
||||||
|
train.setPreProcessor(imageScaler);
|
||||||
|
|
||||||
|
// vectorization of test data
|
||||||
|
File testData = new File(path + "testing");
|
||||||
|
FileSplit testSplit = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
|
||||||
|
ImageRecordReader testRR = new ImageRecordReader(height, width, channels, labelMaker);
|
||||||
|
testRR.initialize(testSplit);
|
||||||
|
DataSetIterator test = new RecordReaderDataSetIterator(testRR, batchSize, 1, outputClasses);
|
||||||
|
// same normalization for better results
|
||||||
|
test.setPreProcessor(imageScaler);
|
||||||
|
|
||||||
|
logger.info("Network configuration and training...");
|
||||||
|
// reduce the learning rate as the number of training epochs increases
|
||||||
|
// iteration #, learning rate
|
||||||
|
Map<Integer, Double> learningRateSchedule = new HashMap<>();
|
||||||
|
learningRateSchedule.put(0, 0.06);
|
||||||
|
learningRateSchedule.put(200, 0.05);
|
||||||
|
learningRateSchedule.put(600, 0.028);
|
||||||
|
learningRateSchedule.put(800, 0.0060);
|
||||||
|
learningRateSchedule.put(1000, 0.001);
|
||||||
|
|
||||||
|
final ConvolutionLayer layer1 = new ConvolutionLayer.Builder(5, 5).nIn(channels)
|
||||||
|
.stride(1, 1)
|
||||||
|
.nOut(20)
|
||||||
|
.activation(Activation.IDENTITY)
|
||||||
|
.build();
|
||||||
|
final SubsamplingLayer layer2 = new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2)
|
||||||
|
.stride(2, 2)
|
||||||
|
.build();
|
||||||
|
// nIn need not specified in later layers
|
||||||
|
final ConvolutionLayer layer3 = new ConvolutionLayer.Builder(5, 5).stride(1, 1)
|
||||||
|
.nOut(50)
|
||||||
|
.activation(Activation.IDENTITY)
|
||||||
|
.build();
|
||||||
|
final DenseLayer layer4 = new DenseLayer.Builder().activation(Activation.RELU)
|
||||||
|
.nOut(500)
|
||||||
|
.build();
|
||||||
|
final OutputLayer layer5 = new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputClasses)
|
||||||
|
.activation(Activation.SOFTMAX)
|
||||||
|
.build();
|
||||||
|
final MultiLayerConfiguration config = new NeuralNetConfiguration.Builder().seed(seed)
|
||||||
|
.l2(0.0005) // ridge regression value
|
||||||
|
.updater(new Nesterovs(new MapSchedule(ScheduleType.ITERATION, learningRateSchedule)))
|
||||||
|
.weightInit(WeightInit.XAVIER)
|
||||||
|
.list()
|
||||||
|
.layer(layer1)
|
||||||
|
.layer(layer2)
|
||||||
|
.layer(layer3)
|
||||||
|
.layer(layer2)
|
||||||
|
.layer(layer4)
|
||||||
|
.layer(layer5)
|
||||||
|
.setInputType(InputType.convolutionalFlat(height, width, channels))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
final MultiLayerNetwork model = new MultiLayerNetwork(config);
|
||||||
|
model.init();
|
||||||
|
model.setListeners(new ScoreIterationListener(100));
|
||||||
|
logger.info("Total num of params: {}", model.numParams());
|
||||||
|
|
||||||
|
// evaluation while training (the score should go down)
|
||||||
|
for (int i = 0; i < epochs; i++) {
|
||||||
|
model.fit(train);
|
||||||
|
logger.info("Completed epoch {}", i);
|
||||||
|
train.reset();
|
||||||
|
test.reset();
|
||||||
|
}
|
||||||
|
Evaluation eval = model.evaluate(test);
|
||||||
|
logger.info(eval.stats());
|
||||||
|
|
||||||
|
ModelSerializer.writeModel(model, modelPath, true);
|
||||||
|
logger.info("The MINIST model has been saved in {}", modelPath.getPath());
|
||||||
|
}
|
||||||
|
}
|
57
ml/src/main/java/com/baeldung/logreg/MnistPrediction.java
Normal file
57
ml/src/main/java/com/baeldung/logreg/MnistPrediction.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.logreg;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
|
||||||
|
import org.datavec.image.loader.NativeImageLoader;
|
||||||
|
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||||
|
import org.deeplearning4j.util.ModelSerializer;
|
||||||
|
import org.nd4j.linalg.api.ndarray.INDArray;
|
||||||
|
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class MnistPrediction {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MnistPrediction.class);
|
||||||
|
private static final File modelPath = new File(System.getProperty("java.io.tmpdir") + "mnist" + File.separator + "mnist-model.zip");
|
||||||
|
private static final int height = 28;
|
||||||
|
private static final int width = 28;
|
||||||
|
private static final int channels = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a popup that allows to select a file from the filesystem.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String fileChose() {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
int ret = fc.showOpenDialog(null);
|
||||||
|
if (ret == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File file = fc.getSelectedFile();
|
||||||
|
return file.getAbsolutePath();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
if (!modelPath.exists()) {
|
||||||
|
logger.info("The model not found. Have you trained it?");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelPath);
|
||||||
|
String path = fileChose();
|
||||||
|
File file = new File(path);
|
||||||
|
|
||||||
|
INDArray image = new NativeImageLoader(height, width, channels).asMatrix(file);
|
||||||
|
new ImagePreProcessingScaler(0, 1).transform(image);
|
||||||
|
|
||||||
|
// Pass through to neural Net
|
||||||
|
INDArray output = model.output(image);
|
||||||
|
|
||||||
|
logger.info("File: {}", path);
|
||||||
|
logger.info("Probabilities: {}", output);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
103
ml/src/main/java/com/baeldung/logreg/Utils.java
Normal file
103
ml/src/main/java/com/baeldung/logreg/Utils.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package com.baeldung.logreg;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||||
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||||
|
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||||
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility class for digit classifier.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Utils {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
|
||||||
|
|
||||||
|
private Utils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download the content of the given url and save it into a file.
|
||||||
|
* @param url
|
||||||
|
* @param file
|
||||||
|
*/
|
||||||
|
public static void downloadAndSave(String url, File file) throws IOException {
|
||||||
|
CloseableHttpClient client = HttpClientBuilder.create()
|
||||||
|
.build();
|
||||||
|
logger.info("Connecting to {}", url);
|
||||||
|
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
if (entity != null) {
|
||||||
|
logger.info("Downloaded {} bytes", entity.getContentLength());
|
||||||
|
try (FileOutputStream outstream = new FileOutputStream(file)) {
|
||||||
|
logger.info("Saving to the local file");
|
||||||
|
entity.writeTo(outstream);
|
||||||
|
outstream.flush();
|
||||||
|
logger.info("Local file saved");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract a "tar.gz" file into a given folder.
|
||||||
|
* @param file
|
||||||
|
* @param folder
|
||||||
|
*/
|
||||||
|
public static void extractTarArchive(File file, String folder) throws IOException {
|
||||||
|
logger.info("Extracting archive {} into folder {}", file.getName(), folder);
|
||||||
|
// @formatter:off
|
||||||
|
try (FileInputStream fis = new FileInputStream(file);
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(fis);
|
||||||
|
GzipCompressorInputStream gzip = new GzipCompressorInputStream(bis);
|
||||||
|
TarArchiveInputStream tar = new TarArchiveInputStream(gzip)) {
|
||||||
|
// @formatter:on
|
||||||
|
TarArchiveEntry entry;
|
||||||
|
while ((entry = (TarArchiveEntry) tar.getNextEntry()) != null) {
|
||||||
|
extractEntry(entry, tar, folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info("Archive extracted");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract an entry of the input stream into a given folder
|
||||||
|
* @param entry
|
||||||
|
* @param tar
|
||||||
|
* @param folder
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static void extractEntry(ArchiveEntry entry, InputStream tar, String folder) throws IOException {
|
||||||
|
final int bufferSize = 4096;
|
||||||
|
final String path = folder + entry.getName();
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
new File(path).mkdirs();
|
||||||
|
} else {
|
||||||
|
int count;
|
||||||
|
byte[] data = new byte[bufferSize];
|
||||||
|
// @formatter:off
|
||||||
|
try (FileOutputStream os = new FileOutputStream(path);
|
||||||
|
BufferedOutputStream dest = new BufferedOutputStream(os, bufferSize)) {
|
||||||
|
// @formatter:off
|
||||||
|
while ((count = tar.read(data, 0, bufferSize)) != -1) {
|
||||||
|
dest.write(data, 0, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
ml/src/main/resources/logback.xml
Normal file
13
ml/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -35,6 +35,11 @@
|
|||||||
<artifactId>java-driver-core</artifactId>
|
<artifactId>java-driver-core</artifactId>
|
||||||
<version>${datastax-cassandra.version}</version>
|
<version>${datastax-cassandra.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.datastax.oss</groupId>
|
||||||
|
<artifactId>java-driver-query-builder</artifactId>
|
||||||
|
<version>${datastax-cassandra.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.netty</groupId>
|
<groupId>io.netty</groupId>
|
||||||
|
@ -26,7 +26,7 @@ public class Application {
|
|||||||
|
|
||||||
KeyspaceRepository keyspaceRepository = new KeyspaceRepository(session);
|
KeyspaceRepository keyspaceRepository = new KeyspaceRepository(session);
|
||||||
|
|
||||||
keyspaceRepository.createKeyspace("testKeyspace", "SimpleStrategy", 1);
|
keyspaceRepository.createKeyspace("testKeyspace", 1);
|
||||||
keyspaceRepository.useKeyspace("testKeyspace");
|
keyspaceRepository.useKeyspace("testKeyspace");
|
||||||
|
|
||||||
VideoRepository videoRepository = new VideoRepository(session);
|
VideoRepository videoRepository = new VideoRepository(session);
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package com.baeldung.datastax.cassandra.repository;
|
package com.baeldung.datastax.cassandra.repository;
|
||||||
|
|
||||||
|
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||||
import com.datastax.oss.driver.api.core.CqlSession;
|
import com.datastax.oss.driver.api.core.CqlSession;
|
||||||
|
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.SchemaBuilder;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace;
|
||||||
|
|
||||||
public class KeyspaceRepository {
|
public class KeyspaceRepository {
|
||||||
private final CqlSession session;
|
private final CqlSession session;
|
||||||
|
|
||||||
@ -9,19 +13,15 @@ public class KeyspaceRepository {
|
|||||||
this.session = session;
|
this.session = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createKeyspace(String keyspaceName, String replicationStrategy, int numberOfReplicas) {
|
public void createKeyspace(String keyspaceName, int numberOfReplicas) {
|
||||||
StringBuilder sb = new StringBuilder("CREATE KEYSPACE IF NOT EXISTS ").append(keyspaceName)
|
CreateKeyspace createKeyspace = SchemaBuilder.createKeyspace(keyspaceName)
|
||||||
.append(" WITH replication = {")
|
.ifNotExists()
|
||||||
.append("'class':'").append(replicationStrategy)
|
.withSimpleStrategy(numberOfReplicas);
|
||||||
.append("','replication_factor':").append(numberOfReplicas)
|
|
||||||
.append("};");
|
|
||||||
|
|
||||||
final String query = sb.toString();
|
session.execute(createKeyspace.build());
|
||||||
|
|
||||||
session.execute(query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void useKeyspace(String keyspace) {
|
public void useKeyspace(String keyspace) {
|
||||||
session.execute("USE " + keyspace);
|
session.execute("USE " + CqlIdentifier.fromCql(keyspace));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,12 @@ import com.datastax.oss.driver.api.core.cql.BoundStatement;
|
|||||||
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
|
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
|
||||||
import com.datastax.oss.driver.api.core.cql.ResultSet;
|
import com.datastax.oss.driver.api.core.cql.ResultSet;
|
||||||
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
|
||||||
|
import com.datastax.oss.driver.api.core.type.DataTypes;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.SchemaBuilder;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.insert.RegularInsert;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.schema.CreateTable;
|
||||||
|
import com.datastax.oss.driver.api.querybuilder.select.Select;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -27,15 +33,12 @@ public class VideoRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createTable(String keyspace) {
|
public void createTable(String keyspace) {
|
||||||
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME).append(" (")
|
CreateTable createTable = SchemaBuilder.createTable(TABLE_NAME).ifNotExists()
|
||||||
.append("video_id UUID,")
|
.withPartitionKey("video_id", DataTypes.UUID)
|
||||||
.append("title TEXT,")
|
.withColumn("title", DataTypes.TEXT)
|
||||||
.append("creation_date TIMESTAMP,")
|
.withColumn("creation_date", DataTypes.TIMESTAMP);
|
||||||
.append("PRIMARY KEY(video_id));");
|
|
||||||
|
|
||||||
String query = sb.toString();
|
executeStatement(createTable.build(), keyspace);
|
||||||
|
|
||||||
executeStatement(SimpleStatement.newInstance(query), keyspace);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public UUID insertVideo(Video video) {
|
public UUID insertVideo(Video video) {
|
||||||
@ -47,17 +50,23 @@ public class VideoRepository {
|
|||||||
|
|
||||||
video.setId(videoId);
|
video.setId(videoId);
|
||||||
|
|
||||||
String absoluteTableName = keyspace != null ? keyspace + "." + TABLE_NAME: TABLE_NAME;
|
RegularInsert insertInto = QueryBuilder.insertInto(TABLE_NAME)
|
||||||
|
.value("video_id", QueryBuilder.bindMarker())
|
||||||
|
.value("title", QueryBuilder.bindMarker())
|
||||||
|
.value("creation_date", QueryBuilder.bindMarker());
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder("INSERT INTO ").append(absoluteTableName)
|
SimpleStatement insertStatement = insertInto.build();
|
||||||
.append("(video_id, title, creation_date) values (:video_id, :title, :creation_date)");
|
|
||||||
|
|
||||||
PreparedStatement preparedStatement = session.prepare(sb.toString());
|
if (keyspace != null) {
|
||||||
|
insertStatement = insertStatement.setKeyspace(keyspace);
|
||||||
|
}
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = session.prepare(insertStatement);
|
||||||
|
|
||||||
BoundStatement statement = preparedStatement.bind()
|
BoundStatement statement = preparedStatement.bind()
|
||||||
.setUuid("video_id", video.getId())
|
.setUuid(0, video.getId())
|
||||||
.setString("title", video.getTitle())
|
.setString(1, video.getTitle())
|
||||||
.setInstant("creation_date", video.getCreationDate());
|
.setInstant(2, video.getCreationDate());
|
||||||
|
|
||||||
session.execute(statement);
|
session.execute(statement);
|
||||||
|
|
||||||
@ -69,11 +78,9 @@ public class VideoRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Video> selectAll(String keyspace) {
|
public List<Video> selectAll(String keyspace) {
|
||||||
StringBuilder sb = new StringBuilder("SELECT * FROM ").append(TABLE_NAME);
|
Select select = QueryBuilder.selectFrom(TABLE_NAME).all();
|
||||||
|
|
||||||
String query = sb.toString();
|
ResultSet resultSet = executeStatement(select.build(), keyspace);
|
||||||
|
|
||||||
ResultSet resultSet = executeStatement(SimpleStatement.newInstance(query), keyspace);
|
|
||||||
|
|
||||||
List<Video> result = new ArrayList<>();
|
List<Video> result = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -60,5 +60,6 @@
|
|||||||
<module>spring-persistence-simple</module>
|
<module>spring-persistence-simple</module>
|
||||||
<module>jpa-hibernate-cascade-type</module>
|
<module>jpa-hibernate-cascade-type</module>
|
||||||
<module>r2dbc</module>
|
<module>r2dbc</module>
|
||||||
|
<module>spring-boot-jdbi</module>
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
|
31
persistence-modules/spring-boot-jdbi/.gitignore
vendored
Normal file
31
persistence-modules/spring-boot-jdbi/.gitignore
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**
|
||||||
|
!**/src/test/**
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
114
persistence-modules/spring-boot-jdbi/.mvn/wrapper/MavenWrapperDownloader.java
vendored
Normal file
114
persistence-modules/spring-boot-jdbi/.mvn/wrapper/MavenWrapperDownloader.java
vendored
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
or more contributor license agreements. See the NOTICE file
|
||||||
|
distributed with this work for additional information
|
||||||
|
regarding copyright ownership. The ASF licenses this file
|
||||||
|
to you under the Apache License, Version 2.0 (the
|
||||||
|
"License"); you may not use this file except in compliance
|
||||||
|
with the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
|
https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing,
|
||||||
|
software distributed under the License is distributed on an
|
||||||
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
KIND, either express or implied. See the License for the
|
||||||
|
specific language governing permissions and limitations
|
||||||
|
under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.channels.Channels;
|
||||||
|
import java.nio.channels.ReadableByteChannel;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class MavenWrapperDownloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
|
||||||
|
*/
|
||||||
|
private static final String DEFAULT_DOWNLOAD_URL =
|
||||||
|
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
|
||||||
|
* use instead of the default one.
|
||||||
|
*/
|
||||||
|
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
|
||||||
|
".mvn/wrapper/maven-wrapper.properties";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Path where the maven-wrapper.jar will be saved to.
|
||||||
|
*/
|
||||||
|
private static final String MAVEN_WRAPPER_JAR_PATH =
|
||||||
|
".mvn/wrapper/maven-wrapper.jar";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the property which should be used to override the default download url for the wrapper.
|
||||||
|
*/
|
||||||
|
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
System.out.println("- Downloader started");
|
||||||
|
File baseDirectory = new File(args[0]);
|
||||||
|
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
|
||||||
|
|
||||||
|
// If the maven-wrapper.properties exists, read it and check if it contains a custom
|
||||||
|
// wrapperUrl parameter.
|
||||||
|
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
|
||||||
|
String url = DEFAULT_DOWNLOAD_URL;
|
||||||
|
if(mavenWrapperPropertyFile.exists()) {
|
||||||
|
FileInputStream mavenWrapperPropertyFileInputStream = null;
|
||||||
|
try {
|
||||||
|
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
|
||||||
|
Properties mavenWrapperProperties = new Properties();
|
||||||
|
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
|
||||||
|
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if(mavenWrapperPropertyFileInputStream != null) {
|
||||||
|
mavenWrapperPropertyFileInputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Ignore ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("- Downloading from: : " + url);
|
||||||
|
|
||||||
|
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
|
||||||
|
if(!outputFile.getParentFile().exists()) {
|
||||||
|
if(!outputFile.getParentFile().mkdirs()) {
|
||||||
|
System.out.println(
|
||||||
|
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
|
||||||
|
try {
|
||||||
|
downloadFileFromURL(url, outputFile);
|
||||||
|
System.out.println("Done");
|
||||||
|
System.exit(0);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
System.out.println("- Error downloading");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
|
||||||
|
URL website = new URL(urlString);
|
||||||
|
ReadableByteChannel rbc;
|
||||||
|
rbc = Channels.newChannel(website.openStream());
|
||||||
|
FileOutputStream fos = new FileOutputStream(destination);
|
||||||
|
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||||
|
fos.close();
|
||||||
|
rbc.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
persistence-modules/spring-boot-jdbi/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
BIN
persistence-modules/spring-boot-jdbi/.mvn/wrapper/maven-wrapper.jar
vendored
Normal file
Binary file not shown.
1
persistence-modules/spring-boot-jdbi/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
1
persistence-modules/spring-boot-jdbi/.mvn/wrapper/maven-wrapper.properties
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
|
286
persistence-modules/spring-boot-jdbi/mvnw
vendored
Normal file
286
persistence-modules/spring-boot-jdbi/mvnw
vendored
Normal file
@ -0,0 +1,286 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
# or more contributor license agreements. See the NOTICE file
|
||||||
|
# distributed with this work for additional information
|
||||||
|
# regarding copyright ownership. The ASF licenses this file
|
||||||
|
# to you under the Apache License, Version 2.0 (the
|
||||||
|
# "License"); you may not use this file except in compliance
|
||||||
|
# with the License. You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing,
|
||||||
|
# software distributed under the License is distributed on an
|
||||||
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
# KIND, either express or implied. See the License for the
|
||||||
|
# specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Maven2 Start Up Batch script
|
||||||
|
#
|
||||||
|
# Required ENV vars:
|
||||||
|
# ------------------
|
||||||
|
# JAVA_HOME - location of a JDK home dir
|
||||||
|
#
|
||||||
|
# Optional ENV vars
|
||||||
|
# -----------------
|
||||||
|
# M2_HOME - location of maven2's installed home dir
|
||||||
|
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||||
|
# e.g. to debug Maven itself, use
|
||||||
|
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||||
|
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
if [ -z "$MAVEN_SKIP_RC" ] ; then
|
||||||
|
|
||||||
|
if [ -f /etc/mavenrc ] ; then
|
||||||
|
. /etc/mavenrc
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$HOME/.mavenrc" ] ; then
|
||||||
|
. "$HOME/.mavenrc"
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
# OS specific support. $var _must_ be set to either true or false.
|
||||||
|
cygwin=false;
|
||||||
|
darwin=false;
|
||||||
|
mingw=false
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN*) cygwin=true ;;
|
||||||
|
MINGW*) mingw=true;;
|
||||||
|
Darwin*) darwin=true
|
||||||
|
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
|
||||||
|
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
|
||||||
|
if [ -z "$JAVA_HOME" ]; then
|
||||||
|
if [ -x "/usr/libexec/java_home" ]; then
|
||||||
|
export JAVA_HOME="`/usr/libexec/java_home`"
|
||||||
|
else
|
||||||
|
export JAVA_HOME="/Library/Java/Home"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [ -z "$JAVA_HOME" ] ; then
|
||||||
|
if [ -r /etc/gentoo-release ] ; then
|
||||||
|
JAVA_HOME=`java-config --jre-home`
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$M2_HOME" ] ; then
|
||||||
|
## resolve links - $0 may be a link to maven's home
|
||||||
|
PRG="$0"
|
||||||
|
|
||||||
|
# need this for relative symlinks
|
||||||
|
while [ -h "$PRG" ] ; do
|
||||||
|
ls=`ls -ld "$PRG"`
|
||||||
|
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||||
|
if expr "$link" : '/.*' > /dev/null; then
|
||||||
|
PRG="$link"
|
||||||
|
else
|
||||||
|
PRG="`dirname "$PRG"`/$link"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
saveddir=`pwd`
|
||||||
|
|
||||||
|
M2_HOME=`dirname "$PRG"`/..
|
||||||
|
|
||||||
|
# make it fully qualified
|
||||||
|
M2_HOME=`cd "$M2_HOME" && pwd`
|
||||||
|
|
||||||
|
cd "$saveddir"
|
||||||
|
# echo Using m2 at $M2_HOME
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Cygwin, ensure paths are in UNIX format before anything is touched
|
||||||
|
if $cygwin ; then
|
||||||
|
[ -n "$M2_HOME" ] &&
|
||||||
|
M2_HOME=`cygpath --unix "$M2_HOME"`
|
||||||
|
[ -n "$JAVA_HOME" ] &&
|
||||||
|
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||||
|
[ -n "$CLASSPATH" ] &&
|
||||||
|
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
# For Mingw, ensure paths are in UNIX format before anything is touched
|
||||||
|
if $mingw ; then
|
||||||
|
[ -n "$M2_HOME" ] &&
|
||||||
|
M2_HOME="`(cd "$M2_HOME"; pwd)`"
|
||||||
|
[ -n "$JAVA_HOME" ] &&
|
||||||
|
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
|
||||||
|
# TODO classpath?
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$JAVA_HOME" ]; then
|
||||||
|
javaExecutable="`which javac`"
|
||||||
|
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
|
||||||
|
# readlink(1) is not available as standard on Solaris 10.
|
||||||
|
readLink=`which readlink`
|
||||||
|
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
|
||||||
|
if $darwin ; then
|
||||||
|
javaHome="`dirname \"$javaExecutable\"`"
|
||||||
|
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
|
||||||
|
else
|
||||||
|
javaExecutable="`readlink -f \"$javaExecutable\"`"
|
||||||
|
fi
|
||||||
|
javaHome="`dirname \"$javaExecutable\"`"
|
||||||
|
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
|
||||||
|
JAVA_HOME="$javaHome"
|
||||||
|
export JAVA_HOME
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$JAVACMD" ] ; then
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="`which java`"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
echo "Error: JAVA_HOME is not defined correctly." >&2
|
||||||
|
echo " We cannot execute $JAVACMD" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$JAVA_HOME" ] ; then
|
||||||
|
echo "Warning: JAVA_HOME environment variable is not set."
|
||||||
|
fi
|
||||||
|
|
||||||
|
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
|
||||||
|
|
||||||
|
# traverses directory structure from process work directory to filesystem root
|
||||||
|
# first directory with .mvn subdirectory is considered project base directory
|
||||||
|
find_maven_basedir() {
|
||||||
|
|
||||||
|
if [ -z "$1" ]
|
||||||
|
then
|
||||||
|
echo "Path not specified to find_maven_basedir"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
basedir="$1"
|
||||||
|
wdir="$1"
|
||||||
|
while [ "$wdir" != '/' ] ; do
|
||||||
|
if [ -d "$wdir"/.mvn ] ; then
|
||||||
|
basedir=$wdir
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
|
||||||
|
if [ -d "${wdir}" ]; then
|
||||||
|
wdir=`cd "$wdir/.."; pwd`
|
||||||
|
fi
|
||||||
|
# end of workaround
|
||||||
|
done
|
||||||
|
echo "${basedir}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# concatenates all lines of a file
|
||||||
|
concat_lines() {
|
||||||
|
if [ -f "$1" ]; then
|
||||||
|
echo "$(tr -s '\n' ' ' < "$1")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
BASE_DIR=`find_maven_basedir "$(pwd)"`
|
||||||
|
if [ -z "$BASE_DIR" ]; then
|
||||||
|
exit 1;
|
||||||
|
fi
|
||||||
|
|
||||||
|
##########################################################################################
|
||||||
|
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||||
|
# This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||||
|
##########################################################################################
|
||||||
|
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Found .mvn/wrapper/maven-wrapper.jar"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
|
||||||
|
fi
|
||||||
|
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"
|
||||||
|
while IFS="=" read key value; do
|
||||||
|
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
|
||||||
|
esac
|
||||||
|
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Downloading from: $jarUrl"
|
||||||
|
fi
|
||||||
|
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
|
||||||
|
|
||||||
|
if command -v wget > /dev/null; then
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Found wget ... using wget"
|
||||||
|
fi
|
||||||
|
wget "$jarUrl" -O "$wrapperJarPath"
|
||||||
|
elif command -v curl > /dev/null; then
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Found curl ... using curl"
|
||||||
|
fi
|
||||||
|
curl -o "$wrapperJarPath" "$jarUrl"
|
||||||
|
else
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo "Falling back to using Java to download"
|
||||||
|
fi
|
||||||
|
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
|
||||||
|
if [ -e "$javaClass" ]; then
|
||||||
|
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo " - Compiling MavenWrapperDownloader.java ..."
|
||||||
|
fi
|
||||||
|
# Compiling the Java class
|
||||||
|
("$JAVA_HOME/bin/javac" "$javaClass")
|
||||||
|
fi
|
||||||
|
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
|
||||||
|
# Running the downloader
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo " - Running MavenWrapperDownloader.java ..."
|
||||||
|
fi
|
||||||
|
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
##########################################################################################
|
||||||
|
# End of extension
|
||||||
|
##########################################################################################
|
||||||
|
|
||||||
|
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
|
||||||
|
if [ "$MVNW_VERBOSE" = true ]; then
|
||||||
|
echo $MAVEN_PROJECTBASEDIR
|
||||||
|
fi
|
||||||
|
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
|
||||||
|
|
||||||
|
# For Cygwin, switch paths to Windows format before running java
|
||||||
|
if $cygwin; then
|
||||||
|
[ -n "$M2_HOME" ] &&
|
||||||
|
M2_HOME=`cygpath --path --windows "$M2_HOME"`
|
||||||
|
[ -n "$JAVA_HOME" ] &&
|
||||||
|
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
|
||||||
|
[ -n "$CLASSPATH" ] &&
|
||||||
|
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
|
||||||
|
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
|
||||||
|
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||||
|
|
||||||
|
exec "$JAVACMD" \
|
||||||
|
$MAVEN_OPTS \
|
||||||
|
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
|
||||||
|
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
|
||||||
|
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
|
161
persistence-modules/spring-boot-jdbi/mvnw.cmd
vendored
Normal file
161
persistence-modules/spring-boot-jdbi/mvnw.cmd
vendored
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
@REM or more contributor license agreements. See the NOTICE file
|
||||||
|
@REM distributed with this work for additional information
|
||||||
|
@REM regarding copyright ownership. The ASF licenses this file
|
||||||
|
@REM to you under the Apache License, Version 2.0 (the
|
||||||
|
@REM "License"); you may not use this file except in compliance
|
||||||
|
@REM with the License. You may obtain a copy of the License at
|
||||||
|
@REM
|
||||||
|
@REM https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@REM
|
||||||
|
@REM Unless required by applicable law or agreed to in writing,
|
||||||
|
@REM software distributed under the License is distributed on an
|
||||||
|
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
@REM KIND, either express or implied. See the License for the
|
||||||
|
@REM specific language governing permissions and limitations
|
||||||
|
@REM under the License.
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
@REM Maven2 Start Up Batch script
|
||||||
|
@REM
|
||||||
|
@REM Required ENV vars:
|
||||||
|
@REM JAVA_HOME - location of a JDK home dir
|
||||||
|
@REM
|
||||||
|
@REM Optional ENV vars
|
||||||
|
@REM M2_HOME - location of maven2's installed home dir
|
||||||
|
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
|
||||||
|
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
|
||||||
|
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||||
|
@REM e.g. to debug Maven itself, use
|
||||||
|
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||||
|
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
|
||||||
|
@echo off
|
||||||
|
@REM set title of command window
|
||||||
|
title %0
|
||||||
|
@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
|
||||||
|
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
|
||||||
|
|
||||||
|
@REM set %HOME% to equivalent of $HOME
|
||||||
|
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
|
||||||
|
|
||||||
|
@REM Execute a user defined script before this one
|
||||||
|
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
|
||||||
|
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
|
||||||
|
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
|
||||||
|
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
|
||||||
|
:skipRcPre
|
||||||
|
|
||||||
|
@setlocal
|
||||||
|
|
||||||
|
set ERROR_CODE=0
|
||||||
|
|
||||||
|
@REM To isolate internal variables from possible post scripts, we use another setlocal
|
||||||
|
@setlocal
|
||||||
|
|
||||||
|
@REM ==== START VALIDATION ====
|
||||||
|
if not "%JAVA_HOME%" == "" goto OkJHome
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo Error: JAVA_HOME not found in your environment. >&2
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||||
|
echo location of your Java installation. >&2
|
||||||
|
echo.
|
||||||
|
goto error
|
||||||
|
|
||||||
|
:OkJHome
|
||||||
|
if exist "%JAVA_HOME%\bin\java.exe" goto init
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo Error: JAVA_HOME is set to an invalid directory. >&2
|
||||||
|
echo JAVA_HOME = "%JAVA_HOME%" >&2
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||||
|
echo location of your Java installation. >&2
|
||||||
|
echo.
|
||||||
|
goto error
|
||||||
|
|
||||||
|
@REM ==== END VALIDATION ====
|
||||||
|
|
||||||
|
:init
|
||||||
|
|
||||||
|
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
|
||||||
|
@REM Fallback to current working directory if not found.
|
||||||
|
|
||||||
|
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
|
||||||
|
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
|
||||||
|
|
||||||
|
set EXEC_DIR=%CD%
|
||||||
|
set WDIR=%EXEC_DIR%
|
||||||
|
:findBaseDir
|
||||||
|
IF EXIST "%WDIR%"\.mvn goto baseDirFound
|
||||||
|
cd ..
|
||||||
|
IF "%WDIR%"=="%CD%" goto baseDirNotFound
|
||||||
|
set WDIR=%CD%
|
||||||
|
goto findBaseDir
|
||||||
|
|
||||||
|
:baseDirFound
|
||||||
|
set MAVEN_PROJECTBASEDIR=%WDIR%
|
||||||
|
cd "%EXEC_DIR%"
|
||||||
|
goto endDetectBaseDir
|
||||||
|
|
||||||
|
:baseDirNotFound
|
||||||
|
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
|
||||||
|
cd "%EXEC_DIR%"
|
||||||
|
|
||||||
|
:endDetectBaseDir
|
||||||
|
|
||||||
|
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
|
||||||
|
|
||||||
|
@setlocal EnableExtensions EnableDelayedExpansion
|
||||||
|
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
|
||||||
|
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
|
||||||
|
|
||||||
|
:endReadAdditionalConfig
|
||||||
|
|
||||||
|
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
|
||||||
|
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
|
||||||
|
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||||
|
|
||||||
|
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"
|
||||||
|
FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO (
|
||||||
|
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
|
||||||
|
)
|
||||||
|
|
||||||
|
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
|
||||||
|
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
|
||||||
|
if exist %WRAPPER_JAR% (
|
||||||
|
echo Found %WRAPPER_JAR%
|
||||||
|
) else (
|
||||||
|
echo Couldn't find %WRAPPER_JAR%, downloading it ...
|
||||||
|
echo Downloading from: %DOWNLOAD_URL%
|
||||||
|
powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"
|
||||||
|
echo Finished downloading %WRAPPER_JAR%
|
||||||
|
)
|
||||||
|
@REM End of extension
|
||||||
|
|
||||||
|
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
|
||||||
|
if ERRORLEVEL 1 goto error
|
||||||
|
goto end
|
||||||
|
|
||||||
|
:error
|
||||||
|
set ERROR_CODE=1
|
||||||
|
|
||||||
|
:end
|
||||||
|
@endlocal & set ERROR_CODE=%ERROR_CODE%
|
||||||
|
|
||||||
|
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
|
||||||
|
@REM check for post script, once with legacy .bat ending and once with .cmd ending
|
||||||
|
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
|
||||||
|
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
|
||||||
|
:skipRcPost
|
||||||
|
|
||||||
|
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
|
||||||
|
if "%MAVEN_BATCH_PAUSE%" == "on" pause
|
||||||
|
|
||||||
|
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
|
||||||
|
|
||||||
|
exit /B %ERROR_CODE%
|
107
persistence-modules/spring-boot-jdbi/pom.xml
Normal file
107
persistence-modules/spring-boot-jdbi/pom.xml
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.boot.jdbi</groupId>
|
||||||
|
<artifactId>spring-boot-jdbi</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>spring-boot-jdbi</name>
|
||||||
|
<description>Sample SpringBoot JDBI Project</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<jdbi.version>3.9.1</jdbi.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>2.1.7.RELEASE</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jdbi</groupId>
|
||||||
|
<artifactId>jdbi3-spring4</artifactId>
|
||||||
|
<version>${jdbi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jdbi</groupId>
|
||||||
|
<artifactId>jdbi3-sqlobject</artifactId>
|
||||||
|
<version>${jdbi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jdbi</groupId>
|
||||||
|
<artifactId>jdbi3-spring4</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jdbi</groupId>
|
||||||
|
<artifactId>jdbi3-sqlobject</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.baeldung.boot.jdbi;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
import org.jdbi.v3.core.mapper.RowMapper;
|
||||||
|
import org.jdbi.v3.core.spi.JdbiPlugin;
|
||||||
|
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarMakerDao;
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarModelDao;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
|
public class JdbiConfiguration {
|
||||||
|
@Bean
|
||||||
|
public Jdbi jdbi(DataSource ds,List<JdbiPlugin> jdbiPlugins, List<RowMapper<?>> rowMappers) {
|
||||||
|
TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds);
|
||||||
|
Jdbi jdbi = Jdbi.create(proxy);
|
||||||
|
|
||||||
|
// Register all available plugins
|
||||||
|
log.info("[I27] Installing plugins... ({} found)", jdbiPlugins.size());
|
||||||
|
jdbiPlugins.forEach(plugin -> jdbi.installPlugin(plugin));
|
||||||
|
|
||||||
|
// Register all available rowMappers
|
||||||
|
log.info("[I31] Installing rowMappers... ({} found)", rowMappers.size());
|
||||||
|
rowMappers.forEach(mapper -> jdbi.registerRowMapper(mapper));
|
||||||
|
|
||||||
|
return jdbi;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JdbiPlugin sqlObjectPlugin() {
|
||||||
|
return new SqlObjectPlugin();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CarMakerDao carMakerDao(Jdbi jdbi) {
|
||||||
|
return jdbi.onDemand(CarMakerDao.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CarModelDao carModelDao(Jdbi jdbi) {
|
||||||
|
return jdbi.onDemand(CarModelDao.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.boot.jdbi;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableTransactionManagement
|
||||||
|
public class SpringBootJdbiApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootJdbiApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.boot.jdbi.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jdbi.v3.sqlobject.customizer.Bind;
|
||||||
|
import org.jdbi.v3.sqlobject.customizer.BindBean;
|
||||||
|
import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlBatch;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlQuery;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarMaker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Philippe
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@UseClasspathSqlLocator
|
||||||
|
public interface CarMakerDao {
|
||||||
|
|
||||||
|
@SqlUpdate
|
||||||
|
@GetGeneratedKeys
|
||||||
|
Long insert(@BindBean CarMaker carMaker);
|
||||||
|
|
||||||
|
@SqlBatch("insert")
|
||||||
|
@GetGeneratedKeys
|
||||||
|
List<Long> bulkInsert(@BindBean List<CarMaker> carMakers);
|
||||||
|
|
||||||
|
@SqlQuery
|
||||||
|
CarMaker findById(@Bind("id") Long id);
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.boot.jdbi.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jdbi.v3.sqlobject.customizer.Bind;
|
||||||
|
import org.jdbi.v3.sqlobject.customizer.BindBean;
|
||||||
|
import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlBatch;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlQuery;
|
||||||
|
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarModel;
|
||||||
|
|
||||||
|
@UseClasspathSqlLocator
|
||||||
|
public interface CarModelDao {
|
||||||
|
|
||||||
|
@SqlUpdate("insert")
|
||||||
|
@GetGeneratedKeys
|
||||||
|
Long insert(@BindBean CarModel carModel);
|
||||||
|
|
||||||
|
@SqlBatch("insert")
|
||||||
|
@GetGeneratedKeys
|
||||||
|
List<Long> bulkInsert(@BindBean List<CarModel> models);
|
||||||
|
|
||||||
|
@SqlQuery
|
||||||
|
CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku );
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.boot.jdbi.domain;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class CarMaker {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private List<CarModel> models;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.boot.jdbi.domain;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Builder
|
||||||
|
@Data
|
||||||
|
public class CarModel {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private Integer year;
|
||||||
|
private String sku;
|
||||||
|
private Long makerId;
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.boot.jdbi.mapper;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import org.jdbi.v3.core.mapper.RowMapper;
|
||||||
|
import org.jdbi.v3.core.statement.StatementContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarMaker;
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarModel;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CarMakerMapper implements RowMapper<CarMaker> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CarMaker map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
|
CarMaker maker = CarMaker.builder()
|
||||||
|
.id(rs.getLong("id"))
|
||||||
|
.name(rs.getString("name"))
|
||||||
|
.models(new ArrayList<CarModel>())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return maker;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.boot.jdbi.mapper;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.jdbi.v3.core.mapper.RowMapper;
|
||||||
|
import org.jdbi.v3.core.statement.StatementContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarModel;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CarModelMapper implements RowMapper<CarModel>{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CarModel map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
|
return CarModel.builder()
|
||||||
|
.id(rs.getLong("id"))
|
||||||
|
.name(rs.getString("name"))
|
||||||
|
.sku(rs.getString("sku"))
|
||||||
|
.year(rs.getInt("year"))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.baeldung.boot.jdbi.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarMakerDao;
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarModelDao;
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarMaker;
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Philippe
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class CarMakerService {
|
||||||
|
|
||||||
|
private CarMakerDao carMakerDao;
|
||||||
|
private CarModelDao carModelDao;
|
||||||
|
|
||||||
|
public CarMakerService(CarMakerDao carMakerDao,CarModelDao carModelDao) {
|
||||||
|
|
||||||
|
this.carMakerDao = carMakerDao;
|
||||||
|
this.carModelDao = carModelDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public int bulkInsert(CarMaker carMaker) {
|
||||||
|
Long carMakerId;
|
||||||
|
if (carMaker.getId() == null ) {
|
||||||
|
carMakerId = carMakerDao.insert(carMaker);
|
||||||
|
carMaker.setId(carMakerId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure all models belong to the same maker
|
||||||
|
carMaker.getModels().forEach(m -> {
|
||||||
|
m.setMakerId(carMaker.getId());
|
||||||
|
carModelDao.insert(m);
|
||||||
|
});
|
||||||
|
|
||||||
|
return carMaker.getModels().size();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
|
@ -0,0 +1,11 @@
|
|||||||
|
--
|
||||||
|
-- findById
|
||||||
|
--
|
||||||
|
select
|
||||||
|
id,
|
||||||
|
name
|
||||||
|
from
|
||||||
|
car_maker
|
||||||
|
where
|
||||||
|
id = :id
|
||||||
|
;
|
@ -0,0 +1,4 @@
|
|||||||
|
--
|
||||||
|
-- Insert
|
||||||
|
--
|
||||||
|
insert into car_maker(id,name) values (:id,:name);
|
@ -0,0 +1,10 @@
|
|||||||
|
--
|
||||||
|
-- Insert
|
||||||
|
--
|
||||||
|
select *
|
||||||
|
from
|
||||||
|
car_model
|
||||||
|
where
|
||||||
|
maker_fk = :makerId and
|
||||||
|
sku = :sku
|
||||||
|
;
|
@ -0,0 +1,8 @@
|
|||||||
|
--
|
||||||
|
-- Insert
|
||||||
|
--
|
||||||
|
insert into car_model(maker_fk,name,sku,year) values (
|
||||||
|
:makerId,
|
||||||
|
:name,
|
||||||
|
:sku,
|
||||||
|
:year );
|
@ -0,0 +1,121 @@
|
|||||||
|
package com.baeldung.boot.jdbi;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarMakerDao;
|
||||||
|
import com.baeldung.boot.jdbi.dao.CarModelDao;
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarMaker;
|
||||||
|
import com.baeldung.boot.jdbi.domain.CarModel;
|
||||||
|
import com.baeldung.boot.jdbi.service.CarMakerService;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
@Slf4j
|
||||||
|
public class SpringBootJdbiApplicationUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CarMakerDao carMakerDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CarModelDao carModelDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CarMakerService carMakerService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNewCarMaker_whenInsertNewCarMaker_thenSuccess() {
|
||||||
|
|
||||||
|
assertNotNull(carMakerDao);
|
||||||
|
|
||||||
|
CarMaker carMaker = CarMaker.builder()
|
||||||
|
.name("Diamond Motors")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Long generatedId = carMakerDao.insert(carMaker);
|
||||||
|
log.info("[I37] generatedId = {}", generatedId);
|
||||||
|
assertThat(generatedId).isGreaterThan(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNewCarMakers_whenInsertNewCarMakers_thenSuccess() {
|
||||||
|
|
||||||
|
assertNotNull(carMakerDao);
|
||||||
|
|
||||||
|
CarMaker carMaker1 = CarMaker.builder()
|
||||||
|
.name("maker1")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CarMaker carMaker2 = CarMaker.builder()
|
||||||
|
.name("maker2")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<CarMaker> makers = new ArrayList<>();
|
||||||
|
makers.add(carMaker1);
|
||||||
|
makers.add(carMaker2);
|
||||||
|
|
||||||
|
List<Long> generatedIds = carMakerDao.bulkInsert(makers);
|
||||||
|
log.info("[I37] generatedIds = {}", generatedIds);
|
||||||
|
assertThat(generatedIds).size().isEqualTo(makers.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExistingCarMaker_whenFindById_thenReturnExistingCarMaker() {
|
||||||
|
|
||||||
|
CarMaker maker = carMakerDao.findById(1l);
|
||||||
|
assertThat(maker).isNotNull();
|
||||||
|
assertThat(maker.getId()).isEqualTo(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExistingCarMaker_whenBulkInsertFails_thenRollback() {
|
||||||
|
|
||||||
|
CarMaker maker = carMakerDao.findById(1l);
|
||||||
|
CarModel m1 = CarModel.builder()
|
||||||
|
.makerId(maker.getId())
|
||||||
|
.name("Model X1")
|
||||||
|
.sku("1-M1")
|
||||||
|
.year(2019)
|
||||||
|
.build();
|
||||||
|
maker.getModels().add(m1);
|
||||||
|
|
||||||
|
CarModel m2 = CarModel.builder()
|
||||||
|
.makerId(maker.getId())
|
||||||
|
.name("Model X1")
|
||||||
|
.sku("1-M1")
|
||||||
|
.year(2019)
|
||||||
|
.build();
|
||||||
|
maker.getModels().add(m2);
|
||||||
|
|
||||||
|
// This insert fails because we have the same SKU
|
||||||
|
try {
|
||||||
|
carMakerService.bulkInsert(maker);
|
||||||
|
assertTrue("Insert must fail", true);
|
||||||
|
}
|
||||||
|
catch(Exception ex) {
|
||||||
|
log.info("[I113] Exception: {}", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
CarModel m = carModelDao.findByMakerIdAndSku(maker.getId(), "1-M1");
|
||||||
|
assertThat(m).isNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
insert into car_maker(id,name) values (1,'Special Motors');
|
||||||
|
insert into car_maker(id,name) values (2,'BWM');
|
||||||
|
insert into car_maker(id,name) values (3,'Dolores');
|
||||||
|
|
||||||
|
insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018);
|
||||||
|
insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008);
|
||||||
|
|
||||||
|
insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008);
|
||||||
|
insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009);
|
||||||
|
insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008);
|
||||||
|
|
@ -0,0 +1,24 @@
|
|||||||
|
--
|
||||||
|
-- Car makers table
|
||||||
|
--
|
||||||
|
create table car_maker(
|
||||||
|
id identity,
|
||||||
|
name varchar(128) not null
|
||||||
|
);
|
||||||
|
|
||||||
|
create unique index ui_car_maker_01 on car_maker(name);
|
||||||
|
|
||||||
|
--
|
||||||
|
-- Car models table
|
||||||
|
--
|
||||||
|
create table car_model(
|
||||||
|
id identity,
|
||||||
|
maker_fk int not null,
|
||||||
|
name varchar(128) not null,
|
||||||
|
sku varchar(128) not null,
|
||||||
|
year int not null
|
||||||
|
);
|
||||||
|
|
||||||
|
create unique index ui_car_model_01 on car_model(maker_fk,sku);
|
||||||
|
create unique index ui_car_model_02 on car_model(maker_fk,name,year);
|
||||||
|
|
@ -1,4 +1,5 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.baeldung</groupId>
|
<groupId>org.baeldung</groupId>
|
||||||
@ -59,13 +60,24 @@
|
|||||||
<version>${spring-framework.version}</version>
|
<version>${spring-framework.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>javax.el-api</artifactId>
|
||||||
|
<version>${javax.el.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish</groupId>
|
||||||
|
<artifactId>javax.el</artifactId>
|
||||||
|
<version>${javax.el.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring-framework.version>4.3.4.RELEASE</spring-framework.version>
|
<spring-framework.version>4.3.4.RELEASE</spring-framework.version>
|
||||||
<spring-data-couchbase.version>2.1.5.RELEASE</spring-data-couchbase.version>
|
<spring-data-couchbase.version>2.1.5.RELEASE</spring-data-couchbase.version>
|
||||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||||
<joda-time.version>2.9.6</joda-time.version>
|
<joda-time.version>2.9.6</joda-time.version>
|
||||||
|
<javax.el.version>3.0.0</javax.el.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -9,6 +9,45 @@ import org.springframework.test.context.TestExecutionListeners;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This LiveTest requires:
|
||||||
|
*
|
||||||
|
* 1- Couchbase instance running (e.g. with `docker run -d --name db -p 8091-8096:8091-8096 -p 11210-11211:11210-11211 couchbase`)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* 2- Couchbase configured with (we can use the console in localhost:8091):
|
||||||
|
*
|
||||||
|
* 2.1- Buckets: named 'baeldung' and 'baeldung2'
|
||||||
|
*
|
||||||
|
* 2.2- Security: users 'baeldung' and 'baeldung2'. Note: in newer versions an empty password is not allowed, then we have to change the passwords in the project)
|
||||||
|
*
|
||||||
|
* 2.3- Spacial View: Add new spacial view (in Index tab) in document 'campus_spatial', view 'byLocation' with the following function:
|
||||||
|
* {@code
|
||||||
|
* function (doc) {
|
||||||
|
* if (doc.location &&
|
||||||
|
* doc._class == "org.baeldung.spring.data.couchbase.model.Campus") {
|
||||||
|
* emit([doc.location.x, doc.location.y], null);
|
||||||
|
* }
|
||||||
|
* }}
|
||||||
|
*
|
||||||
|
* 2.4- MapReduce Views: Add new views in document 'campus':
|
||||||
|
* 2.4.1- view 'all' with function:
|
||||||
|
* {@code
|
||||||
|
* function (doc, meta) {
|
||||||
|
* if(doc._class == "org.baeldung.spring.data.couchbase.model.Campus") {
|
||||||
|
* emit(meta.id, null);
|
||||||
|
* }
|
||||||
|
* }}
|
||||||
|
*
|
||||||
|
* 2.4.2- view 'byName' with function:
|
||||||
|
* {@code
|
||||||
|
* function (doc, meta) {
|
||||||
|
* if(doc._class == "org.baeldung.spring.data.couchbase.model.Campus" &&
|
||||||
|
* doc.name) {
|
||||||
|
* emit(doc.name, null);
|
||||||
|
* }
|
||||||
|
* }}
|
||||||
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { MultiBucketCouchbaseConfig.class, MultiBucketIntegrationTestConfig.class })
|
@ContextConfiguration(classes = { MultiBucketCouchbaseConfig.class, MultiBucketIntegrationTestConfig.class })
|
||||||
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.contexttests.mongoconfig;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.baeldung.config.MongoConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Live test requires:
|
||||||
|
* * mongodb instance running on the environment
|
||||||
|
*
|
||||||
|
* (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = MongoConfig.class)
|
||||||
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.contexttests.mongoreactiveconfig;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.baeldung.config.MongoReactiveConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Live test requires:
|
||||||
|
* * mongodb instance running on the environment
|
||||||
|
*
|
||||||
|
* (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = MongoReactiveConfig.class)
|
||||||
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.contexttests.simplemongoconfig;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.baeldung.config.SimpleMongoConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Live test requires:
|
||||||
|
* * mongodb instance running on the environment
|
||||||
|
*
|
||||||
|
* (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = SimpleMongoConfig.class)
|
||||||
|
public class SpringContextLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
play-framework/introduction/.gitignore
vendored
1
play-framework/introduction/.gitignore
vendored
@ -1,6 +1,7 @@
|
|||||||
logs
|
logs
|
||||||
target
|
target
|
||||||
/.idea
|
/.idea
|
||||||
|
/.g8
|
||||||
/.idea_modules
|
/.idea_modules
|
||||||
/.classpath
|
/.classpath
|
||||||
/.project
|
/.project
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
This software is licensed under the Apache 2 license, quoted below.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with
|
|
||||||
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
|
||||||
language governing permissions and limitations under the License.
|
|
@ -1,49 +0,0 @@
|
|||||||
This is your new Play application
|
|
||||||
=================================
|
|
||||||
|
|
||||||
This file will be packaged with your application when using `activator dist`.
|
|
||||||
|
|
||||||
There are several demonstration files available in this template.
|
|
||||||
|
|
||||||
Controllers
|
|
||||||
===========
|
|
||||||
|
|
||||||
- HomeController.java:
|
|
||||||
|
|
||||||
Shows how to handle simple HTTP requests.
|
|
||||||
|
|
||||||
- AsyncController.java:
|
|
||||||
|
|
||||||
Shows how to do asynchronous programming when handling a request.
|
|
||||||
|
|
||||||
- CountController.java:
|
|
||||||
|
|
||||||
Shows how to inject a component into a controller and use the component when
|
|
||||||
handling requests.
|
|
||||||
|
|
||||||
Components
|
|
||||||
==========
|
|
||||||
|
|
||||||
- Module.java:
|
|
||||||
|
|
||||||
Shows how to use Guice to bind all the components needed by your application.
|
|
||||||
|
|
||||||
- Counter.java:
|
|
||||||
|
|
||||||
An example of a component that contains state, in this case a simple counter.
|
|
||||||
|
|
||||||
- ApplicationTimer.java:
|
|
||||||
|
|
||||||
An example of a component that starts when the application starts and stops
|
|
||||||
when the application stops.
|
|
||||||
|
|
||||||
Filters
|
|
||||||
=======
|
|
||||||
|
|
||||||
- Filters.java:
|
|
||||||
|
|
||||||
Creates the list of HTTP filters used by your application.
|
|
||||||
|
|
||||||
- ExampleFilter.java
|
|
||||||
|
|
||||||
A simple filter that adds a header to every response.
|
|
@ -1,46 +0,0 @@
|
|||||||
import javax.inject.*;
|
|
||||||
import play.*;
|
|
||||||
import play.mvc.EssentialFilter;
|
|
||||||
import play.http.HttpFilters;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import filters.ExampleFilter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class configures filters that run on every request. This
|
|
||||||
* class is queried by Play to get a list of filters.
|
|
||||||
*
|
|
||||||
* Play will automatically use filters from any class called
|
|
||||||
* <code>Filters</code> that is placed the root package. You can load filters
|
|
||||||
* from a different class by adding a `play.http.filters` setting to
|
|
||||||
* the <code>application.conf</code> configuration file.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class Filters implements HttpFilters {
|
|
||||||
|
|
||||||
private final Environment env;
|
|
||||||
private final EssentialFilter exampleFilter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param env Basic environment settings for the current application.
|
|
||||||
* @param exampleFilter A demonstration filter that adds a header to
|
|
||||||
*/
|
|
||||||
@Inject
|
|
||||||
public Filters(Environment env, ExampleFilter exampleFilter) {
|
|
||||||
this.env = env;
|
|
||||||
this.exampleFilter = exampleFilter;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EssentialFilter[] filters() {
|
|
||||||
// Use the example filter if we're running development mode. If
|
|
||||||
// we're running in production or test mode then don't use any
|
|
||||||
// filters at all.
|
|
||||||
if (env.mode().equals(Mode.DEV)) {
|
|
||||||
return new EssentialFilter[] { exampleFilter };
|
|
||||||
} else {
|
|
||||||
return new EssentialFilter[] {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
import com.google.inject.AbstractModule;
|
|
||||||
import java.time.Clock;
|
|
||||||
|
|
||||||
import services.ApplicationTimer;
|
|
||||||
import services.AtomicCounter;
|
|
||||||
import services.Counter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is a Guice module that tells Guice how to bind several
|
|
||||||
* different types. This Guice module is created when the Play
|
|
||||||
* application starts.
|
|
||||||
*
|
|
||||||
* Play will automatically use any class called `Module` that is in
|
|
||||||
* the root package. You can create modules in other locations by
|
|
||||||
* adding `play.modules.enabled` settings to the `application.conf`
|
|
||||||
* configuration file.
|
|
||||||
*/
|
|
||||||
public class Module extends AbstractModule {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configure() {
|
|
||||||
// Use the system clock as the default implementation of Clock
|
|
||||||
bind(Clock.class).toInstance(Clock.systemDefaultZone());
|
|
||||||
// Ask Guice to create an instance of ApplicationTimer when the
|
|
||||||
// application starts.
|
|
||||||
bind(ApplicationTimer.class).asEagerSingleton();
|
|
||||||
// Set AtomicCounter as the implementation for Counter.
|
|
||||||
bind(Counter.class).to(AtomicCounter.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package controllers;
|
|
||||||
|
|
||||||
import akka.actor.ActorSystem;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
|
|
||||||
import play.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import scala.concurrent.duration.Duration;
|
|
||||||
import scala.concurrent.ExecutionContextExecutor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This controller contains an action that demonstrates how to write
|
|
||||||
* simple asynchronous code in a controller. It uses a timer to
|
|
||||||
* asynchronously delay sending a response for 1 second.
|
|
||||||
*
|
|
||||||
* @param actorSystem We need the {@link ActorSystem}'s
|
|
||||||
* {@link Scheduler} to run code after a delay.
|
|
||||||
* @param exec We need a Java {@link Executor} to apply the result
|
|
||||||
* of the {@link CompletableFuture} and a Scala
|
|
||||||
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
|
|
||||||
* An {@link ExecutionContextExecutor} implements both interfaces.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class AsyncController extends Controller {
|
|
||||||
|
|
||||||
private final ActorSystem actorSystem;
|
|
||||||
private final ExecutionContextExecutor exec;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
|
|
||||||
this.actorSystem = actorSystem;
|
|
||||||
this.exec = exec;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An action that returns a plain text message after a delay
|
|
||||||
* of 1 second.
|
|
||||||
* <p>
|
|
||||||
* The configuration in the <code>routes</code> file means that this method
|
|
||||||
* will be called when the application receives a <code>GET</code> request with
|
|
||||||
* a path of <code>/message</code>.
|
|
||||||
*/
|
|
||||||
public CompletionStage<Result> message() {
|
|
||||||
return getFutureMessage(1, TimeUnit.SECONDS).thenApplyAsync(Results::ok, exec);
|
|
||||||
}
|
|
||||||
|
|
||||||
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
|
||||||
CompletableFuture<String> future = new CompletableFuture<>();
|
|
||||||
actorSystem.scheduler().scheduleOnce(
|
|
||||||
Duration.create(time, timeUnit),
|
|
||||||
() -> future.complete("Hi!"),
|
|
||||||
exec
|
|
||||||
);
|
|
||||||
return future;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package controllers;
|
|
||||||
|
|
||||||
import javax.inject.*;
|
|
||||||
import play.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import services.Counter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This controller demonstrates how to use dependency injection to
|
|
||||||
* bind a component into a controller class. The class contains an
|
|
||||||
* action that shows an incrementing count to users. The {@link Counter}
|
|
||||||
* object is injected by the Guice dependency injection system.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class CountController extends Controller {
|
|
||||||
|
|
||||||
private final Counter counter;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public CountController(Counter counter) {
|
|
||||||
this.counter = counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An action that responds with the {@link Counter}'s current
|
|
||||||
* count. The result is plain text. This action is mapped to
|
|
||||||
* <code>GET</code> requests with a path of <code>/count</code>
|
|
||||||
* requests by an entry in the <code>routes</code> config file.
|
|
||||||
*/
|
|
||||||
public Result count() {
|
|
||||||
return ok(Integer.toString(counter.nextCount()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +1,25 @@
|
|||||||
package controllers;
|
package controllers;
|
||||||
|
|
||||||
|
import play.libs.concurrent.HttpExecutionContext;
|
||||||
import play.mvc.*;
|
import play.mvc.*;
|
||||||
|
import play.twirl.api.Html;
|
||||||
|
|
||||||
import views.html.*;
|
import javax.inject.Inject;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
import static java.util.concurrent.CompletableFuture.supplyAsync;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This controller contains an action to handle HTTP requests
|
* This controller contains an action to handle HTTP requests
|
||||||
* to the application's home page.
|
* to the application's home page.
|
||||||
*/
|
*/
|
||||||
public class HomeController extends Controller {
|
public class HomeController extends Controller {
|
||||||
|
private HttpExecutionContext ec;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public HomeController(HttpExecutionContext ec) {
|
||||||
|
this.ec = ec;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An action that renders an HTML page with a welcome message.
|
* An action that renders an HTML page with a welcome message.
|
||||||
@ -17,7 +28,41 @@ public class HomeController extends Controller {
|
|||||||
* <code>GET</code> request with a path of <code>/</code>.
|
* <code>GET</code> request with a path of <code>/</code>.
|
||||||
*/
|
*/
|
||||||
public Result index() {
|
public Result index() {
|
||||||
return ok(index.render("Your new application is ready."));
|
return ok(views.html.index.render());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Result applyHtml() {
|
||||||
|
return ok(Html.apply("<h1>This text will appear as a heading 1</h1>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result badRequestPage() {
|
||||||
|
return badRequest("Your request data has issues.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result notFoundPage() {
|
||||||
|
return notFound("Could not find the page you requested.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result customContentType() {
|
||||||
|
return ok("This is some text content").as("text/html");
|
||||||
|
}
|
||||||
|
|
||||||
|
public CompletionStage<Result> asyncOperation() {
|
||||||
|
return supplyAsync(() -> {
|
||||||
|
return longRunningTask();
|
||||||
|
}, ec.current())
|
||||||
|
.thenApplyAsync(s -> {
|
||||||
|
return ok("Got result -> " + s);
|
||||||
|
}, ec.current());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String longRunningTask() {
|
||||||
|
return "Long running task has completed";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result setHeaders() {
|
||||||
|
return ok("This is some text content")
|
||||||
|
.as("text/html")
|
||||||
|
.withHeader("Header-Key", "Some value");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
package controllers;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import models.Student;
|
|
||||||
import models.StudentStore;
|
|
||||||
import play.libs.Json;
|
|
||||||
import play.mvc.Controller;
|
|
||||||
import play.mvc.Result;
|
|
||||||
import util.Util;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class StudentController extends Controller {
|
|
||||||
public Result create() {
|
|
||||||
JsonNode json = request().body().asJson();
|
|
||||||
if (json == null) {
|
|
||||||
return badRequest(Util.createResponse("Expecting Json data", false));
|
|
||||||
}
|
|
||||||
Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class));
|
|
||||||
JsonNode jsonObject = Json.toJson(student);
|
|
||||||
return created(Util.createResponse(jsonObject, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result update() {
|
|
||||||
JsonNode json = request().body().asJson();
|
|
||||||
if (json == null) {
|
|
||||||
return badRequest(Util.createResponse("Expecting Json data", false));
|
|
||||||
}
|
|
||||||
Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class));
|
|
||||||
if (student == null) {
|
|
||||||
return notFound(Util.createResponse("Student not found", false));
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode jsonObject = Json.toJson(student);
|
|
||||||
return ok(Util.createResponse(jsonObject, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result retrieve(int id) {
|
|
||||||
if (StudentStore.getInstance().getStudent(id) == null) {
|
|
||||||
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
|
|
||||||
}
|
|
||||||
JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id));
|
|
||||||
return ok(Util.createResponse(jsonObjects, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result listStudents() {
|
|
||||||
Set<Student> result = StudentStore.getInstance().getAllStudents();
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
|
|
||||||
JsonNode jsonData = mapper.convertValue(result, JsonNode.class);
|
|
||||||
return ok(Util.createResponse(jsonData, true));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result delete(int id) {
|
|
||||||
if (!StudentStore.getInstance().deleteStudent(id)) {
|
|
||||||
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
|
|
||||||
}
|
|
||||||
return ok(Util.createResponse("Student with id:" + id + " deleted", true));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package filters;
|
|
||||||
|
|
||||||
import akka.stream.Materializer;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import javax.inject.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
import play.mvc.Http.RequestHeader;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a simple filter that adds a header to all requests. It's
|
|
||||||
* added to the application's list of filters by the
|
|
||||||
* {@link Filters} class.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class ExampleFilter extends Filter {
|
|
||||||
|
|
||||||
private final Executor exec;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mat This object is needed to handle streaming of requests
|
|
||||||
* and responses.
|
|
||||||
* @param exec This class is needed to execute code asynchronously.
|
|
||||||
* It is used below by the <code>thenAsyncApply</code> method.
|
|
||||||
*/
|
|
||||||
@Inject
|
|
||||||
public ExampleFilter(Materializer mat, Executor exec) {
|
|
||||||
super(mat);
|
|
||||||
this.exec = exec;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletionStage<Result> apply(
|
|
||||||
Function<RequestHeader, CompletionStage<Result>> next,
|
|
||||||
RequestHeader requestHeader) {
|
|
||||||
|
|
||||||
return next.apply(requestHeader).thenApplyAsync(
|
|
||||||
result -> result.withHeader("X-ExampleFilter", "foo"),
|
|
||||||
exec
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package models;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class StudentStore {
|
|
||||||
private static StudentStore instance;
|
|
||||||
private Map<Integer, Student> students = new HashMap<>();
|
|
||||||
|
|
||||||
public static StudentStore getInstance() {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new StudentStore();
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Student addStudent(Student student) {
|
|
||||||
int id = students.size();
|
|
||||||
student.setId(id);
|
|
||||||
students.put(id, student);
|
|
||||||
return student;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Student getStudent(int id) {
|
|
||||||
return students.get(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<Student> getAllStudents() {
|
|
||||||
return new HashSet<>(students.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Student updateStudent(Student student) {
|
|
||||||
int id = student.getId();
|
|
||||||
if (students.containsKey(id)) {
|
|
||||||
students.put(id, student);
|
|
||||||
return student;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean deleteStudent(int id) {
|
|
||||||
return students.remove(id) != null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
package services;
|
|
||||||
|
|
||||||
import java.time.Clock;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import javax.inject.*;
|
|
||||||
import play.Logger;
|
|
||||||
import play.inject.ApplicationLifecycle;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class demonstrates how to run code when the
|
|
||||||
* application starts and stops. It starts a timer when the
|
|
||||||
* application starts. When the application stops it prints out how
|
|
||||||
* long the application was running for.
|
|
||||||
*
|
|
||||||
* This class is registered for Guice dependency injection in the
|
|
||||||
* {@link Module} class. We want the class to start when the application
|
|
||||||
* starts, so it is registered as an "eager singleton". See the code
|
|
||||||
* in the {@link Module} class to see how this happens.
|
|
||||||
*
|
|
||||||
* This class needs to run code when the server stops. It uses the
|
|
||||||
* application's {@link ApplicationLifecycle} to register a stop hook.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class ApplicationTimer {
|
|
||||||
|
|
||||||
private final Clock clock;
|
|
||||||
private final ApplicationLifecycle appLifecycle;
|
|
||||||
private final Instant start;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ApplicationTimer(Clock clock, ApplicationLifecycle appLifecycle) {
|
|
||||||
this.clock = clock;
|
|
||||||
this.appLifecycle = appLifecycle;
|
|
||||||
// This code is called when the application starts.
|
|
||||||
start = clock.instant();
|
|
||||||
Logger.info("ApplicationTimer demo: Starting application at " + start);
|
|
||||||
|
|
||||||
// When the application starts, register a stop hook with the
|
|
||||||
// ApplicationLifecycle object. The code inside the stop hook will
|
|
||||||
// be run when the application stops.
|
|
||||||
appLifecycle.addStopHook(() -> {
|
|
||||||
Instant stop = clock.instant();
|
|
||||||
Long runningTime = stop.getEpochSecond() - start.getEpochSecond();
|
|
||||||
Logger.info("ApplicationTimer demo: Stopping application at " + clock.instant() + " after " + runningTime + "s.");
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package services;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import javax.inject.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is a concrete implementation of the {@link Counter} trait.
|
|
||||||
* It is configured for Guice dependency injection in the {@link Module}
|
|
||||||
* class.
|
|
||||||
*
|
|
||||||
* This class has a {@link Singleton} annotation because we need to make
|
|
||||||
* sure we only use one counter per application. Without this
|
|
||||||
* annotation we would get a new instance every time a {@link Counter} is
|
|
||||||
* injected.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class AtomicCounter implements Counter {
|
|
||||||
|
|
||||||
private final AtomicInteger atomicCounter = new AtomicInteger();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int nextCount() {
|
|
||||||
return atomicCounter.getAndIncrement();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package services;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This interface demonstrates how to create a component that is injected
|
|
||||||
* into a controller. The interface represents a counter that returns a
|
|
||||||
* incremented number each time it is called.
|
|
||||||
*
|
|
||||||
* The {@link Modules} class binds this interface to the
|
|
||||||
* {@link AtomicCounter} implementation.
|
|
||||||
*/
|
|
||||||
public interface Counter {
|
|
||||||
int nextCount();
|
|
||||||
}
|
|
@ -1,20 +1,5 @@
|
|||||||
@*
|
@()
|
||||||
* This template takes a single argument, a String containing a
|
|
||||||
* message to display.
|
|
||||||
*@
|
|
||||||
@(message: String)
|
|
||||||
|
|
||||||
@*
|
|
||||||
* Call the `main` template with two arguments. The first
|
|
||||||
* argument is a `String` with the title of the page, the second
|
|
||||||
* argument is an `Html` object containing the body of the page.
|
|
||||||
*@
|
|
||||||
@main("Welcome to Play") {
|
@main("Welcome to Play") {
|
||||||
|
<h1>Welcome to Play!</h1>
|
||||||
@*
|
|
||||||
* Get an `Html` object by calling the built-in Play welcome
|
|
||||||
* template and passing a `String` message.
|
|
||||||
*@
|
|
||||||
@play20.welcome(message, style = "Java")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,12 @@
|
|||||||
<title>@title</title>
|
<title>@title</title>
|
||||||
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
|
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
|
||||||
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
|
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
|
||||||
<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@* And here's where we render the `Html` object containing
|
@* And here's where we render the `Html` object containing
|
||||||
* the page content. *@
|
* the page content. *@
|
||||||
@content
|
@content
|
||||||
|
|
||||||
|
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
397
play-framework/introduction/bin/activator
vendored
397
play-framework/introduction/bin/activator
vendored
@ -1,397 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
### ------------------------------- ###
|
|
||||||
### Helper methods for BASH scripts ###
|
|
||||||
### ------------------------------- ###
|
|
||||||
|
|
||||||
realpath () {
|
|
||||||
(
|
|
||||||
TARGET_FILE="$1"
|
|
||||||
FIX_CYGPATH="$2"
|
|
||||||
|
|
||||||
cd "$(dirname "$TARGET_FILE")"
|
|
||||||
TARGET_FILE=$(basename "$TARGET_FILE")
|
|
||||||
|
|
||||||
COUNT=0
|
|
||||||
while [ -L "$TARGET_FILE" -a $COUNT -lt 100 ]
|
|
||||||
do
|
|
||||||
TARGET_FILE=$(readlink "$TARGET_FILE")
|
|
||||||
cd "$(dirname "$TARGET_FILE")"
|
|
||||||
TARGET_FILE=$(basename "$TARGET_FILE")
|
|
||||||
COUNT=$(($COUNT + 1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# make sure we grab the actual windows path, instead of cygwin's path.
|
|
||||||
if [[ "x$FIX_CYGPATH" != "x" ]]; then
|
|
||||||
echo "$(cygwinpath "$(pwd -P)/$TARGET_FILE")"
|
|
||||||
else
|
|
||||||
echo "$(pwd -P)/$TARGET_FILE"
|
|
||||||
fi
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Uses uname to detect if we're in the odd cygwin environment.
|
|
||||||
is_cygwin() {
|
|
||||||
local os=$(uname -s)
|
|
||||||
case "$os" in
|
|
||||||
CYGWIN*) return 0 ;;
|
|
||||||
*) return 1 ;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO - Use nicer bash-isms here.
|
|
||||||
CYGWIN_FLAG=$(if is_cygwin; then echo true; else echo false; fi)
|
|
||||||
|
|
||||||
|
|
||||||
# This can fix cygwin style /cygdrive paths so we get the
|
|
||||||
# windows style paths.
|
|
||||||
cygwinpath() {
|
|
||||||
local file="$1"
|
|
||||||
if [[ "$CYGWIN_FLAG" == "true" ]]; then
|
|
||||||
echo $(cygpath -w $file)
|
|
||||||
else
|
|
||||||
echo $file
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Make something URI friendly
|
|
||||||
make_url() {
|
|
||||||
url="$1"
|
|
||||||
local nospaces=${url// /%20}
|
|
||||||
if is_cygwin; then
|
|
||||||
echo "/${nospaces//\\//}"
|
|
||||||
else
|
|
||||||
echo "$nospaces"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
declare -a residual_args
|
|
||||||
declare -a java_args
|
|
||||||
declare -a scalac_args
|
|
||||||
declare -a sbt_commands
|
|
||||||
declare java_cmd=java
|
|
||||||
declare java_version
|
|
||||||
declare -r real_script_path="$(realpath "$0")"
|
|
||||||
declare -r sbt_home="$(realpath "$(dirname "$(dirname "$real_script_path")")")"
|
|
||||||
declare -r sbt_bin_dir="$(dirname "$real_script_path")"
|
|
||||||
declare -r app_version="1.3.10"
|
|
||||||
|
|
||||||
declare -r script_name=activator
|
|
||||||
declare -r java_opts=( "${ACTIVATOR_OPTS[@]}" "${SBT_OPTS[@]}" "${JAVA_OPTS[@]}" "${java_opts[@]}" )
|
|
||||||
userhome="$HOME"
|
|
||||||
if is_cygwin; then
|
|
||||||
# cygwin sets home to something f-d up, set to real windows homedir
|
|
||||||
userhome="$USERPROFILE"
|
|
||||||
fi
|
|
||||||
declare -r activator_user_home_dir="${userhome}/.activator"
|
|
||||||
declare -r java_opts_config_home="${activator_user_home_dir}/activatorconfig.txt"
|
|
||||||
declare -r java_opts_config_version="${activator_user_home_dir}/${app_version}/activatorconfig.txt"
|
|
||||||
|
|
||||||
echoerr () {
|
|
||||||
echo 1>&2 "$@"
|
|
||||||
}
|
|
||||||
vlog () {
|
|
||||||
[[ $verbose || $debug ]] && echoerr "$@"
|
|
||||||
}
|
|
||||||
dlog () {
|
|
||||||
[[ $debug ]] && echoerr "$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
jar_file () {
|
|
||||||
echo "$(cygwinpath "${sbt_home}/libexec/activator-launch-${app_version}.jar")"
|
|
||||||
}
|
|
||||||
|
|
||||||
acquire_sbt_jar () {
|
|
||||||
sbt_jar="$(jar_file)"
|
|
||||||
|
|
||||||
if [[ ! -f "$sbt_jar" ]]; then
|
|
||||||
echoerr "Could not find launcher jar: $sbt_jar"
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
execRunner () {
|
|
||||||
# print the arguments one to a line, quoting any containing spaces
|
|
||||||
[[ $verbose || $debug ]] && echo "# Executing command line:" && {
|
|
||||||
for arg; do
|
|
||||||
if printf "%s\n" "$arg" | grep -q ' '; then
|
|
||||||
printf "\"%s\"\n" "$arg"
|
|
||||||
else
|
|
||||||
printf "%s\n" "$arg"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
echo ""
|
|
||||||
}
|
|
||||||
|
|
||||||
# THis used to be exec, but we loose the ability to re-hook stty then
|
|
||||||
# for cygwin... Maybe we should flag the feature here...
|
|
||||||
"$@"
|
|
||||||
}
|
|
||||||
|
|
||||||
addJava () {
|
|
||||||
dlog "[addJava] arg = '$1'"
|
|
||||||
java_args=( "${java_args[@]}" "$1" )
|
|
||||||
}
|
|
||||||
addSbt () {
|
|
||||||
dlog "[addSbt] arg = '$1'"
|
|
||||||
sbt_commands=( "${sbt_commands[@]}" "$1" )
|
|
||||||
}
|
|
||||||
addResidual () {
|
|
||||||
dlog "[residual] arg = '$1'"
|
|
||||||
residual_args=( "${residual_args[@]}" "$1" )
|
|
||||||
}
|
|
||||||
addDebugger () {
|
|
||||||
addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
get_mem_opts () {
|
|
||||||
# if we detect any of these settings in ${JAVA_OPTS} we need to NOT output our settings.
|
|
||||||
# The reason is the Xms/Xmx, if they don't line up, cause errors.
|
|
||||||
if [[ "${JAVA_OPTS}" == *-Xmx* ]] || [[ "${JAVA_OPTS}" == *-Xms* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxPermSize* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxMetaspaceSize* ]] || [[ "${JAVA_OPTS}" == *-XX:ReservedCodeCacheSize* ]]; then
|
|
||||||
echo ""
|
|
||||||
else
|
|
||||||
# a ham-fisted attempt to move some memory settings in concert
|
|
||||||
# so they need not be messed around with individually.
|
|
||||||
local mem=${1:-1024}
|
|
||||||
local codecache=$(( $mem / 8 ))
|
|
||||||
(( $codecache > 128 )) || codecache=128
|
|
||||||
(( $codecache < 512 )) || codecache=512
|
|
||||||
local class_metadata_size=$(( $codecache * 2 ))
|
|
||||||
local class_metadata_opt=$([[ "$java_version" < "1.8" ]] && echo "MaxPermSize" || echo "MaxMetaspaceSize")
|
|
||||||
|
|
||||||
echo "-Xms${mem}m -Xmx${mem}m -XX:ReservedCodeCacheSize=${codecache}m -XX:${class_metadata_opt}=${class_metadata_size}m"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
require_arg () {
|
|
||||||
local type="$1"
|
|
||||||
local opt="$2"
|
|
||||||
local arg="$3"
|
|
||||||
if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
|
|
||||||
echo "$opt requires <$type> argument"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
is_function_defined() {
|
|
||||||
declare -f "$1" > /dev/null
|
|
||||||
}
|
|
||||||
|
|
||||||
# If we're *not* running in a terminal, and we don't have any arguments, then we need to add the 'ui' parameter
|
|
||||||
detect_terminal_for_ui() {
|
|
||||||
[[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && {
|
|
||||||
addResidual "ui"
|
|
||||||
}
|
|
||||||
# SPECIAL TEST FOR MAC
|
|
||||||
[[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && {
|
|
||||||
echo "Detected MAC OSX launched script...."
|
|
||||||
echo "Swapping to UI"
|
|
||||||
addResidual "ui"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
process_args () {
|
|
||||||
while [[ $# -gt 0 ]]; do
|
|
||||||
case "$1" in
|
|
||||||
-h|-help) usage; exit 1 ;;
|
|
||||||
-v|-verbose) verbose=1 && shift ;;
|
|
||||||
-d|-debug) debug=1 && shift ;;
|
|
||||||
|
|
||||||
-ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
|
|
||||||
-mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;;
|
|
||||||
-jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
|
|
||||||
-batch) exec </dev/null && shift ;;
|
|
||||||
|
|
||||||
-sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
|
|
||||||
-sbt-version) require_arg version "$1" "$2" && sbt_version="$2" && shift 2 ;;
|
|
||||||
-java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && shift 2 ;;
|
|
||||||
|
|
||||||
-D*) addJava "$1" && shift ;;
|
|
||||||
-J*) addJava "${1:2}" && shift ;;
|
|
||||||
*) addResidual "$1" && shift ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
is_function_defined process_my_args && {
|
|
||||||
myargs=("${residual_args[@]}")
|
|
||||||
residual_args=()
|
|
||||||
process_my_args "${myargs[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
java_version=$("$java_cmd" -Xmx512M -version 2>&1 | awk -F '"' '/version/ {print $2}')
|
|
||||||
vlog "[process_args] java_version = '$java_version'"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Detect that we have java installed.
|
|
||||||
checkJava() {
|
|
||||||
local required_version="$1"
|
|
||||||
# Now check to see if it's a good enough version
|
|
||||||
if [[ "$java_version" == "" ]]; then
|
|
||||||
echo
|
|
||||||
echo No java installations was detected.
|
|
||||||
echo Please go to http://www.java.com/getjava/ and download
|
|
||||||
echo
|
|
||||||
exit 1
|
|
||||||
elif [[ ! "$java_version" > "$required_version" ]]; then
|
|
||||||
echo
|
|
||||||
echo The java installation you have is not up to date
|
|
||||||
echo $script_name requires at least version $required_version+, you have
|
|
||||||
echo version $java_version
|
|
||||||
echo
|
|
||||||
echo Please go to http://www.java.com/getjava/ and download
|
|
||||||
echo a valid Java Runtime and install before running $script_name.
|
|
||||||
echo
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
run() {
|
|
||||||
# no jar? download it.
|
|
||||||
[[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || {
|
|
||||||
# still no jar? uh-oh.
|
|
||||||
echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# process the combined args, then reset "$@" to the residuals
|
|
||||||
process_args "$@"
|
|
||||||
detect_terminal_for_ui
|
|
||||||
set -- "${residual_args[@]}"
|
|
||||||
argumentCount=$#
|
|
||||||
|
|
||||||
# TODO - java check should be configurable...
|
|
||||||
checkJava "1.6"
|
|
||||||
|
|
||||||
#If we're in cygwin, we should use the windows config, and terminal hacks
|
|
||||||
if [[ "$CYGWIN_FLAG" == "true" ]]; then
|
|
||||||
stty -icanon min 1 -echo > /dev/null 2>&1
|
|
||||||
addJava "-Djline.terminal=jline.UnixTerminal"
|
|
||||||
addJava "-Dsbt.cygwin=true"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# run sbt
|
|
||||||
execRunner "$java_cmd" \
|
|
||||||
"-Dactivator.home=$(make_url "$sbt_home")" \
|
|
||||||
${SBT_OPTS:-$default_sbt_opts} \
|
|
||||||
$(get_mem_opts $sbt_mem) \
|
|
||||||
${JAVA_OPTS} \
|
|
||||||
${java_args[@]} \
|
|
||||||
-jar "$sbt_jar" \
|
|
||||||
"${sbt_commands[@]}" \
|
|
||||||
"${residual_args[@]}"
|
|
||||||
|
|
||||||
exit_code=$?
|
|
||||||
|
|
||||||
# Clean up the terminal from cygwin hacks.
|
|
||||||
if [[ "$CYGWIN_FLAG" == "true" ]]; then
|
|
||||||
stty icanon echo > /dev/null 2>&1
|
|
||||||
fi
|
|
||||||
exit $exit_code
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy"
|
|
||||||
declare -r sbt_opts_file=".sbtopts"
|
|
||||||
declare -r etc_sbt_opts_file="${sbt_home}/conf/sbtopts"
|
|
||||||
declare -r win_sbt_opts_file="${sbt_home}/conf/sbtconfig.txt"
|
|
||||||
|
|
||||||
usage() {
|
|
||||||
cat <<EOM
|
|
||||||
Usage: $script_name [options]
|
|
||||||
|
|
||||||
Command:
|
|
||||||
ui Start the Activator UI
|
|
||||||
new [name] [template-id] Create a new project with [name] using template [template-id]
|
|
||||||
list-templates Print all available template names
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-h | -help print this message
|
|
||||||
-v | -verbose this runner is chattier
|
|
||||||
-d | -debug set sbt log level to debug
|
|
||||||
-no-colors disable ANSI color codes
|
|
||||||
-sbt-create start sbt even if current directory contains no sbt project
|
|
||||||
-sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt)
|
|
||||||
-sbt-boot <path> path to shared boot directory (default: ~/.sbt/boot in 0.11 series)
|
|
||||||
-ivy <path> path to local Ivy repository (default: ~/.ivy2)
|
|
||||||
-mem <integer> set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem))
|
|
||||||
-no-share use all local caches; no sharing
|
|
||||||
-no-global uses global caches, but does not use global ~/.sbt directory.
|
|
||||||
-jvm-debug <port> Turn on JVM debugging, open at the given port.
|
|
||||||
-batch Disable interactive mode
|
|
||||||
|
|
||||||
# sbt version (default: from project/build.properties if present, else latest release)
|
|
||||||
-sbt-version <version> use the specified version of sbt
|
|
||||||
-sbt-jar <path> use the specified jar as the sbt launcher
|
|
||||||
-sbt-rc use an RC version of sbt
|
|
||||||
-sbt-snapshot use a snapshot version of sbt
|
|
||||||
|
|
||||||
# java version (default: java from PATH, currently $(java -version 2>&1 | grep version))
|
|
||||||
-java-home <path> alternate JAVA_HOME
|
|
||||||
|
|
||||||
# jvm options and output control
|
|
||||||
JAVA_OPTS environment variable, if unset uses "$java_opts"
|
|
||||||
SBT_OPTS environment variable, if unset uses "$default_sbt_opts"
|
|
||||||
ACTIVATOR_OPTS Environment variable, if unset uses ""
|
|
||||||
.sbtopts if this file exists in the current directory, it is
|
|
||||||
prepended to the runner args
|
|
||||||
/etc/sbt/sbtopts if this file exists, it is prepended to the runner args
|
|
||||||
-Dkey=val pass -Dkey=val directly to the java runtime
|
|
||||||
-J-X pass option -X directly to the java runtime
|
|
||||||
(-J is stripped)
|
|
||||||
-S-X add -X to sbt's scalacOptions (-S is stripped)
|
|
||||||
|
|
||||||
In the case of duplicated or conflicting options, the order above
|
|
||||||
shows precedence: JAVA_OPTS lowest, command line options highest.
|
|
||||||
EOM
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
process_my_args () {
|
|
||||||
while [[ $# -gt 0 ]]; do
|
|
||||||
case "$1" in
|
|
||||||
-no-colors) addJava "-Dsbt.log.noformat=true" && shift ;;
|
|
||||||
-no-share) addJava "$noshare_opts" && shift ;;
|
|
||||||
-no-global) addJava "-Dsbt.global.base=$(pwd)/project/.sbtboot" && shift ;;
|
|
||||||
-sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;;
|
|
||||||
-sbt-dir) require_arg path "$1" "$2" && addJava "-Dsbt.global.base=$2" && shift 2 ;;
|
|
||||||
-debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;;
|
|
||||||
-batch) exec </dev/null && shift ;;
|
|
||||||
|
|
||||||
-sbt-create) sbt_create=true && shift ;;
|
|
||||||
|
|
||||||
*) addResidual "$1" && shift ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# Now, ensure sbt version is used.
|
|
||||||
[[ "${sbt_version}XXX" != "XXX" ]] && addJava "-Dsbt.version=$sbt_version"
|
|
||||||
}
|
|
||||||
|
|
||||||
loadConfigFile() {
|
|
||||||
cat "$1" | sed '/^\#/d' | while read line; do
|
|
||||||
eval echo $line
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO - Pull in config based on operating system... (MSYS + cygwin should pull in txt file).
|
|
||||||
# Here we pull in the global settings configuration.
|
|
||||||
[[ -f "$etc_sbt_opts_file" ]] && set -- $(loadConfigFile "$etc_sbt_opts_file") "$@"
|
|
||||||
# -- Windows behavior stub'd
|
|
||||||
# JAVA_OPTS=$(cat "$WDIR/sbtconfig.txt" | sed -e 's/\r//g' -e 's/^#.*$//g' | sed ':a;N;$!ba;s/\n/ /g')
|
|
||||||
|
|
||||||
|
|
||||||
# Pull in the project-level config file, if it exists.
|
|
||||||
[[ -f "$sbt_opts_file" ]] && set -- $(loadConfigFile "$sbt_opts_file") "$@"
|
|
||||||
|
|
||||||
# if configuration files exist, prepend their contents to the java args so it can be processed by this runner
|
|
||||||
# a "versioned" config trumps one on the top level
|
|
||||||
if [[ -f "$java_opts_config_version" ]]; then
|
|
||||||
addConfigOpts $(loadConfigFile "$java_opts_config_version")
|
|
||||||
elif [[ -f "$java_opts_config_home" ]]; then
|
|
||||||
addConfigOpts $(loadConfigFile "$java_opts_config_home")
|
|
||||||
fi
|
|
||||||
|
|
||||||
run "$@"
|
|
248
play-framework/introduction/bin/activator.bat
vendored
248
play-framework/introduction/bin/activator.bat
vendored
@ -1,248 +0,0 @@
|
|||||||
@REM activator launcher script
|
|
||||||
@REM
|
|
||||||
@REM Environment:
|
|
||||||
@REM In order for Activator to work you must have Java available on the classpath
|
|
||||||
@REM JAVA_HOME - location of a JDK home dir (optional if java on path)
|
|
||||||
@REM CFG_OPTS - JVM options (optional)
|
|
||||||
@REM Configuration:
|
|
||||||
@REM activatorconfig.txt found in the ACTIVATOR_HOME or ACTIVATOR_HOME/ACTIVATOR_VERSION
|
|
||||||
@setlocal enabledelayedexpansion
|
|
||||||
|
|
||||||
@echo off
|
|
||||||
|
|
||||||
set "var1=%~1"
|
|
||||||
if defined var1 (
|
|
||||||
if "%var1%"=="help" (
|
|
||||||
echo.
|
|
||||||
echo Usage activator [options] [command]
|
|
||||||
echo.
|
|
||||||
echo Commands:
|
|
||||||
echo ui Start the Activator UI
|
|
||||||
echo new [name] [template-id] Create a new project with [name] using template [template-id]
|
|
||||||
echo list-templates Print all available template names
|
|
||||||
echo help Print this message
|
|
||||||
echo.
|
|
||||||
echo Options:
|
|
||||||
echo -jvm-debug [port] Turn on JVM debugging, open at the given port. Defaults to 9999 if no port given.
|
|
||||||
echo.
|
|
||||||
echo Environment variables ^(read from context^):
|
|
||||||
echo JAVA_OPTS Environment variable, if unset uses ""
|
|
||||||
echo SBT_OPTS Environment variable, if unset uses ""
|
|
||||||
echo ACTIVATOR_OPTS Environment variable, if unset uses ""
|
|
||||||
echo.
|
|
||||||
echo Please note that in order for Activator to work you must have Java available on the classpath
|
|
||||||
echo.
|
|
||||||
goto :end
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
@REM determine ACTIVATOR_HOME environment variable
|
|
||||||
set BIN_DIRECTORY=%~dp0
|
|
||||||
set BIN_DIRECTORY=%BIN_DIRECTORY:~0,-1%
|
|
||||||
for %%d in (%BIN_DIRECTORY%) do set ACTIVATOR_HOME=%%~dpd
|
|
||||||
set ACTIVATOR_HOME=%ACTIVATOR_HOME:~0,-1%
|
|
||||||
|
|
||||||
echo ACTIVATOR_HOME=%ACTIVATOR_HOME%
|
|
||||||
|
|
||||||
set ERROR_CODE=0
|
|
||||||
set APP_VERSION=1.3.10
|
|
||||||
set ACTIVATOR_LAUNCH_JAR=activator-launch-%APP_VERSION%.jar
|
|
||||||
|
|
||||||
rem Detect if we were double clicked, although theoretically A user could
|
|
||||||
rem manually run cmd /c
|
|
||||||
for %%x in (%cmdcmdline%) do if %%~x==/c set DOUBLECLICKED=1
|
|
||||||
|
|
||||||
set SBT_HOME=%BIN_DIRECTORY
|
|
||||||
|
|
||||||
rem Detect if we were double clicked, although theoretically A user could
|
|
||||||
rem manually run cmd /c
|
|
||||||
for %%x in (%cmdcmdline%) do if %%~x==/c set DOUBLECLICKED=1
|
|
||||||
|
|
||||||
rem FIRST we load the config file of extra options.
|
|
||||||
set FN=%SBT_HOME%\..\conf\sbtconfig.txt
|
|
||||||
set CFG_OPTS=
|
|
||||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%FN%") DO (
|
|
||||||
set DO_NOT_REUSE_ME=%%i
|
|
||||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
|
||||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
|
||||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
|
||||||
)
|
|
||||||
|
|
||||||
rem FIRST we load a config file of extra options (if there is one)
|
|
||||||
set "CFG_FILE_HOME=%UserProfile%\.activator\activatorconfig.txt"
|
|
||||||
set "CFG_FILE_VERSION=%UserProfile%\.activator\%APP_VERSION%\activatorconfig.txt"
|
|
||||||
if exist %CFG_FILE_VERSION% (
|
|
||||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE_VERSION%") DO (
|
|
||||||
set DO_NOT_REUSE_ME=%%i
|
|
||||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
|
||||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
|
||||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if "%CFG_OPTS%"=="" (
|
|
||||||
if exist %CFG_FILE_HOME% (
|
|
||||||
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%CFG_FILE_HOME%") DO (
|
|
||||||
set DO_NOT_REUSE_ME=%%i
|
|
||||||
rem ZOMG (Part #2) WE use !! here to delay the expansion of
|
|
||||||
rem CFG_OPTS, otherwise it remains "" for this loop.
|
|
||||||
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
rem We use the value of the JAVACMD environment variable if defined
|
|
||||||
set _JAVACMD=%JAVACMD%
|
|
||||||
|
|
||||||
if "%_JAVACMD%"=="" (
|
|
||||||
if not "%JAVA_HOME%"=="" (
|
|
||||||
if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
|
|
||||||
|
|
||||||
rem if there is a java home set we make sure it is the first picked up when invoking 'java'
|
|
||||||
SET "PATH=%JAVA_HOME%\bin;%PATH%"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if "%_JAVACMD%"=="" set _JAVACMD=java
|
|
||||||
|
|
||||||
rem Detect if this java is ok to use.
|
|
||||||
for /F %%j in ('"%_JAVACMD%" -version 2^>^&1') do (
|
|
||||||
if %%~j==java set JAVAINSTALLED=1
|
|
||||||
if %%~j==openjdk set JAVAINSTALLED=1
|
|
||||||
)
|
|
||||||
|
|
||||||
rem Detect the same thing about javac
|
|
||||||
if "%_JAVACCMD%"=="" (
|
|
||||||
if not "%JAVA_HOME%"=="" (
|
|
||||||
if exist "%JAVA_HOME%\bin\javac.exe" set "_JAVACCMD=%JAVA_HOME%\bin\javac.exe"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if "%_JAVACCMD%"=="" set _JAVACCMD=javac
|
|
||||||
for /F %%j in ('"%_JAVACCMD%" -version 2^>^&1') do (
|
|
||||||
if %%~j==javac set JAVACINSTALLED=1
|
|
||||||
)
|
|
||||||
|
|
||||||
rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style
|
|
||||||
set JAVAOK=true
|
|
||||||
if not defined JAVAINSTALLED set JAVAOK=false
|
|
||||||
if not defined JAVACINSTALLED set JAVAOK=false
|
|
||||||
|
|
||||||
if "%JAVAOK%"=="false" (
|
|
||||||
echo.
|
|
||||||
echo A Java JDK is not installed or can't be found.
|
|
||||||
if not "%JAVA_HOME%"=="" (
|
|
||||||
echo JAVA_HOME = "%JAVA_HOME%"
|
|
||||||
)
|
|
||||||
echo.
|
|
||||||
echo Please go to
|
|
||||||
echo http://www.oracle.com/technetwork/java/javase/downloads/index.html
|
|
||||||
echo and download a valid Java JDK and install before running Activator.
|
|
||||||
echo.
|
|
||||||
echo If you think this message is in error, please check
|
|
||||||
echo your environment variables to see if "java.exe" and "javac.exe" are
|
|
||||||
echo available via JAVA_HOME or PATH.
|
|
||||||
echo.
|
|
||||||
if defined DOUBLECLICKED pause
|
|
||||||
exit /B 1
|
|
||||||
)
|
|
||||||
|
|
||||||
rem Check what Java version is being used to determine what memory options to use
|
|
||||||
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
|
|
||||||
set JAVA_VERSION=%%g
|
|
||||||
)
|
|
||||||
|
|
||||||
rem Strips away the " characters
|
|
||||||
set JAVA_VERSION=%JAVA_VERSION:"=%
|
|
||||||
|
|
||||||
rem TODO Check if there are existing mem settings in JAVA_OPTS/CFG_OPTS and use those instead of the below
|
|
||||||
for /f "delims=. tokens=1-3" %%v in ("%JAVA_VERSION%") do (
|
|
||||||
set MAJOR=%%v
|
|
||||||
set MINOR=%%w
|
|
||||||
set BUILD=%%x
|
|
||||||
|
|
||||||
set META_SIZE=-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=256M
|
|
||||||
if "!MINOR!" LSS "8" (
|
|
||||||
set META_SIZE=-XX:PermSize=64M -XX:MaxPermSize=256M
|
|
||||||
)
|
|
||||||
|
|
||||||
set MEM_OPTS=!META_SIZE!
|
|
||||||
)
|
|
||||||
|
|
||||||
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
|
|
||||||
set _JAVA_OPTS=%JAVA_OPTS%
|
|
||||||
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
|
|
||||||
|
|
||||||
set DEBUG_OPTS=
|
|
||||||
|
|
||||||
rem Loop through the arguments, building remaining args in args variable
|
|
||||||
set args=
|
|
||||||
:argsloop
|
|
||||||
if not "%~1"=="" (
|
|
||||||
rem Checks if the argument contains "-D" and if true, adds argument 1 with 2 and puts an equal sign between them.
|
|
||||||
rem This is done since batch considers "=" to be a delimiter so we need to circumvent this behavior with a small hack.
|
|
||||||
set arg1=%~1
|
|
||||||
if "!arg1:~0,2!"=="-D" (
|
|
||||||
set "args=%args% "%~1"="%~2""
|
|
||||||
shift
|
|
||||||
shift
|
|
||||||
goto argsloop
|
|
||||||
)
|
|
||||||
|
|
||||||
if "%~1"=="-jvm-debug" (
|
|
||||||
if not "%~2"=="" (
|
|
||||||
rem This piece of magic somehow checks that an argument is a number
|
|
||||||
for /F "delims=0123456789" %%i in ("%~2") do (
|
|
||||||
set var="%%i"
|
|
||||||
)
|
|
||||||
if defined var (
|
|
||||||
rem Not a number, assume no argument given and default to 9999
|
|
||||||
set JPDA_PORT=9999
|
|
||||||
) else (
|
|
||||||
rem Port was given, shift arguments
|
|
||||||
set JPDA_PORT=%~2
|
|
||||||
shift
|
|
||||||
)
|
|
||||||
) else (
|
|
||||||
set JPDA_PORT=9999
|
|
||||||
)
|
|
||||||
shift
|
|
||||||
|
|
||||||
set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=!JPDA_PORT!
|
|
||||||
goto argsloop
|
|
||||||
)
|
|
||||||
rem else
|
|
||||||
set "args=%args% "%~1""
|
|
||||||
shift
|
|
||||||
goto argsloop
|
|
||||||
)
|
|
||||||
|
|
||||||
:run
|
|
||||||
|
|
||||||
if "!args!"=="" (
|
|
||||||
if defined DOUBLECLICKED (
|
|
||||||
set CMDS="ui"
|
|
||||||
) else set CMDS=!args!
|
|
||||||
) else set CMDS=!args!
|
|
||||||
|
|
||||||
rem We add a / in front, so we get file:///C: instead of file://C:
|
|
||||||
rem Java considers the later a UNC path.
|
|
||||||
rem We also attempt a solid effort at making it URI friendly.
|
|
||||||
rem We don't even bother with UNC paths.
|
|
||||||
set JAVA_FRIENDLY_HOME_1=/!ACTIVATOR_HOME:\=/!
|
|
||||||
set JAVA_FRIENDLY_HOME=/!JAVA_FRIENDLY_HOME_1: =%%20!
|
|
||||||
|
|
||||||
rem Checks if the command contains spaces to know if it should be wrapped in quotes or not
|
|
||||||
set NON_SPACED_CMD=%_JAVACMD: =%
|
|
||||||
if "%_JAVACMD%"=="%NON_SPACED_CMD%" %_JAVACMD% %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS%
|
|
||||||
if NOT "%_JAVACMD%"=="%NON_SPACED_CMD%" "%_JAVACMD%" %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS%
|
|
||||||
|
|
||||||
if ERRORLEVEL 1 goto error
|
|
||||||
goto end
|
|
||||||
|
|
||||||
:error
|
|
||||||
set ERROR_CODE=1
|
|
||||||
|
|
||||||
:end
|
|
||||||
|
|
||||||
@endlocal
|
|
||||||
|
|
||||||
exit /B %ERROR_CODE%
|
|
@ -1,13 +1,10 @@
|
|||||||
name := """student-api"""
|
name := """introduction"""
|
||||||
|
organization := "com.baeldung"
|
||||||
|
|
||||||
version := "1.0-SNAPSHOT"
|
version := "1.0-SNAPSHOT"
|
||||||
|
|
||||||
lazy val root = (project in file(".")).enablePlugins(PlayJava)
|
lazy val root = (project in file(".")).enablePlugins(PlayJava)
|
||||||
|
|
||||||
scalaVersion := "2.11.7"
|
scalaVersion := "2.13.0"
|
||||||
|
|
||||||
libraryDependencies ++= Seq(
|
libraryDependencies += guice
|
||||||
javaJdbc,
|
|
||||||
cache,
|
|
||||||
javaWs
|
|
||||||
)
|
|
||||||
|
@ -1,353 +1,2 @@
|
|||||||
# This is the main configuration file for the application.
|
# This is the main configuration file for the application.
|
||||||
# https://www.playframework.com/documentation/latest/ConfigFile
|
# https://www.playframework.com/documentation/latest/ConfigFile
|
||||||
# ~~~~~
|
|
||||||
# Play uses HOCON as its configuration file format. HOCON has a number
|
|
||||||
# of advantages over other config formats, but there are two things that
|
|
||||||
# can be used when modifying settings.
|
|
||||||
#
|
|
||||||
# You can include other configuration files in this main application.conf file:
|
|
||||||
#include "extra-config.conf"
|
|
||||||
#
|
|
||||||
# You can declare variables and substitute for them:
|
|
||||||
#mykey = ${some.value}
|
|
||||||
#
|
|
||||||
# And if an environment variable exists when there is no other subsitution, then
|
|
||||||
# HOCON will fall back to substituting environment variable:
|
|
||||||
#mykey = ${JAVA_HOME}
|
|
||||||
|
|
||||||
## Akka
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaAkka#Configuration
|
|
||||||
# ~~~~~
|
|
||||||
# Play uses Akka internally and exposes Akka Streams and actors in Websockets and
|
|
||||||
# other streaming HTTP responses.
|
|
||||||
akka {
|
|
||||||
# "akka.log-config-on-start" is extraordinarly useful because it log the complete
|
|
||||||
# configuration at INFO level, including defaults and overrides, so it s worth
|
|
||||||
# putting at the very top.
|
|
||||||
#
|
|
||||||
# Put the following in your conf/logback.xml file:
|
|
||||||
#
|
|
||||||
# <logger name="akka.actor" level="INFO" />
|
|
||||||
#
|
|
||||||
# And then uncomment this line to debug the configuration.
|
|
||||||
#
|
|
||||||
#log-config-on-start = true
|
|
||||||
}
|
|
||||||
|
|
||||||
## Secret key
|
|
||||||
# http://www.playframework.com/documentation/latest/ApplicationSecret
|
|
||||||
# ~~~~~
|
|
||||||
# The secret key is used to sign Play's session cookie.
|
|
||||||
# This must be changed for production, but we don't recommend you change it in this file.
|
|
||||||
play.crypto.secret = "changeme"
|
|
||||||
|
|
||||||
## Modules
|
|
||||||
# https://www.playframework.com/documentation/latest/Modules
|
|
||||||
# ~~~~~
|
|
||||||
# Control which modules are loaded when Play starts. Note that modules are
|
|
||||||
# the replacement for "GlobalSettings", which are deprecated in 2.5.x.
|
|
||||||
# Please see https://www.playframework.com/documentation/latest/GlobalSettings
|
|
||||||
# for more information.
|
|
||||||
#
|
|
||||||
# You can also extend Play functionality by using one of the publically available
|
|
||||||
# Play modules: https://playframework.com/documentation/latest/ModuleDirectory
|
|
||||||
play.modules {
|
|
||||||
# By default, Play will load any class called Module that is defined
|
|
||||||
# in the root package (the "app" directory), or you can define them
|
|
||||||
# explicitly below.
|
|
||||||
# If there are any built-in modules that you want to disable, you can list them here.
|
|
||||||
#enabled += my.application.Module
|
|
||||||
|
|
||||||
# If there are any built-in modules that you want to disable, you can list them here.
|
|
||||||
#disabled += ""
|
|
||||||
}
|
|
||||||
|
|
||||||
## IDE
|
|
||||||
# https://www.playframework.com/documentation/latest/IDE
|
|
||||||
# ~~~~~
|
|
||||||
# Depending on your IDE, you can add a hyperlink for errors that will jump you
|
|
||||||
# directly to the code location in the IDE in dev mode. The following line makes
|
|
||||||
# use of the IntelliJ IDEA REST interface:
|
|
||||||
#play.editor="http://localhost:63342/api/file/?file=%s&line=%s"
|
|
||||||
|
|
||||||
## Internationalisation
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaI18N
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaI18N
|
|
||||||
# ~~~~~
|
|
||||||
# Play comes with its own i18n settings, which allow the user's preferred language
|
|
||||||
# to map through to internal messages, or allow the language to be stored in a cookie.
|
|
||||||
play.i18n {
|
|
||||||
# The application languages
|
|
||||||
langs = [ "en" ]
|
|
||||||
|
|
||||||
# Whether the language cookie should be secure or not
|
|
||||||
#langCookieSecure = true
|
|
||||||
|
|
||||||
# Whether the HTTP only attribute of the cookie should be set to true
|
|
||||||
#langCookieHttpOnly = true
|
|
||||||
}
|
|
||||||
|
|
||||||
## Play HTTP settings
|
|
||||||
# ~~~~~
|
|
||||||
play.http {
|
|
||||||
## Router
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaRouting
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaRouting
|
|
||||||
# ~~~~~
|
|
||||||
# Define the Router object to use for this application.
|
|
||||||
# This router will be looked up first when the application is starting up,
|
|
||||||
# so make sure this is the entry point.
|
|
||||||
# Furthermore, it's assumed your route file is named properly.
|
|
||||||
# So for an application router like `my.application.Router`,
|
|
||||||
# you may need to define a router file `conf/my.application.routes`.
|
|
||||||
# Default to Routes in the root package (aka "apps" folder) (and conf/routes)
|
|
||||||
#router = my.application.Router
|
|
||||||
|
|
||||||
## Action Creator
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaActionCreator
|
|
||||||
# ~~~~~
|
|
||||||
#actionCreator = null
|
|
||||||
|
|
||||||
## ErrorHandler
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaRouting
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaRouting
|
|
||||||
# ~~~~~
|
|
||||||
# If null, will attempt to load a class called ErrorHandler in the root package,
|
|
||||||
#errorHandler = null
|
|
||||||
|
|
||||||
## Filters
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaHttpFilters
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaHttpFilters
|
|
||||||
# ~~~~~
|
|
||||||
# Filters run code on every request. They can be used to perform
|
|
||||||
# common logic for all your actions, e.g. adding common headers.
|
|
||||||
# Defaults to "Filters" in the root package (aka "apps" folder)
|
|
||||||
# Alternatively you can explicitly register a class here.
|
|
||||||
#filters = my.application.Filters
|
|
||||||
|
|
||||||
## Session & Flash
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaSessionFlash
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaSessionFlash
|
|
||||||
# ~~~~~
|
|
||||||
session {
|
|
||||||
# Sets the cookie to be sent only over HTTPS.
|
|
||||||
#secure = true
|
|
||||||
|
|
||||||
# Sets the cookie to be accessed only by the server.
|
|
||||||
#httpOnly = true
|
|
||||||
|
|
||||||
# Sets the max-age field of the cookie to 5 minutes.
|
|
||||||
# NOTE: this only sets when the browser will discard the cookie. Play will consider any
|
|
||||||
# cookie value with a valid signature to be a valid session forever. To implement a server side session timeout,
|
|
||||||
# you need to put a timestamp in the session and check it at regular intervals to possibly expire it.
|
|
||||||
#maxAge = 300
|
|
||||||
|
|
||||||
# Sets the domain on the session cookie.
|
|
||||||
#domain = "example.com"
|
|
||||||
}
|
|
||||||
|
|
||||||
flash {
|
|
||||||
# Sets the cookie to be sent only over HTTPS.
|
|
||||||
#secure = true
|
|
||||||
|
|
||||||
# Sets the cookie to be accessed only by the server.
|
|
||||||
#httpOnly = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## Netty Provider
|
|
||||||
# https://www.playframework.com/documentation/latest/SettingsNetty
|
|
||||||
# ~~~~~
|
|
||||||
play.server.netty {
|
|
||||||
# Whether the Netty wire should be logged
|
|
||||||
#log.wire = true
|
|
||||||
|
|
||||||
# If you run Play on Linux, you can use Netty's native socket transport
|
|
||||||
# for higher performance with less garbage.
|
|
||||||
#transport = "native"
|
|
||||||
}
|
|
||||||
|
|
||||||
## WS (HTTP Client)
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaWS#Configuring-WS
|
|
||||||
# ~~~~~
|
|
||||||
# The HTTP client primarily used for REST APIs. The default client can be
|
|
||||||
# configured directly, but you can also create different client instances
|
|
||||||
# with customized settings. You must enable this by adding to build.sbt:
|
|
||||||
#
|
|
||||||
# libraryDependencies += ws // or javaWs if using java
|
|
||||||
#
|
|
||||||
play.ws {
|
|
||||||
# Sets HTTP requests not to follow 302 requests
|
|
||||||
#followRedirects = false
|
|
||||||
|
|
||||||
# Sets the maximum number of open HTTP connections for the client.
|
|
||||||
#ahc.maxConnectionsTotal = 50
|
|
||||||
|
|
||||||
## WS SSL
|
|
||||||
# https://www.playframework.com/documentation/latest/WsSSL
|
|
||||||
# ~~~~~
|
|
||||||
ssl {
|
|
||||||
# Configuring HTTPS with Play WS does not require programming. You can
|
|
||||||
# set up both trustManager and keyManager for mutual authentication, and
|
|
||||||
# turn on JSSE debugging in development with a reload.
|
|
||||||
#debug.handshake = true
|
|
||||||
#trustManager = {
|
|
||||||
# stores = [
|
|
||||||
# { type = "JKS", path = "exampletrust.jks" }
|
|
||||||
# ]
|
|
||||||
#}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## Cache
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaCache
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaCache
|
|
||||||
# ~~~~~
|
|
||||||
# Play comes with an integrated cache API that can reduce the operational
|
|
||||||
# overhead of repeated requests. You must enable this by adding to build.sbt:
|
|
||||||
#
|
|
||||||
# libraryDependencies += cache
|
|
||||||
#
|
|
||||||
play.cache {
|
|
||||||
# If you want to bind several caches, you can bind the individually
|
|
||||||
#bindCaches = ["db-cache", "user-cache", "session-cache"]
|
|
||||||
}
|
|
||||||
|
|
||||||
## Filters
|
|
||||||
# https://www.playframework.com/documentation/latest/Filters
|
|
||||||
# ~~~~~
|
|
||||||
# There are a number of built-in filters that can be enabled and configured
|
|
||||||
# to give Play greater security. You must enable this by adding to build.sbt:
|
|
||||||
#
|
|
||||||
# libraryDependencies += filters
|
|
||||||
#
|
|
||||||
play.filters {
|
|
||||||
## CORS filter configuration
|
|
||||||
# https://www.playframework.com/documentation/latest/CorsFilter
|
|
||||||
# ~~~~~
|
|
||||||
# CORS is a protocol that allows web applications to make requests from the browser
|
|
||||||
# across different domains.
|
|
||||||
# NOTE: You MUST apply the CORS configuration before the CSRF filter, as CSRF has
|
|
||||||
# dependencies on CORS settings.
|
|
||||||
cors {
|
|
||||||
# Filter paths by a whitelist of path prefixes
|
|
||||||
#pathPrefixes = ["/some/path", ...]
|
|
||||||
|
|
||||||
# The allowed origins. If null, all origins are allowed.
|
|
||||||
#allowedOrigins = ["http://www.example.com"]
|
|
||||||
|
|
||||||
# The allowed HTTP methods. If null, all methods are allowed
|
|
||||||
#allowedHttpMethods = ["GET", "POST"]
|
|
||||||
}
|
|
||||||
|
|
||||||
## CSRF Filter
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaCsrf#Applying-a-global-CSRF-filter
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaCsrf#Applying-a-global-CSRF-filter
|
|
||||||
# ~~~~~
|
|
||||||
# Play supports multiple methods for verifying that a request is not a CSRF request.
|
|
||||||
# The primary mechanism is a CSRF token. This token gets placed either in the query string
|
|
||||||
# or body of every form submitted, and also gets placed in the users session.
|
|
||||||
# Play then verifies that both tokens are present and match.
|
|
||||||
csrf {
|
|
||||||
# Sets the cookie to be sent only over HTTPS
|
|
||||||
#cookie.secure = true
|
|
||||||
|
|
||||||
# Defaults to CSRFErrorHandler in the root package.
|
|
||||||
#errorHandler = MyCSRFErrorHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
## Security headers filter configuration
|
|
||||||
# https://www.playframework.com/documentation/latest/SecurityHeaders
|
|
||||||
# ~~~~~
|
|
||||||
# Defines security headers that prevent XSS attacks.
|
|
||||||
# If enabled, then all options are set to the below configuration by default:
|
|
||||||
headers {
|
|
||||||
# The X-Frame-Options header. If null, the header is not set.
|
|
||||||
#frameOptions = "DENY"
|
|
||||||
|
|
||||||
# The X-XSS-Protection header. If null, the header is not set.
|
|
||||||
#xssProtection = "1; mode=block"
|
|
||||||
|
|
||||||
# The X-Content-Type-Options header. If null, the header is not set.
|
|
||||||
#contentTypeOptions = "nosniff"
|
|
||||||
|
|
||||||
# The X-Permitted-Cross-Domain-Policies header. If null, the header is not set.
|
|
||||||
#permittedCrossDomainPolicies = "master-only"
|
|
||||||
|
|
||||||
# The Content-Security-Policy header. If null, the header is not set.
|
|
||||||
#contentSecurityPolicy = "default-src 'self'"
|
|
||||||
}
|
|
||||||
|
|
||||||
## Allowed hosts filter configuration
|
|
||||||
# https://www.playframework.com/documentation/latest/AllowedHostsFilter
|
|
||||||
# ~~~~~
|
|
||||||
# Play provides a filter that lets you configure which hosts can access your application.
|
|
||||||
# This is useful to prevent cache poisoning attacks.
|
|
||||||
hosts {
|
|
||||||
# Allow requests to example.com, its subdomains, and localhost:9000.
|
|
||||||
#allowed = [".example.com", "localhost:9000"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## Evolutions
|
|
||||||
# https://www.playframework.com/documentation/latest/Evolutions
|
|
||||||
# ~~~~~
|
|
||||||
# Evolutions allows database scripts to be automatically run on startup in dev mode
|
|
||||||
# for database migrations. You must enable this by adding to build.sbt:
|
|
||||||
#
|
|
||||||
# libraryDependencies += evolutions
|
|
||||||
#
|
|
||||||
play.evolutions {
|
|
||||||
# You can disable evolutions for a specific datasource if necessary
|
|
||||||
#db.default.enabled = false
|
|
||||||
}
|
|
||||||
|
|
||||||
## Database Connection Pool
|
|
||||||
# https://www.playframework.com/documentation/latest/SettingsJDBC
|
|
||||||
# ~~~~~
|
|
||||||
# Play doesn't require a JDBC database to run, but you can easily enable one.
|
|
||||||
#
|
|
||||||
# libraryDependencies += jdbc
|
|
||||||
#
|
|
||||||
play.db {
|
|
||||||
# The combination of these two settings results in "db.default" as the
|
|
||||||
# default JDBC pool:
|
|
||||||
#config = "db"
|
|
||||||
#default = "default"
|
|
||||||
|
|
||||||
# Play uses HikariCP as the default connection pool. You can override
|
|
||||||
# settings by changing the prototype:
|
|
||||||
prototype {
|
|
||||||
# Sets a fixed JDBC connection pool size of 50
|
|
||||||
#hikaricp.minimumIdle = 50
|
|
||||||
#hikaricp.maximumPoolSize = 50
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
## JDBC Datasource
|
|
||||||
# https://www.playframework.com/documentation/latest/JavaDatabase
|
|
||||||
# https://www.playframework.com/documentation/latest/ScalaDatabase
|
|
||||||
# ~~~~~
|
|
||||||
# Once JDBC datasource is set up, you can work with several different
|
|
||||||
# database options:
|
|
||||||
#
|
|
||||||
# Slick (Scala preferred option): https://www.playframework.com/documentation/latest/PlaySlick
|
|
||||||
# JPA (Java preferred option): https://playframework.com/documentation/latest/JavaJPA
|
|
||||||
# EBean: https://playframework.com/documentation/latest/JavaEbean
|
|
||||||
# Anorm: https://www.playframework.com/documentation/latest/ScalaAnorm
|
|
||||||
#
|
|
||||||
db {
|
|
||||||
# You can declare as many datasources as you want.
|
|
||||||
# By convention, the default datasource is named `default`
|
|
||||||
|
|
||||||
# https://www.playframework.com/documentation/latest/Developing-with-the-H2-Database
|
|
||||||
#default.driver = org.h2.Driver
|
|
||||||
#default.url = "jdbc:h2:mem:play"
|
|
||||||
#default.username = sa
|
|
||||||
#default.password = ""
|
|
||||||
|
|
||||||
# You can turn on SQL logging for any datasource
|
|
||||||
# https://www.playframework.com/documentation/latest/Highlights25#Logging-SQL-statements
|
|
||||||
#default.logSql=true
|
|
||||||
}
|
|
||||||
|
@ -27,12 +27,6 @@
|
|||||||
<logger name="play" level="INFO" />
|
<logger name="play" level="INFO" />
|
||||||
<logger name="application" level="DEBUG" />
|
<logger name="application" level="DEBUG" />
|
||||||
|
|
||||||
<!-- Off these ones as they are annoying, and anyway we manage configuration ourselves -->
|
|
||||||
<logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
|
|
||||||
<logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
|
|
||||||
<logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />
|
|
||||||
<logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />
|
|
||||||
|
|
||||||
<root level="WARN">
|
<root level="WARN">
|
||||||
<appender-ref ref="ASYNCFILE" />
|
<appender-ref ref="ASYNCFILE" />
|
||||||
<appender-ref ref="ASYNCSTDOUT" />
|
<appender-ref ref="ASYNCSTDOUT" />
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
# This file defines all application routes (Higher priority routes first)
|
# This file defines all application routes (Higher priority routes first)
|
||||||
# ~~~~
|
# ~~~~
|
||||||
|
|
||||||
GET / controllers.StudentController.listStudents()
|
# An example controller showing a sample home page
|
||||||
POST /:id controllers.StudentController.retrieve(id:Int)
|
GET / controllers.HomeController.index
|
||||||
POST / controllers.StudentController.create()
|
GET /baeldung/html controllers.HomeController.applyHtml
|
||||||
PUT / controllers.StudentController.update()
|
GET /baeldung/bad-req controllers.HomeController.badRequestPage
|
||||||
DELETE /:id controllers.StudentController.delete(id:Int)
|
GET /baeldung/not-found controllers.HomeController.notFoundPage
|
||||||
|
GET /baeldung/custom-content-type controllers.HomeController.customContentType
|
||||||
|
GET /baeldung/async controllers.HomeController.asyncOperation
|
||||||
|
GET /baeldung/headers controllers.HomeController.setHeaders
|
||||||
|
|
||||||
# Map static resources from the /public folder to the /assets URL path
|
# Map static resources from the /public folder to the /assets URL path
|
||||||
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
|
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
|
||||||
|
Binary file not shown.
@ -1,4 +1 @@
|
|||||||
#Activator-generated Properties
|
sbt.version=1.2.8
|
||||||
#Wed Sep 07 12:29:40 EAT 2016
|
|
||||||
template.uuid=c373963b-f5ad-433b-8e74-178e7ae25b1c
|
|
||||||
sbt.version=0.13.11
|
|
||||||
|
@ -1,21 +1,7 @@
|
|||||||
// The Play plugin
|
// The Play plugin
|
||||||
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.6")
|
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3")
|
||||||
|
|
||||||
// Web plugins
|
// Defines scaffolding (found under .g8 folder)
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
|
// http://www.foundweekends.org/giter8/scaffolding.html
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0")
|
// sbt "g8Scaffold form"
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
|
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0")
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
|
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
|
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
|
|
||||||
addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.2")
|
|
||||||
|
|
||||||
// Play enhancer - this automatically generates getters/setters for public fields
|
|
||||||
// and rewrites accessors of these fields to use the getters/setters. Remove this
|
|
||||||
// plugin if you prefer not to have this feature, or disable on a per project
|
|
||||||
// basis using disablePlugins(PlayEnhancer) in your build.sbt
|
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")
|
|
||||||
|
|
||||||
// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using
|
|
||||||
// enablePlugins(PlayEbean).
|
|
||||||
// addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
|
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
if (window.console) {
|
|
||||||
console.log("Welcome to your Play application's JavaScript!");
|
|
||||||
}
|
|
@ -1,172 +0,0 @@
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import model.Student;
|
|
||||||
import play.test.*;
|
|
||||||
import static play.test.Helpers.*;
|
|
||||||
|
|
||||||
public class ApplicationLiveTest{
|
|
||||||
private static final String BASE_URL = "http://localhost:9000";
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testInServer() throws Exception {
|
|
||||||
TestServer server = testServer(3333);
|
|
||||||
running(server, () -> {
|
|
||||||
try {
|
|
||||||
WSClient ws = play.libs.ws.WS.newClient(3333);
|
|
||||||
CompletionStage<WSResponse> completionStage = ws.url("/").get();
|
|
||||||
WSResponse response = completionStage.toCompletableFuture().get();
|
|
||||||
ws.close();
|
|
||||||
assertEquals(OK, response.getStatus());
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@Test
|
|
||||||
public void whenCreatesRecord_thenCorrect() {
|
|
||||||
Student student = new Student("jody", "west", 50);
|
|
||||||
JSONObject obj = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student)));
|
|
||||||
assertTrue(obj.getBoolean("isSuccessfull"));
|
|
||||||
JSONObject body = obj.getJSONObject("body");
|
|
||||||
assertEquals(student.getAge(), body.getInt("age"));
|
|
||||||
assertEquals(student.getFirstName(), body.getString("firstName"));
|
|
||||||
assertEquals(student.getLastName(), body.getString("lastName"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenDeletesCreatedRecord_thenCorrect() {
|
|
||||||
Student student = new Student("Usain", "Bolt", 25);
|
|
||||||
JSONObject ob1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
|
|
||||||
int id = ob1.getInt("id");
|
|
||||||
JSONObject obj1 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
|
|
||||||
assertTrue(obj1.getBoolean("isSuccessfull"));
|
|
||||||
makeRequest(BASE_URL + "/" + id, "DELETE", null);
|
|
||||||
JSONObject obj2 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject()));
|
|
||||||
assertFalse(obj2.getBoolean("isSuccessfull"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenUpdatesCreatedRecord_thenCorrect() {
|
|
||||||
Student student = new Student("john", "doe", 50);
|
|
||||||
JSONObject body1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body");
|
|
||||||
assertEquals(student.getAge(), body1.getInt("age"));
|
|
||||||
int newAge = 60;
|
|
||||||
body1.put("age", newAge);
|
|
||||||
JSONObject body2 = new JSONObject(makeRequest(BASE_URL, "PUT", body1)).getJSONObject("body");
|
|
||||||
assertFalse(student.getAge() == body2.getInt("age"));
|
|
||||||
assertTrue(newAge == body2.getInt("age"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenGetsAllRecords_thenCorrect() {
|
|
||||||
Student student1 = new Student("jane", "daisy", 50);
|
|
||||||
Student student2 = new Student("john", "daniel", 60);
|
|
||||||
Student student3 = new Student("don", "mason", 55);
|
|
||||||
Student student4 = new Student("scarlet", "ohara", 90);
|
|
||||||
|
|
||||||
makeRequest(BASE_URL, "POST", new JSONObject(student1));
|
|
||||||
makeRequest(BASE_URL, "POST", new JSONObject(student2));
|
|
||||||
makeRequest(BASE_URL, "POST", new JSONObject(student3));
|
|
||||||
makeRequest(BASE_URL, "POST", new JSONObject(student4));
|
|
||||||
|
|
||||||
JSONObject objects = new JSONObject(makeRequest(BASE_URL, "GET", null));
|
|
||||||
assertTrue(objects.getBoolean("isSuccessfull"));
|
|
||||||
JSONArray array = objects.getJSONArray("body");
|
|
||||||
assertTrue(array.length() >= 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String makeRequest(String myUrl, String httpMethod, JSONObject parameters) {
|
|
||||||
|
|
||||||
URL url = null;
|
|
||||||
try {
|
|
||||||
url = new URL(myUrl);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
HttpURLConnection conn = null;
|
|
||||||
try {
|
|
||||||
|
|
||||||
conn = (HttpURLConnection) url.openConnection();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
conn.setDoInput(true);
|
|
||||||
|
|
||||||
conn.setReadTimeout(10000);
|
|
||||||
|
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
|
||||||
DataOutputStream dos = null;
|
|
||||||
int respCode = 0;
|
|
||||||
String inputString = null;
|
|
||||||
try {
|
|
||||||
conn.setRequestMethod(httpMethod);
|
|
||||||
|
|
||||||
if (Arrays.asList("POST", "PUT").contains(httpMethod)) {
|
|
||||||
String params = parameters.toString();
|
|
||||||
|
|
||||||
conn.setDoOutput(true);
|
|
||||||
|
|
||||||
dos = new DataOutputStream(conn.getOutputStream());
|
|
||||||
dos.writeBytes(params);
|
|
||||||
dos.flush();
|
|
||||||
dos.close();
|
|
||||||
}
|
|
||||||
respCode = conn.getResponseCode();
|
|
||||||
if (respCode != 200 && respCode != 201) {
|
|
||||||
String error = inputStreamToString(conn.getErrorStream());
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
inputString = inputStreamToString(conn.getInputStream());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return inputString;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String inputStreamToString(InputStream is) {
|
|
||||||
BufferedReader br = null;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
String line;
|
|
||||||
try {
|
|
||||||
|
|
||||||
br = new BufferedReader(new InputStreamReader(is));
|
|
||||||
while ((line = br.readLine()) != null) {
|
|
||||||
sb.append(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (br != null) {
|
|
||||||
try {
|
|
||||||
br.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,97 @@
|
|||||||
|
package controllers;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import play.Application;
|
||||||
|
import play.inject.guice.GuiceApplicationBuilder;
|
||||||
|
import play.mvc.Http;
|
||||||
|
import play.mvc.Result;
|
||||||
|
import play.test.WithApplication;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static play.mvc.Http.Status.*;
|
||||||
|
import static play.test.Helpers.GET;
|
||||||
|
import static play.test.Helpers.route;
|
||||||
|
|
||||||
|
public class HomeControllerUnitTest extends WithApplication {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Application provideApplication() {
|
||||||
|
return new GuiceApplicationBuilder().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenRootPath_ThenStatusOkay() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertEquals(OK, result.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenHtmlPath_ThenStatusOkay() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/html");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertEquals(OK, result.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenBadRequest_ThenStatusBadRequest() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/bad-req");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertEquals(BAD_REQUEST, result.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenNotFound_ThenStatusNotFound() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/not-found");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertEquals(NOT_FOUND, result.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenCustomContentTypePath_ThenContextTypeTextHtml() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/custom-content-type");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertTrue(result.contentType().isPresent());
|
||||||
|
assertEquals("text/html", result.contentType().get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenAsyncPath_ThenStatusOkay() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/async");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
assertEquals(OK, result.status());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRequest_whenHeadersPath_ThenCustomHeaderFieldSet() {
|
||||||
|
Http.RequestBuilder request = new Http.RequestBuilder()
|
||||||
|
.method(GET)
|
||||||
|
.uri("/baeldung/headers");
|
||||||
|
|
||||||
|
Result result = route(app, request);
|
||||||
|
final Optional<String> headerOptional = result.header("Header-Key");
|
||||||
|
assertTrue(headerOptional.isPresent());
|
||||||
|
assertEquals("Some value", headerOptional.get());
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
This software is licensed under the Apache 2 license, quoted below.
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with
|
|
||||||
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
|
||||||
language governing permissions and limitations under the License.
|
|
@ -1,49 +0,0 @@
|
|||||||
This is your new Play application
|
|
||||||
=================================
|
|
||||||
|
|
||||||
This file will be packaged with your application when using `activator dist`.
|
|
||||||
|
|
||||||
There are several demonstration files available in this template.
|
|
||||||
|
|
||||||
Controllers
|
|
||||||
===========
|
|
||||||
|
|
||||||
- HomeController.java:
|
|
||||||
|
|
||||||
Shows how to handle simple HTTP requests.
|
|
||||||
|
|
||||||
- AsyncController.java:
|
|
||||||
|
|
||||||
Shows how to do asynchronous programming when handling a request.
|
|
||||||
|
|
||||||
- CountController.java:
|
|
||||||
|
|
||||||
Shows how to inject a component into a controller and use the component when
|
|
||||||
handling requests.
|
|
||||||
|
|
||||||
Components
|
|
||||||
==========
|
|
||||||
|
|
||||||
- Module.java:
|
|
||||||
|
|
||||||
Shows how to use Guice to bind all the components needed by your application.
|
|
||||||
|
|
||||||
- Counter.java:
|
|
||||||
|
|
||||||
An example of a component that contains state, in this case a simple counter.
|
|
||||||
|
|
||||||
- ApplicationTimer.java:
|
|
||||||
|
|
||||||
An example of a component that starts when the application starts and stops
|
|
||||||
when the application stops.
|
|
||||||
|
|
||||||
Filters
|
|
||||||
=======
|
|
||||||
|
|
||||||
- Filters.java:
|
|
||||||
|
|
||||||
Creates the list of HTTP filters used by your application.
|
|
||||||
|
|
||||||
- ExampleFilter.java
|
|
||||||
|
|
||||||
A simple filter that adds a header to every response.
|
|
@ -1,46 +0,0 @@
|
|||||||
import javax.inject.*;
|
|
||||||
import play.*;
|
|
||||||
import play.mvc.EssentialFilter;
|
|
||||||
import play.http.HttpFilters;
|
|
||||||
import play.mvc.*;
|
|
||||||
|
|
||||||
import filters.ExampleFilter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class configures filters that run on every request. This
|
|
||||||
* class is queried by Play to get a list of filters.
|
|
||||||
*
|
|
||||||
* Play will automatically use filters from any class called
|
|
||||||
* <code>Filters</code> that is placed the root package. You can load filters
|
|
||||||
* from a different class by adding a `play.http.filters` setting to
|
|
||||||
* the <code>application.conf</code> configuration file.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class Filters implements HttpFilters {
|
|
||||||
|
|
||||||
private final Environment env;
|
|
||||||
private final EssentialFilter exampleFilter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param env Basic environment settings for the current application.
|
|
||||||
* @param exampleFilter A demonstration filter that adds a header to
|
|
||||||
*/
|
|
||||||
@Inject
|
|
||||||
public Filters(Environment env, ExampleFilter exampleFilter) {
|
|
||||||
this.env = env;
|
|
||||||
this.exampleFilter = exampleFilter;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public EssentialFilter[] filters() {
|
|
||||||
// Use the example filter if we're running development mode. If
|
|
||||||
// we're running in production or test mode then don't use any
|
|
||||||
// filters at all.
|
|
||||||
if (env.mode().equals(Mode.DEV)) {
|
|
||||||
return new EssentialFilter[] { exampleFilter };
|
|
||||||
} else {
|
|
||||||
return new EssentialFilter[] {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
import com.google.inject.AbstractModule;
|
|
||||||
import java.time.Clock;
|
|
||||||
|
|
||||||
import services.ApplicationTimer;
|
|
||||||
import services.AtomicCounter;
|
|
||||||
import services.Counter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class is a Guice module that tells Guice how to bind several
|
|
||||||
* different types. This Guice module is created when the Play
|
|
||||||
* application starts.
|
|
||||||
*
|
|
||||||
* Play will automatically use any class called `Module` that is in
|
|
||||||
* the root package. You can create modules in other locations by
|
|
||||||
* adding `play.modules.enabled` settings to the `application.conf`
|
|
||||||
* configuration file.
|
|
||||||
*/
|
|
||||||
public class Module extends AbstractModule {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configure() {
|
|
||||||
// Use the system clock as the default implementation of Clock
|
|
||||||
bind(Clock.class).toInstance(Clock.systemDefaultZone());
|
|
||||||
// Ask Guice to create an instance of ApplicationTimer when the
|
|
||||||
// application starts.
|
|
||||||
bind(ApplicationTimer.class).asEagerSingleton();
|
|
||||||
// Set AtomicCounter as the implementation for Counter.
|
|
||||||
bind(Counter.class).to(AtomicCounter.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +1,13 @@
|
|||||||
package controllers;
|
package controllers;
|
||||||
|
|
||||||
|
import play.libs.concurrent.HttpExecutionContext;
|
||||||
import play.mvc.*;
|
import play.mvc.*;
|
||||||
|
import play.twirl.api.Html;
|
||||||
|
|
||||||
import views.html.*;
|
import javax.inject.Inject;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
|
||||||
|
import static java.util.concurrent.CompletableFuture.supplyAsync;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This controller contains an action to handle HTTP requests
|
* This controller contains an action to handle HTTP requests
|
||||||
@ -16,18 +21,33 @@ public class HomeController extends Controller {
|
|||||||
* this method will be called when the application receives a
|
* this method will be called when the application receives a
|
||||||
* <code>GET</code> request with a path of <code>/</code>.
|
* <code>GET</code> request with a path of <code>/</code>.
|
||||||
*/
|
*/
|
||||||
public Result index(String author,int id) {
|
public Result index() {
|
||||||
return ok("Routing in Play by:"+author+" ID:"+id);
|
return ok(views.html.index.render());
|
||||||
}
|
|
||||||
public Result greet(String name,int age) {
|
|
||||||
return ok("Hello "+name+", you are "+age+" years old");
|
|
||||||
}
|
|
||||||
public Result introduceMe(String data) {
|
|
||||||
String[] clientData=data.split(",");
|
|
||||||
return ok("Your name is "+clientData[0]+", you are "+clientData[1]+" years old");
|
|
||||||
}
|
|
||||||
public Result squareMe(Long num) {
|
|
||||||
return ok(num+" Squared is "+(num*num));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Result writer(String author) {
|
||||||
|
return ok("Routing in Play by " + author);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result viewUser(String userId) {
|
||||||
|
final String response = String.format("Got user id {} in request.", userId);
|
||||||
|
return ok(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result greet(String name, int age) {
|
||||||
|
return ok("Hello " + name + ", you are " + age + " years old");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result squareMe(Long num) {
|
||||||
|
return ok(num + " Squared is " + (num * num));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result writer(String author, int id) {
|
||||||
|
return ok("Routing in Play by: " + author + " ID: " + id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result introduceMe(String data) {
|
||||||
|
String[] clientData = data.split(",");
|
||||||
|
return ok("Your name is " + clientData[0] + ", you are " + clientData[1] + " years old");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
package filters;
|
|
||||||
|
|
||||||
import akka.stream.Materializer;
|
|
||||||
import java.util.concurrent.CompletionStage;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import javax.inject.*;
|
|
||||||
import play.mvc.*;
|
|
||||||
import play.mvc.Http.RequestHeader;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a simple filter that adds a header to all requests. It's
|
|
||||||
* added to the application's list of filters by the
|
|
||||||
* {@link Filters} class.
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class ExampleFilter extends Filter {
|
|
||||||
|
|
||||||
private final Executor exec;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mat This object is needed to handle streaming of requests
|
|
||||||
* and responses.
|
|
||||||
* @param exec This class is needed to execute code asynchronously.
|
|
||||||
* It is used below by the <code>thenAsyncApply</code> method.
|
|
||||||
*/
|
|
||||||
@Inject
|
|
||||||
public ExampleFilter(Materializer mat, Executor exec) {
|
|
||||||
super(mat);
|
|
||||||
this.exec = exec;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletionStage<Result> apply(
|
|
||||||
Function<RequestHeader, CompletionStage<Result>> next,
|
|
||||||
RequestHeader requestHeader) {
|
|
||||||
|
|
||||||
return next.apply(requestHeader).thenApplyAsync(
|
|
||||||
result -> result.withHeader("X-ExampleFilter", "foo"),
|
|
||||||
exec
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user