Search in sources :

Example 76 with BrokerException

use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.

the class ErrorHandler method handle.

/**
 * Method to handle error messages
 */
@Override
public boolean handle(IMQConnection con, Packet msg) throws BrokerException {
    Exception ex = new Exception();
    ex.fillInStackTrace();
    Globals.getLogger().logStack(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "received unexpected error in handler ", ex);
    return true;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 77 with BrokerException

use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.

the class GenerateUIDHandler method handle.

/**
 * Method to handle GenerateUID packet. We generate one or more unique ID's and return them in the body of the reply.
 */
@Override
public boolean handle(IMQConnection con, Packet msg) throws BrokerException {
    if (DEBUG) {
        logger.log(Logger.DEBUGHIGH, "GenerateUIDHandler: handle() [ Received GenerateUID Packet]");
    }
    Hashtable props = null;
    try {
        props = msg.getProperties();
    } catch (Exception ex) {
        logger.logStack(Logger.WARNING, "GEN-UID Packet.getProperties()", ex);
        props = new Hashtable();
    }
    Integer value = null;
    int quantity = 1;
    if (props != null) {
        value = (Integer) props.get("JMQQuantity");
        if (value != null) {
            quantity = value.intValue();
        }
    }
    int status = Status.OK;
    // Each UID is a long (8 bytes);
    int size = quantity * 8;
    ByteBuffer body = ByteBuffer.allocate(size);
    // Get the prefix used by this broker and allocate IDs.
    // We could also do this by creating new jmq.util.UID's, but by
    // doing it this way we avoid the object creation overhead.
    short prefix = UID.getPrefix();
    for (int n = 0; n < quantity; n++) {
        body.putLong(UniqueID.generateID(prefix));
    }
    props = new Hashtable();
    props.put("JMQStatus", Integer.valueOf(status));
    props.put("JMQQuantity", Integer.valueOf(quantity));
    if (((IMQBasicConnection) con).getDumpPacket() || ((IMQBasicConnection) con).getDumpOutPacket()) {
        props.put("JMQReqID", msg.getSysMessageID().toString());
    }
    // Send reply
    Packet pkt = new Packet(con.useDirectBuffers());
    pkt.setPacketType(PacketType.GENERATE_UID_REPLY);
    pkt.setConsumerID(msg.getConsumerID());
    pkt.setProperties(props);
    body.rewind();
    pkt.setMessageBody(body);
    con.sendControlMessage(pkt);
    return true;
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) Hashtable(java.util.Hashtable) ByteBuffer(java.nio.ByteBuffer) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 78 with BrokerException

use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.

the class InfoRequestHandler method sendInfoPacket.

// used by other method to get the INFO packet
public static void sendInfoPacket(int requestType, IMQConnection con, long consumerUID) {
    Logger logger = Globals.getLogger();
    ClusterManager cfg = Globals.getClusterManager();
    String reason = null;
    int status = Status.OK;
    Packet pkt = new Packet(con.useDirectBuffers());
    pkt.setPacketType(PacketType.INFO);
    pkt.setConsumerID(consumerUID);
    Hashtable replyprops = new Hashtable();
    Hashtable bodyProperties = null;
    if (cfg == null) {
        status = Status.ERROR;
        reason = "Internal Error: NOT VALID CLUSTER ";
        logger.log(Logger.INFO, reason);
    } else if (requestType == REQUEST_STATUS_INFO) {
        ClusteredBroker cb = cfg.getLocalBroker();
        // OK, set properties
        bodyProperties = getInfo(cb);
        try {
            if (cb.getState() == BrokerState.SHUTDOWN_STARTED) {
                long timeout = Globals.getBrokerStateHandler().getShutdownRemaining();
                bodyProperties.put("ShutdownMS", Long.valueOf(timeout));
            }
        } catch (BrokerException ex) {
            logger.logStack(Logger.WARNING, "INFO-REQUEST", ex);
        }
    } else if (requestType == REQUEST_CLUSTER_INFO) {
        bodyProperties = getAllInfo();
    } else {
        status = Status.BAD_REQUEST;
        reason = "Internal Error: Bad JMQRequestType set " + requestType;
        logger.log(Logger.INFO, reason);
    }
    // XXX - TBD
    // should we write out the contents in a less intensive way
    // (e.g. use the same format used for properties)
    // or just serialize Hashtable (which is whay we are doing now)
    // 
    String list = null;
    if (cfg != null) {
        Iterator itr = cfg.getKnownBrokers(true);
        Set s = new HashSet();
        // ok get rid of dups
        while (itr.hasNext()) {
            ClusteredBroker cb = (ClusteredBroker) itr.next();
            s.add(cb.getBrokerURL().toString());
        }
        // OK .. now convert to a string
        itr = s.iterator();
        while (itr.hasNext()) {
            if (list == null) {
                list = itr.next().toString();
            } else {
                list += "," + itr.next().toString();
            }
        }
        if (list != null) {
            replyprops.put("JMQBrokerList", list);
        }
    }
    if (bodyProperties != null) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(bodyProperties);
            oos.flush();
            bos.flush();
            pkt.setMessageBody(bos.toByteArray());
            bos.close();
        } catch (Exception ex) {
            logger.logStack(Logger.WARNING, "INFO-REQUEST", ex);
        }
    }
    // OK deal with properties, etc
    replyprops.put("JMQRequestType", Integer.valueOf(requestType));
    replyprops.put("JMQStatus", Integer.valueOf(status));
    if (reason != null) {
        replyprops.put("JMQReason", reason);
    }
    pkt.setProperties(replyprops);
    con.sendControlMessage(pkt);
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) Logger(com.sun.messaging.jmq.util.log.Logger) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 79 with BrokerException

use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.

the class ClientIDHandler method validate.

/**
 * method to validate a client ID
 *
 * @param clientid the client ID sent from client
 * @param con the connection
 * @exception if clientid uses JMQ reserved name space "${u:" or null or in case of ${u} expansion if connection not
 * authenticated
 */
private String validate(String clientid, Connection con) throws BrokerException {
    String cid = clientid;
    if (clientid != null) {
        if (clientid.startsWith("${u}")) {
            AccessController ac = con.getAccessController();
            String user = ac.getAuthenticatedName().getName();
            cid = "${u:" + user + "}" + clientid.substring(4);
        } else if (clientid.startsWith("${u:")) {
            cid = null;
        } else if (clientid.indexOf("${%%}") != -1) {
            logger.log(Logger.DEBUG, "bad client id ${%%}");
            cid = null;
        }
    }
    if (cid == null || cid.trim().length() == 0) {
        throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_INVALID_CLIENTID, (clientid == null) ? "null" : clientid));
    }
    if (DEBUG) {
        logger.log(Logger.DEBUG, "ClientIDHandler:validated client ID:" + cid + ":");
    }
    return cid;
}
Also used : AccessController(com.sun.messaging.jmq.jmsserver.auth.AccessController) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Example 80 with BrokerException

use of com.sun.messaging.jmq.jmsserver.util.BrokerException in project openmq by eclipse-ee4j.

the class ClientIDHandler method setClientID.

public void setClientID(IMQConnection con, String cclientid, String namespace, boolean shared) throws BrokerException {
    int status = Status.OK;
    String reason = null;
    try {
        // validate and expand the specified clientID
        String clientid = cclientid == null ? null : validate(cclientid, con);
        // retrieve the old client id
        String oldid = (String) con.getClientData(IMQConnection.CLIENT_ID);
        if (DEBUG && oldid != null) {
            logger.log(Logger.DEBUG, "ClientIDHandler: replacing clientID " + oldid + " with " + clientid);
        }
        if (clientid != null && (oldid == null || !oldid.equals(clientid))) {
            if (namespace != null) {
                // a namespace was specified
                // lock the combination of namespace and clientID
                String unspace = namespace + "${%%}" + clientid;
                int lockstatus = Globals.getClusterBroadcast().lockClientID(unspace, con.getConnectionUID(), false);
                if (lockstatus != ClusterBroadcast.LOCK_SUCCESS) {
                    if (lockstatus == ClusterBroadcast.LOCK_TIMEOUT) {
                        String emsg = Globals.getBrokerResources().getKString(BrokerResources.X_CLUSTER_CLIENTID_LOCK_REQUEST_TIMEOUT, unspace, con.getRemoteConnectionString());
                        logger.log(Logger.INFO, emsg);
                        status = Status.TIMEOUT;
                        throw new BrokerException(emsg, status);
                    } else {
                        // namespace/clientID combination already in use
                        logger.log(Logger.INFO, BrokerResources.I_CLIENT_ID_IN_USE, con.getRemoteConnectionString(), unspace);
                        Connection owner = Globals.getConnectionManager().matchProperty(IMQConnection.CLIENT_ID, unspace);
                        assert owner == null || owner instanceof IMQConnection;
                        if (owner == null) {
                            // remote
                            logger.log(Logger.INFO, BrokerResources.I_RMT_CID_OWNER, unspace);
                        } else {
                            logger.log(Logger.INFO, BrokerResources.I_LOCAL_CID_OWNER, unspace, ((IMQConnection) owner).getRemoteConnectionString());
                        }
                        reason = "conflict w/ clientID";
                        status = Status.CONFLICT;
                        throw new BrokerException(reason, status);
                    }
                }
            }
            // now lock the clientID itself (whether shared or unshared)
            int lockstatus = Globals.getClusterBroadcast().lockClientID(clientid, con.getConnectionUID(), shared);
            if (lockstatus != ClusterBroadcast.LOCK_SUCCESS) {
                if (lockstatus == ClusterBroadcast.LOCK_TIMEOUT) {
                    String emsg = Globals.getBrokerResources().getKString(BrokerResources.X_CLUSTER_CLIENTID_LOCK_REQUEST_TIMEOUT, clientid, con.getRemoteConnectionString());
                    logger.log(Logger.INFO, emsg);
                    status = Status.TIMEOUT;
                    throw new BrokerException(emsg, status);
                } else {
                    logger.log(Logger.INFO, BrokerResources.I_CLIENT_ID_IN_USE, con.getRemoteConnectionString(), clientid);
                    Connection owner = Globals.getConnectionManager().matchProperty(IMQConnection.CLIENT_ID, clientid);
                    assert owner == null || owner instanceof IMQConnection;
                    if (owner == null) {
                        // remote
                        logger.log(Logger.INFO, BrokerResources.I_RMT_CID_OWNER, clientid);
                    } else {
                        logger.log(Logger.INFO, BrokerResources.I_LOCAL_CID_OWNER, clientid, ((IMQConnection) owner).getRemoteConnectionString());
                    }
                    reason = "conflict w/ clientID";
                    status = Status.CONFLICT;
                    throw new BrokerException(reason, status);
                }
            }
        } else if (oldid != null && !oldid.equals(clientid)) {
            /**
             * we are explicitly clearing an old clientID unlock the old namespace/clientID combination (assume specified namespace
             * is the same as the old one)
             */
            String oldunspace = namespace + "${%%}" + oldid;
            logger.log(Logger.DEBUG, "ClientIDHandler: " + "removing old namespace/clientID " + oldunspace);
            Globals.getClusterBroadcast().unlockClientID(oldunspace, con.getConnectionUID());
            // unlock the old clientID
            logger.log(Logger.DEBUG, "ClientIDHandler: " + "removing old clientID " + oldid);
            Globals.getClusterBroadcast().unlockClientID(oldid, con.getConnectionUID());
            // remove the specified clientID from the connection
            con.removeClientData(IMQConnection.CLIENT_ID);
        }
        // save the specified clientID with the connection
        if (clientid != null && status != Status.CONFLICT) {
            con.addClientData(IMQConnection.CLIENT_ID, clientid);
        }
    } catch (BrokerException ex) {
        if (ex.getStatusCode() == Status.CONFLICT) {
            throw ex;
        }
        if (ex.getStatusCode() == Status.TIMEOUT) {
            throw ex;
        }
        logger.log(Logger.WARNING, BrokerResources.W_CLIENT_ID_INVALID, cclientid, con.toString(), ex);
        status = Status.BAD_REQUEST;
        reason = ex.getMessage();
        throw new BrokerException(reason, ex, status);
    } catch (OutOfMemoryError err) {
        // throw so it is handled by higher-level memory handling code
        throw err;
    } catch (Throwable thr) {
        logger.log(Logger.WARNING, "unexpected error processing clientid ", thr);
        reason = thr.getMessage();
        status = Status.ERROR;
        throw new BrokerException(reason, thr, status);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) IMQConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection) Connection(com.sun.messaging.jmq.jmsserver.service.Connection) IMQBasicConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQBasicConnection) IMQConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection)

Aggregations

BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)290 IOException (java.io.IOException)72 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)33 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)31 SizeString (com.sun.messaging.jmq.util.SizeString)31 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)29 Iterator (java.util.Iterator)26 ArrayList (java.util.ArrayList)25 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)24 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)23 HashMap (java.util.HashMap)22 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)21 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)21 LoadException (com.sun.messaging.jmq.jmsserver.persist.api.LoadException)21 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)18 List (java.util.List)18 Packet (com.sun.messaging.jmq.io.Packet)16 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)16 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)16 ConsumerAlreadyAddedException (com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException)15