Search in sources :

Example 1 with QmfData

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

the class QpidConfig method addExchange.

/**
 * Add an exchange using the QMF "create" method.
 * @param args the exchange type is the first argument and the exchange name is the second argument.
 * The remaining QMF method properties are populated form config parsed from the command line.
 */
private void addExchange(final String[] args) {
    if (args.length < 2) {
        usage();
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    if (_durable) {
        properties.put("durable", true);
    }
    properties.put("exchange-type", args[0]);
    if (_msgSequence) {
        properties.put(MSG_SEQUENCE, 1l);
    }
    if (_ive) {
        properties.put(IVE, 1l);
    }
    if (_altExchange != null) {
        properties.put("alternate-exchange", _altExchange);
    }
    QmfData arguments = new QmfData();
    arguments.setValue("type", "exchange");
    arguments.setValue("name", args[1]);
    arguments.setValue("properties", properties);
    try {
        _broker.invokeMethod("create", arguments);
    } catch (QmfException e) {
        System.out.println(e.getMessage());
    }
// passive exchange creation not implemented yet (not sure how to do it using QMF2)
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) HashMap(java.util.HashMap) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 2 with QmfData

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

the class QpidConfig method unbind.

/**
 * Remove a binding using the QMF "delete" method.
 * @param args the exchange name is the first argument, the queue name is the second argument and the binding key
 * is the third argument.
 * The remaining QMF method properties are populated form config parsed from the command line.
 */
private void unbind(final String[] args) {
    if (args.length < 2) {
        usage();
    }
    String bindingIdentifier = args[0] + "/" + args[1];
    if (args.length > 2) {
        bindingIdentifier = bindingIdentifier + "/" + args[2];
    }
    QmfData arguments = new QmfData();
    arguments.setValue("type", "binding");
    arguments.setValue("name", bindingIdentifier);
    try {
        _broker.invokeMethod("delete", arguments);
    } catch (QmfException e) {
        System.out.println(e.getMessage());
    }
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 3 with QmfData

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

the class QpidConfig method addQueue.

/**
 * Add a queue using the QMF "create" method.
 * @param args the queue name is the first argument.
 * The remaining QMF method properties are populated form config parsed from the command line.
 */
private void addQueue(final String[] args) {
    if (args.length < 1) {
        usage();
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    for (String a : extraArguments) {
        String[] r = a.split("=");
        String value = r.length == 2 ? r[1] : null;
        properties.put(r[0], value);
    }
    if (_durable) {
        properties.put("durable", true);
        properties.put(FILECOUNT, _fileCount);
        properties.put(FILESIZE, _fileSize);
    }
    if (_maxQueueSize > 0) {
        properties.put(MAX_QUEUE_SIZE, _maxQueueSize);
    }
    if (_maxQueueCount > 0) {
        properties.put(MAX_QUEUE_COUNT, _maxQueueCount);
    }
    if (_limitPolicy.equals("reject")) {
        properties.put(POLICY_TYPE, "reject");
    } else if (_limitPolicy.equals("flow-to-disk")) {
        properties.put(POLICY_TYPE, "flow_to_disk");
    } else if (_limitPolicy.equals("ring")) {
        properties.put(POLICY_TYPE, "ring");
    } else if (_limitPolicy.equals("ring-strict")) {
        properties.put(POLICY_TYPE, "ring_strict");
    }
    if (_order.equals("lvq")) {
        properties.put(LVQ, 1l);
    } else if (_order.equals("lvq-no-browse")) {
        properties.put(LVQNB, 1l);
    }
    if (_altExchange != null) {
        properties.put("alternate-exchange", _altExchange);
    }
    if (_flowStopSize > 0) {
        properties.put(FLOW_STOP_SIZE, _flowStopSize);
    }
    if (_flowResumeSize > 0) {
        properties.put(FLOW_RESUME_SIZE, _flowResumeSize);
    }
    if (_flowStopCount > 0) {
        properties.put(FLOW_STOP_COUNT, _flowStopCount);
    }
    if (_flowResumeCount > 0) {
        properties.put(FLOW_RESUME_COUNT, _flowResumeCount);
    }
    QmfData arguments = new QmfData();
    arguments.setValue("type", "queue");
    arguments.setValue("name", args[0]);
    arguments.setValue("properties", properties);
    try {
        _broker.invokeMethod("create", arguments);
    } catch (QmfException e) {
        System.out.println(e.getMessage());
    }
// passive queue creation not implemented yet (not sure how to do it using QMF2)
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) HashMap(java.util.HashMap) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 4 with QmfData

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

the class QpidConfig method bind.

/**
 * Add a binding using the QMF "create" method.
 * @param args the exchange name is the first argument, the queue name is the second argument and the binding key
 * is the third argument.
 * The remaining QMF method properties are populated form config parsed from the command line.
 */
private void bind(final String[] args) {
    if (args.length < 2) {
        usage();
    }
    // Look up exchange objects to find the type of the selected exchange
    String exchangeType = null;
    List<QmfConsoleData> exchanges = _console.getObjects("org.apache.qpid.broker", "exchange");
    for (QmfConsoleData exchange : exchanges) {
        String name = exchange.getStringValue("name");
        if (args[0].equals(name)) {
            exchangeType = exchange.getStringValue("type");
            break;
        }
    }
    if (exchangeType == null) {
        System.out.println("Exchange " + args[0] + " is invalid");
        return;
    }
    Map<String, Object> properties = new HashMap<String, Object>();
    if (exchangeType.equals("xml")) {
        if (_file == null) {
            System.out.println("Invalid args to bind xml:  need an input file or stdin");
            return;
        }
        String xquery = null;
        if (_file.equals("-")) {
            // Read xquery off stdin
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            try {
                StringBuilder buf = new StringBuilder();
                String line;
                while (// read until eof
                (line = in.readLine()) != null) {
                    buf.append(line + "\n");
                }
                xquery = buf.toString();
            } catch (IOException ioe) {
                System.out.println("Exception " + ioe + " while reading stdin");
                return;
            }
        } else {
            // Read xquery from input file
            File file = new File(_file);
            try {
                FileInputStream fin = new FileInputStream(file);
                try {
                    byte[] content = new byte[(int) file.length()];
                    fin.read(content);
                    xquery = new String(content);
                } finally {
                    fin.close();
                }
            } catch (FileNotFoundException e) {
                System.out.println("File " + _file + " not found");
                return;
            } catch (IOException ioe) {
                System.out.println("Exception " + ioe + " while reading " + _file);
                return;
            }
        }
        properties.put("xquery", xquery);
    } else if (exchangeType.equals("headers")) {
        if (args.length < 5) {
            System.out.println("Invalid args to bind headers: need 'any'/'all' plus conditions");
            return;
        }
        String op = args[3];
        if (op.equals("all") || op.equals("any")) {
            properties.put("x-match", op);
            String[] bindings = Arrays.copyOfRange(args, 4, args.length);
            for (String binding : bindings) {
                if (binding.contains("=")) {
                    binding = binding.split(",")[0];
                    String[] kv = binding.split("=");
                    properties.put(kv[0], kv[1]);
                }
            }
        } else {
            System.out.println("Invalid condition arg to bind headers, need 'any' or 'all', not '" + op + "'");
            return;
        }
    }
    String bindingIdentifier = args[0] + "/" + args[1];
    if (args.length > 2) {
        bindingIdentifier = bindingIdentifier + "/" + args[2];
    }
    QmfData arguments = new QmfData();
    arguments.setValue("type", "binding");
    arguments.setValue("name", bindingIdentifier);
    arguments.setValue("properties", properties);
    try {
        _broker.invokeMethod("create", arguments);
    } catch (QmfException e) {
        System.out.println(e.getMessage());
    }
}
Also used : QmfData(org.apache.qpid.qmf2.common.QmfData) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) File(java.io.File) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 5 with QmfData

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

the class QpidServer method doPost.

/**
 * Called by the Web Server to allow a Server to handle a POST request.
 * <pre>
 * POST: &lt;host&gt;:&lt;port&gt;/qpid/connection/&lt;name&gt;/object/&lt;ObjectId&gt;
 *      HTTP body: {"_method_name":&lt;method&gt;,"_arguments":&lt;inArgs&gt;}
 *      &lt;method&gt;: A string containing the QMF2 method name e.g. "getLogLevel", "setLogLevel", "create", "delete".
 *      &lt;inArgs&gt;: A JSON string containing the method arguments e.g. {"level":"debug+:Broker"} for setLogLevel.
 *      HTTP response: A JSON string containing the response e.g. {"level":"notice+"} for getLogLevel (may be empty).
 *
 *      This method invokes the QMF2 method &lt;method&gt; with arguments &lt;inArgs&gt; on the object &lt;ObjectId&gt;
 * </pre>
 * @param tx the HttpTransaction containing the request from the client and used to send the response.
 */
@SuppressWarnings("unchecked")
public void doPost(final HttpTransaction tx) throws IOException {
    String path = tx.getRequestURI();
    if (path.startsWith("/qpid/connection/")) {
        path = path.substring(17);
        String user = tx.getPrincipal();
        String connectionName = path;
        int i = path.indexOf("/");
        if (// Can use > rather than >= as we've already tested for "/qpid/connection/" above.
        i > 0) {
            connectionName = user + "." + path.substring(0, i);
            path = path.substring(i + 1);
            // Find the Connection with the name extracted from the URI.
            ConnectionProxy connection = _connections.get(connectionName);
            if (connection == null) {
                _log.info("QpidServer.doPost path: {} Connection not found.", tx.getRequestURI());
                tx.sendResponse(HTTP_NOT_FOUND, "text/plain", "404 Not Found.");
            } else if (!connection.isConnected()) {
                tx.sendResponse(HTTP_INTERNAL_ERROR, "text/plain", "500 Broker Disconnected.");
            } else {
                // If we get this far we should have found a Qpid Connection so retrieve the QMF2 Console Object.
                Console console = connection.getConsole();
                if (path.startsWith("object/")) {
                    path = path.substring(7);
                    // The ObjectId has been passed in the URI create an ObjectId and retrieve the Agent Name.
                    ObjectId oid = new ObjectId(path);
                    String agentName = oid.getAgentName();
                    // The qpidd ManagementAgent doesn't populate AgentName, if it's empty assume it's the broker.
                    agentName = agentName.equals("") ? "broker" : agentName;
                    // Find the Agent we got the QmfData from.
                    Agent agent = console.getAgent(agentName);
                    if (agent == null) {
                        _log.info("QpidServer.doPost path: {} Agent: {} not found.", tx.getRequestURI(), agentName);
                        tx.sendResponse(HTTP_NOT_FOUND, "text/plain", "404 Not Found.");
                    } else {
                        // If we get this far we can create the Object and invoke the method.
                        // We can create a QmfConsoleData with nothing but an ObjectId and the agent.
                        QmfConsoleData object = new QmfConsoleData(Collections.EMPTY_MAP, agent);
                        object.setObjectId(oid);
                        String request = tx.getRequestString();
                        _log.info("QpidServer.doPost path: {} body: {}", tx.getRequestURI(), request);
                        // System.out.println(request);
                        String method = "";
                        try {
                            Map<String, Object> reqMap = JSON.toMap(request);
                            method = (String) reqMap.get("_method_name");
                            Object arguments = reqMap.get("_arguments");
                            Map args = (arguments instanceof Map) ? (Map) arguments : null;
                            // System.out.println("method: " + method + ", args: " + args);
                            // Parse the args if present into a QmfData (needed by invokeMethod).
                            QmfData inArgs = (args == null) ? new QmfData() : new QmfData(args);
                            // Invoke the specified method on the QmfConsoleData we've created.
                            MethodResult results = null;
                            _log.info("invokeMethod: {}", request);
                            results = object.invokeMethod(method, inArgs);
                            tx.sendResponse(HTTP_OK, "application/json", JSON.fromObject(results));
                        } catch (QmfException qmfe) {
                            _log.info("QpidServer.doPost() caught Exception {}", qmfe.getMessage());
                            tx.sendResponse(HTTP_INTERNAL_ERROR, "text/plain", "invokeMethod(" + method + ") -> " + qmfe.getMessage());
                        } catch (Exception e) {
                            _log.info("QpidServer.doPost() caught Exception {}", e.getMessage());
                            tx.sendResponse(HTTP_INTERNAL_ERROR, "text/plain", "500 " + e.getMessage());
                        }
                    }
                } else {
                    tx.sendResponse(HTTP_NOT_FOUND, "text/plain", "404 Not Found.");
                }
            }
        } else {
            tx.sendResponse(HTTP_NOT_FOUND, "text/plain", "404 Not Found.");
        }
    } else {
        tx.sendResponse(HTTP_NOT_FOUND, "text/plain", "404 Not Found.");
    }
}
Also used : Agent(org.apache.qpid.qmf2.console.Agent) QmfData(org.apache.qpid.qmf2.common.QmfData) ObjectId(org.apache.qpid.qmf2.common.ObjectId) QmfException(org.apache.qpid.qmf2.common.QmfException) IOException(java.io.IOException) Console(org.apache.qpid.qmf2.console.Console) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) MethodResult(org.apache.qpid.qmf2.console.MethodResult) QmfException(org.apache.qpid.qmf2.common.QmfException)

Aggregations

QmfData (org.apache.qpid.qmf2.common.QmfData)16 QmfException (org.apache.qpid.qmf2.common.QmfException)13 HashMap (java.util.HashMap)8 JMSException (javax.jms.JMSException)7 Map (java.util.Map)6 MapMessage (javax.jms.MapMessage)6 ObjectId (org.apache.qpid.qmf2.common.ObjectId)6 QmfConsoleData (org.apache.qpid.qmf2.console.QmfConsoleData)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Message (javax.jms.Message)4 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)4 Handle (org.apache.qpid.qmf2.common.Handle)4 MethodCallParams (org.apache.qpid.qmf2.agent.MethodCallParams)3 MethodCallWorkItem (org.apache.qpid.qmf2.agent.MethodCallWorkItem)3 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)3 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)3 IOException (java.io.IOException)2 Destination (javax.jms.Destination)2 AddressParser (org.apache.qpid.messaging.util.AddressParser)2 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)2