AMQProtocolSession session = stateManager.getProtocolSession();
VirtualHost virtualHost = session.getVirtualHost();
ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
final QueueBindBody body = evt.getMethod();
final AMQQueue queue;
if (body.queue == null)
{
AMQChannel channel = session.getChannel(evt.getChannelId());
if (channel == null)
{
throw body.getChannelNotFoundException(evt.getChannelId());
}
queue = channel.getDefaultQueue();
if (queue == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "No default queue defined on channel and queue was null");
}
if (body.routingKey == null)
{
body.routingKey = queue.getName();
}
}
else
{
queue = queueRegistry.getQueue(body.queue);
}
if (queue == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Queue " + body.queue + " does not exist.");
}
final Exchange exch = exchangeRegistry.getExchange(body.exchange);
if (exch == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Exchange " + body.exchange + " does not exist.");
}
if (body.routingKey != null)
{
body.routingKey = body.routingKey.intern();
}
try
{
if (!exch.isBound(body.routingKey, body.arguments, queue))
{
queue.bind(body.routingKey, body.arguments, exch);
}
}
catch (AMQInvalidRoutingKeyException rke)
{
throw body.getChannelException(AMQConstant.INVALID_ROUTING_KEY, body.routingKey.toString());
}
catch (AMQException e)
{
throw body.getChannelException(AMQConstant.CHANNEL_ERROR, e.toString());
}
if (_log.isInfoEnabled())
{
_log.info("Binding queue " + queue + " to exchange " + exch + " with routing key " + body.routingKey);