1 17 package org.apache.servicemix.components.jdbc; 18 19 import org.apache.servicemix.MessageExchangeListener; 20 import org.apache.servicemix.components.util.TransformComponentSupport; 21 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 22 import org.apache.servicemix.jbi.jaxp.StringSource; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.commons.logging.Log; 25 import org.apache.xpath.CachedXPathAPI; 26 import org.w3c.dom.Node ; 27 28 import javax.jbi.messaging.MessageExchange; 29 import javax.jbi.messaging.MessagingException; 30 import javax.jbi.messaging.NormalizedMessage; 31 import javax.sql.DataSource ; 32 import javax.xml.transform.Source ; 33 import java.sql.Connection ; 34 import java.sql.Statement ; 35 import java.sql.ResultSet ; 36 import java.sql.SQLException ; 37 import java.sql.ResultSetMetaData ; 38 39 public class JdbcComponent extends TransformComponentSupport implements MessageExchangeListener { 40 private static final Log log = LogFactory.getLog(JdbcComponent.class); 41 42 private DataSource dataSource; 43 private boolean responseRequired = false; 44 45 public boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 46 log.debug("Received a JDBC request. Datasource=" + dataSource + ", ResponseRequired=" + responseRequired); 47 Connection conn = null; 48 Statement stmt = null; 49 ResultSet rs = null; 50 51 try { 52 53 SourceTransformer domTransform = new SourceTransformer(); 54 Node domNode = domTransform.toDOMNode(in); 55 56 63 String query = getQuery(domNode); 64 log.debug("Retrieved query: " + query); 65 66 conn = dataSource.getConnection(); 67 68 stmt = conn.createStatement(); 69 70 Source outMsg = null; 71 if (query != null && query.length() > 0) { 72 if (stmt.execute(query)) { 73 rs = stmt.getResultSet(); 75 76 log.debug("Formatting ResultSet: " + rs); 77 outMsg = toXmlSource(rs); 78 } else { 79 int updateCount = stmt.getUpdateCount(); 80 if (updateCount > -1) { 81 log.debug("Formatting UpdateCount: " + updateCount); 82 outMsg = toXmlSource(updateCount); 84 } else { 85 log.debug("Formatting NoResult."); 86 outMsg = null; 88 } 89 } 90 } 91 92 if (outMsg != null) { 93 log.debug("Response: " + domTransform.toString(outMsg)); 95 out.setContent(outMsg); 96 return true; 97 98 } else if (responseRequired) { 99 log.debug("Response: Empty Response"); 101 out.setContent(toXmlSource()); 102 return true; 103 104 } else { 105 log.debug("Response: No Response"); 106 return false; 108 } 109 } catch (Exception e) { 110 log.error("JDBC Component Exception: ", e); 111 throw new MessagingException(e); 114 } finally { 115 if (rs != null) { 116 try { 117 rs.close(); 118 } catch (SQLException e) { 119 } 121 } 122 123 if (stmt != null) { 124 try { 125 stmt.close(); 126 } catch (SQLException e) { 127 } 129 } 130 131 if (conn != null) { 132 try { 133 conn.close(); 134 } catch (SQLException e) { 135 } 137 } 138 } 139 } 140 141 public String getQuery(Node node) throws Exception { 142 CachedXPathAPI xpath = new CachedXPathAPI(); 143 144 node = xpath.selectSingleNode(node, "sql/child::text()"); 145 146 if (node == null) { 148 throw new IllegalStateException ("Expecting <sql></sql> node. Found: " + node); 149 } 150 151 return node.getNodeValue(); 152 } 153 154 public void setDataSource(DataSource ds) { 155 dataSource = ds; 156 } 157 158 public DataSource getDataSource() { 159 return dataSource; 160 } 161 162 166 public void setResponseRequired(boolean val) { 167 responseRequired = val; 168 } 169 170 public boolean getResponseRequired() { 171 return responseRequired; 172 } 173 174 protected Source toXmlSource(ResultSet rs) throws Exception { 175 176 ResultSetMetaData meta = rs.getMetaData(); 177 int colCount = meta.getColumnCount(); 178 179 StringBuffer buff = new StringBuffer (""); 180 181 while (rs.next()) { 182 buff.append("<row "); 183 for (int i=1; i<=colCount; i++) { 184 String colName = meta.getColumnName(i); 185 if (colName.equals("")) { 186 colName = "__unknown_column__"; 187 } 188 buff.append(colName.toLowerCase() + "='" + rs.getString(i) + "' "); 189 } 190 buff.append("/>"); 191 } 192 193 if (buff.length() > 0) { 194 buff.insert(0, "<sqlResult><resultSet>"); 196 buff.append("</resultSet></sqlResult>"); 197 } else { 198 return null; 200 } 201 202 return new StringSource(buff.toString()); 203 } 204 205 protected Source toXmlSource(int updateCount) throws Exception { 206 return new StringSource("<sqlResult><updateCount value='" + updateCount + "'/></sqlResult>"); 207 } 208 209 protected Source toXmlSource() throws Exception { 210 return new StringSource("<sqlResult></sqlResult>"); 211 } 212 213 214 223 } 224 | Popular Tags |