Search in sources :

Example 6 with SchemaObjectClass

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

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

the class Agent method registerObjectClass.

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

Example 8 with SchemaObjectClass

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

Aggregations

SchemaObjectClass (org.apache.qpid.qmf2.common.SchemaObjectClass)6 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)4 SchemaClass (org.apache.qpid.qmf2.common.SchemaClass)3 SchemaEventClass (org.apache.qpid.qmf2.common.SchemaEventClass)3 SchemaMethod (org.apache.qpid.qmf2.common.SchemaMethod)3 SchemaProperty (org.apache.qpid.qmf2.common.SchemaProperty)3 ObjectId (org.apache.qpid.qmf2.common.ObjectId)2 QmfException (org.apache.qpid.qmf2.common.QmfException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 AMQPMessage (org.apache.qpid.qmf2.common.AMQPMessage)1 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)1