Skip to content

Commit eaea7b8

Browse files
committed
Initial commit
0 parents  commit eaea7b8

17 files changed

+294
-0
lines changed

.classpath

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="test" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
33+
<attributes>
34+
<attribute name="maven.pomderived" value="true"/>
35+
</attributes>
36+
</classpathentry>
37+
<classpathentry kind="src" path="target/generated-sources/annotations">
38+
<attributes>
39+
<attribute name="optional" value="true"/>
40+
<attribute name="maven.pomderived" value="true"/>
41+
<attribute name="ignore_optional_problems" value="true"/>
42+
<attribute name="m2e-apt" value="true"/>
43+
</attributes>
44+
</classpathentry>
45+
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
46+
<attributes>
47+
<attribute name="optional" value="true"/>
48+
<attribute name="maven.pomderived" value="true"/>
49+
<attribute name="ignore_optional_problems" value="true"/>
50+
<attribute name="m2e-apt" value="true"/>
51+
<attribute name="test" value="true"/>
52+
</attributes>
53+
</classpathentry>
54+
<classpathentry kind="output" path="target/classes"/>
55+
</classpath>

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
.mvn/wrapper/maven-wrapper.jar

.project

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>shortbus</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.jdt.core.javanature</nature>
26+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
27+
</natures>
28+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding//src/test/java=UTF-8
5+
encoding//src/test/resources=UTF-8
6+
encoding/<project>=UTF-8
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.apt.aptEnabled=false

.settings/org.eclipse.jdt.core.prefs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
5+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
6+
org.eclipse.jdt.core.compiler.processAnnotations=disabled
7+
org.eclipse.jdt.core.compiler.release=disabled
8+
org.eclipse.jdt.core.compiler.source=1.8

.settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boot.validation.initialized=true
2+
eclipse.preferences.version=1

pom.xml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.kazuhito</groupId>
6+
<artifactId>shortbus</artifactId>
7+
<version>0.1.0</version>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>2.0.5.RELEASE</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
</dependencies>
21+
22+
<properties>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
<maven.compiler.target>1.8</maven.compiler.target>
25+
</properties>
26+
</project>
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package shortbus;
2+
3+
import java.util.Arrays;
4+
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.context.annotation.Bean;
10+
11+
import shortbus.example.HelloWorldCommand;
12+
import shortbus.example.HelloWorldCommandHandler;
13+
14+
@SpringBootApplication
15+
public class Application {
16+
17+
public static void main(String[] args) {
18+
ApplicationContext ctx = SpringApplication.run(Application.class, args);
19+
20+
Mediator mediator = new MediatorImpl(ctx);
21+
Response<String> response = mediator.request(new HelloWorldCommand());
22+
System.out.println(response);
23+
}
24+
25+
@Bean
26+
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
27+
return args -> {
28+
HelloWorldCommandHandler commandhandler = ctx.getBean(HelloWorldCommandHandler.class);
29+
System.out.println(commandhandler);
30+
31+
System.out.println("Let's inspect the beans provided by Spring Boot:");
32+
33+
String[] beanNames = ctx.getBeanDefinitionNames();
34+
Arrays.sort(beanNames);
35+
for (String beanName : beanNames) {
36+
System.out.println(beanName);
37+
}
38+
};
39+
}
40+
41+
}

src/main/java/shortbus/Mediator.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package shortbus;
2+
3+
public interface Mediator {
4+
public <T> Response<T> request(Request<T> request);
5+
}
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package shortbus;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.ParameterizedType;
6+
import java.lang.reflect.Type;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
10+
import org.springframework.context.ApplicationContext;
11+
12+
public class MediatorImpl implements Mediator {
13+
14+
private ApplicationContext ctx;
15+
16+
public MediatorImpl(ApplicationContext ctx) {
17+
this.ctx = ctx;
18+
}
19+
20+
@Override
21+
public <T> Response<T> request(Request<T> request) {
22+
Response<T> response = new Response<>();
23+
try {
24+
MediatorPlan<T> plan = new MediatorPlan<>(RequestHandler.class, "handle", request.getClass(), ctx);
25+
response.data = plan.invoke(request);
26+
} catch (Exception e) {
27+
response.exception = e;
28+
}
29+
return response;
30+
}
31+
32+
class MediatorPlan<T> {
33+
Method handleMethod;
34+
Object handlerInstanceBuilder;
35+
36+
public MediatorPlan(Class<?> handlerType, String handlerMethodName, Class<?> messageType,
37+
ApplicationContext context) throws NoSuchMethodException, SecurityException, ClassNotFoundException {
38+
handlerInstanceBuilder = getBean(handlerType, messageType, context);
39+
handleMethod = handlerInstanceBuilder.getClass().getDeclaredMethod(handlerMethodName, messageType);
40+
}
41+
42+
private Object getBean(Class<?> handlerType, Class<?> messageType, ApplicationContext context)
43+
throws ClassNotFoundException {
44+
Map<String, ?> beans = context.getBeansOfType(handlerType);
45+
for (Entry<String, ?> entry : beans.entrySet()) {
46+
Class<?> clazz = entry.getValue().getClass();
47+
Type[] interfaces = clazz.getGenericInterfaces();
48+
for (Type interace : interfaces) {
49+
Type parameterType = ((ParameterizedType) interace).getActualTypeArguments()[0];
50+
if (parameterType.equals(messageType)) {
51+
return entry.getValue();
52+
}
53+
}
54+
}
55+
56+
throw new ClassNotFoundException("Handler not found. Did you forget to register this?");
57+
}
58+
59+
public T invoke(Request<T> request)
60+
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
61+
return (T) handleMethod.invoke(handlerInstanceBuilder, request);
62+
}
63+
}
64+
65+
}

src/main/java/shortbus/Request.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package shortbus;
2+
3+
public interface Request<T> {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package shortbus;
2+
3+
public interface RequestHandler<T, R> {
4+
public R handle(T request);
5+
}

src/main/java/shortbus/Response.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package shortbus;
2+
3+
public class Response<T> {
4+
public T data;
5+
public Exception exception;
6+
7+
public boolean hasException() {
8+
return exception != null;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package shortbus.example;
2+
3+
import shortbus.Request;
4+
5+
public class HelloWorldCommand implements Request<String> {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package shortbus.example;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import shortbus.RequestHandler;
6+
7+
@Component
8+
public class HelloWorldCommandHandler implements RequestHandler<HelloWorldCommand, String> {
9+
10+
@Override
11+
public String handle(HelloWorldCommand request) {
12+
return "Hello";
13+
}
14+
15+
}

0 commit comments

Comments
 (0)