View Javadoc

1   /*
2    * $Id: NestedRouter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.routing.nested;
12  
13  import org.mule.management.stats.RouterStatistics;
14  import org.mule.routing.AbstractRouter;
15  import org.mule.routing.outbound.OutboundPassThroughRouter;
16  import org.mule.umo.MessagingException;
17  import org.mule.umo.UMOEvent;
18  import org.mule.umo.UMOMessage;
19  import org.mule.umo.endpoint.UMOEndpoint;
20  import org.mule.umo.endpoint.UMOImmutableEndpoint;
21  import org.mule.umo.routing.UMONestedRouter;
22  import org.mule.umo.routing.UMOOutboundRouter;
23  
24  import java.lang.reflect.Proxy;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  public class NestedRouter extends AbstractRouter implements UMONestedRouter
30  {
31  
32      protected static Log logger = LogFactory.getLog(NestedRouter.class);
33  
34      private Class interfaceClass;
35  
36      private String methodName;
37  
38      private UMOEndpoint endpoint;
39  
40      //The router ued to actually ispatch the message
41      protected UMOOutboundRouter outboundRouter;
42  
43      public NestedRouter()
44      {
45          setRouterStatistics(new RouterStatistics(RouterStatistics.TYPE_NESTED));
46      }
47  
48      public UMOMessage route(final UMOEvent event) throws MessagingException
49      {
50          final UMOImmutableEndpoint endpoint = event.getEndpoint();
51  
52          return outboundRouter.route(event.getMessage(), event.getSession(), endpoint.isSynchronous());
53      }
54  
55  
56      public void setInterface(Class interfaceClass)
57      {
58          this.interfaceClass = interfaceClass;
59      }
60  
61      public Class getInterface()
62      {
63          return interfaceClass;
64      }
65  
66  
67      public String getMethod()
68      {
69          return methodName;
70      }
71  
72      public void setMethod(String methodName)
73      {
74          this.methodName = methodName;
75      }
76  
77      /* (non-Javadoc)
78       * @see org.mule.umo.routing.UMONestedRouter#createProxy(java.lang.Object, UMODescriptor descriptor java.lang.Class)
79       */
80      public Object createProxy(Object target)
81      {
82          try
83          {
84              Object proxy = Proxy.newProxyInstance(getInterface().getClassLoader(),
85                      new Class[]{getInterface()}, new NestedInvocationHandler(this));
86              return proxy;
87  
88          }
89          catch (Exception e)
90          {
91              logger.error(e);
92              throw new RuntimeException(e);
93          }
94      }
95  
96      public UMOEndpoint getEndpoint()
97      {
98          return endpoint;
99      }
100 
101     public void setEndpoint(UMOEndpoint e)
102     {
103         endpoint = e;
104         //TODO RM** endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_SENDER_AND_RECEIVER);
105         outboundRouter = new OutboundPassThroughRouter();
106         outboundRouter.addEndpoint(endpoint);
107         outboundRouter.setTransactionConfig(endpoint.getTransactionConfig());
108     }
109 
110 
111     public Class getInterfaceClass()
112     {
113         return interfaceClass;
114     }
115 
116 
117     public String toString()
118     {
119         final StringBuffer sb = new StringBuffer();
120         sb.append("NestedRouter");
121         sb.append("{method='").append(methodName).append('\'');
122         sb.append(", interface=").append(interfaceClass);
123         sb.append('}');
124         return sb.toString();
125     }
126 }