use of org.apache.qpid.qmf2.common.Handle in project qpid by apache.
the class Agent method sendSubscriptionIndicate.
// methods implementing SubscribableAgent interface
// ********************************************************************************************************
/**
* Send a list of updated subscribed data to the Console.
*
* @param handle the console reply handle.
* @param results a list of subscribed data in Map encoded form.
*/
public final void sendSubscriptionIndicate(final Handle handle, final List<Map> results) {
try {
Message response = AMQPMessage.createListMessage(_syncSession);
response.setJMSCorrelationID(handle.getCorrelationId());
response.setStringProperty("x-amqp-0-10.app-id", "qmf2");
response.setStringProperty("method", "indication");
response.setStringProperty("qmf.opcode", "_data_indication");
response.setStringProperty("qmf.content", "_data");
response.setStringProperty("qmf.agent", _name);
response.setStringProperty("qpid.subject", handle.getRoutingKey());
AMQPMessage.setList(response, results);
sendResponse(handle, response);
} catch (JMSException jmse) {
_log.info("JMSException {} caught in sendSubscriptionIndicate()", jmse.getMessage());
}
}
use of org.apache.qpid.qmf2.common.Handle 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();
}
}
}
use of org.apache.qpid.qmf2.common.Handle 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.");
}
}
use of org.apache.qpid.qmf2.common.Handle 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);
}
use of org.apache.qpid.qmf2.common.Handle in project qpid by apache.
the class Agent method handleQueryRequest.
/**
* Handle the query request and send the response back to the Console.
* @param handle the reply handle that contains the replyTo Address.
* @param query the inbound query from the Console.
*/
@SuppressWarnings("unchecked")
private final void handleQueryRequest(final Handle handle, final QmfQuery query) {
QmfQueryTarget target = query.getTarget();
if (target == QmfQueryTarget.SCHEMA_ID) {
List<Map> results = new ArrayList<Map>(_schemaCache.size());
// Look up all SchemaClassId objects
for (SchemaClassId classId : _schemaCache.keySet()) {
results.add(classId.mapEncode());
}
// Send the response back to the Console.
queryResponse(handle, results, "_schema_id");
} else if (target == QmfQueryTarget.SCHEMA) {
List<Map> results = new ArrayList<Map>(1);
// Look up a SchemaClass object by the SchemaClassId obtained from the query
SchemaClassId classId = query.getSchemaClassId();
SchemaClass schema = _schemaCache.get(classId);
if (schema != null) {
results.add(schema.mapEncode());
}
// Send the response back to the Console.
queryResponse(handle, results, "_schema");
} else if (target == QmfQueryTarget.OBJECT_ID) {
List<Map> results = new ArrayList<Map>(_objectIndex.size());
// Look up all ObjectId objects
for (ObjectId objectId : _objectIndex.keySet()) {
results.add(objectId.mapEncode());
}
// Send the response back to the Console.
queryResponse(handle, results, "_object_id");
} else if (target == QmfQueryTarget.OBJECT) {
// If this is implementing the AgentExternal model we pass the QmfQuery on in a QueryWorkItem
if (this instanceof AgentExternal) {
_eventListener.onEvent(new QueryWorkItem(handle, query));
return;
} else {
// qmfContentType = "_data";
if (query.getObjectId() != null) {
List<Map> results = new ArrayList<Map>(1);
// 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()) {
results.add(object.mapEncode());
}
// Send the response back to the Console.
queryResponse(handle, results, "_data");
} else {
// Look up QmfAgentData objects by the SchemaClassId obtained from the query
// This is implemented by a linear search and allows searches with only the className specified.
// Linear searches clearly don't scale brilliantly, but the number of QmfAgentData objects managed
// by an Agent is generally fairly small, so it should be OK. Note that this is the same approach
// taken by the C++ broker ManagementAgent, so if it's a problem here........
// N.B. the results list declared here is a generic List of Objects. We *must* only pass a List of
// Map to queryResponse(), but conversely if the response items are sortable we need to sort them
// before doing mapEncode(). Unfortunately we don't know if the items are sortable a priori so
// we either add a Map or we add a QmfAgentData, then sort then mapEncode() each item. I'm not
// sure of a more elegant way to do this without creating two lists, which might not be so bad
// but we don't know the size of the list a priori either.
List results = new ArrayList(_objectIndex.size());
// It's unlikely that evaluating this query will return a mixture of sortable and notSortable
// QmfAgentData objects, but it's best to check if that has occurred as accidentally passing a
// List of QmfAgentData instead of a List of Map to queryResponse() will break things.
boolean sortable = false;
boolean notSortable = false;
for (QmfAgentData object : _objectIndex.values()) {
if (!object.isDeleted() && query.evaluate(object)) {
if (object.isSortable()) {
// If QmfAgentData is marked sortable we add the QmfAgentData object to the List
// so we can sort first before mapEncoding.
results.add(object);
sortable = true;
} else {
// If QmfAgentData is not marked sortable we mapEncode immediately and add the Map to List.
results.add(object.mapEncode());
notSortable = true;
}
}
}
// results List to avoid sending unconvertable data. Hopefully this condition should never occur.
if (sortable && notSortable) {
_log.info("Query resulted in inconsistent mixture of sortable and non-sortable data.");
results.clear();
} else if (sortable) {
Collections.sort(results);
int length = results.size();
for (int i = 0; i < length; i++) {
QmfAgentData object = (QmfAgentData) results.get(i);
results.set(i, object.mapEncode());
}
}
// Send the response back to the Console.
queryResponse(handle, results, "_data");
}
}
} else {
raiseException(handle, "Query for _what => '" + target + "' not supported");
return;
}
}
Aggregations