Search in sources :

Example 16 with ObjectId

use of org.apache.qpid.qmf2.common.ObjectId in project qpid by apache.

the class QpidConfig method exchangeListRecurse.

/**
 * For every exchange list the bindings (equivalent of qpid-config -b exchanges).
 *
 * More or less a direct Java port of ExchangeListRecurse in qpid-config, which handles qpid-config -b exchanges.
 *
 * @param filter specifies the exchange name to display info for, if set to "" displays info for every exchange.
 */
private void exchangeListRecurse(final String filter) {
    List<QmfConsoleData> exchanges = _console.getObjects("org.apache.qpid.broker", "exchange");
    List<QmfConsoleData> bindings = _console.getObjects("org.apache.qpid.broker", "binding");
    List<QmfConsoleData> queues = _console.getObjects("org.apache.qpid.broker", "queue");
    for (QmfConsoleData exchange : exchanges) {
        ObjectId exchangeId = exchange.getObjectId();
        String name = exchange.getStringValue("name");
        if (filter.equals("") || filter.equals(name)) {
            System.out.printf("Exchange '%s' (%s)\n", name, exchange.getStringValue("type"));
            for (QmfConsoleData binding : bindings) {
                ObjectId exchangeRef = binding.getRefValue("exchangeRef");
                if (exchangeRef.equals(exchangeId)) {
                    ObjectId queueRef = binding.getRefValue("queueRef");
                    QmfConsoleData queue = findById(queues, queueRef);
                    String queueName = "<unknown>";
                    if (queue != null) {
                        queueName = queue.getStringValue("name");
                        if (queueName.equals("")) {
                            queueName = "''";
                        }
                    }
                    String bindingKey = binding.getStringValue("bindingKey");
                    Map arguments = (Map) binding.getValue("arguments");
                    if (arguments == null || arguments.isEmpty()) {
                        System.out.printf("    bind [%s] => %s\n", bindingKey, queueName);
                    } else {
                        // If there are binding arguments then it's a headers exchange
                        System.out.printf("    bind [%s] => %s %s\n", bindingKey, queueName, arguments);
                    }
                }
            }
        }
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with ObjectId

use of org.apache.qpid.qmf2.common.ObjectId in project qpid by apache.

the class QpidConfig method exchangeList.

/**
 * For every exchange list detailed info (equivalent of qpid-config exchanges).
 *
 * More or less a direct Java port of ExchangeList in qpid-config, which handles qpid-config exchanges.
 *
 * @param filter specifies the exchange name to display info for, if set to "" displays info for every exchange.
 */
private void exchangeList(final String filter) {
    List<QmfConsoleData> exchanges = _console.getObjects("org.apache.qpid.broker", "exchange");
    String caption1 = "Type      ";
    String caption2 = "Exchange Name";
    int maxNameLen = caption2.length();
    for (QmfConsoleData exchange : exchanges) {
        String name = exchange.getStringValue("name");
        if (filter.equals("") || filter.equals(name)) {
            if (name.length() > maxNameLen) {
                maxNameLen = name.length();
            }
        }
    }
    System.out.printf("%s%-" + maxNameLen + "s Attributes\n", caption1, caption2);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < (((maxNameLen + caption1.length()) / 5) + 5); i++) {
        buf.append("=====");
    }
    String line = buf.toString();
    System.out.println(line);
    for (QmfConsoleData exchange : exchanges) {
        String name = exchange.getStringValue("name");
        if (filter.equals("") || filter.equals(name)) {
            System.out.printf("%-10s%-" + maxNameLen + "s ", exchange.getStringValue("type"), name);
            Map args = (Map) exchange.getValue("arguments");
            args = (args == null) ? Collections.EMPTY_MAP : args;
            if (exchange.getBooleanValue("durable")) {
                System.out.printf("--durable ");
            }
            if (args.containsKey(MSG_SEQUENCE) && QmfData.getLong(args.get(MSG_SEQUENCE)) == 1) {
                System.out.printf("--sequence ");
            }
            if (args.containsKey(IVE) && QmfData.getLong(args.get(IVE)) == 1) {
                System.out.printf("--ive ");
            }
            if (exchange.hasValue("altExchange")) {
                ObjectId altExchangeRef = exchange.getRefValue("altExchange");
                QmfConsoleData altExchange = findById(exchanges, altExchangeRef);
                if (altExchange != null) {
                    System.out.printf("--alternate-exchange=%s", altExchange.getStringValue("name"));
                }
            }
            System.out.println();
        }
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ObjectId

use of org.apache.qpid.qmf2.common.ObjectId in project qpid by apache.

the class AgentExternalTest method addObject.

public void addObject(QmfAgentData object) throws QmfException {
    ObjectId addr = _agent.allocObjectId(UUID.randomUUID().toString());
    object.setObjectId(addr);
    _objectIndex.put(addr, object);
    // Does the new object match any Subscriptions? If so add a reference to the matching Subscription and publish.
    for (Subscription subscription : _subscriptions.values()) {
        QmfQuery query = subscription.getQuery();
        if (query.getObjectId() != null) {
            if (query.getObjectId().equals(addr)) {
                object.addSubscription(subscription.getSubscriptionId(), subscription);
                object.publish();
            }
        } else if (query.evaluate(object)) {
            object.addSubscription(subscription.getSubscriptionId(), subscription);
            object.publish();
        }
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) Subscription(org.apache.qpid.qmf2.agent.Subscription) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery)

Example 19 with ObjectId

use of org.apache.qpid.qmf2.common.ObjectId in project qpid by apache.

the class AgentExternalTest method onEvent.

public void onEvent(WorkItem wi) {
    System.out.println("WorkItem type: " + wi.getType());
    if (wi.getType() == METHOD_CALL) {
        _control.incValue("methodCount", 1);
        MethodCallWorkItem item = (MethodCallWorkItem) wi;
        MethodCallParams methodCallParams = item.getMethodCallParams();
        String methodName = methodCallParams.getName();
        ObjectId objectId = methodCallParams.getObjectId();
        String userId = methodCallParams.getUserId();
        QmfData inArgs = methodCallParams.getArgs();
        ObjectId controlAddress = _control.getObjectId();
        System.out.println("Method Call User ID = " + userId);
        try {
            if (objectId == null) {
                // Method invoked directly on Agent
                if (methodName.equals("toString")) {
                    QmfData outArgs = new QmfData();
                    outArgs.setValue("string", _agent.toString());
                    _agent.methodResponse(methodName, item.getHandle(), outArgs, null);
                }
            } else if (objectId.equals(controlAddress)) {
                if (methodName.equals("stop")) {
                    System.out.println("Invoked stop method");
                    String message = inArgs.getStringValue("message");
                    System.out.println("Stopping: message = " + message);
                    _agent.methodResponse(methodName, item.getHandle(), null, null);
                    _agent.destroy();
                    System.exit(1);
                } else if (methodName.equals("echo")) {
                    System.out.println("Invoked echo method");
                    _agent.methodResponse(methodName, item.getHandle(), inArgs, null);
                } else if (methodName.equals("event")) {
                    System.out.println("Invoked event method");
                    QmfEvent event = new QmfEvent(_eventSchema);
                    event.setSeverity((int) inArgs.getLongValue("severity"));
                    event.setValue("text", inArgs.getStringValue("text"));
                    _agent.raiseEvent(event);
                    _agent.methodResponse(methodName, item.getHandle(), null, null);
                } else if (methodName.equals("fail")) {
                    System.out.println("Invoked fail method");
                    QmfData error = new QmfData();
                    if (inArgs.getBooleanValue("useString")) {
                        error.setValue("error_text", inArgs.getStringValue("stringVal"));
                    } else {
                        error.setValue("whatHappened", "It Failed");
                        error.setValue("howBad", 75);
                        error.setValue("details", inArgs.getValue("details"));
                    }
                    _agent.methodResponse(methodName, item.getHandle(), null, error);
                } else if (methodName.equals("create_child")) {
                    System.out.println("Invoked create_child method");
                    String childName = inArgs.getStringValue("name");
                    System.out.println("childName = " + childName);
                    QmfAgentData child = new QmfAgentData(_childSchema);
                    child.setValue("name", childName);
                    addObject(child);
                    QmfData outArgs = new QmfData();
                    // Set suptype just to test
                    outArgs.setRefValue("childAddr", child.getObjectId(), "reference");
                    _agent.methodResponse(methodName, item.getHandle(), outArgs, null);
                }
            }
        } catch (QmfException qmfe) {
            System.err.println("QmfException " + qmfe.getMessage() + " caught: AgentExternalTest failed");
            QmfData error = new QmfData();
            error.setValue("error_text", qmfe.getMessage());
            _agent.methodResponse(methodName, item.getHandle(), null, error);
        }
    }
    if (wi.getType() == QUERY) {
        QueryWorkItem item = (QueryWorkItem) wi;
        QmfQuery query = item.getQmfQuery();
        System.out.println("Query User ID = " + item.getUserId());
        if (query.getObjectId() != null) {
            // Look up a QmfAgentData object by the ObjectId obtained from the query
            ObjectId objectId = query.getObjectId();
            QmfAgentData object = _objectIndex.get(objectId);
            if (object != null && !object.isDeleted()) {
                _agent.queryResponse(item.getHandle(), object);
            }
            _agent.queryComplete(item.getHandle(), 0);
        } else {
            // taken by the C++ broker ManagementAgent, so if it's a problem here........
            for (QmfAgentData object : _objectIndex.values()) {
                if (!object.isDeleted() && query.evaluate(object)) {
                    _agent.queryResponse(item.getHandle(), object);
                }
            }
            _agent.queryComplete(item.getHandle(), 0);
        }
    }
    if (wi.getType() == SUBSCRIBE_REQUEST) {
        SubscribeRequestWorkItem item = (SubscribeRequestWorkItem) wi;
        SubscriptionParams params = item.getSubscriptionParams();
        Handle handle = item.getHandle();
        System.out.println("Subscribe Request User ID = " + params.getUserId());
        try {
            Subscription subscription = new Subscription(this, params);
            _subscriptions.put(subscription.getSubscriptionId(), subscription);
            _timer.schedule(subscription, 0, params.getPublishInterval());
            if (subscription == null) {
                System.out.println("Requested Subscription has already expired or been cancelled");
                QmfData error = new QmfData();
                error.setValue("error_text", "Requested Subscription has already expired or been cancelled");
                _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), null, 0, 0, error);
            } else {
                _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
            }
        } catch (QmfException qmfe) {
            _agent.raiseException(handle, "Subscribe Request failed, invalid Query: " + qmfe.getMessage());
        }
    }
    if (wi.getType() == RESUBSCRIBE_REQUEST) {
        ResubscribeRequestWorkItem item = (ResubscribeRequestWorkItem) wi;
        ResubscribeParams params = item.getResubscribeParams();
        Handle handle = item.getHandle();
        System.out.println("Resubscribe Request User ID = " + params.getUserId());
        String subscriptionId = params.getSubscriptionId();
        Subscription subscription = _subscriptions.get(subscriptionId);
        if (subscription != null) {
            subscription.refresh(params);
            _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), subscription.getSubscriptionId(), subscription.getDuration(), subscription.getInterval(), null);
        } else {
            System.out.println("Requested Subscription has already expired or been cancelled");
            QmfData error = new QmfData();
            error.setValue("error_text", "Requested Subscription has already expired or been cancelled");
            _agent.subscriptionResponse(handle, subscription.getConsoleHandle(), null, 0, 0, error);
        }
    }
    if (wi.getType() == UNSUBSCRIBE_REQUEST) {
        UnsubscribeRequestWorkItem item = (UnsubscribeRequestWorkItem) wi;
        String subscriptionId = item.getSubscriptionId();
        System.out.println("Received cancellation request for " + subscriptionId);
        Subscription subscription = _subscriptions.get(subscriptionId);
        if (subscription != null) {
            subscription.cancel();
        }
    }
}
Also used : MethodCallWorkItem(org.apache.qpid.qmf2.agent.MethodCallWorkItem) QmfData(org.apache.qpid.qmf2.common.QmfData) UnsubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.UnsubscribeRequestWorkItem) ObjectId(org.apache.qpid.qmf2.common.ObjectId) QmfEvent(org.apache.qpid.qmf2.common.QmfEvent) SubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.SubscribeRequestWorkItem) Handle(org.apache.qpid.qmf2.common.Handle) SubscriptionParams(org.apache.qpid.qmf2.agent.SubscriptionParams) ResubscribeParams(org.apache.qpid.qmf2.agent.ResubscribeParams) MethodCallParams(org.apache.qpid.qmf2.agent.MethodCallParams) QmfAgentData(org.apache.qpid.qmf2.agent.QmfAgentData) QueryWorkItem(org.apache.qpid.qmf2.agent.QueryWorkItem) Subscription(org.apache.qpid.qmf2.agent.Subscription) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) ResubscribeRequestWorkItem(org.apache.qpid.qmf2.agent.ResubscribeRequestWorkItem) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 20 with ObjectId

use of org.apache.qpid.qmf2.common.ObjectId 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

ObjectId (org.apache.qpid.qmf2.common.ObjectId)28 QmfConsoleData (org.apache.qpid.qmf2.console.QmfConsoleData)15 Map (java.util.Map)10 HashMap (java.util.HashMap)8 QmfException (org.apache.qpid.qmf2.common.QmfException)7 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)6 QmfData (org.apache.qpid.qmf2.common.QmfData)6 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)6 ArrayList (java.util.ArrayList)5 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)5 Agent (org.apache.qpid.qmf2.console.Agent)5 MethodCallParams (org.apache.qpid.qmf2.agent.MethodCallParams)4 MethodCallWorkItem (org.apache.qpid.qmf2.agent.MethodCallWorkItem)4 AgentHeartbeatWorkItem (org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem)4 MethodResult (org.apache.qpid.qmf2.console.MethodResult)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)3 SchemaClass (org.apache.qpid.qmf2.common.SchemaClass)3 EventReceivedWorkItem (org.apache.qpid.qmf2.console.EventReceivedWorkItem)3 MethodResponseWorkItem (org.apache.qpid.qmf2.console.MethodResponseWorkItem)3