Skip to content

Instantly share code, notes, and snippets.

@timofey
Last active April 11, 2019 15:45
Show Gist options
  • Save timofey/92b89f053046786ee2f9acd8ca430755 to your computer and use it in GitHub Desktop.
Save timofey/92b89f053046786ee2f9acd8ca430755 to your computer and use it in GitHub Desktop.
Adds support for properties in business process definitions
package ru.teamidea.utils.process;
import de.hybris.platform.processengine.definition.ProcessDefinitionResource;
import de.hybris.platform.util.Config;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Timofey Klyubin on 07.04.17
*/
public class PropertiesAwareProcessDefinitionResource extends ProcessDefinitionResource {
@SuppressWarnings("unused")
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesAwareProcessDefinitionResource.class);
private static final Pattern propertiesRegex = Pattern.compile("\\$\\{([^}]+)\\}");
private static final char ESCAPE = '$';
private Resource initialResource;
private SoftReference<Resource> cachedResource;
private boolean changed = false;
private final Map<String,String> paramsCache;
public PropertiesAwareProcessDefinitionResource() {
super();
paramsCache = Config.getAllParameters();
}
public PropertiesAwareProcessDefinitionResource(final Map<String, String> paramMap) {
super();
paramsCache = paramMap;
}
private Resource proceedWithResource() {
final String xmlDefinition;
try {
xmlDefinition = IOUtils.toString(initialResource.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.error("Couldn't read resource [".concat(initialResource.getFilename()).concat("]"), e);
throw new RuntimeException(e);
}
final StringBuilder output = new StringBuilder(xmlDefinition);
final Matcher propMatcher = propertiesRegex.matcher(xmlDefinition);
int offset = 0;
while(propMatcher.find()) {
final String propKey = propMatcher.group(1);
final String value = paramsCache.get(propKey);
if (value != null && output.charAt(propMatcher.start() + offset - 1) != ESCAPE) {
output.replace(propMatcher.start() + offset, propMatcher.end() + offset, value);
offset += value.length() - propMatcher.end() + propMatcher.start();
}
}
return new InputStreamResource(new ByteArrayInputStream(output.toString().getBytes(StandardCharsets.UTF_8)));
}
@Override
public void setResource(Resource resource) {
this.initialResource = resource;
this.changed = true;
}
@Override
public Resource getResource() {
if (changed) {
this.changed = false;
this.cachedResource = new SoftReference<>(proceedWithResource());
return this.cachedResource.get();
}
if (this.cachedResource.get() != null) {
return this.cachedResource.get();
}
this.changed = true;
return this.getResource();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment