Search in sources :

Example 21 with SynapseException

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

the class JDBCMessageStore method getProcessedRows.

/**
 * Will return the list of processed message rows.
 *
 * @param statement the statement executed in the DB.
 * @return the rows which contains the column data wrapped inside a map.
 */
protected List<Map> getProcessedRows(Statement statement) {
    Connection con = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    List<Map> elements;
    try {
        con = jdbcConfiguration.getConnection();
        ps = con.prepareStatement(statement.getStatement());
        int index = 1;
        for (Object param : statement.getParameters()) {
            if (param instanceof String) {
                ps.setString(index, (String) param);
            } else if (param instanceof Long) {
                ps.setLong(index, (Long) param);
            } else if (param instanceof Integer) {
                ps.setInt(index, (Integer) param);
            }
            index++;
        }
        rs = ps.executeQuery();
        elements = statement.getResult(rs);
    } catch (SQLException e) {
        throw new SynapseException("Processing Statement failed : " + statement.getStatement() + " against DataSource : " + jdbcConfiguration.getDSName(), e);
    } finally {
        close(con, ps, rs);
    }
    return elements;
}
Also used : SynapseException(org.apache.synapse.SynapseException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with SynapseException

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

the class JDBCMessageStore method processNonResultingStatement.

/**
 * Process statements that do not give a ResultSet
 *
 * @param statements - Statement to process
 * @return - Success or Failure of the process
 */
private boolean processNonResultingStatement(List<Statement> statements) throws SynapseException {
    Connection connection = null;
    boolean result;
    PreparedStatement preparedStatement = null;
    try {
        connection = jdbcConfiguration.getConnection();
        connection.setAutoCommit(false);
        for (Statement statement : statements) {
            preparedStatement = connection.prepareStatement(statement.getStatement());
            int index = 1;
            for (Object param : statement.getParameters()) {
                if (param instanceof String) {
                    preparedStatement.setString(index, (String) param);
                } else if (param instanceof Long) {
                    preparedStatement.setLong(index, (Long) param);
                } else if (param instanceof StorableMessage) {
                    // Serialize the object into byteArray and update the statement
                    preparedStatement.setBytes(index, serialize(param));
                }
                index++;
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Executing statement:" + preparedStatement);
            }
            preparedStatement.execute();
        }
        connection.commit();
        result = true;
    } catch (SQLException | IOException e) {
        rollback(connection, "deleting message");
        throw new SynapseException("Processing Statement failed against DataSource : " + jdbcConfiguration.getDSName(), e);
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                logger.error("Error while closing prepared statement", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error("Error while closing connection", e);
            }
        }
    }
    return result;
}
Also used : SynapseException(org.apache.synapse.SynapseException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) SQLException(java.sql.SQLException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 23 with SynapseException

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

the class JDBCMessageStore method store.

/**
 * Add a message to the end of the table. If fetching success return true else false
 *
 * @param messageContext message to insert
 * @return -  success/failure of fetching
 */
public boolean store(MessageContext messageContext) throws SynapseException {
    if (messageContext == null) {
        logger.error("Message is null, can't store into database");
        return false;
    }
    boolean cleaningState = false;
    try {
        if (cleaningFlag.get()) {
            try {
                cleanUpOfferLock.lock();
                cleaningState = true;
            } catch (Exception e) {
                logger.error("Message Cleanup lock released unexpectedly", e);
            }
        }
        ArrayList<Statement> statements = new ArrayList<>();
        Statement statement = getStoreMessageStatement(messageContext, null);
        statements.add(statement);
        return processNonResultingStatement(statements);
    } catch (Exception e) {
        throw new SynapseException("Error while creating StorableMessage", e);
    } finally {
        if (cleaningState) {
            cleanUpOfferLock.unlock();
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

Example 24 with SynapseException

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

the class JDBCMessageStore method peek.

/**
 * Select and return the first element in current table
 *
 * @return - Select and return the first element from the table
 */
public MessageContext peek() throws SynapseException {
    MessageContext msg;
    try {
        Statement statement = new Statement("SELECT message FROM " + jdbcConfiguration.getTableName() + " WHERE indexId=(SELECT min(indexId) from " + jdbcConfiguration.getTableName() + ")") {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                return messageContentResultSet(resultSet, this.getStatement());
            }
        };
        msg = getResultMessageContextFromDatabase(statement);
    } catch (SynapseException se) {
        throw new SynapseException("Error while peek the message", se);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with SynapseException

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

the class JDBCMessageStore method deserializeMessage.

/**
 * Will convert the byte[] message to store-able message.
 *
 * @param msgObj serialized message read from the database.
 * @return converted message context.
 */
protected MessageContext deserializeMessage(byte[] msgObj) {
    MessageContext messageContext = null;
    if (msgObj != null) {
        ObjectInputStream ios = null;
        try {
            // Convert back to MessageContext and add to list
            ios = new ObjectInputStream(new ByteArrayInputStream(msgObj));
            Object msg = ios.readObject();
            if (msg instanceof StorableMessage) {
                StorableMessage jdbcMsg = (StorableMessage) msg;
                org.apache.axis2.context.MessageContext axis2Mc = this.newAxis2Mc();
                MessageContext synapseMc = this.newSynapseMc(axis2Mc);
                messageContext = MessageConverter.toMessageContext(jdbcMsg, axis2Mc, synapseMc);
            }
        } catch (IOException e) {
            throw new SynapseException("Error reading object input stream", e);
        } catch (ClassNotFoundException e) {
            throw new SynapseException("Could not find the class", e);
        } finally {
            closeStream(ios);
        }
    } else {
        throw new SynapseException("Retrieved Object is null");
    }
    return messageContext;
}
Also used : SynapseException(org.apache.synapse.SynapseException) ByteArrayInputStream(java.io.ByteArrayInputStream) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

SynapseException (org.apache.synapse.SynapseException)136 OMElement (org.apache.axiom.om.OMElement)31 OMAttribute (org.apache.axiom.om.OMAttribute)23 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)16 QName (javax.xml.namespace.QName)15 Iterator (java.util.Iterator)14 JaxenException (org.jaxen.JaxenException)14 XMLStreamException (javax.xml.stream.XMLStreamException)13 AxisFault (org.apache.axis2.AxisFault)13 Map (java.util.Map)12 Endpoint (org.apache.synapse.endpoints.Endpoint)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)8 OMNode (org.apache.axiom.om.OMNode)7 Mediator (org.apache.synapse.Mediator)7 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)7