Search in sources :

Example 16 with QmfAgentData

use of org.apache.qpid.qmf2.agent.QmfAgentData in project qpid by apache.

the class Broker method invokeMethod.

/**
     * This method acts as a single entry point for QMF methods invoked on the Broker Object.
     *
     * @param agent the org.apache.qpid.qmf2.agent.Agent instance that we call methodResponse() and raiseException() on.
     * @param handle the reply handle used by methodResponse() and raiseException().
     * @param methodName the name of the QMF method being invoked.
     * @param inArgs a Map of input arguments wrapped in a QmfData Object.
     */
@SuppressWarnings("unchecked")
public void invokeMethod(Agent agent, Handle handle, String methodName, QmfData inArgs) {
    if (methodName.equals("create") || methodName.equals("delete")) {
        QmfData outArgs = new QmfData();
        String name = inArgs.getStringValue("name");
        String type = inArgs.getStringValue("type");
        NameParser nameParser = new NameParser(name, type);
        String vhostName = nameParser.getVirtualHostName();
        VirtualHost vhost = nameParser.getVirtualHost();
        if (vhost == null) {
            if (vhostName == null) {
                agent.raiseException(handle, "VirtualHost names for exchange and queue must match.");
            } else {
                agent.raiseException(handle, "VirtualHost " + vhostName + " not found.");
            }
        } else {
            if (// method = create
            methodName.equals("create")) {
                try {
                    //boolean strict = inArgs.getBooleanValue("strict");
                    Map<String, Object> properties = inArgs.getValue("properties");
                    boolean durable = false;
                    Object property = properties.get("durable");
                    if (property != null && property instanceof Boolean) {
                        Boolean durableProperty = (Boolean) property;
                        durable = durableProperty.booleanValue();
                        properties.remove("durable");
                    }
                    if (// create exchange.
                    type.equals("exchange")) {
                        /*
System.out.println("Create Exchange");
System.out.println("vhostName = " + vhostName);
System.out.println("exchange name = " + nameParser.getExchangeName());
System.out.println("properties = " + properties);
*/
                        String exchangeType = "";
                        property = properties.get("exchange-type");
                        if (property != null && property instanceof String) {
                            exchangeType = property.toString();
                            properties.remove("exchange-type");
                        }
                        String alternateExchange = parseAlternateExchange(vhostName, properties);
                        if (alternateExchange != null && alternateExchange.equals("invalid")) {
                            agent.raiseException(handle, "Alternate Exchange must belong to the same Virtual Host as the Exchange being added.");
                            return;
                        }
                        // Note that for Qpid 0.20 the "qpid.msg_sequence=1" and "qpid.ive=1" properties are
                        // not suppored, indeed no exchange properties seem to be supported yet.
                        Map<String, Object> attributes = new HashMap<>();
                        attributes.put(Exchange.NAME, nameParser.getExchangeName());
                        attributes.put(Exchange.STATE, State.ACTIVE);
                        attributes.put(Exchange.DURABLE, durable);
                        attributes.put(Exchange.LIFETIME_POLICY, LifetimePolicy.PERMANENT);
                        attributes.put(Exchange.TYPE, exchangeType);
                        attributes.put(Exchange.ALTERNATE_EXCHANGE, alternateExchange);
                        vhost.createExchange(attributes);
                    } else // End of create exchange.
                    if (// create queue.
                    type.equals("queue")) {
                        /*
System.out.println("Create Queue");
System.out.println("vhostName = " + vhostName);
System.out.println("queue name = " + nameParser.getQueueName());
System.out.println("properties = " + properties);
*/
                        // TODO Try to map from the QMF create queue properties to the closest equivalents on
                        // the Java Broker. Unfortunately there are a *lot* of frustrating little differences.
                        String alternateExchange = parseAlternateExchange(vhostName, properties);
                        if (alternateExchange != null && alternateExchange.equals("invalid")) {
                            agent.raiseException(handle, "Alternate Exchange must belong to the same Virtual Host as the Queue being added.");
                            return;
                        }
                        // I don't *think* that it make sense to allow setting exclusive or autoDelete to
                        // a queue created from config.
                        Map<String, Object> attributes = new HashMap<String, Object>(properties);
                        attributes.put(Queue.NAME, nameParser.getQueueName());
                        attributes.put(Queue.DURABLE, durable);
                        attributes.put(Queue.LIFETIME_POLICY, LifetimePolicy.PERMANENT);
                        // to work, so we may as well make use of this convenience here too.
                        if (alternateExchange != null) {
                            ObjectId objectId = new ObjectId("", "org.apache.qpid.broker:exchange:" + alternateExchange, 0);
                            // Look up Exchange QmfAgentData by ObjectId from the Agent's internal Object store.
                            QmfAgentData object = agent.getObject(objectId);
                            if (object != null) {
                                org.apache.qpid.server.qmf2.agentdata.Exchange ex = (org.apache.qpid.server.qmf2.agentdata.Exchange) object;
                                Exchange altEx = ex.getExchange();
                                attributes.put(Queue.ALTERNATE_EXCHANGE, altEx.getId());
                            }
                        }
                        Queue queue = vhost.createQueue(attributes);
                    } else if (// create binding.
                    type.equals("binding")) {
                        Exchange exchange = nameParser.getExchange();
                        if (exchange == null) {
                            agent.raiseException(handle, "Cannot create binding on Exchange " + nameParser.getExchangeName());
                            return;
                        } else {
                            Map<String, Object> attributes = Collections.emptyMap();
                            exchange.createBinding(nameParser.getBindingKey(), nameParser.getQueue(), properties, attributes);
                        }
                    }
                    agent.methodResponse(methodName, handle, outArgs, null);
                } catch (Exception e) {
                    agent.raiseException(handle, e.getMessage());
                }
            } else // method = delete
            {
                try {
                    if (// delete exchange.
                    type.equals("exchange")) {
                        Exchange exchange = nameParser.getExchange();
                        if (exchange != null) {
                            exchange.delete();
                        }
                    } else if (// delete queue.
                    type.equals("queue")) {
                        Queue queue = nameParser.getQueue();
                        if (queue != null) {
                            queue.deleteAndReturnCount();
                        }
                    } else if (// delete binding.
                    type.equals("binding")) {
                        Binding binding = nameParser.getBinding();
                        if (binding != null) {
                            binding.delete();
                        }
                    }
                    agent.methodResponse(methodName, handle, outArgs, null);
                } catch (Exception e) {
                    agent.raiseException(handle, e.getMessage());
                }
            }
        }
    } else // If methodName is not create or delete.
    {
        agent.raiseException(handle, methodName + " not yet implemented on Broker.");
    }
}
Also used : Binding(org.apache.qpid.server.model.Binding) QmfData(org.apache.qpid.qmf2.common.QmfData) HashMap(java.util.HashMap) ObjectId(org.apache.qpid.qmf2.common.ObjectId) Exchange(org.apache.qpid.server.model.Exchange) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) VirtualHost(org.apache.qpid.server.model.VirtualHost) HashMap(java.util.HashMap) Map(java.util.Map) Queue(org.apache.qpid.server.model.Queue)

Aggregations

QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)11 ObjectId (org.apache.qpid.qmf2.common.ObjectId)11 QmfException (org.apache.qpid.qmf2.common.QmfException)5 ArrayList (java.util.ArrayList)4 Queue (org.apache.qpid.server.model.Queue)4 MethodCallParams (org.apache.qpid.qmf2.agent.MethodCallParams)3 MethodCallWorkItem (org.apache.qpid.qmf2.agent.MethodCallWorkItem)3 QmfData (org.apache.qpid.qmf2.common.QmfData)3 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)3 SchemaClass (org.apache.qpid.qmf2.common.SchemaClass)3 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)3 Binding (org.apache.qpid.server.model.Binding)3 Exchange (org.apache.qpid.server.model.Exchange)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Subscription (org.apache.qpid.qmf2.agent.Subscription)2 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)2 SchemaObjectClass (org.apache.qpid.qmf2.common.SchemaObjectClass)2 Broker (org.apache.qpid.server.model.Broker)2 Connection (org.apache.qpid.server.model.Connection)2