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;
}
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) {
}
}
}
}
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;
}
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;
}
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;
}
Aggregations