diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/engine/ExecutionContextFactory.java b/s-pipes-core/src/main/java/cz/cvut/spipes/engine/ExecutionContextFactory.java index 3823faed..f8513cc1 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/engine/ExecutionContextFactory.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/engine/ExecutionContextFactory.java @@ -1,7 +1,13 @@ package cz.cvut.spipes.engine; +import cz.cvut.spipes.util.CoreConfigProperies; +import org.apache.jena.query.QuerySolution; +import org.apache.jena.query.QuerySolutionMap; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.ResourceFactory; + +import java.util.Map; public class ExecutionContextFactory { @@ -11,21 +17,21 @@ public class ExecutionContextFactory { public static ExecutionContext createEmptyContext() { ExecutionContextImpl context = new ExecutionContextImpl(); context.setDefaultModel(ModelFactory.createDefaultModel()); - context.setVariablesBinding(new VariablesBinding()); + context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding())); return context; } public static ExecutionContext createContext(Model defaultModel, VariablesBinding variablesBinding) { ExecutionContextImpl context = new ExecutionContextImpl(); context.setDefaultModel(defaultModel); - context.setVariablesBinding(variablesBinding); + context.setVariablesBinding(extendByConfigurationVariables(variablesBinding)); return context; } public static ExecutionContext createContext(VariablesBinding variablesBinding) { ExecutionContextImpl context = new ExecutionContextImpl(); context.setDefaultModel(ModelFactory.createDefaultModel()); - context.setVariablesBinding(variablesBinding); + context.setVariablesBinding(extendByConfigurationVariables(variablesBinding)); return context; } @@ -34,8 +40,25 @@ public static ExecutionContext createContext(VariablesBinding variablesBinding) public static ExecutionContext createContext(Model defaultModel) { ExecutionContextImpl context = new ExecutionContextImpl(); context.setDefaultModel(defaultModel); - context.setVariablesBinding(new VariablesBinding()); + context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding())); return context; } + private static VariablesBinding extendByConfigurationVariables(VariablesBinding variable) { + Map map = CoreConfigProperies.getConfigurationVariables(); + variable.extendConsistently(new VariablesBinding(transform(map))); + return variable; + } + + private static QuerySolution transform(final Map parameters) { + final QuerySolutionMap querySolution = new QuerySolutionMap(); + + parameters.entrySet() + .stream() + .filter(parameter -> parameter.getKey().startsWith(CoreConfigProperies.variableAssignmentPrefix)) + .forEach( + e -> querySolution.add(e.getKey(), ResourceFactory.createPlainLiteral(e.getValue())) + ); + return querySolution; + } } diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/engine/VariablesBinding.java b/s-pipes-core/src/main/java/cz/cvut/spipes/engine/VariablesBinding.java index ed532209..a4fb5bfd 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/engine/VariablesBinding.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/engine/VariablesBinding.java @@ -1,5 +1,6 @@ package cz.cvut.spipes.engine; +import cz.cvut.spipes.util.CoreConfigProperies; import org.apache.jena.query.QuerySolution; import org.apache.jena.query.QuerySolutionMap; import org.apache.jena.rdf.model.*; @@ -179,12 +180,14 @@ public void load(final InputStream is, final String lang) throws IOException { @Override public String toString() { - return binding.asMap().toString(); + return getVariablesWithoutConfigurationOnes().toString(); } public String toTruncatedString() { - return binding.asMap().entrySet().stream() - .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue().toString()))). + Map map = getVariablesWithoutConfigurationOnes(); + + return map.entrySet().stream() + .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue()))). collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).toString(); } @@ -194,4 +197,11 @@ private static String getTruncatedValue(@NotNull String value) { } return value; } + + private Map getVariablesWithoutConfigurationOnes(){ + return binding.asMap().entrySet() + .stream() + .filter(entry -> !entry.getKey().startsWith(CoreConfigProperies.variableAssignmentPrefix)) + .collect(Collectors.toMap(Map.Entry::getKey, o -> o.getValue().toString(), (x, y) -> y, HashMap::new)); + } } diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/util/CoreConfigProperies.java b/s-pipes-core/src/main/java/cz/cvut/spipes/util/CoreConfigProperies.java index 00be2af4..05a3c768 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/util/CoreConfigProperies.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/util/CoreConfigProperies.java @@ -6,8 +6,12 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; public class CoreConfigProperies { + public static final String variableAssignmentPrefix = "variable.assignment"; private static final String CONFIG_FILE = "config-core.properties"; private static final java.util.Properties prop = new java.util.Properties(); private static final Logger LOG = LoggerFactory.getLogger(CoreConfigProperies.class); @@ -38,6 +42,13 @@ public class CoreConfigProperies { } } + public static Map getConfigurationVariables(){ + Map map = new HashMap<>((Map) prop); + return map.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, HashMap::new)); + } + public static String get(String name) { String value = prop.getProperty(name); if (value == null) { diff --git a/s-pipes-modules/module-rdf4j/src/main/java/cz/cvut/spipes/modules/Rdf4jModule.java b/s-pipes-modules/module-rdf4j/src/main/java/cz/cvut/spipes/modules/Rdf4jModule.java index 69f2a698..81c62ade 100644 --- a/s-pipes-modules/module-rdf4j/src/main/java/cz/cvut/spipes/modules/Rdf4jModule.java +++ b/s-pipes-modules/module-rdf4j/src/main/java/cz/cvut/spipes/modules/Rdf4jModule.java @@ -4,6 +4,7 @@ import cz.cvut.spipes.engine.ExecutionContext; import cz.cvut.spipes.engine.ExecutionContextFactory; import cz.cvut.spipes.engine.VariablesBinding; +import cz.cvut.spipes.util.CoreConfigProperies; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.ResourceFactory; @@ -29,12 +30,12 @@ import java.io.StringWriter; import java.util.Optional; -public class Rdf4jModule extends AbstractModule { +public class Rdf4jModule extends AnnotatedAbstractModule { private static final Logger LOG = LoggerFactory.getLogger(Rdf4jModule.class); private static String TYPE_URI = KBSS_MODULE.getURI()+"deploy"; - private static String PROPERTY_PREFIX_URI = KBSS_MODULE.getURI()+"rdf4j"; + private static final String PROPERTY_PREFIX_URI = KBSS_MODULE.uri + "rdf4j"; private static Property getParameter(final String name) { return ResourceFactory.createProperty(PROPERTY_PREFIX_URI + "/" + name); @@ -64,6 +65,12 @@ private static Property getParameter(final String name) { static final Property P_IS_REPLACE_CONTEXT_IRI = getParameter("p-is-replace"); private boolean isReplaceContext; + @Parameter(urlPrefix = PROPERTY_PREFIX_URI + "/", name = "p-rdf4j-secured-username-variable") + private String securedUsername; + + @Parameter(urlPrefix = PROPERTY_PREFIX_URI + "/", name = "p-rdf4j-secured-password-variable") + private String securedPassword; + public String getRdf4jServerURL() { return rdf4jServerURL; } @@ -108,12 +115,12 @@ ExecutionContext executeSelf() { VariablesBinding variablesBinding = getExecutionContext().getVariablesBinding(); String username = Optional - .ofNullable(variablesBinding.getNode("p-username")) + .ofNullable(variablesBinding.getNode(CoreConfigProperies.variableAssignmentPrefix + "." + securedUsername)) .map(RDFNode::toString) .orElse(null); String password = Optional - .ofNullable(variablesBinding.getNode("p-password")) + .ofNullable(variablesBinding.getNode(CoreConfigProperies.variableAssignmentPrefix + "." + securedPassword)) .map(RDFNode::toString) .orElse(null); @@ -181,6 +188,7 @@ public String getTypeURI() { @Override public void loadConfiguration() { + super.loadConfiguration(); rdf4jServerURL = getEffectiveValue(P_RDF4J_SERVER_URL).asLiteral().getString(); rdf4jRepositoryName = getEffectiveValue(P_RDF4J_REPOSITORY_NAME).asLiteral().getString(); if (this.getPropertyValue(P_RDF4J_CONTEXT_IRI) != null) {