View Javadoc

1   /*
2    * $Id: JdbcMessageDispatcher.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.jdbc;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.ImmutableEndpoint;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.transport.AbstractMessageDispatcher;
18  import org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategy;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * The Jdbc Message dispatcher is responsible for executing SQL queries against a
25   * database.
26   */
27  public class JdbcMessageDispatcher extends AbstractMessageDispatcher
28  {
29  
30      protected static Log staticLogger = LogFactory.getLog(AbstractMessageDispatcher.class);
31  
32      protected JdbcConnector connector;
33  
34      public JdbcMessageDispatcher(OutboundEndpoint endpoint)
35      {
36          super(endpoint);
37          this.connector = (JdbcConnector) endpoint.getConnector();
38      }
39  
40      @Override
41      protected void doDispose()
42      {
43          // template method
44      } 
45      
46      @Override
47      protected void doDispatch(MuleEvent event) throws Exception
48      {
49          if (logger.isDebugEnabled())
50          {
51              logger.debug("Dispatch event: " + event);
52          }
53          
54          doSend(event);
55      }
56  
57      @Override
58      protected MuleMessage doSend(MuleEvent event) throws Exception
59      {
60          // Use a strategy pattern to choose a particular strategy to handle the SQL request
61          ImmutableEndpoint ep = event.getEndpoint();
62          JdbcConnector jdbcConnector = (JdbcConnector) ep.getConnector();
63          String statement = jdbcConnector.getStatement(ep);
64          Object payload = event.getMessage().getPayload();
65          
66          SqlStatementStrategy strategy = 
67              jdbcConnector.getSqlStatementStrategyFactory().create(statement, payload);
68          return strategy.executeStatement(jdbcConnector, ep, event, event.getTimeout());
69      }
70  
71      @Override
72      protected void doConnect() throws Exception
73      {
74          // template method
75      }
76  
77      @Override
78      protected void doDisconnect() throws Exception
79      {
80          // template method
81      }
82  }