initial commit
						commit
						0c2cfaa13b
					
				@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					*.iml
 | 
				
			||||||
 | 
					.idea/
 | 
				
			||||||
 | 
					target/
 | 
				
			||||||
@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					<?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>de.joshavg</groupId>
 | 
				
			||||||
 | 
					    <artifactId>simpledic</artifactId>
 | 
				
			||||||
 | 
					    <version>0.1-SNAPSHOT</version>
 | 
				
			||||||
 | 
					    <name>SimpleDiContainer</name>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <build>
 | 
				
			||||||
 | 
					        <plugins>
 | 
				
			||||||
 | 
					            <plugin>
 | 
				
			||||||
 | 
					                <groupId>org.apache.maven.plugins</groupId>
 | 
				
			||||||
 | 
					                <artifactId>maven-compiler-plugin</artifactId>
 | 
				
			||||||
 | 
					                <configuration>
 | 
				
			||||||
 | 
					                    <source>1.8</source>
 | 
				
			||||||
 | 
					                    <target>1.8</target>
 | 
				
			||||||
 | 
					                </configuration>
 | 
				
			||||||
 | 
					            </plugin>
 | 
				
			||||||
 | 
					        </plugins>
 | 
				
			||||||
 | 
					    </build>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <dependencies>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>junit</groupId>
 | 
				
			||||||
 | 
					            <artifactId>junit</artifactId>
 | 
				
			||||||
 | 
					            <version>4.12</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.google.collections</groupId>
 | 
				
			||||||
 | 
					            <artifactId>google-collections</artifactId>
 | 
				
			||||||
 | 
					            <version>1.0</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					    </dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</project>
 | 
				
			||||||
@ -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<Class<?>> services = fetchAllServices();
 | 
				
			||||||
 | 
					        checkConstructorDependencies(services);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<Class<?>> 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<Class<?>> 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<Constructor>(),
 | 
				
			||||||
 | 
					                        (l, arr) -> {
 | 
				
			||||||
 | 
					                            l.addAll(Arrays.asList(arr));
 | 
				
			||||||
 | 
					                            return l;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        (l1, l2) -> l1)
 | 
				
			||||||
 | 
					                // get all parameter types
 | 
				
			||||||
 | 
					                .stream()
 | 
				
			||||||
 | 
					                .map(Constructor::getParameterTypes)
 | 
				
			||||||
 | 
					                .reduce(new ArrayList<Class<?>>(),
 | 
				
			||||||
 | 
					                        (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.");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic.exception;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class ContainerInitException extends RuntimeException {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ContainerInitException(String message, Throwable cause) {
 | 
				
			||||||
 | 
					        super(message, cause);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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()));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic.exception;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class NoDefaultConstructorException extends RuntimeException {
 | 
				
			||||||
 | 
					    public NoDefaultConstructorException(Throwable cause) {
 | 
				
			||||||
 | 
					        super(cause);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic.exception;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class SdicClassNotFoundException extends RuntimeException {
 | 
				
			||||||
 | 
					    public SdicClassNotFoundException(Throwable cause) {
 | 
				
			||||||
 | 
					        super(cause);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class NoDependencies {
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class ServiceOne {
 | 
				
			||||||
 | 
					    public ServiceOne(NoDependencies b) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					package de.joshavg.simpledic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class TestProperties {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void testIntegrityCheck() {
 | 
				
			||||||
 | 
					        SdiContainer.load("test.properties");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					service.one: de.joshavg.simpledic.ServiceOne
 | 
				
			||||||
 | 
					service.nodeps: de.joshavg.simpledic.NoDependencies
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue