View Javadoc

1   /*
2    * $Id: ValidateMessageType.java 8525 2007-09-20 19:53:00Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.bpm.jbpm.actions;
12  
13  import org.mule.util.ClassUtils;
14  
15  import org.jbpm.JbpmException;
16  import org.jbpm.graph.exe.ExecutionContext;
17  
18  /**
19   * Throws an exception if the incoming message's class is not as expected.
20   * 
21   *  <action class="org.mule.providers.bpm.jbpm.actions.ValidateMessageType">
22   *     <expectedType>com.mycompany.MyClass</expectedType> 
23   *  </action>
24   *  
25   * @param expectedType the expected class type
26   * @param strict if true, the class must match exactly, otherwise it can be a subclass
27   * @throws JbpmException 
28   */
29  public class ValidateMessageType extends IntegrationActionHandler
30  {
31  
32      private static final long serialVersionUID = 1L;
33  
34      protected String expectedType;
35      protected boolean strict = false;
36  
37      public void execute(ExecutionContext executionContext) throws Exception
38      {
39          super.execute(executionContext);
40          Object message = getIncomingMessage();
41          if (message == null)
42          {
43              throw new JbpmException("Incoming message is null.");
44          }
45  
46          Class expectedClass = ClassUtils.loadClass(expectedType, this.getClass());
47          boolean match;
48          if (strict)
49          {
50              match = message.getClass().equals(expectedClass);
51          }
52          else
53          {
54              match = expectedClass.isAssignableFrom(message.getClass());
55          }
56          if (!match)
57          {
58              throw new JbpmException("Incoming message type is " + message.getClass() + ", expected type is "
59                              + expectedType);
60          }
61      }
62  }