Skip to content

Instantly share code, notes, and snippets.

@ullgren
Created January 5, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ullgren/aca46a4c9bc64403cec9 to your computer and use it in GitHub Desktop.
Save ullgren/aca46a4c9bc64403cec9 to your computer and use it in GitHub Desktop.
Dynamic lookup of properties from a secure-property-placeholder in a Mule flow
number=![0J0O/Z2I0NzSJnpFF8cGOQ==]
bob=![665bhZoDiRU=]
alice=![IuCmQm1Dh6A=]
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.AbstractBeanFactory;
/**
* Helper bean to allow indirect access to properties loaded by Spring property placeholders.
* Code taken from StackOverflow (http://stackoverflow.com/a/17697586)
*/
public class PropertiesAccessor {
private final AbstractBeanFactory beanFactory;
private final Map<String,String> cache = new ConcurrentHashMap<String,String>();
@Autowired
protected PropertiesAccessor(AbstractBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public String getProperty(String key) {
if(cache.containsKey(key)){
return cache.get(key);
}
String foundProp = null;
try {
foundProp = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}");
cache.put(key,foundProp);
} catch (IllegalArgumentException ex) {
// ok - property was not found
}
return foundProp;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:context="http://www.springframework.org/schema/context"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:secure-property-placeholder="http://www.mulesoft.org/schema/mule/secure-property-placeholder" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/secure-property-placeholder http://www.mulesoft.org/schema/mule/secure-property-placeholder/current/mule-secure-property-placeholder.xsd">
<spring:beans>
<context:annotation-config />
<spring:bean id="propertiesAccessor" class="PropertiesAccessor" />
</spring:beans>
<secure-property-placeholder:config name="Secure_Property_Placeholder"
encryptionAlgorithm="Blowfish" key="secretkey"
location="data.properties"
doc:name="Secure Property Placeholder"/>
<!-- "Static" lookup of property -->
<flow name="getNumber" doc:name="getNumber">
<http:inbound-endpoint host="localhost" port="8081" path="getNumber" doc:name="HTTP"/>
<set-payload value="The number is ${number}" doc:name="Set Payload"/>
</flow>
<!-- "Dynamic" lookup of property based on user input-->
<flow name="getDynamicPin" doc:name="getDynamicPin">
<http:inbound-endpoint host="localhost" port="8081" path="getDynamicPin" doc:name="HTTP"/>
<choice >
<when expression="#[message.inboundProperties.user]">
<set-payload value="#[message.inboundProperties.user + ' has pin '+ app.registry.propertiesAccessor.getProperty(message.inboundProperties.user)]" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="You have to provide a user"/>
</otherwise>
</choice>
</flow>
</mule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment