public void methodReceived(AMQStateManager stateManager, ExchangeDeclareBody body, int channelId) throws AMQException
{
AMQProtocolSession session = stateManager.getProtocolSession();
VirtualHost virtualHost = session.getVirtualHost();
final AMQChannel channel = session.getChannel(channelId);
if (channel == null)
{
throw body.getChannelNotFoundException(channelId);
}
final AMQShortString exchangeName = body.getExchange();
if (_logger.isDebugEnabled())
{
_logger.debug("Request to declare exchange of type " + body.getType() + " with name " + exchangeName);
}
Exchange exchange;
if (body.getPassive())
{
exchange = virtualHost.getExchange(exchangeName == null ? null : exchangeName.toString());
if(exchange == null)
{
throw body.getChannelException(AMQConstant.NOT_FOUND, "Unknown exchange: " + exchangeName);
}
else if (!exchange.getTypeShortString().equals(body.getType()) && !(body.getType() == null || body.getType().length() ==0))
{
throw new AMQConnectionException(AMQConstant.NOT_ALLOWED, "Attempt to redeclare exchange: " +
exchangeName + " of type " + exchange.getTypeShortString()
+ " to " + body.getType() +".",body.getClazz(), body.getMethod(),body.getMajor(),body.getMinor(),null);
}
}
else
{
try
{
exchange = virtualHost.createExchange(null,
exchangeName == null ? null : exchangeName.intern().toString(),
body.getType() == null ? null : body.getType().intern().toString(),
body.getDurable(),
body.getAutoDelete(),
null);
}
catch(ReservedExchangeNameException e)
{
throw body.getConnectionException(AMQConstant.NOT_ALLOWED,
"Attempt to declare exchange: " + exchangeName +
" which begins with reserved prefix.");
}
catch(ExchangeExistsException e)
{
exchange = e.getExistingExchange();
if(!exchange.getTypeShortString().equals(body.getType()))
{
throw new AMQConnectionException(AMQConstant.NOT_ALLOWED, "Attempt to redeclare exchange: "
+ exchangeName + " of type "
+ exchange.getTypeShortString()
+ " to " + body.getType() +".",
body.getClazz(), body.getMethod(),
body.getMajor(), body.getMinor(),null);
}
}
catch(AMQUnknownExchangeType e)
{
throw body.getConnectionException(AMQConstant.COMMAND_INVALID, "Unknown exchange: " + exchangeName,e);
}
}
if(!body.getNowait())
{
MethodRegistry methodRegistry = session.getMethodRegistry();
AMQMethodBody responseBody = methodRegistry.createExchangeDeclareOkBody();
channel.sync();
session.writeFrame(responseBody.generateFrame(channelId));
}
}