public boolean transform()

in common/servicemix-components/src/main/java/org/apache/servicemix/components/jdbc/JdbcComponent.java [51:145]


    public boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
        logger.debug("Received a JDBC request. Datasource=" + dataSource + ", ResponseRequired=" + responseRequired);
        Connection conn   = null;
        Statement stmt    = null;
        ResultSet rs      = null;

        try {

            SourceTransformer domTransform = new SourceTransformer();
            Node domNode = domTransform.toDOMNode(in);

            // Return the exception message
//            if (isExceptionXml(domNode)) {
//                LOGGER.debug("Found an exception message: " + domNode);
//                out.setContent(in.getContent());
//                return true;
//            }

            String query = getQuery(domNode);
            logger.debug("Retrieved query: " + query);

            conn = dataSource.getConnection();

            stmt  = conn.createStatement();

            Source outMsg = null;
            if (query != null && query.length() > 0) {
                if (stmt.execute(query)) {
                    // Result is a ResultSet object
                    rs = stmt.getResultSet();

                    logger.debug("Formatting ResultSet: " + rs);
                    outMsg = toXmlSource(rs);
                } else {
                    int updateCount = stmt.getUpdateCount();
                    if (updateCount > -1) {
                        logger.debug("Formatting UpdateCount: " + updateCount);
                        // Result is an update count
                        outMsg = toXmlSource(updateCount);
                    } else {
                        logger.debug("Formatting NoResult.");
                        // Result is neither a result set nor an update count
                        outMsg = null;
                    }
                }
            }

            if (outMsg != null) {
                // There is a valid response
                logger.debug("Response: " + domTransform.toString(outMsg));
                out.setContent(outMsg);
                return true;

            } else if (responseRequired) {
                // Create an empty <sqlResult> element
                logger.debug("Response: Empty Response");
                out.setContent(toXmlSource());
                return true;

            } else {
                logger.debug("Response: No Response");
                // There is no valid response
                return false;
            }
        } catch (Exception e) {
            logger.error("JDBC Component Exception: ", e);
//            out.setContent(createExceptionXml(e));
//            return true;
            throw new MessagingException(e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // Ignore
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // Ignore
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // Ignore
                }
            }
        }
    }