Search in sources :

Example 11 with SchemaClassId

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

the class Agent method registerEventClass.

/**
     * Register a schema for an event class with the Agent.
     * <p>
     * The Agent must have a registered schema for an event class before it can handle events of that class.
     *
     * @param schema the SchemaEventClass to be registered
     */
public final void registerEventClass(final SchemaEventClass schema) {
    SchemaClassId classId = schema.getClassId();
    _schemaCache.put(classId, schema);
}
Also used : SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId)

Example 12 with SchemaClassId

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

the class Agent method addObject.

/**
     * Passes a reference to an instance of a managed QMF object to the Agent.
     * <p>
     * The object's name must uniquely identify this object among all objects known to this Agent.
     * <p>
     * This method creates an ObjectId for the QmfAgentData being added, it does this by first checking
     * the schema.
     * <p>
     * If an associated schema exists we look for the set of property names that have been
     * specified as idNames. If idNames exists we look for their values within the object and use that
     * to create the objectName. If we can't create a sensible name we use a randomUUID.
     * @param object the QmfAgentData object to be added
     */
public void addObject(final QmfAgentData object) throws QmfException {
    // There are some cases where a QmfAgentData Object might have already set its ObjectId, for example where
    // it may need to have a "well known" ObjectId. This is the case with the Java Broker Management Agent
    // where tools such as qpid-config might have made assumptions about its ObjectId rather than doing "discovery".
    ObjectId addr = object.getObjectId();
    if (addr == null) {
        SchemaClassId classId = object.getSchemaClassId();
        SchemaClass schema = _schemaCache.get(classId);
        // Try to create an objectName using the property names that have been specified as idNames in the schema
        StringBuilder buf = new StringBuilder();
        // Initialise idNames as an empty array as we want to check if a key has been used to construct the name.
        String[] idNames = {};
        if (schema != null && schema instanceof SchemaObjectClass) {
            idNames = ((SchemaObjectClass) schema).getIdNames();
            for (String property : idNames) {
                buf.append(object.getStringValue(property));
            }
        }
        String objectName = buf.toString();
        // exchange has name == "")
        if (objectName.length() == 0 && idNames.length == 0)
            objectName = UUID.randomUUID().toString();
        // Finish up the name by incorporating package and class names
        objectName = classId.getPackageName() + ":" + classId.getClassName() + ":" + objectName;
        // Now we've got a good name for the object we create its ObjectId and add that to the object
        addr = new ObjectId(_name, objectName, _epoch);
        object.setObjectId(addr);
    }
    QmfAgentData foundObject = _objectIndex.get(addr);
    if (foundObject != null) {
        // If a duplicate object has actually been Deleted we can reuse the address.
        if (!foundObject.isDeleted()) {
            throw new QmfException("Duplicate QmfAgentData Address");
        }
    }
    _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) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 13 with SchemaClassId

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

the class Console method getClasses.

/**
     * Return a list of SchemaClassIds for all available Schema for the specified Agent.
     * @param agent the Agent being queried
     * @return a list of SchemaClassIds for all available Schema for the specified Agent.
     */
public List<SchemaClassId> getClasses(final Agent agent) {
    // First look to see if there are cached results and if there are return those.
    List<SchemaClassId> results = agent.getClasses();
    if (results.size() > 0) {
        return results;
    }
    String agentName = agent.getName();
    results = new ArrayList<SchemaClassId>();
    try {
        MapMessage request = _syncSession.createMapMessage();
        request.setJMSReplyTo(_replyAddress);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "SCHEMA_ID" target
        request.setObject("_what", "SCHEMA_ID");
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            Message response = _responder.receive(_replyTimeout * 1000);
            if (response == null) {
                _log.info("No response received in getClasses()");
                return Collections.emptyList();
            }
            if (AMQPMessage.isAMQPList(response)) {
                List<Map> mapResults = AMQPMessage.getList(response);
                for (Map content : mapResults) {
                    //new SchemaClassId(content).listValues();
                    results.add(new SchemaClassId(content));
                }
            } else if (AMQPMessage.isAMQPMap(response)) {
            // Error responses are returned as MapMessages, though they are being ignored here.
            //System.out.println("Console.getClasses() no results for " + agentName);
            //QmfData exception = new QmfData(AMQPMessage.getMap(response));
            //System.out.println(agentName + " " + exception.getStringValue("error_text"));
            } else {
                _log.info("getClasses() Received response message in incorrect format");
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getClasses()", jmse.getMessage());
    }
    agent.setClasses(results);
    return results;
}
Also used : AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 14 with SchemaClassId

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

the class Console method getSchema.

/**
     * Return a list of all available class SchemaClass from a specified Agent.
     * <p>
     * This call will return cached information if it is available.  If not, it will send a query message
     * to the remote agent and block waiting for a response. The timeout argument specifies the maximum time
     * to wait for a response from the agent.
     *
     * @param schemaClassId the SchemaClassId we wish to return schema information for.
     * @param agent the Agent we want to retrieve the schema from
     */
public List<SchemaClass> getSchema(final SchemaClassId schemaClassId, final Agent agent) {
    // First look to see if there are cached results and if there are return those.
    List<SchemaClass> results = agent.getSchema(schemaClassId);
    if (results.size() > 0) {
        return results;
    }
    String agentName = agent.getName();
    //System.out.println("getSchema for agent " + agentName);
    results = new ArrayList<SchemaClass>();
    try {
        MapMessage request = _syncSession.createMapMessage();
        request.setJMSReplyTo(_replyAddress);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "SCHEMA" target
        request.setObject("_what", "SCHEMA");
        request.setObject("_schema_id", schemaClassId.mapEncode());
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            Message response = _responder.receive(_replyTimeout * 1000);
            if (response == null) {
                _log.info("No response received in getSchema()");
                return Collections.emptyList();
            }
            if (AMQPMessage.isAMQPList(response)) {
                List<Map> mapResults = AMQPMessage.getList(response);
                for (Map content : mapResults) {
                    SchemaClass schema = new SchemaObjectClass(content);
                    if (schema.getClassId().getType().equals("_event")) {
                        schema = new SchemaEventClass(content);
                    }
                    //schema.listValues();
                    results.add(schema);
                }
            } else if (AMQPMessage.isAMQPMap(response)) {
            // Error responses are returned as MapMessages, though they are being ignored here.
            //System.out.println("Console.getSchema() no results for " + agentName);
            //QmfData exception = new QmfData(AMQPMessage.getMap(response));
            //System.out.println(agentName + " " + exception.getStringValue("error_text"));
            } else {
                _log.info("getSchema() Received response message in incorrect format");
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getSchema()", jmse.getMessage());
    }
    agent.setSchema(schemaClassId, results);
    return results;
}
Also used : AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) SchemaEventClass(org.apache.qpid.qmf2.common.SchemaEventClass) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 15 with SchemaClassId

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

the class Console method getObjects.

/**
     * Perform a query for QmfConsoleData objects. Returns a list (possibly empty) of matching objects.
     * If replyHandle is null this method will block until the agent replies, or the timeout expires.
     * Once the timeout expires, all data retrieved to date is returned. If replyHandle is non-null an
     * asynchronous request is performed
     * 
     * @param agent the Agent being queried
     * @param query the ObjectId or SchemaClassId being queried for.
     * @param replyHandle the correlation handle used to tie asynchronous method requests with responses
     * @param timeout the time to wait for a reply from the Agent, a value of -1 means use the default timeout
     * @return a List of QMF Objects describing that class
     */
private List<QmfConsoleData> getObjects(final Agent agent, final QmfData query, final String replyHandle, int timeout) {
    String agentName = agent.getName();
    timeout = (timeout < 1) ? _replyTimeout : timeout;
    List<QmfConsoleData> results = Collections.emptyList();
    try {
        Destination destination = (replyHandle == null) ? _replyAddress : _asyncReplyAddress;
        MapMessage request = _syncSession.createMapMessage();
        request.setJMSReplyTo(destination);
        request.setJMSCorrelationID(replyHandle);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setStringProperty("qpid.subject", agentName);
        // Create a QMF Query for an "OBJECT" target using either a schema ID or object ID
        String queryType = (query instanceof SchemaClassId) ? "_schema_id" : "_object_id";
        request.setObject("_what", "OBJECT");
        request.setObject(queryType, query.mapEncode());
        // it would be somewhat unfortunate if their response got interleaved with ours!!
        synchronized (this) {
            _requester.send(request);
            if (replyHandle == null) {
                boolean lastResult = true;
                ArrayList<QmfConsoleData> partials = new ArrayList<QmfConsoleData>();
                do {
                    // Wrap in a do/while loop to cater for the case where the Agent may send partial results.
                    Message response = _responder.receive(timeout * 1000);
                    if (response == null) {
                        _log.info("No response received in getObjects()");
                        return partials;
                    }
                    lastResult = !response.propertyExists("partial");
                    if (AMQPMessage.isAMQPList(response)) {
                        List<Map> mapResults = AMQPMessage.getList(response);
                        partials.ensureCapacity(partials.size() + mapResults.size());
                        for (Map content : mapResults) {
                            partials.add(new QmfConsoleData(content, agent));
                        }
                    } else if (AMQPMessage.isAMQPMap(response)) {
                    // Error responses are returned as MapMessages, though they are being ignored here.
                    //QmfData exception = new QmfData(AMQPMessage.getMap(response));
                    //System.out.println(agentName + " " + exception.getStringValue("error_text"));
                    } else {
                        _log.info("getObjects() Received response message in incorrect format");
                    }
                } while (!lastResult);
                results = partials;
            }
        }
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in getObjects()", jmse.getMessage());
    }
    return results;
}
Also used : Destination(javax.jms.Destination) AMQPMessage(org.apache.qpid.qmf2.common.AMQPMessage) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)14 SchemaClass (org.apache.qpid.qmf2.common.SchemaClass)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 JMSException (javax.jms.JMSException)4 ObjectId (org.apache.qpid.qmf2.common.ObjectId)4 MapMessage (javax.jms.MapMessage)3 Message (javax.jms.Message)3 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)3 QmfException (org.apache.qpid.qmf2.common.QmfException)3 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)3 SchemaObjectClass (org.apache.qpid.qmf2.common.SchemaObjectClass)3 List (java.util.List)2 Destination (javax.jms.Destination)1 Handle (org.apache.qpid.qmf2.common.Handle)1 QmfEvent (org.apache.qpid.qmf2.common.QmfEvent)1 QmfQueryTarget (org.apache.qpid.qmf2.common.QmfQueryTarget)1 SchemaEventClass (org.apache.qpid.qmf2.common.SchemaEventClass)1