Search in sources :

Example 6 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class AbstractDBMediator method getPreparedStatement.

/**
 * Return a Prepared statement for the given Statement object, which is ready to be executed
 *
 * @param stmnt  SQL stataement to be executed
 * @param con    The connection to be used
 * @param msgCtx Current message context
 * @return a PreparedStatement
 * @throws SQLException on error
 */
protected PreparedStatement getPreparedStatement(Statement stmnt, Connection con, MessageContext msgCtx) throws SQLException {
    SynapseLog synLog = getLog(msgCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Getting a connection from DataSource " + getDSName() + " and preparing statement : " + stmnt.getRawStatement());
    }
    if (con == null) {
        String msg = "Connection from DataSource " + getDSName() + " is null.";
        log.error(msg);
        throw new SynapseException(msg);
    }
    if (dataSource instanceof BasicDataSource) {
        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
        int numActive = basicDataSource.getNumActive();
        int numIdle = basicDataSource.getNumIdle();
        String connectionId = Integer.toHexString(con.hashCode());
        DBPoolView dbPoolView = getDbPoolView();
        if (dbPoolView != null) {
            dbPoolView.setNumActive(numActive);
            dbPoolView.setNumIdle(numIdle);
            dbPoolView.updateConnectionUsage(connectionId);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("[ DB Connection : " + con + " ]");
            synLog.traceOrDebug("[ DB Connection instance identifier : " + connectionId + " ]");
            synLog.traceOrDebug("[ Number of Active Connection : " + numActive + " ]");
            synLog.traceOrDebug("[ Number of Idle Connection : " + numIdle + " ]");
        }
    }
    PreparedStatement ps = con.prepareStatement(stmnt.getRawStatement());
    // set parameters if any
    List<Statement.Parameter> params = stmnt.getParameters();
    int column = 1;
    for (Statement.Parameter param : params) {
        if (param == null) {
            continue;
        }
        String value = (param.getPropertyName() != null ? param.getPropertyName() : param.getXpath().stringValueOf(msgCtx));
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Setting as parameter : " + column + " value : " + value + " as JDBC Type : " + param.getType() + "(see java.sql.Types for valid " + "types)");
        }
        switch(param.getType()) {
            // according to J2SE 1.5 /docs/guide/jdbc/getstart/mapping.html
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                {
                    if (value != null && value.length() != 0) {
                        ps.setString(column++, value);
                    } else {
                        ps.setString(column++, null);
                    }
                    break;
                }
            case Types.NUMERIC:
            case Types.DECIMAL:
                {
                    if (value != null && value.length() != 0) {
                        ps.setBigDecimal(column++, new BigDecimal(value));
                    } else {
                        ps.setBigDecimal(column++, null);
                    }
                    break;
                }
            case Types.BIT:
                {
                    if (value != null && value.length() != 0) {
                        ps.setBoolean(column++, Boolean.parseBoolean(value));
                    } else {
                        ps.setNull(column++, Types.BIT);
                    }
                    break;
                }
            case Types.TINYINT:
                {
                    if (value != null && value.length() != 0) {
                        ps.setByte(column++, Byte.parseByte(value));
                    } else {
                        ps.setNull(column++, Types.TINYINT);
                    }
                    break;
                }
            case Types.SMALLINT:
                {
                    if (value != null && value.length() != 0) {
                        ps.setShort(column++, Short.parseShort(value));
                    } else {
                        ps.setNull(column++, Types.SMALLINT);
                    }
                    break;
                }
            case Types.INTEGER:
                {
                    if (value != null && value.length() != 0) {
                        ps.setInt(column++, Integer.parseInt(value));
                    } else {
                        ps.setNull(column++, Types.INTEGER);
                    }
                    break;
                }
            case Types.BIGINT:
                {
                    if (value != null && value.length() != 0) {
                        ps.setLong(column++, Long.parseLong(value));
                    } else {
                        ps.setNull(column++, Types.BIGINT);
                    }
                    break;
                }
            case Types.REAL:
                {
                    if (value != null && value.length() != 0) {
                        ps.setFloat(column++, Float.parseFloat(value));
                    } else {
                        ps.setNull(column++, Types.REAL);
                    }
                    break;
                }
            case Types.FLOAT:
                {
                    if (value != null && value.length() != 0) {
                        ps.setDouble(column++, Double.parseDouble(value));
                    } else {
                        ps.setNull(column++, Types.FLOAT);
                    }
                    break;
                }
            case Types.DOUBLE:
                {
                    if (value != null && value.length() != 0) {
                        ps.setDouble(column++, Double.parseDouble(value));
                    } else {
                        ps.setNull(column++, Types.DOUBLE);
                    }
                    break;
                }
            // skip BINARY, VARBINARY and LONGVARBINARY
            case Types.DATE:
                {
                    if (value != null && value.length() != 0) {
                        ps.setDate(column++, Date.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.DATE);
                    }
                    break;
                }
            case Types.TIME:
                {
                    if (value != null && value.length() != 0) {
                        ps.setTime(column++, Time.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.TIME);
                    }
                    break;
                }
            case Types.TIMESTAMP:
                {
                    if (value != null && value.length() != 0) {
                        ps.setTimestamp(column++, Timestamp.valueOf(value));
                    } else {
                        ps.setNull(column++, Types.TIMESTAMP);
                    }
                    break;
                }
            // skip CLOB, BLOB, ARRAY, DISTINCT, STRUCT, REF, JAVA_OBJECT
            default:
                {
                    String msg = "Trying to set an un-supported JDBC Type : " + param.getType() + " against column : " + column + " and statement : " + stmnt.getRawStatement() + " used by a DB mediator against DataSource : " + getDSName() + " (see java.sql.Types for valid type values)";
                    handleException(msg, msgCtx);
                }
        }
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Successfully prepared statement : " + stmnt.getRawStatement() + " against DataSource : " + getDSName());
    }
    return ps;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) DBPoolView(org.apache.synapse.commons.datasource.DBPoolView) SynapseException(org.apache.synapse.SynapseException) PreparedStatement(java.sql.PreparedStatement) PreparedStatement(java.sql.PreparedStatement) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) BigDecimal(java.math.BigDecimal)

Example 7 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class DBLookupMediator method processStatement.

protected void processStatement(Statement stmnt, MessageContext msgCtx) {
    SynapseLog synLog = getLog(msgCtx);
    // execute the prepared statement, and extract the first result row and
    // set as message context properties, any results that have been specified
    Connection con = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    boolean threadInTx = false;
    try {
        if (TranscationManger.isThreadHasEnlistment()) {
            threadInTx = true;
            try {
                con = TranscationManger.addConnection(this.getDataSource());
            } catch (Exception e) {
                handleException("SQL Error while adding/getting connection to/from cache : " + stmnt.getRawStatement() + " against DataSource : " + getDSName(), e, msgCtx);
            }
        } else {
            con = this.getDataSource().getConnection();
        }
        ps = getPreparedStatement(stmnt, con, msgCtx);
        rs = ps.executeQuery();
        if (rs.next()) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Processing the first row returned : " + stmnt.getRawStatement());
            }
            for (String propName : stmnt.getResultsMap().keySet()) {
                String columnStr = stmnt.getResultsMap().get(propName);
                Object obj;
                try {
                    int colNum = Integer.parseInt(columnStr);
                    obj = rs.getObject(colNum);
                } catch (NumberFormatException ignore) {
                    obj = rs.getObject(columnStr);
                }
                if (obj != null) {
                    if (synLog.isTraceOrDebugEnabled()) {
                        synLog.traceOrDebug("Column : " + columnStr + " returned value : " + obj + " Setting this as the message property : " + propName);
                    }
                    msgCtx.setProperty(propName, obj.toString());
                } else {
                    if (synLog.isTraceOrDebugEnabled()) {
                        synLog.traceOrDebugWarn("Column : " + columnStr + " returned null Skip setting message property : " + propName);
                    }
                }
            }
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Statement : " + stmnt.getRawStatement() + " returned 0 rows");
            }
        }
    } catch (SQLException e) {
        handleException("SQL Exception occurred while executing statement : " + stmnt.getRawStatement() + " against DataSource : " + getDSName(), e, msgCtx);
    } catch (Exception e) {
        handleException("Error executing statement : " + stmnt.getRawStatement() + " against DataSource : " + getDSName(), e, msgCtx);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException ignore) {
            }
        }
        if (con != null && !threadInTx) {
            try {
                con.close();
            } catch (SQLException ignore) {
            }
        }
    }
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 8 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class FilterMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Filter mediator : Mediating from ContinuationState");
    }
    boolean result;
    int subBranch = ((ReliantContinuationState) continuationState).getSubBranch();
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    if (subBranch == 0) {
        if (!continuationState.hasChild()) {
            result = super.mediate(synCtx, continuationState.getPosition() + 1);
        } else {
            FlowContinuableMediator mediator = (FlowContinuableMediator) getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (isStatisticsEnabled) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
        }
    } else {
        if (!continuationState.hasChild()) {
            result = elseMediator.mediate(synCtx, continuationState.getPosition() + 1);
        } else {
            FlowContinuableMediator mediator = (FlowContinuableMediator) elseMediator.getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (isStatisticsEnabled) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
        }
        if (isStatisticsEnabled) {
            elseMediator.reportCloseStatistics(synCtx, null);
        }
    }
    return result;
}
Also used : ReliantContinuationState(org.apache.synapse.continuation.ReliantContinuationState) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) ListMediator(org.apache.synapse.mediators.ListMediator) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator)

Example 9 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class InMediator method mediate.

/**
 * Executes the list of sub/child mediators, if the filter condition is satisfied
 *
 * @param synCtx the current message
 * @return true if filter condition fails. else returns as per List mediator semantics
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : In mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    boolean result = true;
    if (test(synCtx)) {
        synLog.traceOrDebug("Current message is incoming - executing child mediators");
        ContinuationStackManager.addReliantContinuationState(synCtx, 0, getMediatorPosition());
        result = super.mediate(synCtx);
        if (result) {
            ContinuationStackManager.removeReliantContinuationState(synCtx);
        }
    } else {
        synLog.traceOrDebug("Current message is a response - skipping child mediators");
    }
    synLog.traceOrDebug("End : In mediator");
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog)

Example 10 with SynapseLog

use of org.apache.synapse.SynapseLog in project wso2-synapse by wso2.

the class InMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("In mediator : Mediating from ContinuationState");
    }
    boolean result;
    if (!continuationState.hasChild()) {
        result = super.mediate(synCtx, continuationState.getPosition() + 1);
    } else {
        FlowContinuableMediator mediator = (FlowContinuableMediator) getChild(continuationState.getPosition());
        result = mediator.mediate(synCtx, continuationState.getChildContState());
        if (RuntimeStatisticCollector.isStatisticsEnabled()) {
            ((Mediator) mediator).reportCloseStatistics(synCtx, null);
        }
    }
    return result;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator)

Aggregations

SynapseLog (org.apache.synapse.SynapseLog)53 Mediator (org.apache.synapse.Mediator)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)10 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)9 MessageContext (org.apache.synapse.MessageContext)8 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)8 OMNode (org.apache.axiom.om.OMNode)7 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)6 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)6 JaxenException (org.jaxen.JaxenException)5 PreparedStatement (java.sql.PreparedStatement)4 Set (java.util.Set)4 AxisFault (org.apache.axis2.AxisFault)4 OperationContext (org.apache.axis2.context.OperationContext)4 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 ReliantContinuationState (org.apache.synapse.continuation.ReliantContinuationState)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2