Search in sources :

Example 1 with PacketReadEOFException

use of com.sun.messaging.jmq.io.PacketReadEOFException in project openmq by eclipse-ee4j.

the class MessageDAOImpl method getMessage.

/**
 * Get a Message.
 *
 * @param conn database connection
 * @param dstUID the destination
 * @param id the system message id of the message
 * @return Packet the message
 */
@Override
public Packet getMessage(Connection conn, DestinationUID dstUID, String id) throws BrokerException {
    Packet msg = null;
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        pstmt = dbMgr.createPreparedStatement(conn, selectSQL);
        pstmt.setString(1, id);
        rs = pstmt.executeQuery();
        msg = (Packet) loadData(rs, true);
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED + "[" + selectSQL + "]", rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof InvalidPacketException) {
            InvalidPacketException ipe = (InvalidPacketException) e;
            ipe.appendMessage("[" + id + ", " + dstUID + "][" + selectSQL + "]");
            ex = ipe;
        } else if (e instanceof PacketReadEOFException) {
            PacketReadEOFException pre = (PacketReadEOFException) e;
            pre.appendMessage("[" + id + ", " + dstUID + "][" + selectSQL + "]");
            ex = pre;
        } else if (e instanceof IOException) {
            ex = DBManager.wrapIOException("[" + selectSQL + "]", (IOException) e);
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + selectSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_LOAD_MESSAGE_FAILED, id), ex);
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    if (msg == null) {
        throw new BrokerException(br.getKString(BrokerResources.E_MSG_NOT_FOUND_IN_STORE, id, dstUID), Status.NOT_FOUND);
    }
    return msg;
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException)

Example 2 with PacketReadEOFException

use of com.sun.messaging.jmq.io.PacketReadEOFException in project openmq by eclipse-ee4j.

the class MessageDAOImpl method loadData.

/**
 * Load a single message or messages from a ResultSet.
 *
 * @param rs the ResultSet
 * @param isSingleRow specify interesed in only the 1st row of the ResultSet
 * @return a message or a List of messages
 */
protected Object loadData(ResultSet rs, boolean isSingleRow) throws IOException, SQLException {
    ArrayList list = null;
    if (!isSingleRow) {
        list = new ArrayList(100);
    }
    while (rs.next()) {
        Packet msg = new Packet(false);
        msg.generateTimestamp(false);
        msg.generateSequenceNumber(false);
        InputStream is = null;
        InvalidPacketException ipex = null;
        IOException origex = null;
        Blob blob = null;
        long bloblen = -1;
        try {
            if (getMsgColumnType(rs, 1) == Types.BLOB) {
                blob = rs.getBlob(1);
                is = blob.getBinaryStream();
                bloblen = blob.length();
            } else {
                is = rs.getBinaryStream(1);
            }
            try {
                if (fi.FAULT_INJECTION) {
                    if (fi.checkFault(FaultInjection.FAULT_HA_BADPKT_EXCEPTION, null)) {
                        fi.unsetFault(FaultInjection.FAULT_HA_BADPKT_EXCEPTION);
                        throw new StreamCorruptedException(FaultInjection.FAULT_HA_BADPKT_EXCEPTION);
                    }
                    if (fi.checkFault(FaultInjection.FAULT_HA_PKTREADEOF_RECONNECT_EXCEPTION, null)) {
                        fi.unsetFault(FaultInjection.FAULT_HA_PKTREADEOF_RECONNECT_EXCEPTION);
                        PacketReadEOFException e = new PacketReadEOFException(FaultInjection.FAULT_HA_PKTREADEOF_RECONNECT_EXCEPTION);
                        e.setBytesRead(0);
                        throw e;
                    }
                }
                msg.readPacket(is);
                if (fi.FAULT_INJECTION) {
                    if (fi.checkFault(FaultInjection.FAULT_HA_PKTREADEOF_EXCEPTION, null)) {
                        fi.unsetFault(FaultInjection.FAULT_HA_PKTREADEOF_EXCEPTION);
                        PacketReadEOFException e = new PacketReadEOFException(FaultInjection.FAULT_HA_PKTREADEOF_EXCEPTION + ":pktsize=" + msg.getPacketSize() + ", blobsize=" + (blob != null ? bloblen : rs.getBytes(1).length));
                        e.setBytesRead(msg.getPacketSize());
                        throw e;
                    }
                }
            } catch (StreamCorruptedException e) {
                origex = e;
                ipex = new InvalidPacketException(e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                origex = new IOException(e.getMessage(), e);
                ipex = new InvalidPacketException(e.getMessage(), e);
            } catch (PacketReadEOFException e) {
                origex = e;
                if (blob != null && e.getBytesRead() < bloblen) {
                    // for auto-reconnect
                    throw e;
                }
                ipex = new InvalidPacketException(e.getMessage(), e);
            }
            if (ipex != null) {
                try {
                    is.close();
                } catch (Exception e) {
                /* ignore */
                }
                is = null;
                byte[] bytes = null;
                if (blob != null) {
                    try {
                        bytes = blob.getBytes(1L, (int) bloblen);
                    } catch (Exception e) {
                        String es = SupportUtil.getStackTraceString(e);
                        bytes = es.getBytes("UTF-8");
                    }
                } else {
                    bytes = rs.getBytes(1);
                    if (bytes != null && (origex instanceof PacketReadEOFException)) {
                        if (((PacketReadEOFException) origex).getBytesRead() < bytes.length) {
                            throw origex;
                        }
                    }
                }
                if (bytes == null) {
                    throw origex;
                }
                ipex.setBytes(bytes);
                throw ipex;
            }
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                /* ignore */
                }
            }
        }
        if (DEBUG) {
            logger.log(Logger.INFO, "MessageDAOImpl.loadData(isSingleRow=" + isSingleRow + "):" + "Loaded message from database for " + msg.getMessageID());
        }
        if (isSingleRow) {
            return msg;
        } else {
            list.add(msg);
        }
    }
    if (DEBUG && list != null) {
        logger.log(Logger.INFO, "MessageDAOImpl.loadData(): ResultSet[size=" + list.size() + "]");
    }
    return list;
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException)

Example 3 with PacketReadEOFException

use of com.sun.messaging.jmq.io.PacketReadEOFException in project openmq by eclipse-ee4j.

the class MessageDAOImpl method getMessagesByBroker.

/**
 * Get all message IDs for a broker.
 *
 * @param conn database connection
 * @param brokerID the broker ID
 * @return a List of all messages the specified broker owns
 */
@Override
public List getMessagesByBroker(Connection conn, String brokerID) throws BrokerException {
    List list = Collections.EMPTY_LIST;
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Exception myex = null;
    try {
        // Get a connection
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myConn = true;
        }
        // Retrieve all messages for the target broker
        pstmt = dbMgr.createPreparedStatement(conn, selectMsgsByBrokerSQL);
        pstmt.setString(1, brokerID);
        rs = pstmt.executeQuery();
        list = (List) loadData(rs, false);
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof InvalidPacketException) {
            InvalidPacketException ipe = (InvalidPacketException) e;
            ipe.appendMessage("[" + selectMsgsByBrokerSQL + "]");
            ex = ipe;
        } else if (e instanceof PacketReadEOFException) {
            PacketReadEOFException pre = (PacketReadEOFException) e;
            pre.appendMessage("[" + selectMsgsByBrokerSQL + "]");
            ex = pre;
        } else if (e instanceof IOException) {
            ex = DBManager.wrapIOException("[" + selectMsgsByBrokerSQL + "]", (IOException) e);
        } else if (e instanceof SQLException) {
            ex = DBManager.wrapSQLException("[" + selectMsgsByBrokerSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.E_LOAD_MSG_FOR_BROKER_FAILED, brokerID), ex);
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    return list;
}
Also used : PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException)

Aggregations

InvalidPacketException (com.sun.messaging.jmq.io.InvalidPacketException)3 PacketReadEOFException (com.sun.messaging.jmq.io.PacketReadEOFException)3 Packet (com.sun.messaging.jmq.io.Packet)2