Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
General: new test-cases, fixed @singleton handling ; Logging: better …
Browse files Browse the repository at this point in the history
…logging
  • Loading branch information
eledhwen committed Mar 16, 2021
1 parent 2db2c97 commit 107a29e
Show file tree
Hide file tree
Showing 28 changed files with 667 additions and 62 deletions.
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.lumiomedical</groupId>
<artifactId>lumio-vault</artifactId>
<version>0.10</version>
<version>0.11</version>
<packaging>jar</packaging>

<name>Lumio Vault</name>
Expand All @@ -30,6 +30,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<slf4j.version>1.7.30</slf4j.version>
</properties>

<dependencies>
Expand All @@ -51,6 +52,19 @@
<version>[0.9,)</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/com/lumiomedical/vault/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public static VaultBuilder builder()
return new VaultBuilder();
}

/**
*
* @return
* @throws VaultException
*/
public static Vault with() throws VaultException
{
return Vault.builder().build();
}

/**
* Helper method for creating a simple one-stage Vault instance.
*
Expand Down Expand Up @@ -130,6 +140,7 @@ public static Vault with(List<String> paths, VaultAdjuster adjuster) throws Vaul
}

/**
* Helper method for creating a simple one-stage Vault instance.
*
* @param adjuster
* @param paths
Expand Down Expand Up @@ -163,6 +174,14 @@ public <T> T instance(Class<T> type)
return provider(Key.of(type), null).get();
}

/**
* @return an instance of type
*/
public <T> T instance(Class<T> type, String name)
{
return provider(Key.of(type, name), null).get();
}

/**
* @return instance specified by key (type and qualifier)
*/
Expand Down Expand Up @@ -206,7 +225,7 @@ public <T> T inject(T target) throws VaultException
Key<?> key = (Key<?>) f[2];

try {
field.set(target, (boolean) f[1] ? provider(key) : instance(key));
field.set(target, (boolean) f[1] ? this.provider(key) : this.instance(key));
}
catch (IllegalAccessException e) {
throw new VaultException(String.format("Can't inject field %s in %s", field.getName(), target.getClass().getName()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.lumiomedical.vault.container.definition.Definitions;
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.factory.VaultFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This BuildStage implementation registers services using a pre-existing Definitions instance.
Expand All @@ -18,6 +20,8 @@ public class CellarDefinitionStage implements BuildStage
private final VaultFactory factory;
private final Definitions definitions;

private static final Logger logger = LoggerFactory.getLogger(CellarDefinitionStage.class);

/**
*
* @param factory
Expand All @@ -32,6 +36,8 @@ public CellarDefinitionStage(VaultFactory factory, Definitions definitions)
@Override
public void build(Vault vault) throws VaultException
{
logger.debug("Populating vault using pre-compiled Definitions");

Cellar cellar = this.factory.populate(new Cellar(), this.definitions);
new CellarStage(cellar).build(vault);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.factory.VaultFactory;
import com.lumiomedical.vault.parser.adjuster.VaultAdjuster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
Expand All @@ -24,6 +26,8 @@ public class CellarPathStage implements BuildStage
private final List<String> paths;
private final VaultAdjuster adjuster;

private static final Logger logger = LoggerFactory.getLogger(CellarPathStage.class);

/**
*
* @param factory
Expand Down Expand Up @@ -74,6 +78,8 @@ public void build(Vault vault) throws VaultException

definitions = this.factory.parser().extractOrigin(this.paths, definitions, this.adjuster);

logger.debug("Populating vault using configuration files {}", this.paths);

Cellar cellar = this.factory.populate(new Cellar(), definitions);

new CellarStage(cellar).build(vault);
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/com/lumiomedical/vault/builder/CellarStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.lumiomedical.vault.Vault;
import com.lumiomedical.vault.container.Cellar;
import com.lumiomedical.vault.legacy.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This BuildStage implementation registers services using a pre-existing Cellar instance.
Expand All @@ -14,6 +16,8 @@ public class CellarStage implements BuildStage
{
private final Cellar cellar;

private static final Logger logger = LoggerFactory.getLogger(CellarStage.class);

/**
*
* @param cellar
Expand All @@ -26,15 +30,32 @@ public CellarStage(Cellar cellar)
@Override
public void build(Vault vault)
{
logger.debug("Populating vault using pre-compiled Cellar ({} service and {} variable references found)", this.cellar.getServices().size(), this.cellar.getVariables().size());

this.cellar.getServices().forEach((name, service) -> {
vault.register(Key.of(service.getClass(), name), () -> service, this.cellar.isCloseable(name));
Key namedKey = Key.of(service.getClass(), name);
Key typeKey = Key.of(service.getClass());
boolean isCloseable = this.cellar.isCloseable(name);

vault.register(typeKey, () -> service, isCloseable);
vault.register(namedKey, () -> service, isCloseable);
});
this.cellar.getVariables().forEach((name, variable) -> {
Key key = variable != null
? Key.of(variable.getClass(), name)
: Key.of(Object.class, name)
;
vault.register(key, () -> variable);
Key namedKey = Key.of(getVariableClass(variable), name);
Key typeKey = Key.of(getVariableClass(variable));

vault.register(namedKey, () -> variable);
vault.register(typeKey, () -> variable);
});
}

/**
*
* @param instance
* @return
*/
private static Class<?> getVariableClass(Object instance)
{
return instance != null ? instance.getClass() : Object.class;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/lumiomedical/vault/builder/ModuleStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.lumiomedical.vault.Vault;
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.legacy.VaultLegacyCompiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

Expand All @@ -16,6 +18,8 @@ public class ModuleStage implements BuildStage
{
private final Object[] modules;

private static final Logger logger = LoggerFactory.getLogger(ModuleStage.class);

public ModuleStage(Object... modules)
{
this.modules = modules;
Expand All @@ -26,6 +30,8 @@ public void build(Vault vault) throws VaultException
{
for (Object module : this.modules)
{
logger.debug("Populating vault using module class {}", module.getClass().getName());

if (module instanceof Class)
throw new VaultException(String.format("%s provided as class instead of an instance.", ((Class) module).getName()));
for (Method providerMethod : VaultLegacyCompiler.providers(module.getClass()))
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/lumiomedical/vault/builder/ProviderStage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.lumiomedical.vault.builder;

import com.lumiomedical.vault.Vault;
import com.lumiomedical.vault.legacy.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Provider;

/**
* @author Pierre Lecerf (plecerf@lumiomedical.com)
* Created on 2021/03/13
*/
public class ProviderStage <T> implements BuildStage
{
private final Key<T> key;
private final Provider<T> provider;
private final boolean closeable;

private static final Logger logger = LoggerFactory.getLogger(ProviderStage.class);

/**
*
* @param key
* @param provider
* @param closeable
*/
public ProviderStage(Key<T> key, Provider<T> provider, boolean closeable)
{
this.key = key;
this.provider = provider;
this.closeable = closeable;
}

/**
*
* @param key
* @param provider
*/
public ProviderStage(Key<T> key, Provider<T> provider)
{
this(key, provider, false);
}

@Override
public void build(Vault vault)
{
logger.debug("Populating vault using provider {} (key={}, closeable={})", this.provider.getClass().getName(), this.key, this.closeable);
vault.register(this.key, this.provider, this.closeable);
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/lumiomedical/vault/builder/VaultBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import com.lumiomedical.vault.container.definition.Definitions;
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.factory.VaultFactory;
import com.lumiomedical.vault.legacy.Key;
import com.lumiomedical.vault.parser.adjuster.VaultAdjuster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Provider;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -21,6 +25,8 @@ public final class VaultBuilder
private final List<BuildStage> stages;
private VaultFactory factory;

private static final Logger logger = LoggerFactory.getLogger(VaultBuilder.class);

/**
*
*/
Expand Down Expand Up @@ -143,6 +149,33 @@ public VaultBuilder with(VaultAdjuster adjuster, String... paths)
return this;
}

/**
*
* @param key
* @param provider
* @param <T>
* @return
*/
public <T> VaultBuilder with(Key<T> key, Provider<T> provider)
{
this.stages.add(new ProviderStage<>(key, provider));
return this;
}

/**
*
* @param key
* @param provider
* @param closeable
* @param <T>
* @return
*/
public <T> VaultBuilder with(Key<T> key, Provider<T> provider, boolean closeable)
{
this.stages.add(new ProviderStage<>(key, provider, closeable));
return this;
}

/**
*
* @param modules
Expand All @@ -163,8 +196,13 @@ public Vault build() throws VaultException
{
Vault vault = new Vault();

logger.debug("Building vault with {} registered stages", this.stages.size());

for (BuildStage stage : this.stages)
{
logger.debug("Building vault stage {}", stage.getClass().getName());
stage.build(vault);
}

return vault;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/lumiomedical/vault/container/Cellar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.lumiomedical.vault.exception.RuntimeVaultException;
import com.lumiomedical.vault.exception.VaultNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -17,6 +19,8 @@ public class Cellar implements AutoCloseable
private final Map<String, Object> variables;
private final Set<String> closeables;

private static final Logger logger = LoggerFactory.getLogger(Cellar.class);

public Cellar()
{
this.services = new HashMap<>();
Expand Down Expand Up @@ -151,6 +155,8 @@ public Object getVariable(String name)
@Override
public void close()
{
logger.debug("Closing Cellar ({} closeables registered)", this.closeables.size());

for (String closeable : this.closeables)
{
try {
Expand Down
Loading

0 comments on commit 107a29e

Please sign in to comment.