Merge branch 'master' into SpringReactiveKotlin
This commit is contained in:
@@ -19,7 +19,7 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
|
||||
|
||||
Building the project
|
||||
====================
|
||||
To do the full build, do: `mvn install -Dgib.enabled=false`
|
||||
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
|
||||
|
||||
|
||||
Working with the code in Eclipse
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-avro-tutorial</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<compiler-plugin.version>3.5</compiler-plugin.version>
|
||||
<avro.version>1.8.2</avro.version>
|
||||
<java.version>1.8</java.version>
|
||||
<slf4j.version>1.7.25</slf4j.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-compiler</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>schemas</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>schema</goal>
|
||||
<goal>protocol</goal>
|
||||
<goal>idl-protocol</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
|
||||
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.avro.util;
|
||||
|
||||
import org.apache.avro.Schema;
|
||||
import org.apache.avro.compiler.specific.SpecificCompiler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroClassGenerator {
|
||||
public void generateAvroClasses() throws IOException {
|
||||
SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/resources/avroHttpRequest-schema.avsc")));
|
||||
compiler.compileToDestination(new File("src/main/resources"), new File("src/main/java"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.avro.util;
|
||||
|
||||
|
||||
import org.apache.avro.Schema;
|
||||
import org.apache.avro.SchemaBuilder;
|
||||
|
||||
public class AvroSchemaBuilder {
|
||||
|
||||
public Schema createAvroHttpRequestSchema(){
|
||||
|
||||
Schema clientIdentifier = SchemaBuilder.record("ClientIdentifier").namespace("com.baeldung.avro.model")
|
||||
.fields().requiredString("hostName").requiredString("ipAddress").endRecord();
|
||||
|
||||
Schema avroHttpRequest = SchemaBuilder.record("AvroHttpRequest").namespace("com.baeldung.avro.model").fields()
|
||||
.requiredLong("requestTime")
|
||||
.name("clientIdentifier").type(clientIdentifier).noDefault()
|
||||
.name("employeeNames").type().array().items().stringType().arrayDefault(null)
|
||||
.name("active").type().enumeration("Active").symbols("YES", "NO").noDefault()
|
||||
.endRecord();
|
||||
return avroHttpRequest;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Autogenerated by Avro
|
||||
*
|
||||
* DO NOT EDIT DIRECTLY
|
||||
*/
|
||||
package com.baeldung.avro.util.model;
|
||||
@SuppressWarnings("all")
|
||||
@org.apache.avro.specific.AvroGenerated
|
||||
public enum Active {
|
||||
YES, NO ;
|
||||
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Active\",\"namespace\":\"com.baeldung.avro.model\",\"symbols\":[\"YES\",\"NO\"]}");
|
||||
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import org.apache.avro.io.DatumReader;
|
||||
import org.apache.avro.io.Decoder;
|
||||
import org.apache.avro.io.DecoderFactory;
|
||||
import org.apache.avro.specific.SpecificDatumReader;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroDeSerealizer {
|
||||
|
||||
public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data){
|
||||
DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class);
|
||||
Decoder decoder = null;
|
||||
try {
|
||||
decoder = DecoderFactory.get().jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data));
|
||||
return reader.read(null, decoder);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data){
|
||||
DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class);
|
||||
Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
|
||||
try {
|
||||
return employeeReader.read(null, decoder);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import org.apache.avro.io.*;
|
||||
import org.apache.avro.specific.SpecificDatumWriter;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroSerealizer {
|
||||
|
||||
public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request){
|
||||
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
|
||||
byte[] data = new byte[0];
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Encoder jsonEncoder = null;
|
||||
try {
|
||||
jsonEncoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.getClassSchema(), stream);
|
||||
writer.write(request, jsonEncoder);
|
||||
jsonEncoder.flush();
|
||||
data = stream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
data =null;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request){
|
||||
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
|
||||
byte[] data = new byte[0];
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Encoder jsonEncoder = EncoderFactory.get().binaryEncoder(stream,null);
|
||||
try {
|
||||
writer.write(request, jsonEncoder);
|
||||
jsonEncoder.flush();
|
||||
data = stream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
data = null;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"type":"record",
|
||||
"name":"AvroHttpRequest",
|
||||
"namespace":"com.baeldung.avro.model",
|
||||
"fields":[
|
||||
{
|
||||
"name":"requestTime",
|
||||
"type":"long"
|
||||
},
|
||||
{
|
||||
"name":"clientIdentifier",
|
||||
"type":{
|
||||
"type":"record",
|
||||
"name":"ClientIdentifier",
|
||||
"fields":[
|
||||
{
|
||||
"name":"hostName",
|
||||
"type":"string"
|
||||
},
|
||||
{
|
||||
"name":"ipAddress",
|
||||
"type":"string"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name":"employeeNames",
|
||||
"type":{
|
||||
"type":"array",
|
||||
"items":"string"
|
||||
},
|
||||
"default":null
|
||||
},
|
||||
{
|
||||
"name":"active",
|
||||
"type":{
|
||||
"type":"enum",
|
||||
"name":"Active",
|
||||
"symbols":[
|
||||
"YES",
|
||||
"NO"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.Active;
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import com.baeldung.avro.util.model.ClientIdentifier;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AvroSerealizerDeSerealizerTest {
|
||||
|
||||
AvroSerealizer serealizer;
|
||||
AvroDeSerealizer deSerealizer;
|
||||
AvroHttpRequest request;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
serealizer = new AvroSerealizer();
|
||||
deSerealizer = new AvroDeSerealizer();
|
||||
|
||||
ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder().
|
||||
setHostName("localhost").setIpAddress("255.255.255.0").build();
|
||||
|
||||
List<CharSequence> employees = new ArrayList();
|
||||
employees.add("James");
|
||||
employees.add("Alice");
|
||||
employees.add("David");
|
||||
employees.add("Han");
|
||||
|
||||
request = AvroHttpRequest.newBuilder().setRequestTime(01l)
|
||||
.setActive(Active.YES).setClientIdentifier(clientIdentifier)
|
||||
.setEmployeeNames(employees).build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenSerialized_UsingJSONEncoder_ObjectGetsSerialized(){
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
|
||||
assertTrue(Objects.nonNull(data));
|
||||
assertTrue(data.length > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenSerialized_UsingBinaryEncoder_ObjectGetsSerialized(){
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
|
||||
assertTrue(Objects.nonNull(data));
|
||||
assertTrue(data.length > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenDeserialize_UsingJSONDecoder_ActualAndExpectedObjectsAreEqual(){
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
|
||||
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data);
|
||||
assertEquals(actualRequest,request);
|
||||
assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenDeserialize_UsingBinaryecoder_ActualAndExpectedObjectsAreEqual(){
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
|
||||
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data);
|
||||
assertEquals(actualRequest,request);
|
||||
assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
public class Animal {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Animal(int id, String name) {
|
||||
this.id = id;
|
||||
this.setName(name);
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
public class ConvertListToMapService {
|
||||
|
||||
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
for (Animal animal : list) {
|
||||
map.put(animal.getId(), animal);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
|
||||
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = Maps.uniqueIndex(list, Animal::getId);
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithApacheCommons1(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
|
||||
IterableUtils.forEach(list, animal -> {
|
||||
map.put(animal.getId(), animal);
|
||||
});
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public Map<Integer, Animal> convertListWithApacheCommons2(List<Animal> list) {
|
||||
|
||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
||||
|
||||
MapUtils.populateMap(map, list, Animal::getId);
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConvertListToMapServiceUnitTest {
|
||||
List<Animal> list;
|
||||
|
||||
private ConvertListToMapService convertListService;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.convertListService = new ConvertListToMapService();
|
||||
this.list = new ArrayList<>();
|
||||
|
||||
Animal cat = new Animal(1, "Cat");
|
||||
list.add(cat);
|
||||
Animal dog = new Animal(2, "Dog");
|
||||
list.add(dog);
|
||||
Animal pig = new Animal(3, "Pig");
|
||||
list.add(pig);
|
||||
Animal cow = new Animal(4, "Cow");
|
||||
list.add(cow);
|
||||
Animal goat = new Animal(5, "Goat");
|
||||
list.add(goat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListAfterJava8(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithGuava(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons2(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
}
|
||||
+3
-14
@@ -50,22 +50,9 @@ public class HashSetInitalizingUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
|
||||
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toSet());
|
||||
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
@Test
|
||||
public void whenUsingJava8_fromStringArray_thenCorrectSize() {
|
||||
String[] stringArray = {"a","b","c"};
|
||||
Set<String> set = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
// Requires Java9 - uncomment if you are using Java 9 or higher
|
||||
/*@Test
|
||||
public void whenUsingJava9_usingCollectOnStream_thenCorrectSize() {
|
||||
Set set = Set.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
||||
@@ -78,4 +65,6 @@ public class HashSetInitalizingUnitTest {
|
||||
Set<String> set = ImmutableSet.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -60,7 +60,6 @@
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin-reflect.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlinx</groupId>
|
||||
@@ -224,10 +223,11 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin-maven-plugin.version>1.2.41</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.41</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.41</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.41</kotlin-reflect.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
|
||||
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
|
||||
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
|
||||
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
|
||||
<kotlinx.version>0.22.5</kotlinx.version>
|
||||
<ktor.io.version>0.9.2</ktor.io.version>
|
||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
|
||||
open class LoggerAsExtensionOnAny {
|
||||
val logger = logger()
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionSubclass : LoggerAsExtensionOnAny()
|
||||
|
||||
fun <T : Any> T.logger(): Logger = getLogger(getClassForLogging(javaClass))
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnAny().log("test")
|
||||
ExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
1.logger().info("uh-oh!")
|
||||
SomeOtherClass().logger()
|
||||
}
|
||||
|
||||
class SomeOtherClass {
|
||||
fun logger(): String {
|
||||
return "foo"
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
interface Logging
|
||||
|
||||
inline fun <reified T : Logging> T.logger(): Logger =
|
||||
//Wrong logger name!
|
||||
//LoggerFactory.getLogger(javaClass.name + " w/interface")
|
||||
LoggerFactory.getLogger(getClassForLogging(T::class.java).name + " w/interface")
|
||||
|
||||
open class LoggerAsExtensionOnMarkerInterface : Logging {
|
||||
companion object : Logging {
|
||||
val logger = logger()
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
logger().info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
class MarkerExtensionSubclass : LoggerAsExtensionOnMarkerInterface()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsExtensionOnMarkerInterface().log("test")
|
||||
MarkerExtensionSubclass().log("sub")
|
||||
"foo".logger().info("foo")
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerAsProperty {
|
||||
private val logger = getLogger(javaClass)
|
||||
|
||||
fun log(s: String) {
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class PropertySubclass : LoggerAsProperty()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsProperty().log("test")
|
||||
PropertySubclass().log("sub")
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
open class LoggerAsPropertyDelegate {
|
||||
private val lazyLogger by lazyLogger()
|
||||
protected val logger by LoggerDelegate()
|
||||
private val logger2 = logger
|
||||
|
||||
companion object {
|
||||
private val lazyLoggerComp by lazyLogger()
|
||||
private val loggerComp by LoggerDelegate()
|
||||
}
|
||||
|
||||
open fun log(s: String) {
|
||||
logger.info(s)
|
||||
logger2.info(s)
|
||||
lazyLogger.info(s)
|
||||
loggerComp.info(s)
|
||||
lazyLoggerComp.info(s)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DelegateSubclass : LoggerAsPropertyDelegate() {
|
||||
override fun log(s: String) {
|
||||
logger.info("-- in sub")
|
||||
super.log(s)
|
||||
}
|
||||
}
|
||||
|
||||
fun lazyLogger(forClass: Class<*>): Lazy<Logger> =
|
||||
lazy { getLogger(getClassForLogging(forClass)) }
|
||||
|
||||
fun <T : Any> T.lazyLogger(): Lazy<Logger> = lazyLogger(javaClass)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerAsPropertyDelegate().log("test")
|
||||
DelegateSubclass().log("sub")
|
||||
}
|
||||
|
||||
class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>) =
|
||||
getLogger(getClassForLogging(thisRef.javaClass))
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
open class LoggerInCompanionObject {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(LoggerInCompanionObject::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
|
||||
class Inner {
|
||||
companion object {
|
||||
private val loggerWithExplicitClass = getLogger(Inner::class.java)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val loggerWithWrongClass = getLogger(javaClass)
|
||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||
@JvmStatic
|
||||
private val logger = getLogger(javaClass.enclosingClass)
|
||||
}
|
||||
|
||||
fun log(s: String) {
|
||||
loggerWithExplicitClass.info(s)
|
||||
loggerWithWrongClass.info(s)
|
||||
logger.info(s)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CompanionSubclass : LoggerInCompanionObject()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
LoggerInCompanionObject().log("test")
|
||||
LoggerInCompanionObject.Inner().log("test")
|
||||
CompanionSubclass().log("sub")
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.kotlin.logging
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.reflect.full.companionObject
|
||||
|
||||
fun getLogger(forClass: Class<*>): Logger = LoggerFactory.getLogger(forClass)
|
||||
|
||||
fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
|
||||
return javaClass.enclosingClass?.takeIf {
|
||||
it.kotlin.companionObject?.java == javaClass
|
||||
} ?: javaClass
|
||||
}
|
||||
+57
-56
@@ -1,61 +1,62 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>geotools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>geotools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>geotools</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>geotools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools-shapefile.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools-swing.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-shapefile</artifactId>
|
||||
<version>${geotools-shapefile.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-epsg-hsql</artifactId>
|
||||
<version>${geotools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.geotools</groupId>
|
||||
<artifactId>gt-swing</artifactId>
|
||||
<version>${geotools-swing.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<geotools-swing.version>15.2</geotools-swing.version>
|
||||
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
||||
</properties>
|
||||
|
||||
<properties>
|
||||
<geotools.version>15.2</geotools.version>
|
||||
<geotools-swing.version>15.2</geotools-swing.version>
|
||||
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>hibernate5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>hibernate5</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@@ -59,9 +57,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<!-- Maven plugins -->
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<hibernate.version>5.3.2.Final</hibernate.version>
|
||||
<mysql.version>6.0.6</mysql.version>
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern> %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -3,28 +3,9 @@
|
||||
<configuration scan="true">
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||
|
||||
<!-- The FILE and ASYNC appenders are here as examples for a production configuration -->
|
||||
<!--
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<maxHistory>90</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<charset>utf-8</charset>
|
||||
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<logger name="com.baeldung" level="INFO"/>
|
||||
|
||||
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<queueSize>512</queueSize>
|
||||
<appender-ref ref="FILE"/>
|
||||
</appender>
|
||||
-->
|
||||
|
||||
<logger name="com.baeldung" level="#logback.loglevel#"/>
|
||||
|
||||
<logger name="io.github.jhipster" level="DEBUG"/>
|
||||
<logger name="io.github.jhipster" level="INFO"/>
|
||||
|
||||
<logger name="javax.activation" level="WARN"/>
|
||||
<logger name="javax.mail" level="WARN"/>
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.jnosql</groupId>
|
||||
<artifactId>jnosql</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>jnosql-artemis</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>${artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
||||
<artifactId>liberty-maven-plugin</artifactId>
|
||||
<version>${liberty-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<assemblyArtifact>
|
||||
<groupId>io.openliberty</groupId>
|
||||
<artifactId>openliberty-webProfile8</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<type>zip</type>
|
||||
</assemblyArtifact>
|
||||
<installAppPackages>project</installAppPackages>
|
||||
<looseApplication>true</looseApplication>
|
||||
<configFile>src/main/liberty/config/server.xml</configFile>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-server</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>install-server</goal>
|
||||
<goal>create-server</goal>
|
||||
<goal>install-feature</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>install-apps</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>install-apps</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-web-api</artifactId>
|
||||
<version>8.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jnosql.artemis</groupId>
|
||||
<artifactId>artemis-configuration</artifactId>
|
||||
<version>${jnosql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jnosql.artemis</groupId>
|
||||
<artifactId>artemis-document</artifactId>
|
||||
<version>${jnosql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jnosql.diana</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
<version>${jnosql.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.jnosql.artemis;
|
||||
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
|
||||
@ApplicationPath("")
|
||||
public class AppConfig extends Application {
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.jnosql.artemis;
|
||||
|
||||
import de.flapdoodle.embed.mongo.MongodExecutable;
|
||||
import de.flapdoodle.embed.mongo.MongodProcess;
|
||||
import de.flapdoodle.embed.mongo.MongodStarter;
|
||||
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
|
||||
import de.flapdoodle.embed.mongo.config.Net;
|
||||
import de.flapdoodle.embed.mongo.distribution.Version;
|
||||
import de.flapdoodle.embed.process.runtime.Network;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Destroyed;
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Observes;
|
||||
import java.io.IOException;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EmbeddedMongoDBSetup {
|
||||
|
||||
private static final String MONGODB_HOST = "localhost";
|
||||
private static final int MONGODB_PORT = 27019;
|
||||
|
||||
private static final MongodStarter starter = MongodStarter.getDefaultInstance();
|
||||
private static MongodExecutable _mongodExe;
|
||||
private static MongodProcess _mongod;
|
||||
|
||||
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
|
||||
try {
|
||||
System.out.println("Starting Embedded MongoDB");
|
||||
initdb();
|
||||
System.out.println("Embedded MongoDB started");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Embedded MongoDB starting error !!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void initdb() throws IOException {
|
||||
_mongodExe = starter.prepare(new MongodConfigBuilder()
|
||||
.version(Version.Main.DEVELOPMENT)
|
||||
.net(new Net(MONGODB_HOST, MONGODB_PORT, Network.localhostIsIPv6()))
|
||||
.build());
|
||||
_mongod = _mongodExe.start();
|
||||
}
|
||||
|
||||
public void destroy(@Observes @Destroyed(ApplicationScoped.class) Object init) {
|
||||
System.out.println("Stopping Embedded MongoDB");
|
||||
_mongod.stop();
|
||||
_mongodExe.stop();
|
||||
System.out.println("Embedded MongoDB stopped !");
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.jnosql.artemis;
|
||||
|
||||
import org.jnosql.artemis.ConfigurationUnit;
|
||||
import org.jnosql.diana.api.document.DocumentCollectionManager;
|
||||
import org.jnosql.diana.api.document.DocumentCollectionManagerFactory;
|
||||
import org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager;
|
||||
import org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Disposes;
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EntityManagerProducer {
|
||||
|
||||
private static final String DATABASE = "todos";
|
||||
|
||||
@Inject
|
||||
@ConfigurationUnit(name = "document")
|
||||
private DocumentCollectionManagerFactory<MongoDBDocumentCollectionManager> managerFactory;
|
||||
|
||||
@Produces
|
||||
public DocumentCollectionManager getEntityManager() {
|
||||
return managerFactory.get(DATABASE);
|
||||
}
|
||||
|
||||
public void close(@Disposes DocumentCollectionManager entityManager) {
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user