From 0c2cfaa13b28b69707e5324b08bde8bdb808b229 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Sun, 5 Feb 2017 22:25:03 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 + pom.xml | 39 +++++++ .../de/joshavg/simpledic/SdiContainer.java | 105 ++++++++++++++++++ .../exception/ContainerInitException.java | 8 ++ .../DependencyNotSatisfiedException.java | 7 ++ .../NoDefaultConstructorException.java | 7 ++ .../exception/SdicClassNotFoundException.java | 7 ++ src/main/resources/sdic.properties | 0 .../de/joshavg/simpledic/NoDependencies.java | 4 + .../java/de/joshavg/simpledic/ServiceOne.java | 6 + .../de/joshavg/simpledic/TestProperties.java | 11 ++ src/test/resources/test.properties | 2 + 12 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/de/joshavg/simpledic/SdiContainer.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/ContainerInitException.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java create mode 100644 src/main/resources/sdic.properties create mode 100644 src/test/java/de/joshavg/simpledic/NoDependencies.java create mode 100644 src/test/java/de/joshavg/simpledic/ServiceOne.java create mode 100644 src/test/java/de/joshavg/simpledic/TestProperties.java create mode 100644 src/test/resources/test.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea6cc40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.iml +.idea/ +target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e27e8c8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + de.joshavg + simpledic + 0.1-SNAPSHOT + SimpleDiContainer + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.12 + + + com.google.collections + google-collections + 1.0 + + + + + \ No newline at end of file diff --git a/src/main/java/de/joshavg/simpledic/SdiContainer.java b/src/main/java/de/joshavg/simpledic/SdiContainer.java new file mode 100644 index 0000000..239ec5a --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/SdiContainer.java @@ -0,0 +1,105 @@ +package de.joshavg.simpledic; + +import com.google.common.annotations.VisibleForTesting; +import de.joshavg.simpledic.exception.ContainerInitException; +import de.joshavg.simpledic.exception.DependencyNotSatisfiedException; +import de.joshavg.simpledic.exception.SdicClassNotFoundException; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class SdiContainer { + + private final Properties props; + + @VisibleForTesting + SdiContainer(Properties props) { + this.props = props; + } + + public static SdiContainer load() { + return load("sdic.properties"); + } + + public static SdiContainer load(String filename) { + Properties props = new Properties(); + + InputStream inputStream = SdiContainer.class.getClassLoader().getResourceAsStream(filename); + try { + props.load(inputStream); + } catch (IOException e) { + throw new ContainerInitException("failed loading properties", e); + } + + SdiContainer container = new SdiContainer(props); + container.integrityCheck(); + return container; + } + + private void integrityCheck() { + List> services = fetchAllServices(); + checkConstructorDependencies(services); + } + + private List> fetchAllServices() { + return props.entrySet().stream() + .filter(e -> isServiceName(e.getKey().toString())) + .map(e -> e.getValue().toString()) + .map(n -> { + try { + return Class.forName(n); + } catch (ClassNotFoundException e) { + throw new SdicClassNotFoundException(e); + } + }).collect(Collectors.toList()); + } + + private void checkConstructorDependencies(List> services) { + for(Class c : services) { + Constructor[] constructors = c.getDeclaredConstructors(); + for(Constructor constructor : constructors) { + Class[] parameterTypes = constructor.getParameterTypes(); + System.out.println(Arrays.toString(parameterTypes)); + System.out.println(Arrays.asList(parameterTypes)); + } + } + /* services.stream() + // get all constructors + .map(Class::getDeclaredConstructors) + .reduce(new ArrayList(), + (l, arr) -> { + l.addAll(Arrays.asList(arr)); + return l; + }, + (l1, l2) -> l1) + // get all parameter types + .stream() + .map(Constructor::getParameterTypes) + .reduce(new ArrayList>(), + (l, arr) -> { + l.addAll(Arrays.asList(arr)); + return l; + }, + (l1, l2) -> l1) + // search for services with needed types + .stream() + .distinct() + .forEach(t -> { + if (!services.contains(t)) { + throw new DependencyNotSatisfiedException(t); + } + });*/ + } + + @VisibleForTesting + static boolean isServiceName(String name) { + return name.startsWith("service."); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/ContainerInitException.java b/src/main/java/de/joshavg/simpledic/exception/ContainerInitException.java new file mode 100644 index 0000000..a3263af --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/ContainerInitException.java @@ -0,0 +1,8 @@ +package de.joshavg.simpledic.exception; + +public class ContainerInitException extends RuntimeException { + + public ContainerInitException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java b/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java new file mode 100644 index 0000000..54119d8 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception; + +public class DependencyNotSatisfiedException extends RuntimeException { + public DependencyNotSatisfiedException(Class dependency) { + super(String.format("Dependency %s not satisfied", dependency.getName())); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java b/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java new file mode 100644 index 0000000..b06a330 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception; + +public class NoDefaultConstructorException extends RuntimeException { + public NoDefaultConstructorException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java b/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java new file mode 100644 index 0000000..924b78c --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception; + +public class SdicClassNotFoundException extends RuntimeException { + public SdicClassNotFoundException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/resources/sdic.properties b/src/main/resources/sdic.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/de/joshavg/simpledic/NoDependencies.java b/src/test/java/de/joshavg/simpledic/NoDependencies.java new file mode 100644 index 0000000..865cbcd --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/NoDependencies.java @@ -0,0 +1,4 @@ +package de.joshavg.simpledic; + +public class NoDependencies { +} diff --git a/src/test/java/de/joshavg/simpledic/ServiceOne.java b/src/test/java/de/joshavg/simpledic/ServiceOne.java new file mode 100644 index 0000000..959efc3 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/ServiceOne.java @@ -0,0 +1,6 @@ +package de.joshavg.simpledic; + +public class ServiceOne { + public ServiceOne(NoDependencies b) { + } +} diff --git a/src/test/java/de/joshavg/simpledic/TestProperties.java b/src/test/java/de/joshavg/simpledic/TestProperties.java new file mode 100644 index 0000000..b9d0f87 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/TestProperties.java @@ -0,0 +1,11 @@ +package de.joshavg.simpledic; + +import org.junit.Test; + +public class TestProperties { + + @Test + public void testIntegrityCheck() { + SdiContainer.load("test.properties"); + } +} diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties new file mode 100644 index 0000000..41055f8 --- /dev/null +++ b/src/test/resources/test.properties @@ -0,0 +1,2 @@ +service.one: de.joshavg.simpledic.ServiceOne +service.nodeps: de.joshavg.simpledic.NoDependencies