Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -1,19 +1,32 @@
|
||||
package com.baeldung.examples.asm.instrumentation;
|
||||
|
||||
import com.baeldung.examples.asm.CustomClassWriter;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author baeldung
|
||||
*/
|
||||
public class Premain {
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
inst.addTransformer((l, name, c, d, b) -> {
|
||||
inst.addTransformer(new ClassFileTransformer() {
|
||||
|
||||
if (name.equals("java/lang/Integer")) {
|
||||
CustomClassWriter cr = new CustomClassWriter(b);
|
||||
return cr.addField();
|
||||
@Override
|
||||
public byte[] transform(ClassLoader l, String name, Class c,
|
||||
ProtectionDomain d, byte[] b)
|
||||
throws IllegalClassFormatException {
|
||||
|
||||
if (name.equals("java/lang/Integer")) {
|
||||
CustomClassWriter cr = new CustomClassWriter(b);
|
||||
return cr.addField();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
return b;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
/**
|
||||
* This class demonstrates the use of static fields and static methods
|
||||
* the instance variables engine and displacement are distinct for
|
||||
* each and every object whereas static/class variable numberOfCars
|
||||
* is unique and is shared across all objects of this class.
|
||||
*
|
||||
* @author baeldung
|
||||
*
|
||||
*/
|
||||
public class Car {
|
||||
private String name;
|
||||
private String engine;
|
||||
|
||||
public static int numberOfCars;
|
||||
|
||||
public Car(String name, String engine) {
|
||||
this.name = name;
|
||||
this.engine = engine;
|
||||
numberOfCars++;
|
||||
}
|
||||
|
||||
//getters and setters
|
||||
public static int getNumberOfCars() {
|
||||
return numberOfCars;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
public void setEngine(String engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public static void setNumberOfCars(int numberOfCars) {
|
||||
Car.numberOfCars = numberOfCars;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
public class Singleton {
|
||||
private Singleton() {}
|
||||
|
||||
private static class SingletonHolder {
|
||||
public static final Singleton instance = new Singleton();
|
||||
}
|
||||
|
||||
public static Singleton getInstance() {
|
||||
return SingletonHolder.instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class StaticBlock {
|
||||
private static List<String> ranks = new LinkedList<>();
|
||||
|
||||
static {
|
||||
ranks.add("Lieutenant");
|
||||
ranks.add("Captain");
|
||||
ranks.add("Major");
|
||||
}
|
||||
|
||||
static {
|
||||
ranks.add("Colonel");
|
||||
ranks.add("General");
|
||||
}
|
||||
|
||||
//getters and setters
|
||||
public static List<String> getRanks() {
|
||||
return ranks;
|
||||
}
|
||||
|
||||
public static void setRanks(List<String> ranks) {
|
||||
StaticBlock.ranks = ranks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CarIntegrationTest {
|
||||
@Test
|
||||
public void whenNumberOfCarObjectsInitialized_thenStaticCounterIncreases() {
|
||||
new Car("Jaguar", "V8");
|
||||
new Car("Bugatti", "W16");
|
||||
assertEquals(2, Car.numberOfCars);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SingletonIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenStaticInnerClass_whenMultipleTimesInstanceCalled_thenOnlyOneTimeInitialized() {
|
||||
Singleton object1 = Singleton.getInstance();
|
||||
Singleton object2 = Singleton.getInstance();
|
||||
|
||||
Assert.assertSame(object1, object2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StaticBlockIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() {
|
||||
List<String> actualList = StaticBlock.getRanks();
|
||||
assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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>
|
||||
|
||||
<artifactId>drools-backward-chaining</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>drools-backward-chaining</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<runtime.version>6.4.0.Final</runtime.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.kie</groupId>
|
||||
<artifactId>kie-api</artifactId>
|
||||
<version>${runtime.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-core</artifactId>
|
||||
<version>${runtime.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-decisiontables</artifactId>
|
||||
<version>${runtime.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
|
||||
import com.baeldung.model.Beatle;
|
||||
|
||||
public class BackwardChainingBeatles {
|
||||
public static void main(String[] args) {
|
||||
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
KieSession kSession = kContainer.newKieSession("ksession-backward-chaining");
|
||||
// drools session base on the xml configuration (<strong>kmodule.xml</strong>)
|
||||
|
||||
// graph population
|
||||
kSession.insert(new Beatle("Starr", "drums"));
|
||||
kSession.insert(new Beatle("McCartney", "bass"));
|
||||
kSession.insert(new Beatle("Lennon", "guitar"));
|
||||
kSession.insert(new Beatle("Harrison", "guitar"));
|
||||
|
||||
kSession.insert("Ringo"); // invoke the rule that calls the query implentation of backward chaining
|
||||
kSession.fireAllRules(); // invoke all the rules
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import org.kie.api.definition.type.Position;
|
||||
|
||||
public class Beatle {
|
||||
|
||||
@Position(0)
|
||||
private String lastName;
|
||||
@Position(1)
|
||||
private String instrument;
|
||||
|
||||
public Beatle(String lastName, String instrument) {
|
||||
this.lastName = lastName;
|
||||
this.instrument = instrument;
|
||||
}
|
||||
|
||||
public String getInstrument() {
|
||||
return instrument;
|
||||
}
|
||||
|
||||
public void setInstrument(String instrument) {
|
||||
this.instrument = instrument;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
|
||||
<kbase name="backward_chaining" packages="backward_chaining">
|
||||
<ksession name="ksession-backward-chaining"/>
|
||||
</kbase>
|
||||
</kmodule>
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung
|
||||
|
||||
import com.baeldung.model.Beatle;
|
||||
|
||||
|
||||
query whichBeatle(String lastName, String instrument)
|
||||
Beatle(lastName, instrument;)
|
||||
or
|
||||
(Beatle(lastName, null;)
|
||||
and
|
||||
whichBeatle(null, instrument;)) //recursive call to the function that allows to search in a derivation tree structure
|
||||
end
|
||||
|
||||
rule "Ringo"
|
||||
when
|
||||
String(this == "Ringo")
|
||||
whichBeatle("Starr", "drums";)
|
||||
then
|
||||
System.out.println("The beatle is Ringo Starr");
|
||||
end
|
||||
|
||||
rule "Paul"
|
||||
when
|
||||
String(this == "Paul")
|
||||
whichBeatle("McCartney", "bass";)
|
||||
then
|
||||
System.out.println("The beatle is Paul McCartney");
|
||||
end
|
||||
|
||||
rule "John"
|
||||
when
|
||||
String(this == "John")
|
||||
whichBeatle("Lennon", "guitar";)
|
||||
then
|
||||
System.out.println("The beatle is John Lennon");
|
||||
end
|
||||
|
||||
rule "George"
|
||||
when
|
||||
String(this == "George")
|
||||
whichBeatle("Harrison", "guitar";)
|
||||
then
|
||||
System.out.println("The beatle is George Harrison");
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1 @@
|
||||
## Relevant articles:
|
||||
@@ -0,0 +1,59 @@
|
||||
<?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.forketyfork.guest</groupId>
|
||||
<artifactId>spring-mvc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-mvc</name>
|
||||
<description>Spring MVC sample project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.M5</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.forketyfork.guest.springmvc;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.forketyfork.guest.springmvc"})
|
||||
public class Spring5Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Spring5Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.forketyfork.guest.springmvc.model;
|
||||
|
||||
public class LoginData {
|
||||
|
||||
private String login;
|
||||
|
||||
private String password;
|
||||
|
||||
public LoginData() {
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.forketyfork.guest.springmvc.web;
|
||||
|
||||
import com.forketyfork.guest.springmvc.model.LoginData;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Controller
|
||||
public class InternalsController {
|
||||
|
||||
private static final String LOGIN = "jack";
|
||||
private static final String PASSWORD = "halloween";
|
||||
|
||||
@GetMapping("/")
|
||||
public String hello() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ModelAndView login(LoginData loginData) {
|
||||
if (LOGIN.equals(loginData.getLogin()) && PASSWORD.equals(loginData.getPassword())) {
|
||||
return new ModelAndView("success", Collections.singletonMap("login", loginData.getLogin()));
|
||||
} else {
|
||||
return new ModelAndView("failure", Collections.singletonMap("login", loginData.getLogin()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Login Failure</title>
|
||||
</head>
|
||||
<p>
|
||||
User with login <span th:text="${login}"></span> not found, please <a href="/">try again</a>.
|
||||
</p>
|
||||
</html>
|
||||
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head><title>Login Form</title></head>
|
||||
<body>
|
||||
|
||||
<form method="post" action="/login">
|
||||
<input type="text" placeholder="Login" name="login"/>
|
||||
<input type="password" placeholder="Password" name="password"/>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user