You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2019/09/27 06:33:20 UTC
[syncope] branch 2_0_X updated: [SYNCOPE-1498] Injecting Spring
Environment into ContentLoaderHandler and resolving variables via
StringSubstitutor
This is an automated email from the ASF dual-hosted git repository.
ilgrosso pushed a commit to branch 2_0_X
in repository https://gitbox.apache.org/repos/asf/syncope.git
The following commit(s) were added to refs/heads/2_0_X by this push:
new d8fcd41 [SYNCOPE-1498] Injecting Spring Environment into ContentLoaderHandler and resolving variables via StringSubstitutor
d8fcd41 is described below
commit d8fcd41c0a64a6035808eae7fadab3a9e4e049a3
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Fri Sep 27 08:17:16 2019 +0200
[SYNCOPE-1498] Injecting Spring Environment into ContentLoaderHandler and resolving variables via StringSubstitutor
---
.../jpa/content/ContentLoaderHandler.java | 69 +++++++++++++++-------
.../persistence/jpa/content/XMLContentLoader.java | 7 ++-
2 files changed, 53 insertions(+), 23 deletions(-)
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/ContentLoaderHandler.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/ContentLoaderHandler.java
index 9bc30f7..11e4603 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/ContentLoaderHandler.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/ContentLoaderHandler.java
@@ -27,9 +27,13 @@ import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringSubstitutor;
+import org.apache.commons.text.lookup.StringLookup;
import org.apache.syncope.core.provisioning.api.utils.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
@@ -50,10 +54,25 @@ public class ContentLoaderHandler extends DefaultHandler {
private final boolean continueOnError;
- public ContentLoaderHandler(final DataSource dataSource, final String rootElement, final boolean continueOnError) {
+ private final StringSubstitutor envParamSubstitutor;
+
+ public ContentLoaderHandler(
+ final DataSource dataSource,
+ final String rootElement,
+ final boolean continueOnError,
+ final Environment env) {
+
this.dataSource = dataSource;
this.rootElement = rootElement;
this.continueOnError = continueOnError;
+ this.envParamSubstitutor = new StringSubstitutor(new StringLookup() {
+
+ @Override
+ public String lookup(final String key) {
+ String value = env.getProperty(key);
+ return StringUtils.isBlank(value) ? null : value;
+ }
+ });
}
private Object[] getParameters(final String tableName, final Attributes attrs) {
@@ -82,15 +101,21 @@ public class ContentLoaderHandler extends DefaultHandler {
colType = Types.VARCHAR;
}
+ String value = envParamSubstitutor.replace(attrs.getValue(i));
+ if (value == null) {
+ LOG.warn("Variable ${} could not be resolved", attrs.getValue(i));
+ value = attrs.getValue(i);
+ }
+
switch (colType) {
case Types.INTEGER:
case Types.TINYINT:
case Types.SMALLINT:
try {
- parameters[i] = Integer.valueOf(attrs.getValue(i));
+ parameters[i] = Integer.valueOf(value);
} catch (NumberFormatException e) {
- LOG.error("Unparsable Integer '{}'", attrs.getValue(i));
- parameters[i] = attrs.getValue(i);
+ LOG.error("Unparsable Integer '{}'", value);
+ parameters[i] = value;
}
break;
@@ -98,29 +123,29 @@ public class ContentLoaderHandler extends DefaultHandler {
case Types.DECIMAL:
case Types.BIGINT:
try {
- parameters[i] = Long.valueOf(attrs.getValue(i));
+ parameters[i] = Long.valueOf(value);
} catch (NumberFormatException e) {
- LOG.error("Unparsable Long '{}'", attrs.getValue(i));
- parameters[i] = attrs.getValue(i);
+ LOG.error("Unparsable Long '{}'", value);
+ parameters[i] = value;
}
break;
case Types.DOUBLE:
try {
- parameters[i] = Double.valueOf(attrs.getValue(i));
+ parameters[i] = Double.valueOf(value);
} catch (NumberFormatException e) {
- LOG.error("Unparsable Double '{}'", attrs.getValue(i));
- parameters[i] = attrs.getValue(i);
+ LOG.error("Unparsable Double '{}'", value);
+ parameters[i] = value;
}
break;
case Types.REAL:
case Types.FLOAT:
try {
- parameters[i] = Float.valueOf(attrs.getValue(i));
+ parameters[i] = Float.valueOf(value);
} catch (NumberFormatException e) {
- LOG.error("Unparsable Float '{}'", attrs.getValue(i));
- parameters[i] = attrs.getValue(i);
+ LOG.error("Unparsable Float '{}'", value);
+ parameters[i] = value;
}
break;
@@ -128,41 +153,41 @@ public class ContentLoaderHandler extends DefaultHandler {
case Types.TIME:
case Types.TIMESTAMP:
try {
- parameters[i] = FormatUtils.parseDate(attrs.getValue(i));
+ parameters[i] = FormatUtils.parseDate(value);
} catch (ParseException e) {
- LOG.error("Unparsable Date '{}'", attrs.getValue(i));
- parameters[i] = attrs.getValue(i);
+ LOG.error("Unparsable Date '{}'", value);
+ parameters[i] = value;
}
break;
case Types.BIT:
case Types.BOOLEAN:
- parameters[i] = "1".equals(attrs.getValue(i)) ? Boolean.TRUE : Boolean.FALSE;
+ parameters[i] = "1".equals(value) ? Boolean.TRUE : Boolean.FALSE;
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
try {
- parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
+ parameters[i] = Hex.decodeHex(value.toCharArray());
} catch (DecoderException | IllegalArgumentException e) {
- parameters[i] = attrs.getValue(i);
+ parameters[i] = value;
}
break;
case Types.BLOB:
try {
- parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
+ parameters[i] = Hex.decodeHex(value.toCharArray());
} catch (DecoderException | IllegalArgumentException e) {
LOG.warn("Error decoding hex string to specify a blob parameter", e);
- parameters[i] = attrs.getValue(i);
+ parameters[i] = value;
} catch (Exception e) {
LOG.warn("Error creating a new blob parameter", e);
}
break;
default:
- parameters[i] = attrs.getValue(i);
+ parameters[i] = value;
}
}
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentLoader.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentLoader.java
index e6e4531..cdc01d0 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentLoader.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentLoader.java
@@ -31,6 +31,8 @@ import org.apache.syncope.core.spring.ApplicationContextProvider;
import org.apache.syncope.core.spring.ResourceWithFallbackLoader;
import org.apache.syncope.core.persistence.api.content.ContentLoader;
import org.apache.syncope.core.persistence.jpa.entity.conf.JPAConf;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -49,6 +51,9 @@ public class XMLContentLoader extends AbstractContentDealer implements ContentLo
@Resource(name = "indexesXML")
private ResourceWithFallbackLoader indexesXML;
+ @Autowired
+ private Environment env;
+
@Override
public Integer getPriority() {
return 0;
@@ -104,7 +109,7 @@ public class XMLContentLoader extends AbstractContentDealer implements ContentLo
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
try (InputStream in = contentXML.getResource().getInputStream()) {
SAXParser parser = factory.newSAXParser();
- parser.parse(in, new ContentLoaderHandler(dataSource, ROOT_ELEMENT, true));
+ parser.parse(in, new ContentLoaderHandler(dataSource, ROOT_ELEMENT, true, env));
LOG.debug("[{}] Default content successfully loaded", domain);
}
}