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

Commit

Permalink
Merge pull request #10 from lumio-medical/feature/extensions
Browse files Browse the repository at this point in the history
Feature: introducing the parser extension system
  • Loading branch information
eledhwen authored Nov 28, 2020
2 parents 95f4af0 + 0d45557 commit 8697719
Show file tree
Hide file tree
Showing 46 changed files with 1,457 additions and 908 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add the following in your `pom.xml`:
<dependency>
<groupId>com.lumiomedical</groupId>
<artifactId>lumio-vault</artifactId>
<version>0.8</version>
<version>0.9</version>
</dependency>
```

Expand Down Expand Up @@ -92,6 +92,7 @@ Other features that will need to be documented include:
* service aliasing
* service closing
* service container composition
* custom modules

_TODO_

Expand Down
2 changes: 1 addition & 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.8</version>
<version>0.9</version>
<packaging>jar</packaging>

<name>Lumio Vault</name>
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/lumiomedical/vault/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.legacy.Key;
import com.lumiomedical.vault.legacy.VaultLegacyCompiler;
import com.lumiomedical.vault.parser.adjuster.VaultAdjuster;

import javax.inject.Provider;
import javax.inject.Singleton;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
* @author Pierre Lecerf (plecerf@lumiomedical.com)
Expand Down Expand Up @@ -108,7 +111,7 @@ public static Vault with(String... paths) throws VaultException
* @return
* @throws VaultException
*/
public static Vault with(String path, Consumer<Definitions> adjuster) throws VaultException
public static Vault with(String path, VaultAdjuster adjuster) throws VaultException
{
return Vault.builder().with(path, adjuster).build();
}
Expand All @@ -121,7 +124,7 @@ public static Vault with(String path, Consumer<Definitions> adjuster) throws Vau
* @return
* @throws VaultException
*/
public static Vault with(List<String> paths, Consumer<Definitions> adjuster) throws VaultException
public static Vault with(List<String> paths, VaultAdjuster adjuster) throws VaultException
{
return Vault.builder().with(paths, adjuster).build();
}
Expand All @@ -133,7 +136,7 @@ public static Vault with(List<String> paths, Consumer<Definitions> adjuster) thr
* @return
* @throws VaultException
*/
public static Vault with(Consumer<Definitions> adjuster, String... paths) throws VaultException
public static Vault with(VaultAdjuster adjuster, String... paths) throws VaultException
{
return Vault.builder().with(adjuster, paths).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.lumiomedical.vault.container.definition.Definitions;
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.factory.VaultFactory;
import com.lumiomedical.vault.parser.adjuster.VaultAdjuster;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
* This BuildStage implementation registers services using a path to a Cellar configuration file.
Expand All @@ -22,7 +22,7 @@ public class CellarPathStage implements BuildStage
{
private final VaultFactory factory;
private final List<String> paths;
private final Consumer<Definitions> adjuster;
private final VaultAdjuster adjuster;

/**
*
Expand All @@ -39,7 +39,7 @@ public CellarPathStage(VaultFactory factory, String path)
* @param factory
* @param path
*/
public CellarPathStage(VaultFactory factory, String path, Consumer<Definitions> adjuster)
public CellarPathStage(VaultFactory factory, String path, VaultAdjuster adjuster)
{
this(factory, Collections.singletonList(path), adjuster);
}
Expand All @@ -60,7 +60,7 @@ public CellarPathStage(VaultFactory factory, List<String> paths)
* @param paths
* @param adjuster
*/
public CellarPathStage(VaultFactory factory, List<String> paths, Consumer<Definitions> adjuster)
public CellarPathStage(VaultFactory factory, List<String> paths, VaultAdjuster adjuster)
{
this.factory = factory;
this.paths = paths;
Expand All @@ -72,10 +72,7 @@ public void build(Vault vault) throws VaultException
{
Definitions definitions = new Definitions();

for (String path : this.paths)
definitions = this.factory.parse(path, definitions);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.lumiomedical.vault.container.definition.Definitions;
import com.lumiomedical.vault.exception.VaultException;
import com.lumiomedical.vault.factory.VaultFactory;
import com.lumiomedical.vault.parser.adjuster.VaultAdjuster;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
* A builder class for queuing BuildStages over the creation of a Vault instance.
Expand Down Expand Up @@ -102,7 +102,7 @@ public VaultBuilder with(String path)
* @param adjuster
* @return
*/
public VaultBuilder with(String path, Consumer<Definitions> adjuster)
public VaultBuilder with(String path, VaultAdjuster adjuster)
{
this.stages.add(new CellarPathStage(this.factory, path, adjuster));
return this;
Expand All @@ -125,7 +125,7 @@ public VaultBuilder with(List<String> paths)
* @param adjuster
* @return
*/
public VaultBuilder with(List<String> paths, Consumer<Definitions> adjuster)
public VaultBuilder with(List<String> paths, VaultAdjuster adjuster)
{
this.stages.add(new CellarPathStage(this.factory, paths, adjuster));
return this;
Expand All @@ -137,7 +137,7 @@ public VaultBuilder with(List<String> paths, Consumer<Definitions> adjuster)
* @param paths
* @return
*/
public VaultBuilder with(Consumer<Definitions> adjuster, String... paths)
public VaultBuilder with(VaultAdjuster adjuster, String... paths)
{
this.stages.add(new CellarPathStage(this.factory, List.of(paths), adjuster));
return this;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/lumiomedical/vault/container/Cellar.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,35 @@ public boolean isEmpty()
** Services
*/

/**
*
* @param name
* @return
*/
public Object getService(String name)
{
if (!this.services.containsKey(name))
throw new VaultNotFoundException("The cellar has no declared \""+name+"\" service.");
return this.services.get(name);
}

/**
*
* @param name
* @param type
* @param <T>
* @return
*/
public <T> T getService(String name, Class<T> type)
{
Object service = this.getService(name);

if (!type.isInstance(service))
throw new VaultNotFoundException("The cellar has a \""+name+"\" service but its type does not match the required "+type.getName());

return type.cast(service);
}

public boolean hasService(String name)
{
return this.services.containsKey(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ public Collection<ServiceDefinition> listDefinitions()
return this.definitions.values();
}

/**
*
* @return
*/
public Map<String, ServiceDefinition> getDefinitions()
{
return this.definitions;
}

/**
*
* @param identifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.lumiomedical.vault.container.definition;

import com.lumiomedical.vault.exception.VaultCompilationException;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Collection;

/**
* @author Pierre Lecerf (pierre@noleme.com)
Expand All @@ -13,21 +9,20 @@
public class Variable
{
private final String name;
private final Collection<String> dependencies;
private Object value;
private Collection<String> dependencies;
private static final Pattern variablePattern = Pattern.compile("(##(.*?)##)");
private static final Pattern envPattern = Pattern.compile("(\\$\\{(.*?)})");

/**
*
* @param name
* @param value
* @param dependencies
*/
public Variable(String name, Object value)
public Variable(String name, Object value, Collection<String> dependencies)
{
this.name = name;
this.value = value;
this.dependencies = value instanceof String ? findVariables((String)value) : new ArrayList<>();
this.dependencies = dependencies;
}

/**
Expand All @@ -52,9 +47,10 @@ public Object getValue()
*
* @param value
*/
public void setValue(Object value)
public Variable setValue(Object value)
{
this.value = value;
return this;
}

/**
Expand All @@ -74,115 +70,4 @@ public Collection<String> getDependencies()
{
return this.dependencies;
}

/**
*
* @param string
* @param definitions
* @return
*/
public static Map<String, Object> findReplacements(String string, Definitions definitions) throws VaultCompilationException
{
Map<String, Object> replacements = new HashMap<>();

for (String var : findVariables(string))
{
if (!definitions.hasVariable(var))
throw new VaultCompilationException("The requested variable '"+var+"' could not be found in the container.");
replacements.put("##"+var+"##", definitions.getVariable(var));
}

return replacements;
}

/**
*
* @param string
* @return
*/
public static Collection<String> findVariables(String string)
{
List<String> variables = new ArrayList<>();

Matcher matcher = variablePattern.matcher(string);
if (matcher.find())
{
variables.add(matcher.group(2));

matcher.results().forEach(matchResult -> {
String variable = matchResult.group(2);
variables.add(variable);
});
}

return variables;
}

/**
*
* @param string
* @param definitions
* @return
*/
public static String replace(String string, Definitions definitions) throws VaultCompilationException
{
String newString = string;
for (Map.Entry<String, Object> replacement : findReplacements(string, definitions).entrySet())
{
if (replacement.getValue() == null)
continue;
newString = newString.replace(replacement.getKey(), replacement.getValue().toString());
newString = replaceEnv(newString);
}
return newString;
}

/**
*
* @param params
* @param definitions
* @return
*/
public static Object[] replaceParameters(Object[] params, Definitions definitions) throws VaultCompilationException
{
for (int i = 0 ; i < params.length ; ++i)
{
Object param = params[i];
if (param instanceof String)
{
for (Map.Entry<String, Object> replacement : findReplacements((String)param, definitions).entrySet())
{
/* If the sequence to be replaced is the whole parameter, we replace the parameter with the corresponding typed variable */
if (replacement.getKey().length() == ((String)param).length())
param = replacement.getValue();
/* Otherwise, the new parameter will always be a string */
else
param = ((String)param).replace(replacement.getKey(), replacement.getValue().toString());
}
/* If param is still an instance of String, it can still contain references to ENV variables */
if (param instanceof String)
param = replaceEnv((String)param);
params[i] = param;
}
}
return params;
}

/**
*
* @param value
* @return
*/
public static String replaceEnv(String value)
{
Matcher matcher = envPattern.matcher(value);
if (matcher.find())
{
return matcher.replaceAll(mr -> {
String env = System.getenv(mr.group(2));
return env != null ? env : "";
});
}
return value;
}
}
Loading

0 comments on commit 8697719

Please sign in to comment.