Monday, September 12, 2016

Accessing registry property value using class mediator


How to retrieve the properties in Synapse 


The get-property() function allows any XPath expression used in a configuration to look up information from the current message context. Using the Property mediator, we can retrieve properties for different scopes. You can find the different scopes to retrieve properties here.

How to use get-property() to access the property saved in registry.


To retrieve the prorerties saved in registry, we need to use the scope "registry".The property name should be appended to the property path using @ sign.

 get-property('registry', String registryPath@propertyName)  

How to access the property saved in registry using class mediator.


Step 1


We need to write the following logic in the class mediator. In the below sample we have saved the property name as "lastAccessTime" in the registry path  "gov:/tasks/EventAlertScheduledTask".


 package Mypackage;  
 import org.apache.synapse.MessageContext;   
 import org.apache.synapse.mediators.AbstractMediator;  
 import org.apache.synapse.registry.Registry;  
 import org.apache.synapse.config.Entry;  
 import java.util.Properties;  
 public class Sample extends AbstractMediator {   
      public boolean mediate(MessageContext synCtx) {   
           // TODO Implement your mediation logic here  
           String[] regParam = "gov:/tasks/EventAlertScheduledTask@lastAccessTime".split("@");  
            String regPath = regParam[0];  
      String propName = regParam[1];  
     Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);  
     Registry registry = synCtx.getConfiguration().getRegistry();  
     if (registry != null) {  
       registry.getResource(propEntry, new Properties());  
       if (propName != null) {  
         Properties reqProperties = propEntry.getEntryProperties();  
         if (reqProperties != null) {  
           if (reqProperties.get(propName) != null) {  
             System.out.println("Property value is : "+reqProperties.getProperty(propName));  
           }  
         }  
       }  
      }  
     return true;  
 }  
 }  


Step 2


The following proxy service can be used to invoke the above class mediator.

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="TestProxy"  
     transports="https,http"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <property name="property_value"  
           expression="get-property('registry','gov:/tasks/EventAlertScheduledTask@lastAccessTime')"/>  
      <log>  
       <property name="Access using property mediator :"  
            expression="get-property('property_value')"/>  
      </log>  
      <class name="Mypackage.Sample"/>  
      <drop/>  
    </inSequence>  
   </target>  
   <description/>  
 </proxy>  

No comments:

Post a Comment