DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Replacing Standard Mule Transformer w/ Custom Implementation

$$anonymous$$ user avatar by
$$anonymous$$
·
Feb. 19, 13 · Interview
Like (0)
Save
Tweet
Share
5.90K Views

Join the DZone community and get the full member experience.

Join For Free
Although there is a lot of documentation about Mule ESB the issue in this post still took me some time to get it right. In this post I give a complete example of how I replaced standard Mule functionality (a transformer in this case) with my own implementation.

My issue started with this question which I posted at the Mule Forum without any response so far. This made me look for a solution myself, which is one of the benefits of open source; it offers you the opportunity to do so.

I soon realized that the behavior I want could be quite easily be implemented by a small change in the MessagePropertiesTransformer. The original code (I am using Mule CE version 3.2.1) was like this:
protected void addProperties(MuleMessage message)
{
   ...
   final String key = entry.getKey();

   Object value= entry.getValue();
   Object realValue = value;

   //Enable expression support for property values
   if (muleContext.getExpressionManager().isExpression(value.toString()))
   {
     realValue = muleContext.getExpressionManager().evaluate(value.toString(), message);
   }
   ...
What we see here is a piece of the addProperties method of the MessagePropertiesTransformer. The code checks if the supplied value of an added property is an expression and if so it resolves the expression to the ‘realValue’.
The way I modified it:
protected void addProperties(MuleMessage message)
{
  ...
  final String key = entry.getKey();

  Object value= entry.getValue();
  Object realValue = value;

  // Modified to be able to execute expression in expressions :-)
  while (muleContext.getExpressionManager().isExpression(realValue.toString())){
     realValue = muleContext.getExpressionManager().evaluate(realValue.toString(), message);
  }
  ...  
As you can see I am iterating over the realValue till it is no longer an expression anymore. With this line of code I can get my wanted behavior: Supplying an expression that resolves to an expression that resolves to a real value. So far for the easy part. Now I wanted to use my modified transformer instead of the standard one if I use a construction like:
<message-properties-transformer scope="invocation">
  <add-message-property key="field1" value="#[xpath:#[header:INVOCATION:my.attribute.1]]"/>
</message-properties-transformer>
The way I solved it was by adding my own namespace in the Mule configuration. Although it is described here I think it is handy to supply a complete example here:

  • Define the schema for your element
  • In my case it was simple since I didn’t change any at this level, so I just copied the existing element in my own schema file which I saved as ‘my-schema.xsd’ in the META-INF directory of my project:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.pascalalma.net/schema/core" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://www.pascalalma.net/schema/core"        
            xmlns:mule="http://www.mulesoft.org/schema/mule/core"                      
            elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xsd:import namespace="http://www.mulesoft.org/schema/mule/core" schemaLocation="http://www.mulesoft.org/schema/mule/core/3.2/mule.xsd"/>
  <xsd:annotation>
    <xsd:documentation>My Mule Extension.</xsd:documentation>
  </xsd:annotation>
  <!-- My Transformer definition -->
  <xsd:element name="message-properties-transformer" type="mule:messagePropertiesTransformerType"
                 substitutionGroup="mule:abstract-transformer">
    <xsd:annotation>
      <xsd:documentation>
        A transformer that can add, delete or rename message properties.
      </xsd:documentation>
    </xsd:annotation>
  </xsd:element>
</xsd:schema>
Just make sure you prefix the type and substitutionGroup you refer to in your XSD because you will get errors about unknown XML elements otherwise.

  • Write the Namespace Handler
  • In this case it is almost a copy of the existing NamespaceHandler for the transformer:

package net.pascalalma.mule.config;

import net.pascalalma.mule.config.spring.parsers.specific.MessagePropertiesTransformerDefinitionParser;
import org.mule.config.spring.handlers.AbstractMuleNamespaceHandler;

/**
 *
 * @author pascal
 */
public class MyNamespaceHandler extends AbstractMuleNamespaceHandler {

    @Override
    public void init() {
        registerBeanDefinitionParser("message-properties-transformer", new MessagePropertiesTransformerDefinitionParser());
    }
}
    • Write the Definition Parser
    • It becomes boring but this is also largely a copy of the existing MessagePropertiesTransformerDefinitionParser:

package net.pascalalma.mule.config.spring.parsers.specific;

import net.pascalalma.mule.transformer.MessagePropertiesTransformer;
import org.mule.config.spring.parsers.specific.MessageProcessorDefinitionParser;

/**
 *
 * @author pascal
 */
public class MessagePropertiesTransformerDefinitionParser extends MessageProcessorDefinitionParser
{
    public MessagePropertiesTransformerDefinitionParser()
    {
        super(MessagePropertiesTransformer.class);
        addAlias("scope", "scopeName");
    }
    
}

      Just make sure you import the correct MessagePropertiesTransformer here!

      • Set the Spring handler mapping
      • Add the file ‘spring.handlers’ to the project in the META-INF directory if it doesn’t exist already. In the file put the following:

http\://www.pascalalma.net/schema/core=net.pascalalma.mule.config.MyNamespaceHandler
        • Set the local schema mapping
        • Add the file ‘spring.schemas’ to the project in the META-INF directory if it doesn’t exist already. In the file put the following:

http\://www.pascalalma.net/schema/core/1.0/my-schema.xsd=META-INF/my-schema.xsd
        • Modify the Mule config to include the custom namespace
        • I added the following parts to my Mule config. First add the schema declaration :

xmlns:pan="http://www.pascalalma.net/schema/core"
     ...
      xsi:schemaLocation="http://www.pascalalma.net/schema/core http://www.pascalalma.net/schema/core/1.0/my-schema.xsd 	

          Next I use this namespace prefix with my transformer like this:

<pan:message-properties-transformer scope="invocation">
  <add-message-property key="field1" value="#[header:INVOCATION:id.attribute.1]"/>
</pan:message-properties-transformer>

        As you can see I changed the content of field1 so it matches my needs. What is happening now is that the expression ‘#[header:INVOCATION:id.attribute.1]‘ resolves to a XPath expression like ‘#[xpath:/msg/header/ID]‘ and this resolves to a real value that I can use in the remainder of the flow.



Implementation

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • The Quest for REST

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: