View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jdbc;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.endpoint.ImmutableEndpoint;
12  import org.mule.api.endpoint.OutboundEndpoint;
13  import org.mule.transport.AbstractMessageDispatcher;
14  import org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategy;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * The Jdbc Message dispatcher is responsible for executing SQL queries against a
21   * database.
22   */
23  public class JdbcMessageDispatcher extends AbstractMessageDispatcher
24  {
25  
26      protected static Log staticLogger = LogFactory.getLog(AbstractMessageDispatcher.class);
27  
28      protected JdbcConnector connector;
29  
30      public JdbcMessageDispatcher(OutboundEndpoint endpoint)
31      {
32          super(endpoint);
33          this.connector = (JdbcConnector) endpoint.getConnector();
34      }
35  
36      @Override
37      protected void doDispose()
38      {
39          // template method
40      } 
41      
42      @Override
43      protected void doDispatch(MuleEvent event) throws Exception
44      {
45          if (logger.isDebugEnabled())
46          {
47              logger.debug("Dispatch event: " + event);
48          }
49          
50          doSend(event);
51      }
52  
53      @Override
54      protected MuleMessage doSend(MuleEvent event) throws Exception
55      {
56          // Use a strategy pattern to choose a particular strategy to handle the SQL request
57          ImmutableEndpoint ep = event.getEndpoint();
58          JdbcConnector jdbcConnector = (JdbcConnector) ep.getConnector();
59          String statement = jdbcConnector.getStatement(ep);
60          Object payload = event.getMessage().getPayload();
61          
62          SqlStatementStrategy strategy = 
63              jdbcConnector.getSqlStatementStrategyFactory().create(statement, payload);
64          return strategy.executeStatement(jdbcConnector, ep, event, event.getTimeout());
65      }
66  
67      @Override
68      protected void doConnect() throws Exception
69      {
70          // template method
71      }
72  
73      @Override
74      protected void doDisconnect() throws Exception
75      {
76          // template method
77      }
78  }