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:18 UTC
[syncope] branch 2_1_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_1_X
in repository https://gitbox.apache.org/repos/asf/syncope.git
The following commit(s) were added to refs/heads/2_1_X by this push:
new ec3a397 [SYNCOPE-1498] Injecting Spring Environment into ContentLoaderHandler and resolving variables via StringSubstitutor
ec3a397 is described below
commit ec3a3974c4b691ef7d419bc882bbb1faee9ae962
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 | 77 +++++++++++++---------
.../persistence/jpa/content/XMLContentLoader.java | 7 +-
2 files changed, 53 insertions(+), 31 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 e67d5c8..69d13ca 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
@@ -18,16 +18,18 @@
*/
package org.apache.syncope.core.persistence.jpa.content;
-import java.sql.ResultSet;
import java.sql.Types;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
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;
@@ -47,22 +49,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", (final ResultSet rs) -> {
- Map<String, Integer> colTypes1 = new HashMap<>();
- for (int i = 1; i <= rs.getMetaData().getColumnCount();
- i++) {
- colTypes1.
- put(rs.getMetaData().getColumnName(i).toUpperCase(), rs.getMetaData().getColumnType(i));
+ "SELECT * FROM " + tableName + " WHERE 0=1", rs -> {
+ Map<String, Integer> types = new HashMap<>();
+ for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
+ types.put(rs.getMetaData().getColumnName(i).toUpperCase(), rs.getMetaData().getColumnType(i));
}
- return colTypes1;
+ return types;
});
Object[] parameters = new Object[attrs.getLength()];
@@ -73,15 +84,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 +106,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 +136,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 4adfde6..a209a36 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;
@@ -50,6 +52,9 @@ public class XMLContentLoader extends AbstractContentDealer implements ContentLo
@Resource(name = "indexesXML")
private ResourceWithFallbackLoader indexesXML;
+ @Autowired
+ private Environment env;
+
@Override
public Integer getPriority() {
return 0;
@@ -105,7 +110,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);
}
}