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:19 UTC
[syncope] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/syncope.git
The following commit(s) were added to refs/heads/master by this push:
new 5922db7 [SYNCOPE-1498] Injecting Spring Environment into ContentLoaderHandler and resolving variables via StringSubstitutor
5922db7 is described below
commit 5922db7a689ed8287a04bf107ea9beeaaff02d02
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 | 71 ++++++++++++++--------
.../persistence/jpa/content/XMLContentLoader.java | 11 +++-
2 files changed, 53 insertions(+), 29 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 a7d036b..15f9e04 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
@@ -26,9 +26,12 @@ import java.util.Objects;
import javax.sql.DataSource;
import javax.xml.bind.DatatypeConverter;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringSubstitutor;
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.xml.sax.Attributes;
@@ -48,21 +51,31 @@ 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.jdbcTemplate = new JdbcTemplate(dataSource);
this.rootElement = rootElement;
this.continueOnError = continueOnError;
+ this.envParamSubstitutor = new StringSubstitutor(key -> {
+ String value = env.getProperty(key);
+ return StringUtils.isBlank(value) ? null : value;
+ });
}
private Object[] getParameters(final String tableName, final Attributes attrs) {
Map<String, Integer> colTypes = jdbcTemplate.query(
"SELECT * FROM " + tableName + " WHERE 0=1", rs -> {
- Map<String, Integer> colTypes1 = new HashMap<>();
+ Map<String, Integer> types = new HashMap<>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
- colTypes1.put(
- rs.getMetaData().getColumnName(i).toUpperCase(), rs.getMetaData().getColumnType(i));
+ types.put(rs.getMetaData().getColumnName(i).toUpperCase(), rs.getMetaData().getColumnType(i));
}
- return colTypes1;
+ return types;
});
Object[] parameters = new Object[attrs.getLength()];
@@ -73,15 +86,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;
@@ -89,29 +108,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;
@@ -119,41 +138,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] = DatatypeConverter.parseHexBinary(attrs.getValue(i));
+ parameters[i] = DatatypeConverter.parseHexBinary(value);
} catch (IllegalArgumentException e) {
- parameters[i] = attrs.getValue(i);
+ parameters[i] = value;
}
break;
case Types.BLOB:
try {
- parameters[i] = DatatypeConverter.parseHexBinary(attrs.getValue(i));
+ parameters[i] = DatatypeConverter.parseHexBinary(value);
} catch (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 d202922..db95a6a 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
@@ -33,6 +33,8 @@ import org.apache.syncope.core.spring.ApplicationContextProvider;
import org.apache.syncope.core.spring.ResourceWithFallbackLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+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;
@@ -54,6 +56,9 @@ public class XMLContentLoader implements ContentLoader {
@Resource(name = "indexesXML")
private ResourceWithFallbackLoader indexesXML;
+ @Autowired
+ private Environment env;
+
@Override
public int getOrder() {
return 400;
@@ -101,15 +106,15 @@ public class XMLContentLoader implements ContentLoader {
}
}
- private static void loadDefaultContent(
- final String domain, final InputStream contentXML, final DataSource dataSource)
+ private void loadDefaultContent(
+ final String domain, final InputStream contentXML, final DataSource dataSource)
throws IOException, ParserConfigurationException, SAXException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
try (contentXML) {
SAXParser parser = factory.newSAXParser();
- parser.parse(contentXML, new ContentLoaderHandler(dataSource, ROOT_ELEMENT, true));
+ parser.parse(contentXML, new ContentLoaderHandler(dataSource, ROOT_ELEMENT, true, env));
LOG.debug("[{}] Default content successfully loaded", domain);
}
}