View Javadoc

1   /*
2    * $Id: DefaultNestedRouter.java 12269 2008-07-10 04:19:03Z dfeist $
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.api.MessagingException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleRuntimeException;
16  import org.mule.api.MuleSession;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.routing.NestedRouter;
19  import org.mule.api.routing.OutboundRouter;
20  import org.mule.config.i18n.CoreMessages;
21  import org.mule.management.stats.RouterStatistics;
22  import org.mule.routing.AbstractRouter;
23  import org.mule.routing.outbound.OutboundPassThroughRouter;
24  
25  import java.lang.reflect.Proxy;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  public class DefaultNestedRouter extends AbstractRouter implements NestedRouter
31  {
32  
33      private static final Log logger = LogFactory.getLog(DefaultNestedRouter.class);
34  
35      private Class interfaceClass;
36  
37      private String methodName;
38  
39      // The router used to actually dispatch the message
40      protected OutboundRouter outboundRouter;
41  
42      public DefaultNestedRouter()
43      {
44          setRouterStatistics(new RouterStatistics(RouterStatistics.TYPE_NESTED));
45      }
46  
47      public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous) throws MessagingException
48      {
49          return outboundRouter.route(message, session, synchronous);
50      }
51  
52      public void setInterface(Class interfaceClass)
53      {
54          this.interfaceClass = interfaceClass;
55      }
56  
57      public Class getInterface()
58      {
59          return interfaceClass;
60      }
61  
62      public String getMethod()
63      {
64          return methodName;
65      }
66  
67      public void setMethod(String methodName)
68      {
69          this.methodName = methodName;
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see org.mule.api.routing.NestedRouter#createProxy(java.lang.Object,
76       *      UMODescriptor descriptor java.lang.Class)
77       */
78      public Object createProxy(Object target)
79      {
80          try
81          {
82              Object proxy = Proxy.newProxyInstance(getInterface().getClassLoader(), new Class[]{getInterface()},
83                  new NestedInvocationHandler(this));
84              logger.debug("Have proxy?: " + (null != proxy));
85              return proxy;
86  
87          }
88          catch (Exception e)
89          {
90              throw new MuleRuntimeException(CoreMessages.failedToCreateProxyFor(target), e);
91          }
92      }
93  
94      public void setEndpoint(OutboundEndpoint e)
95      {
96          outboundRouter = new OutboundPassThroughRouter();
97          outboundRouter.addEndpoint(e);
98          outboundRouter.setTransactionConfig(e.getTransactionConfig());
99      }
100 
101     public Class getInterfaceClass()
102     {
103         return interfaceClass;
104     }
105 
106     public String toString()
107     {
108         final StringBuffer sb = new StringBuffer();
109         sb.append("DefaultNestedRouter");
110         sb.append("{method='").append(methodName).append('\'');
111         sb.append(", interface=").append(interfaceClass);
112         sb.append('}');
113         return sb.toString();
114     }
115 
116     public OutboundEndpoint getEndpoint()
117     {
118         if (outboundRouter != null)
119         {
120             return (OutboundEndpoint) outboundRouter.getEndpoints().get(0);
121         }
122         else
123         {
124             return null;
125         }
126     }
127 }