use of org.apache.qpid.server.model.LifetimePolicy in project qpid-broker-j by apache.
the class ExchangeSendingDestination method getQueue.
private static Queue<?> getQueue(Exchange<?> exchange, Source source, String subscriptionName, BindingInfo bindingInfo) throws AmqpErrorException {
boolean isDurable = source.getExpiryPolicy() == TerminusExpiryPolicy.NEVER;
boolean isShared = hasCapability(source.getCapabilities(), SHARED_CAPABILITY);
QueueManagingVirtualHost virtualHost;
if (exchange.getAddressSpace() instanceof QueueManagingVirtualHost) {
virtualHost = (QueueManagingVirtualHost) exchange.getAddressSpace();
} else {
throw new AmqpErrorException(new Error(AmqpError.INTERNAL_ERROR, "Address space of unexpected type"));
}
Queue<?> queue;
final Map<String, Object> attributes = new HashMap<>();
ExclusivityPolicy exclusivityPolicy;
if (isShared) {
exclusivityPolicy = ExclusivityPolicy.SHARED_SUBSCRIPTION;
} else {
exclusivityPolicy = ExclusivityPolicy.LINK;
}
org.apache.qpid.server.model.LifetimePolicy lifetimePolicy = getLifetimePolicy(source.getExpiryPolicy());
attributes.put(Queue.ID, UUID.randomUUID());
attributes.put(Queue.NAME, subscriptionName);
attributes.put(Queue.LIFETIME_POLICY, lifetimePolicy);
attributes.put(Queue.EXCLUSIVE, exclusivityPolicy);
attributes.put(Queue.DURABLE, isDurable);
Map<String, Map<String, Object>> bindings = bindingInfo.getBindings();
try {
queue = virtualHost.getSubscriptionQueue(exchange.getName(), attributes, bindings);
} catch (NotFoundException e) {
throw new AmqpErrorException(new Error(AmqpError.NOT_FOUND, e.getMessage()));
} catch (IllegalStateException e) {
throw new AmqpErrorException(new Error(AmqpError.RESOURCE_LOCKED, "Subscription is already in use"));
}
return queue;
}
use of org.apache.qpid.server.model.LifetimePolicy in project qpid-broker-j by apache.
the class AMQChannel method receiveQueueDeclare.
@Override
public void receiveQueueDeclare(final AMQShortString queueStr, final boolean passive, final boolean durable, final boolean exclusive, final boolean autoDelete, final boolean nowait, final FieldTable arguments) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("RECV[" + _channelId + "] QueueDeclare[" + " queue: " + queueStr + " passive: " + passive + " durable: " + durable + " exclusive: " + exclusive + " autoDelete: " + autoDelete + " nowait: " + nowait + " arguments: " + arguments + " ]");
}
NamedAddressSpace virtualHost = _connection.getAddressSpace();
final AMQShortString queueName;
// if we aren't given a queue name, we create one which we return to the client
if ((queueStr == null) || (queueStr.length() == 0)) {
queueName = AMQShortString.createAMQShortString("tmp_" + UUID.randomUUID());
} else {
queueName = queueStr;
}
Queue<?> queue;
if (passive) {
queue = getQueue(queueName.toString());
if (queue == null) {
closeChannel(ErrorCodes.NOT_FOUND, "Queue: '" + queueName + "' not found on VirtualHost '" + virtualHost.getName() + "'.");
} else {
if (!queue.verifySessionAccess(this)) {
_connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue '" + queue.getName() + "' is exclusive, but not created on this Connection.", getChannelId());
} else {
// set this as the default queue on the channel:
setDefaultQueue(queue);
if (!nowait) {
sync();
MethodRegistry methodRegistry = _connection.getMethodRegistry();
QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
_connection.writeFrame(responseBody.generateFrame(getChannelId()));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Queue " + queueName + " declared successfully");
}
}
}
}
} else {
try {
final String queueNameString = AMQShortString.toString(queueName);
Map<String, Object> wireArguments = FieldTable.convertToMap(arguments);
Object alternateExchange = wireArguments.get(ALTERNATE_EXCHANGE);
if (alternateExchange != null) {
String alternateExchangeName = String.valueOf(alternateExchange);
validateAlternateExchangeIsNotQueue(virtualHost, alternateExchangeName);
}
Queue.BehaviourOnUnknownDeclareArgument unknownArgumentBehaviour = getConnection().getContextValue(Queue.BehaviourOnUnknownDeclareArgument.class, Queue.UNKNOWN_QUEUE_DECLARE_ARGUMENT_BEHAVIOUR_NAME);
Map<String, Object> attributes = QueueArgumentsConverter.convertWireArgsToModel(queueNameString, wireArguments, getModel(), unknownArgumentBehaviour);
attributes.put(Queue.NAME, queueNameString);
attributes.put(Queue.DURABLE, durable);
LifetimePolicy lifetimePolicy;
ExclusivityPolicy exclusivityPolicy;
if (exclusive) {
lifetimePolicy = autoDelete ? LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS : durable ? LifetimePolicy.PERMANENT : LifetimePolicy.DELETE_ON_CONNECTION_CLOSE;
exclusivityPolicy = durable ? ExclusivityPolicy.CONTAINER : ExclusivityPolicy.CONNECTION;
} else {
lifetimePolicy = autoDelete ? LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS : LifetimePolicy.PERMANENT;
exclusivityPolicy = ExclusivityPolicy.NONE;
}
if (!attributes.containsKey(Queue.EXCLUSIVE)) {
attributes.put(Queue.EXCLUSIVE, exclusivityPolicy);
}
if (!attributes.containsKey(Queue.LIFETIME_POLICY)) {
attributes.put(Queue.LIFETIME_POLICY, lifetimePolicy);
}
queue = virtualHost.createMessageSource(Queue.class, attributes);
setDefaultQueue(queue);
if (!nowait) {
sync();
MethodRegistry methodRegistry = _connection.getMethodRegistry();
QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
_connection.writeFrame(responseBody.generateFrame(getChannelId()));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Queue " + queueName + " declared successfully");
}
}
} catch (AbstractConfiguredObject.DuplicateNameException qe) {
queue = (Queue<?>) qe.getExisting();
if (!queue.verifySessionAccess(this)) {
_connection.sendConnectionClose(ErrorCodes.NOT_ALLOWED, "Queue '" + queue.getName() + "' is exclusive, but not created on this Connection.", getChannelId());
} else if (queue.isExclusive() != exclusive) {
closeChannel(ErrorCodes.ALREADY_EXISTS, "Cannot re-declare queue '" + queue.getName() + "' with different exclusivity (was: " + queue.isExclusive() + " requested " + exclusive + ")");
} else if ((autoDelete && queue.getLifetimePolicy() == LifetimePolicy.PERMANENT) || (!autoDelete && queue.getLifetimePolicy() != ((exclusive && !durable) ? LifetimePolicy.DELETE_ON_CONNECTION_CLOSE : LifetimePolicy.PERMANENT))) {
closeChannel(ErrorCodes.ALREADY_EXISTS, "Cannot re-declare queue '" + queue.getName() + "' with different lifetime policy (was: " + queue.getLifetimePolicy() + " requested autodelete: " + autoDelete + ")");
} else {
setDefaultQueue(queue);
if (!nowait) {
sync();
MethodRegistry methodRegistry = _connection.getMethodRegistry();
QueueDeclareOkBody responseBody = methodRegistry.createQueueDeclareOkBody(queueName, queue.getQueueDepthMessages(), queue.getConsumerCount());
_connection.writeFrame(responseBody.generateFrame(getChannelId()));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Queue " + queueName + " declared successfully");
}
}
}
} catch (AccessControlException e) {
_connection.sendConnectionClose(ErrorCodes.ACCESS_REFUSED, e.getMessage(), getChannelId());
} catch (UnknownAlternateBindingException e) {
final String message = String.format("Unknown alternate destination: '%s'", e.getAlternateBindingName());
_connection.sendConnectionClose(ErrorCodes.NOT_FOUND, message, getChannelId());
} catch (IllegalArgumentException | IllegalConfigurationException e) {
String message = String.format("Error creating queue '%s': %s", queueName, e.getMessage());
_connection.sendConnectionClose(ErrorCodes.INVALID_ARGUMENT, message, getChannelId());
}
}
}
use of org.apache.qpid.server.model.LifetimePolicy in project qpid-broker-j by apache.
the class ServerSessionDelegate method queueDeclare.
@Override
public void queueDeclare(ServerSession session, final QueueDeclare method) {
final NamedAddressSpace addressSpace = getAddressSpace(session);
String queueName = method.getQueue();
Queue<?> queue;
// TODO: do we need to check that the queue already exists with exactly the same "configuration"?
final boolean exclusive = method.getExclusive();
final boolean autoDelete = method.getAutoDelete();
if (method.getPassive()) {
queue = getQueue(addressSpace, queueName);
if (queue == null) {
String description = "Queue: " + queueName + " not found on VirtualHost(" + addressSpace + ").";
ExecutionErrorCode errorCode = ExecutionErrorCode.NOT_FOUND;
exception(session, method, errorCode, description);
} else if (exclusive) {
if (queue.getExclusive() == ExclusivityPolicy.NONE) {
String description = "Cannot passively declare queue ('" + queueName + "')" + " as exclusive as queue with same name is" + " already declared as non-exclusive";
ExecutionErrorCode errorCode = ExecutionErrorCode.RESOURCE_LOCKED;
exception(session, method, errorCode, description);
} else if (!verifySessionAccess(session, queue)) {
String description = "Cannot passively declare queue('" + queueName + "')," + " as exclusive queue with same name " + "declared on another session";
ExecutionErrorCode errorCode = ExecutionErrorCode.RESOURCE_LOCKED;
exception(session, method, errorCode, description);
}
}
} else {
try {
Queue.BehaviourOnUnknownDeclareArgument unknownArgumentBehaviour = session.getAMQPConnection().getContextValue(Queue.BehaviourOnUnknownDeclareArgument.class, Queue.UNKNOWN_QUEUE_DECLARE_ARGUMENT_BEHAVIOUR_NAME);
final Map<String, Object> arguments = QueueArgumentsConverter.convertWireArgsToModel(queueName, method.getArguments(), session.getAMQPConnection().getModel(), unknownArgumentBehaviour);
final String alternateExchangeName = method.getAlternateExchange();
if (method.hasAlternateExchange() && !nameNullOrEmpty(alternateExchangeName)) {
validateAlternateExchangeIsNotQueue(addressSpace, alternateExchangeName);
arguments.put(Queue.ALTERNATE_BINDING, Collections.singletonMap(AlternateBinding.DESTINATION, alternateExchangeName));
}
arguments.put(Queue.NAME, queueName);
if (!arguments.containsKey(Queue.LIFETIME_POLICY)) {
LifetimePolicy lifetime;
if (autoDelete) {
lifetime = exclusive ? LifetimePolicy.DELETE_ON_SESSION_END : LifetimePolicy.DELETE_ON_NO_OUTBOUND_LINKS;
} else {
lifetime = LifetimePolicy.PERMANENT;
}
arguments.put(Queue.LIFETIME_POLICY, lifetime);
}
if (!arguments.containsKey(Queue.EXCLUSIVE)) {
ExclusivityPolicy exclusivityPolicy = exclusive ? ExclusivityPolicy.SESSION : ExclusivityPolicy.NONE;
arguments.put(Queue.EXCLUSIVE, exclusivityPolicy);
}
arguments.put(Queue.DURABLE, method.getDurable());
queue = addressSpace.createMessageSource(Queue.class, arguments);
} catch (AbstractConfiguredObject.DuplicateNameException qe) {
queue = (Queue<?>) qe.getExisting();
if (!verifySessionAccess(session, queue)) {
String description = "Cannot declare queue('" + queueName + "')," + " as exclusive queue with same name " + "declared on another session";
ExecutionErrorCode errorCode = ExecutionErrorCode.RESOURCE_LOCKED;
exception(session, method, errorCode, description);
}
} catch (AccessControlException e) {
exception(session, method, ExecutionErrorCode.UNAUTHORIZED_ACCESS, e.getMessage());
} catch (UnknownAlternateBindingException e) {
exception(session, method, ExecutionErrorCode.NOT_FOUND, String.format("Unknown alternate destination '%s'", e.getAlternateBindingName()));
} catch (IllegalArgumentException | IllegalConfigurationException e) {
exception(session, method, ExecutionErrorCode.ILLEGAL_ARGUMENT, e.getMessage());
}
}
}
Aggregations