Monthly Archives: March 2013

XmlBeans: removing namespace attributes from the generated xml

public static void removeNamespaces(XmlObject root)
 String s;
 XmlCursor cursor = root.newCursor();
 while (cursor.hasNextToken())
 if (cursor.isNamespace())
 if (cursor.isStart() || cursor.isAttr())
 s = cursor.getName().getLocalPart();
 cursor.setName(new QName(s));


Tagged ,

JMock – Lessons learned

  • Expectations are asserted after the run of each test method
  • Expectations are additive, so remember to call mock.reset() if you are planning to set a new expectation in each test method
  • If u r using JUnit 4 and above then you no need to extend from MockObjectTestCase. You can use @RunWith(JMock.class). But that means we can not use other JUnit 4 runners like @RunWith(Parameterized.class). You can look into XJ4 (extensions for JUnit 4) project @ to see how it can help
Tagged ,

JMock – mocking the same interface multiple times

A subject can have multiple addresses which i wanted to mock. but the below attempt throws the fololwing error
java.lang.IllegalArgumentException: a mock with name tokenizedUnitedKingdomAddress already exists

UnitedKingdomConsumerSubject primarySubj = context.mock(UnitedKingdomConsumerSubject.class);
TokenizedUnitedKingdomAddress currAddr = context.mock(TokenizedUnitedKingdomAddress.class);
TokenizedUnitedKingdomAddress prevAddr = context.mock(TokenizedUnitedKingdomAddress.class);
TokenizedUnitedKingdomAddress prevAddr2 = context.mock(TokenizedUnitedKingdomAddress.class);
primarySubj.setAddress(CURRENT, currAddr);
primarySubj.setAddress(FORMER, prevAddr);
primarySubj.setAddress(SECOND_FORMER, prevAddr2);

Seems like we have to use the overloaded method public T mock(Class typeToMock, String name). So the below fixed the error

TokenizedUnitedKingdomAddress currAddr = context.mock(TokenizedUnitedKingdomAddress.class, "current address");
TokenizedUnitedKingdomAddress prevAddr = context.mock(TokenizedUnitedKingdomAddress.class, "former address");
TokenizedUnitedKingdomAddress prevAddr2 = context.mock(TokenizedUnitedKingdomAddress.class, "2nd former address"
Tagged ,

Handling Exceptions in Mule

Proper exception handling is a requirement of any integration application. Handling errors with Mule seems to be challenging for a beginner. So here i present various ways of dealing with exceptions to the best of my knowledge.

Note: check the hello example that comes bundled with mule. The below example is based upon that with little bit more explanation


Send a friendly error message back to the user in case of any business exceptions


In request-response MEP (Message Exchange Pattern), the service provider has to send a meaningful error message back to the service consumer when a business exception occurs. The suggested way of dealing with this scenario is to use Exception based filtering (payload-type-filter) in Mule.

MEP: Request-Response

Flow: Idea is to expose the existing BusinessService class over http so that it can receive requests through browser. I have written a NEW BusinessServiceUMO class that acts as a mediator between the requestor and BusinessService. If the BusinessService class throws an exception, BusinessServiceUMO catches it and returns the exception as the NEW payload. I have also configured a payload type filter on the outbound endpoint of BusinessServiceUMO to route the exceptions to UserErrorHandler service. UserErrorHandler service just returns a message contained in the exception and Mule sends this message as a response to the user using the response transformer. All this will be pretty clear if we go through the mule-config.


Important things to be noted w.r.t this example:

  • BusinessServiceUMO returing exception itself as the payload
  • synchronous=”true” on all the participating inbound endpoints enabling Request-Response MEP (so that the response is sent back to the caller)
  • use of payload-type-filter
  • use of responseTransformer-refs=”ExceptionToString” on the UserErrorHandler

BusinessService: Class implementing the business logic. This class can throw two kinds of exceptions: ValidationException and BusinessException. This class has no mule specific logic.

BusinessServiceUMO: receives input events from Mule and delegates the processing to

BusinessService.process(String req) method. This class has the logic specific to Mule. Look at how each of the exceptions are handled. For the payload-type-filter to work, the onEvent method should return the exception as the payload.

public Object onEvent(final Object req)
 Object payload = null;

  payload = service.process((String) req);
 catch (BusinessException be)
  payload = be;
 catch (ValidationException ve)
  payload = ve;

 return payload;

UserErrorHandler: Important thing to note here is the responseTransformer-refs=”ExceptionToString”. All the transformer does is to return the message contained in the exception. Mule then takes this message and returns back to the browser as response


 <spring:bean id="businessService" class="com.aravind.mule.errorhandling.BusinessService" />
 <spring:bean id="businessUMO" class="com.aravind.mule.errorhandling.BusinessServiceUMO">
 <spring:property name="service" ref="businessService"></spring:property>

<!-- Global Transformers -->
<custom-transformer name="HttpRequestToString" class="org.mule.example.hello.HttpRequestToNameString" />
<custom-transformer name="ExceptionToString" class="com.aravind.mule.errorhandling.ExceptionToString" />

<model name="my-service-model">
 <service name="BusinessService">
 <http:inbound-endpoint address="http://localhost:9988"
 transformer-refs="HttpRequestToString" synchronous="true">
 <wildcard-filter pattern="/favicon.ico" />
 <spring-object bean="businessUMO" />
 <vm:outbound-endpoint path="userErrorHandler" />
 <payload-type-filter expectedType="java.lang.Exception" />

<!-- User error handling returns an error message to the end user -->
 <service name="UserErrorHandler">
 <vm:inbound-endpoint path="userErrorHandler"
 responseTransformer-refs="ExceptionToString" synchronous="true" />

Dada Harir’s Well in Gujarat, 1880s.

— My Country, My People

Tagged , ,