From e907b48c21bb9cbbda8f0c12528015aeb631e983 Mon Sep 17 00:00:00 2001 From: Robert Godfrey Date: Sun, 17 Mar 2013 18:03:37 +0000 Subject: QPID-4654 : [Java Client 1.0] Add connection redirect support to the AMQP 1.0 JMS client git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1457505 13f79535-47bb-0310-9956-ffa450edef68 --- .../qpid/amqp_1_0/jms/impl/ConnectionImpl.java | 67 +- .../amqp_1_0/jms/impl/MessageConsumerImpl.java | 34 +- .../amqp_1_0/jms/impl/MessageProducerImpl.java | 19 + .../qpid/amqp_1_0/jms/impl/QueueBrowserImpl.java | 13 +- .../qpid/amqp_1_0/jms/impl/QueueReceiverImpl.java | 9 +- .../qpid/amqp_1_0/jms/impl/QueueSessionImpl.java | 1 + .../apache/qpid/amqp_1_0/jms/impl/SessionImpl.java | 29 +- .../qpid/amqp_1_0/jms/impl/TopicSessionImpl.java | 1 + .../amqp_1_0/jms/impl/TopicSubscriberImpl.java | 14 +- .../java/org/apache/qpid/amqp_1_0/client/Demo.java | 13 +- .../java/org/apache/qpid/amqp_1_0/client/Dump.java | 2 +- .../apache/qpid/amqp_1_0/client/Filereceiver.java | 6 +- .../apache/qpid/amqp_1_0/client/Filesender.java | 2 +- .../org/apache/qpid/amqp_1_0/client/Receive.java | 6 +- .../org/apache/qpid/amqp_1_0/client/Request.java | 2 +- .../org/apache/qpid/amqp_1_0/client/Respond.java | 20 +- .../java/org/apache/qpid/amqp_1_0/client/Send.java | 4 +- .../java/org/apache/qpid/amqp_1_0/client/Util.java | 2 +- .../apache/qpid/amqp_1_0/client/Connection.java | 46 +- .../amqp_1_0/client/ConnectionClosedException.java | 11 + .../amqp_1_0/client/ConnectionErrorException.java | 19 + .../qpid/amqp_1_0/client/ConnectionException.java | 14 + .../org/apache/qpid/amqp_1_0/client/Receiver.java | 9 +- .../org/apache/qpid/amqp_1_0/client/Sender.java | 19 +- .../org/apache/qpid/amqp_1_0/client/Session.java | 78 +- .../codec/AbstractDescribedTypeWriter.java | 374 ++-- .../qpid/amqp_1_0/codec/AbstractListWriter.java | 82 +- .../qpid/amqp_1_0/codec/AbstractMapWriter.java | 190 +- .../qpid/amqp_1_0/codec/ArrayTypeConstructor.java | 224 +-- .../apache/qpid/amqp_1_0/codec/ArrayWriter.java | 162 +- .../apache/qpid/amqp_1_0/codec/BinaryString.java | 136 +- .../qpid/amqp_1_0/codec/BinaryTypeConstructor.java | 158 +- .../apache/qpid/amqp_1_0/codec/BinaryWriter.java | 148 +- .../apache/qpid/amqp_1_0/codec/BooleanWriter.java | 138 +- .../qpid/amqp_1_0/codec/ByteArrayWriter.java | 130 +- .../qpid/amqp_1_0/codec/ByteBufferWriter.java | 148 +- .../qpid/amqp_1_0/codec/ByteTypeConstructor.java | 118 +- .../org/apache/qpid/amqp_1_0/codec/ByteWriter.java | 178 +- .../qpid/amqp_1_0/codec/CharTypeConstructor.java | 132 +- .../org/apache/qpid/amqp_1_0/codec/CharWriter.java | 104 +- .../qpid/amqp_1_0/codec/CompoundTypeAssembler.java | 72 +- .../amqp_1_0/codec/CompoundTypeConstructor.java | 382 ++-- .../apache/qpid/amqp_1_0/codec/CompoundWriter.java | 838 ++++----- .../codec/DefaultDescribedTypeConstructor.java | 140 +- .../qpid/amqp_1_0/codec/DelegatingValueWriter.java | 104 +- .../apache/qpid/amqp_1_0/codec/DescribedType.java | 170 +- .../amqp_1_0/codec/DescribedTypeConstructor.java | 82 +- .../codec/DescribedTypeConstructorRegistry.java | 70 +- .../qpid/amqp_1_0/codec/DoubleTypeConstructor.java | 114 +- .../apache/qpid/amqp_1_0/codec/DoubleWriter.java | 106 +- .../qpid/amqp_1_0/codec/FixedEightWriter.java | 214 +-- .../qpid/amqp_1_0/codec/FixedFourWriter.java | 242 +-- .../qpid/amqp_1_0/codec/FixedSixteenWriter.java | 300 +-- .../apache/qpid/amqp_1_0/codec/FixedTwoWriter.java | 190 +- .../qpid/amqp_1_0/codec/FloatTypeConstructor.java | 114 +- .../apache/qpid/amqp_1_0/codec/FloatWriter.java | 106 +- .../apache/qpid/amqp_1_0/codec/FrameWriter.java | 520 +++--- .../qpid/amqp_1_0/codec/IntTypeConstructor.java | 114 +- .../apache/qpid/amqp_1_0/codec/IntegerWriter.java | 210 +-- .../org/apache/qpid/amqp_1_0/codec/ListWriter.java | 342 ++-- .../qpid/amqp_1_0/codec/LongTypeConstructor.java | 114 +- .../org/apache/qpid/amqp_1_0/codec/LongWriter.java | 220 +-- .../org/apache/qpid/amqp_1_0/codec/MapWriter.java | 202 +-- .../org/apache/qpid/amqp_1_0/codec/NullWriter.java | 140 +- .../qpid/amqp_1_0/codec/ProtocolHandler.java | 60 +- .../qpid/amqp_1_0/codec/ProtocolHeaderHandler.java | 292 +-- .../amqp_1_0/codec/RestrictedTypeValueWriter.java | 110 +- .../qpid/amqp_1_0/codec/ShortTypeConstructor.java | 114 +- .../apache/qpid/amqp_1_0/codec/ShortWriter.java | 108 +- .../amqp_1_0/codec/SimpleVariableWidthWriter.java | 134 +- .../qpid/amqp_1_0/codec/SmallIntConstructor.java | 116 +- .../qpid/amqp_1_0/codec/SmallLongConstructor.java | 116 +- .../qpid/amqp_1_0/codec/SmallUIntConstructor.java | 116 +- .../qpid/amqp_1_0/codec/SmallULongConstructor.java | 118 +- .../qpid/amqp_1_0/codec/StringTypeConstructor.java | 262 +-- .../apache/qpid/amqp_1_0/codec/StringWriter.java | 290 +-- .../qpid/amqp_1_0/codec/SymbolTypeConstructor.java | 216 +-- .../apache/qpid/amqp_1_0/codec/SymbolWriter.java | 202 +-- .../amqp_1_0/codec/TimestampTypeConstructor.java | 118 +- .../qpid/amqp_1_0/codec/TimestampWriter.java | 112 +- .../qpid/amqp_1_0/codec/TypeConstructor.java | 64 +- .../qpid/amqp_1_0/codec/UByteTypeConstructor.java | 118 +- .../qpid/amqp_1_0/codec/UIntTypeConstructor.java | 116 +- .../qpid/amqp_1_0/codec/ULongTypeConstructor.java | 120 +- .../qpid/amqp_1_0/codec/UShortTypeConstructor.java | 118 +- .../qpid/amqp_1_0/codec/UUIDTypeConstructor.java | 122 +- .../org/apache/qpid/amqp_1_0/codec/UUIDWriter.java | 126 +- .../qpid/amqp_1_0/codec/UnsignedByteWriter.java | 182 +- .../qpid/amqp_1_0/codec/UnsignedIntegerWriter.java | 294 +-- .../qpid/amqp_1_0/codec/UnsignedLongWriter.java | 302 ++-- .../qpid/amqp_1_0/codec/UnsignedShortWriter.java | 110 +- .../apache/qpid/amqp_1_0/codec/ValueHandler.java | 318 ++-- .../codec/ValueProducingProtocolHandler.java | 62 +- .../apache/qpid/amqp_1_0/codec/ValueWriter.java | 116 +- .../codec/VariableWidthTypeConstructor.java | 96 +- .../qpid/amqp_1_0/codec/VariableWidthWriter.java | 338 ++-- .../amqp_1_0/codec/WrapperTypeValueWriter.java | 110 +- .../org/apache/qpid/amqp_1_0/framing/AMQFrame.java | 152 +- .../framing/AMQPProtocolHeaderHandler.java | 170 +- .../qpid/amqp_1_0/framing/ConnectionHandler.java | 1160 ++++++------ .../apache/qpid/amqp_1_0/framing/FrameHandler.java | 658 +++---- .../qpid/amqp_1_0/framing/FrameParsingError.java | 68 +- .../qpid/amqp_1_0/framing/FrameTypeHandler.java | 62 +- .../apache/qpid/amqp_1_0/framing/SASLFrame.java | 84 +- .../qpid/amqp_1_0/framing/SASLFrameHandler.java | 622 +++---- .../qpid/amqp_1_0/messaging/MessageAttributes.java | 54 +- .../qpid/amqp_1_0/messaging/SectionDecoder.java | 70 +- .../amqp_1_0/messaging/SectionDecoderImpl.java | 124 +- .../qpid/amqp_1_0/messaging/SectionEncoder.java | 68 +- .../amqp_1_0/messaging/SectionEncoderImpl.java | 222 +-- .../amqp_1_0/transport/ConnectionEndpoint.java | 1912 ++++++++++---------- .../qpid/amqp_1_0/transport/ConnectionState.java | 63 +- .../apache/qpid/amqp_1_0/transport/Container.java | 158 +- .../amqp_1_0/transport/FrameOutputHandler.java | 72 +- .../qpid/amqp_1_0/transport/LinkEndpoint.java | 1084 +++++------ .../org/apache/qpid/amqp_1_0/transport/Node.java | 52 +- .../amqp_1_0/transport/ReceivingLinkEndpoint.java | 912 +++++----- .../transport/ReceivingSessionHalfEndpoint.java | 52 +- .../qpid/amqp_1_0/transport/SASLEndpointImpl.java | 582 +++--- .../amqp_1_0/transport/SendingLinkEndpoint.java | 428 ++--- .../transport/SendingSessionHalfEndpoint.java | 52 +- .../qpid/amqp_1_0/transport/SequenceNumber.java | 220 +-- .../qpid/amqp_1_0/transport/SessionAttachment.java | 184 +- .../qpid/amqp_1_0/transport/SessionEndpoint.java | 1603 ++++++++-------- .../amqp_1_0/transport/SessionHalfEndpoint.java | 52 +- .../qpid/amqp_1_0/transport/SessionState.java | 68 +- .../qpid/amqp_1_0/transport/UnsettledTransfer.java | 106 +- .../qpid/amqp_1_0/type/AmqpErrorException.java | 108 +- .../java/org/apache/qpid/amqp_1_0/type/Binary.java | 322 ++-- .../apache/qpid/amqp_1_0/type/DeliveryState.java | 52 +- .../qpid/amqp_1_0/type/DistributionMode.java | 52 +- .../apache/qpid/amqp_1_0/type/ErrorCondition.java | 52 +- .../org/apache/qpid/amqp_1_0/type/FrameBody.java | 58 +- .../org/apache/qpid/amqp_1_0/type/GlobalTxId.java | 52 +- .../apache/qpid/amqp_1_0/type/LifetimePolicy.java | 52 +- .../org/apache/qpid/amqp_1_0/type/Outcome.java | 52 +- .../apache/qpid/amqp_1_0/type/RestrictedType.java | 52 +- .../apache/qpid/amqp_1_0/type/SaslFrameBody.java | 54 +- .../org/apache/qpid/amqp_1_0/type/Section.java | 54 +- .../java/org/apache/qpid/amqp_1_0/type/Source.java | 52 +- .../java/org/apache/qpid/amqp_1_0/type/Symbol.java | 184 +- .../java/org/apache/qpid/amqp_1_0/type/Target.java | 52 +- .../apache/qpid/amqp_1_0/type/TxnCapability.java | 52 +- .../apache/qpid/amqp_1_0/type/UnsignedByte.java | 266 +-- .../apache/qpid/amqp_1_0/type/UnsignedInteger.java | 298 +-- .../apache/qpid/amqp_1_0/type/UnsignedLong.java | 322 ++-- .../apache/qpid/amqp_1_0/type/UnsignedShort.java | 262 +-- .../org/apache/qpid/amqp_1_0/type/WrapperType.java | 54 +- 148 files changed, 13497 insertions(+), 13283 deletions(-) create mode 100644 qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionClosedException.java create mode 100644 qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionErrorException.java create mode 100644 qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionException.java (limited to 'qpid/java') diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/ConnectionImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/ConnectionImpl.java index be1c2d6514..73701889b5 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/ConnectionImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/ConnectionImpl.java @@ -18,6 +18,8 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; +import org.apache.qpid.amqp_1_0.client.ConnectionException; import org.apache.qpid.amqp_1_0.jms.Connection; import org.apache.qpid.amqp_1_0.jms.ConnectionMetaData; import org.apache.qpid.amqp_1_0.jms.Session; @@ -27,6 +29,9 @@ import javax.jms.*; import javax.jms.IllegalStateException; import javax.jms.Queue; import java.util.*; +import org.apache.qpid.amqp_1_0.type.Symbol; +import org.apache.qpid.amqp_1_0.type.transport.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; public class ConnectionImpl implements Connection, QueueConnection, TopicConnection { @@ -42,11 +47,11 @@ public class ConnectionImpl implements Connection, QueueConnection, TopicConnect private boolean _isQueueConnection; private boolean _isTopicConnection; private final Collection _closeTasks = new ArrayList(); - private final String _host; - private final int _port; + private String _host; + private int _port; private final String _username; private final String _password; - private final String _remoteHost; + private String _remoteHost; private final boolean _ssl; private String _clientId; private String _queuePrefix; @@ -102,7 +107,7 @@ public class ConnectionImpl implements Connection, QueueConnection, TopicConnect // TODO - retrieve negotiated AMQP version _connectionMetaData = new ConnectionMetaDataImpl(1,0,0); } - catch (org.apache.qpid.amqp_1_0.client.Connection.ConnectionException e) + catch (ConnectionException e) { JMSException jmsEx = new JMSException(e.getMessage()); jmsEx.setLinkedException(e); @@ -146,23 +151,63 @@ public class ConnectionImpl implements Connection, QueueConnection, TopicConnect public SessionImpl createSession(final Session.AcknowledgeMode acknowledgeMode) throws JMSException { + boolean started = false; synchronized(_lock) { if(_state == State.CLOSED) { throw new IllegalStateException("Cannot create a session on a closed connection"); } - connect(); - SessionImpl session = new SessionImpl(this, acknowledgeMode); - session.setQueueSession(_isQueueConnection); - session.setTopicSession(_isTopicConnection); - _sessions.add(session); + else if(_state == State.UNCONNECTED) + { + connect(); + started = true; + } + try + { + SessionImpl session = new SessionImpl(this, acknowledgeMode); + session.setQueueSession(_isQueueConnection); + session.setTopicSession(_isTopicConnection); + _sessions.add(session); + + return session; + } + catch(JMSException e) + { + Error remoteError; + if(started + && e.getLinkedException() instanceof ConnectionErrorException + && (remoteError = ((ConnectionErrorException)e.getLinkedException()).getRemoteError()).getCondition() == ConnectionError.REDIRECT) + { + String networkHost = (String) remoteError.getInfo().get(Symbol.valueOf("network-host")); + int port = (Integer) remoteError.getInfo().get(Symbol.valueOf("port")); + String hostName = (String) remoteError.getInfo().get(Symbol.valueOf("hostname")); + reconnect(networkHost,port,hostName); + return createSession(acknowledgeMode); + + } + else + { + throw e; + } + } - return session; } } + private void reconnect(String networkHost, int port, String hostName) + { + synchronized(_lock) + { + _state = State.UNCONNECTED; + _host = networkHost; + _port = port; + _remoteHost = hostName; + _conn = null; + } + } + public String getClientID() throws JMSException { checkClosed(); @@ -335,7 +380,7 @@ public class ConnectionImpl implements Connection, QueueConnection, TopicConnect if (_isQueueConnection) { throw new IllegalStateException("QueueConnection cannot be used to create Pub/Sub based resources."); - } + } return null; //TODO } diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageConsumerImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageConsumerImpl.java index 3c15c74d6f..def5ae5931 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageConsumerImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageConsumerImpl.java @@ -18,18 +18,30 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.jms.Destination; +import javax.jms.IllegalStateException; +import javax.jms.InvalidDestinationException; +import javax.jms.InvalidSelectorException; +import javax.jms.JMSException; +import javax.jms.MessageListener; import org.apache.qpid.amqp_1_0.client.AcknowledgeMode; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; import org.apache.qpid.amqp_1_0.client.Message; import org.apache.qpid.amqp_1_0.client.Receiver; import org.apache.qpid.amqp_1_0.client.Transaction; import org.apache.qpid.amqp_1_0.jms.MessageConsumer; -import org.apache.qpid.amqp_1_0.jms.QueueReceiver; import org.apache.qpid.amqp_1_0.jms.Queue; +import org.apache.qpid.amqp_1_0.jms.QueueReceiver; import org.apache.qpid.amqp_1_0.jms.Session; import org.apache.qpid.amqp_1_0.jms.TemporaryDestination; import org.apache.qpid.amqp_1_0.jms.Topic; import org.apache.qpid.amqp_1_0.jms.TopicSubscriber; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; import org.apache.qpid.amqp_1_0.type.Binary; import org.apache.qpid.amqp_1_0.type.Symbol; import org.apache.qpid.amqp_1_0.type.UnsignedInteger; @@ -40,20 +52,6 @@ import org.apache.qpid.amqp_1_0.type.messaging.NoLocalFilter; import org.apache.qpid.amqp_1_0.type.transport.AmqpError; import org.apache.qpid.amqp_1_0.type.transport.Error; - -import javax.jms.Destination; -import javax.jms.IllegalStateException; -import javax.jms.InvalidDestinationException; -import javax.jms.InvalidSelectorException; -import javax.jms.JMSException; -import javax.jms.MessageListener; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - public class MessageConsumerImpl implements MessageConsumer, QueueReceiver, TopicSubscriber { private static final Symbol NO_LOCAL_FILTER_NAME = Symbol.valueOf("no-local"); @@ -130,9 +128,9 @@ public class MessageConsumerImpl implements MessageConsumer, QueueReceiver, Topi return _session.getClientSession(). createReceiver(_session.toAddress(_destination), AcknowledgeMode.ALO, _linkName, _durable, getFilters(), null); } - catch (AmqpErrorException e) + catch (ConnectionErrorException e) { - Error error = e.getError(); + Error error = e.getRemoteError(); if(AmqpError.INVALID_FIELD.equals(error.getCondition())) { throw new InvalidSelectorException(e.getMessage()); diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java index badc20472b..77544e4112 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/MessageProducerImpl.java @@ -18,6 +18,7 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; +import org.apache.qpid.amqp_1_0.client.ConnectionClosedException; import org.apache.qpid.amqp_1_0.client.Sender; import org.apache.qpid.amqp_1_0.jms.MessageProducer; import org.apache.qpid.amqp_1_0.jms.QueueSender; @@ -54,6 +55,7 @@ public class MessageProducerImpl implements MessageProducer, QueueSender, TopicP { throw new InvalidDestinationException("Invalid Destination Class" + destination.getClass().getName()); } + _session = session; if(_destination != null) @@ -70,6 +72,15 @@ public class MessageProducerImpl implements MessageProducer, QueueSender, TopicP jmsEx.setLinkedException(e); throw jmsEx; } + catch (ConnectionClosedException e) + { + + // TODO - refine exception + JMSException jmsEx = new JMSException(e.getMessage()); + jmsEx.initCause(e); + jmsEx.setLinkedException(e); + throw jmsEx; + } } } @@ -320,6 +331,14 @@ public class MessageProducerImpl implements MessageProducer, QueueSender, TopicP jmsEx.setLinkedException(e); throw jmsEx; } + catch (ConnectionClosedException e) + { + + JMSException jmsEx = new JMSException(e.getMessage()); + jmsEx.initCause(e); + jmsEx.setLinkedException(e); + throw jmsEx; + } finally { _sender = null; diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueBrowserImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueBrowserImpl.java index 8fab315b10..ec117400b5 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueBrowserImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueBrowserImpl.java @@ -18,14 +18,19 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Map; +import java.util.NoSuchElementException; import javax.jms.InvalidSelectorException; import javax.jms.JMSException; import org.apache.qpid.amqp_1_0.client.AcknowledgeMode; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; import org.apache.qpid.amqp_1_0.client.Message; import org.apache.qpid.amqp_1_0.client.Receiver; import org.apache.qpid.amqp_1_0.jms.QueueBrowser; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; import org.apache.qpid.amqp_1_0.type.Symbol; import org.apache.qpid.amqp_1_0.type.UnsignedInteger; import org.apache.qpid.amqp_1_0.type.messaging.Filter; @@ -107,9 +112,9 @@ public class QueueBrowserImpl implements QueueBrowser _filters, null); _receiver.setCredit(UnsignedInteger.valueOf(100), true); } - catch(AmqpErrorException e) + catch(ConnectionErrorException e) { - org.apache.qpid.amqp_1_0.type.transport.Error error = e.getError(); + org.apache.qpid.amqp_1_0.type.transport.Error error = e.getRemoteError(); if(AmqpError.INVALID_FIELD.equals(error.getCondition())) { throw new InvalidSelectorException(e.getMessage()); diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueReceiverImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueReceiverImpl.java index 67b597f5cf..52bf61b4c9 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueReceiverImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueReceiverImpl.java @@ -18,12 +18,11 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; +import javax.jms.JMSException; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; import org.apache.qpid.amqp_1_0.client.Receiver; import org.apache.qpid.amqp_1_0.jms.Queue; import org.apache.qpid.amqp_1_0.jms.QueueReceiver; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -import javax.jms.*; public class QueueReceiverImpl extends MessageConsumerImpl implements QueueReceiver { @@ -43,9 +42,9 @@ public class QueueReceiverImpl extends MessageConsumerImpl implements QueueRecei { return getSession().getClientSession().createMovingReceiver(getSession().toAddress(getDestination())); } - catch (AmqpErrorException e) + catch (ConnectionErrorException e) { - throw new JMSException(e.getMessage(), e.getError().getCondition().toString()); + throw new JMSException(e.getMessage(), e.getRemoteError().getCondition().toString()); } } diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueSessionImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueSessionImpl.java index e5ed8b3b3d..7a60658041 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueSessionImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/QueueSessionImpl.java @@ -26,6 +26,7 @@ import javax.jms.Queue; public class QueueSessionImpl extends SessionImpl implements QueueSession { protected QueueSessionImpl(final ConnectionImpl connection, final AcknowledgeMode acknowledgeMode) + throws JMSException { super(connection, acknowledgeMode); setQueueSession(true); diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/SessionImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/SessionImpl.java index 58b7d4f625..ba487cc3f6 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/SessionImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/SessionImpl.java @@ -37,6 +37,9 @@ import javax.jms.StreamMessage; import javax.jms.TextMessage; import javax.jms.Topic; import org.apache.qpid.amqp_1_0.client.Connection; +import org.apache.qpid.amqp_1_0.client.ConnectionClosedException; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; +import org.apache.qpid.amqp_1_0.client.ConnectionException; import org.apache.qpid.amqp_1_0.client.Message; import org.apache.qpid.amqp_1_0.client.Receiver; import org.apache.qpid.amqp_1_0.client.Sender; @@ -49,7 +52,6 @@ import org.apache.qpid.amqp_1_0.jms.TemporaryDestination; import org.apache.qpid.amqp_1_0.jms.TopicPublisher; import org.apache.qpid.amqp_1_0.jms.TopicSession; import org.apache.qpid.amqp_1_0.jms.TopicSubscriber; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; import org.apache.qpid.amqp_1_0.type.messaging.Source; import org.apache.qpid.amqp_1_0.type.messaging.Target; import org.apache.qpid.amqp_1_0.type.transport.AmqpError; @@ -73,12 +75,21 @@ public class SessionImpl implements Session, QueueSession, TopicSession private boolean _isTopicSession; private Transaction _txn; - protected SessionImpl(final ConnectionImpl connection, final AcknowledgeMode acknowledgeMode) + protected SessionImpl(final ConnectionImpl connection, final AcknowledgeMode acknowledgeMode) throws JMSException { _connection = connection; _acknowledgeMode = acknowledgeMode; Connection clientConn = _connection.getClientConnection(); - _session = clientConn.createSession(); + try + { + _session = clientConn.createSession(); + } + catch (ConnectionException e) + { + final JMSException jmsException = new JMSException(e.getMessage()); + jmsException.setLinkedException(e); + throw jmsException; + } if(_acknowledgeMode == AcknowledgeMode.SESSION_TRANSACTED) { _txn = _session.createSessionLocalTransaction(); @@ -490,6 +501,10 @@ public class SessionImpl implements Session, QueueSession, TopicSession { throw new JMSException("Unable to create temporary queue"); } + catch (ConnectionClosedException e) + { + throw new JMSException("Unable to create temporary queue"); + } } public TemporaryTopicImpl createTemporaryTopic() throws JMSException @@ -507,6 +522,10 @@ public class SessionImpl implements Session, QueueSession, TopicSession { throw new JMSException("Unable to create temporary queue"); } + catch (ConnectionClosedException e) + { + throw new JMSException("Unable to create temporary queue"); + } } public void unsubscribe(final String s) throws JMSException @@ -534,9 +553,9 @@ public class SessionImpl implements Session, QueueSession, TopicSession } receiver.close(); } - catch(AmqpErrorException e) + catch(ConnectionErrorException e) { - if(e.getError().getCondition() == AmqpError.NOT_FOUND) + if(e.getRemoteError().getCondition() == AmqpError.NOT_FOUND) { throw new InvalidDestinationException(s); } diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSessionImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSessionImpl.java index 052a3f2a6b..4bae1efaf1 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSessionImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSessionImpl.java @@ -26,6 +26,7 @@ import javax.jms.Topic; public class TopicSessionImpl extends SessionImpl implements TopicSession { protected TopicSessionImpl(final ConnectionImpl connection, final AcknowledgeMode acknowledgeMode) + throws JMSException { super(connection, acknowledgeMode); setTopicSession(true); diff --git a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSubscriberImpl.java b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSubscriberImpl.java index f267794796..6dc8fc12b5 100644 --- a/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSubscriberImpl.java +++ b/qpid/java/amqp-1-0-client-jms/src/main/java/org/apache/qpid/amqp_1_0/jms/impl/TopicSubscriberImpl.java @@ -18,18 +18,18 @@ */ package org.apache.qpid.amqp_1_0.jms.impl; +import java.util.Map; +import javax.jms.InvalidSelectorException; +import javax.jms.JMSException; import org.apache.qpid.amqp_1_0.client.AcknowledgeMode; +import org.apache.qpid.amqp_1_0.client.ConnectionErrorException; import org.apache.qpid.amqp_1_0.client.Receiver; import org.apache.qpid.amqp_1_0.jms.Topic; import org.apache.qpid.amqp_1_0.jms.TopicSubscriber; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; import org.apache.qpid.amqp_1_0.type.Symbol; import org.apache.qpid.amqp_1_0.type.messaging.Filter; import org.apache.qpid.amqp_1_0.type.messaging.StdDistMode; -import org.apache.qpid.amqp_1_0.type.transport.*; - -import javax.jms.*; -import java.util.Map; +import org.apache.qpid.amqp_1_0.type.transport.AmqpError; public class TopicSubscriberImpl extends MessageConsumerImpl implements TopicSubscriber { @@ -96,9 +96,9 @@ public class TopicSubscriberImpl extends MessageConsumerImpl implements TopicSub return receiver; } - catch (AmqpErrorException e) + catch (ConnectionErrorException e) { - org.apache.qpid.amqp_1_0.type.transport.Error error = e.getError(); + org.apache.qpid.amqp_1_0.type.transport.Error error = e.getRemoteError(); if(AmqpError.INVALID_FIELD.equals(error.getCondition())) { throw new InvalidSelectorException(e.getMessage()); diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java index b58ce6bfe5..c3193f9fea 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java @@ -21,22 +21,19 @@ package org.apache.qpid.amqp_1_0.client; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.qpid.amqp_1_0.type.AmqpErrorException; import org.apache.qpid.amqp_1_0.type.Section; import org.apache.qpid.amqp_1_0.type.UnsignedInteger; -import org.apache.qpid.amqp_1_0.type.UnsignedLong; import org.apache.qpid.amqp_1_0.type.messaging.AmqpValue; import org.apache.qpid.amqp_1_0.type.messaging.ApplicationProperties; -import org.apache.qpid.amqp_1_0.type.messaging.Header; import org.apache.qpid.amqp_1_0.type.messaging.Properties; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - public class Demo extends Util { private static final String USAGE_STRING = "demo [options] [ ...]\n\nOptions:"; @@ -374,7 +371,7 @@ public class Demo extends Util conn.close(); } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); //TODO. } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java index 65d27b21f8..998d68cfa6 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java @@ -121,7 +121,7 @@ public class Dump extends Util session.close(); conn.close(); - } catch (Connection.ConnectionException e) + } catch (ConnectionException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java index 4d98655ad2..e65d1324ef 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java @@ -321,7 +321,7 @@ public class Filereceiver extends Util session.close(); conn.close(); } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); } @@ -333,10 +333,6 @@ public class Filereceiver extends Util { e.printStackTrace(); //TODO. } - catch (AmqpErrorException e) - { - e.printStackTrace(); //TODO. - } } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java index 46e6ba537f..37550ea52f 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java @@ -248,7 +248,7 @@ public class Filesender extends Util session.close(); conn.close(); } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java index 0da9dc3fb7..6f99bbc2ee 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java @@ -226,11 +226,7 @@ public class Receive extends Util conn.close(); System.out.println("Total Messages Received: " + receivedCount); } - catch (Connection.ConnectionException e) - { - e.printStackTrace(); //TODO. - } - catch (AmqpErrorException e) + catch (ConnectionException e) { e.printStackTrace(); //TODO. } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java index 6e1d15376c..d8da58dc76 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java @@ -216,7 +216,7 @@ public class Request extends Util conn2.close(); } } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); //TODO. } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java index 8d9de4893f..6b1b70476e 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java @@ -21,14 +21,18 @@ package org.apache.qpid.amqp_1_0.client; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Random; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; import org.apache.qpid.amqp_1_0.type.Section; import org.apache.qpid.amqp_1_0.type.UnsignedInteger; import org.apache.qpid.amqp_1_0.type.UnsignedLong; import org.apache.qpid.amqp_1_0.type.messaging.Properties; -import org.apache.commons.cli.*; - -import java.util.*; public class Respond extends Util { @@ -270,7 +274,7 @@ public class Respond extends Util _conn.close(); System.out.println("Received: " + receivedCount); } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); //TODO. } @@ -282,13 +286,9 @@ public class Respond extends Util { e.printStackTrace(); //TODO. } - catch (AmqpErrorException e) - { - e.printStackTrace(); //TODO. - } } - private void respond(Message m) throws Sender.SenderCreationException + private void respond(Message m) throws Sender.SenderCreationException, ConnectionClosedException { List
sections = m.getPayload(); String replyTo = null; diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java index 6f6575e083..ef1a31005c 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java @@ -40,7 +40,7 @@ public class Send extends Util private static final char[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; - public static void main(String[] args) throws Sender.SenderCreationException, Sender.SenderClosingException, Connection.ConnectionException + public static void main(String[] args) throws Sender.SenderCreationException, Sender.SenderClosingException, ConnectionException { new Send(args).run(); } @@ -223,7 +223,7 @@ public class Send extends Util { e.printStackTrace(); //TODO. } - catch (Connection.ConnectionException e) + catch (ConnectionException e) { e.printStackTrace(); //TODO. } diff --git a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java index 6fe2a6d510..33af4729d9 100644 --- a/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java +++ b/qpid/java/amqp-1-0-client/example/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java @@ -498,7 +498,7 @@ public abstract class Util protected abstract void run(); - public Connection newConnection() throws Connection.ConnectionException + public Connection newConnection() throws ConnectionException { Container container = getContainerName() == null ? new Container() : new Container(getContainerName()); return getUsername() == null ? new Connection(getHost(), getPort(), null, null, _frameSize, container, diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java index e501662dbb..4c5ffeb177 100644 --- a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java @@ -135,6 +135,7 @@ public class Connection this(address, port, username, password, MAX_FRAME_SIZE,container,remoteHost,ssl); } + public Connection(final String address, final int port, final String username, @@ -273,6 +274,10 @@ public class Connection } + private Connection(ConnectionEndpoint endpoint) + { + _conn = endpoint; + } private void doRead(final AMQPTransport transport, final InputStream inputStream) @@ -314,17 +319,45 @@ public class Connection } - public Session createSession() + public Session createSession() throws ConnectionException { + checkNotClosed(); Session session = new Session(this,String.valueOf(_sessionCount++)); return session; } + void checkNotClosed() throws ConnectionClosedException + { + if(getEndpoint().isClosed()) + { + throw new ConnectionClosedException(getEndpoint().getRemoteError()); + } + } + public ConnectionEndpoint getEndpoint() { return _conn; } + public void awaitOpen() + { + synchronized(getEndpoint().getLock()) + { + while(!getEndpoint().isOpen() && !getEndpoint().isClosed()) + { + try + { + getEndpoint().getLock().wait(); + } + catch (InterruptedException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + } + + } + private void doRead(final ConnectionHandler handler, final InputStream inputStream) { byte[] buf = new byte[2<<15]; @@ -376,15 +409,4 @@ public class Connection } } } - - - public static class ConnectionException extends Exception - { - public ConnectionException(Throwable cause) - { - super(cause); - } - } - - } diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionClosedException.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionClosedException.java new file mode 100644 index 0000000000..d7f89a397f --- /dev/null +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionClosedException.java @@ -0,0 +1,11 @@ +package org.apache.qpid.amqp_1_0.client; + +public class ConnectionClosedException extends ConnectionErrorException +{ + + public ConnectionClosedException(org.apache.qpid.amqp_1_0.type.transport.Error remoteError) + { + super(remoteError); + } + +} diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionErrorException.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionErrorException.java new file mode 100644 index 0000000000..6e21a655e2 --- /dev/null +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionErrorException.java @@ -0,0 +1,19 @@ +package org.apache.qpid.amqp_1_0.client; + +import org.apache.qpid.amqp_1_0.type.transport.Error; + +public class ConnectionErrorException extends ConnectionException +{ + protected final Error _remoteError; + + public ConnectionErrorException(Error remoteError) + { + super(); + _remoteError = remoteError; + } + + public org.apache.qpid.amqp_1_0.type.transport.Error getRemoteError() + { + return _remoteError; + } +} diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionException.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionException.java new file mode 100644 index 0000000000..1fe0347dc7 --- /dev/null +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ConnectionException.java @@ -0,0 +1,14 @@ +package org.apache.qpid.amqp_1_0.client; + +public class ConnectionException extends Exception +{ + public ConnectionException(Throwable cause) + { + super(cause); + } + + ConnectionException() + { + + } +} diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java index 8b792db1f1..6996171707 100644 --- a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java @@ -55,7 +55,7 @@ public class Receiver implements DeliveryStateHandler final String linkName, final Target target, final Source source, - final AcknowledgeMode ackMode) throws AmqpErrorException + final AcknowledgeMode ackMode) throws ConnectionErrorException { this(session, linkName, target, source, ackMode, false); } @@ -65,7 +65,7 @@ public class Receiver implements DeliveryStateHandler final Target target, final Source source, final AcknowledgeMode ackMode, - boolean isDurable) throws AmqpErrorException + boolean isDurable) throws ConnectionErrorException { this(session,linkName,target,source,ackMode,isDurable,null); } @@ -76,9 +76,10 @@ public class Receiver implements DeliveryStateHandler final Source source, final AcknowledgeMode ackMode, final boolean isDurable, - final Map unsettled) throws AmqpErrorException + final Map unsettled) throws ConnectionErrorException { + session.getConnection().checkNotClosed(); _session = session; if(isDurable) { @@ -162,7 +163,7 @@ public class Receiver implements DeliveryStateHandler } } } - throw new AmqpErrorException(getError()); + throw new ConnectionErrorException(getError()); } else { diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java index c20eec6c8e..cf9f44af75 100644 --- a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java @@ -46,55 +46,55 @@ public class Sender implements DeliveryStateHandler private boolean _closed; public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, targetAddr, sourceAddr, false); } public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr, boolean synchronous) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, targetAddr, sourceAddr, synchronous ? 1 : 0); } public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr, - int window) throws SenderCreationException + int window) throws SenderCreationException, ConnectionClosedException { this(session, linkName, targetAddr, sourceAddr, window, AcknowledgeMode.ALO); } public Sender(final Session session, final String linkName, final org.apache.qpid.amqp_1_0.type.messaging.Target target, final org.apache.qpid.amqp_1_0.type.messaging.Source source, - int window) throws SenderCreationException + int window) throws SenderCreationException, ConnectionClosedException { this(session, linkName, target, source, window, AcknowledgeMode.ALO); } public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr, int window, AcknowledgeMode mode) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, targetAddr, sourceAddr, window, mode, null); } public Sender(final Session session, final String linkName, final org.apache.qpid.amqp_1_0.type.messaging.Target target, final org.apache.qpid.amqp_1_0.type.messaging.Source source, int window, AcknowledgeMode mode) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, target, source, window, mode, null); } public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr, int window, AcknowledgeMode mode, Map unsettled) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, targetAddr, sourceAddr, window, mode, false, unsettled); } public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr, int window, AcknowledgeMode mode, boolean isDurable, Map unsettled) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { this(session, linkName, createTarget(targetAddr, isDurable), createSource(sourceAddr), window, mode, unsettled); } @@ -120,10 +120,11 @@ public class Sender implements DeliveryStateHandler public Sender(final Session session, final String linkName, final org.apache.qpid.amqp_1_0.type.messaging.Target target, final org.apache.qpid.amqp_1_0.type.messaging.Source source, int window, AcknowledgeMode mode, Map unsettled) - throws SenderCreationException + throws SenderCreationException, ConnectionClosedException { _session = session; + session.getConnection().checkNotClosed(); _endpoint = session.getEndpoint().createSendingLinkEndpoint(linkName, source, target, unsettled); diff --git a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java index 5e1e1b1d7c..182d904a9c 100644 --- a/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java +++ b/qpid/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java @@ -60,12 +60,14 @@ public class Session } - public synchronized Sender createSender(final String targetName) throws Sender.SenderCreationException + public synchronized Sender createSender(final String targetName) + throws Sender.SenderCreationException, ConnectionClosedException { return createSender(targetName, false); } - public synchronized Sender createSender(final String targetName, boolean synchronous) throws Sender.SenderCreationException + public synchronized Sender createSender(final String targetName, boolean synchronous) + throws Sender.SenderCreationException, ConnectionClosedException { final String sourceName = UUID.randomUUID().toString(); @@ -73,30 +75,35 @@ public class Session } - public synchronized Sender createSender(final String targetName, int window) throws Sender.SenderCreationException + public synchronized Sender createSender(final String targetName, int window) + throws Sender.SenderCreationException, ConnectionClosedException { final String sourceName = UUID.randomUUID().toString(); return new Sender(this, targetName+"<-"+sourceName, targetName, sourceName, window); } - public Sender createSender(String targetName, int window, AcknowledgeMode mode) throws Sender.SenderCreationException + public Sender createSender(String targetName, int window, AcknowledgeMode mode) + throws Sender.SenderCreationException, ConnectionClosedException { return createSender(targetName, window, mode, null); } - public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName) throws Sender.SenderCreationException + public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName) + throws Sender.SenderCreationException, ConnectionClosedException { return createSender(targetName, window, mode, linkName, null); } - public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName, Map unsettled) throws Sender.SenderCreationException + public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName, Map unsettled) + throws Sender.SenderCreationException, ConnectionClosedException { return createSender(targetName, window, mode, linkName, false, unsettled); } public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName, - boolean isDurable, Map unsettled) throws Sender.SenderCreationException + boolean isDurable, Map unsettled) + throws Sender.SenderCreationException, ConnectionClosedException { return new Sender(this, linkName == null ? "->" + targetName + '(' + UUID.randomUUID().toString()+')': linkName, targetName, null, window, mode, isDurable, unsettled); @@ -104,79 +111,84 @@ public class Session } - public Receiver createReceiver(final String sourceAddr) throws AmqpErrorException + public Receiver createReceiver(final String sourceAddr) throws ConnectionErrorException { return createReceiver(sourceAddr, null, AcknowledgeMode.ALO); } - public Receiver createReceiver(final String queue, final AcknowledgeMode mode) throws AmqpErrorException + public Receiver createReceiver(final String queue, final AcknowledgeMode mode) + throws ConnectionErrorException { return createReceiver(queue, null, mode); } public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(queue, null, mode, linkName); } public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName, boolean isDurable) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(queue, null, mode, linkName, isDurable); } public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName, boolean isDurable, Map filters, Map unsettled) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(queue, null, mode, linkName, isDurable, filters, unsettled); } public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName, - boolean isDurable, Map unsettled) throws AmqpErrorException + boolean isDurable, Map unsettled) + throws ConnectionErrorException { return createReceiver(queue, null, mode, linkName, isDurable, unsettled); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(sourceAddr, mode, AcknowledgeMode.ALO); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, String linkName) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(sourceAddr, mode, AcknowledgeMode.ALO, linkName); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, - final AcknowledgeMode ackMode) throws AmqpErrorException + final AcknowledgeMode ackMode) + throws ConnectionErrorException { return createReceiver(sourceAddr, mode, ackMode, null); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, - final AcknowledgeMode ackMode, String linkName) throws AmqpErrorException + final AcknowledgeMode ackMode, String linkName) + throws ConnectionErrorException { return createReceiver(sourceAddr,mode, ackMode, linkName, false); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, final AcknowledgeMode ackMode, String linkName, boolean isDurable) - throws AmqpErrorException + throws ConnectionErrorException { return createReceiver(sourceAddr, mode, ackMode, linkName, isDurable, null); } private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, final AcknowledgeMode ackMode, String linkName, boolean isDurable, - Map unsettled) throws AmqpErrorException + Map unsettled) + throws ConnectionErrorException { return createReceiver(sourceAddr,mode,ackMode, linkName, isDurable, null, unsettled); } @@ -184,7 +196,7 @@ public class Session public synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, final AcknowledgeMode ackMode, String linkName, boolean isDurable, Map filters, Map unsettled) - throws AmqpErrorException + throws ConnectionErrorException { final Target target = new Target(); @@ -207,17 +219,17 @@ public class Session } - public synchronized Receiver createCopyingReceiver(final String sourceAddr) throws AmqpErrorException + public synchronized Receiver createCopyingReceiver(final String sourceAddr) throws ConnectionErrorException { return createReceiver(sourceAddr, StdDistMode.COPY); } - public synchronized Receiver createMovingReceiver(final String sourceAddr) throws AmqpErrorException + public synchronized Receiver createMovingReceiver(final String sourceAddr) throws ConnectionErrorException { return createReceiver(sourceAddr, StdDistMode.MOVE); } - public Receiver createTemporaryQueueReceiver() throws AmqpErrorException + public Receiver createTemporaryQueueReceiver() throws AmqpErrorException, ConnectionErrorException { Source source = new Source(); source.setDynamic(true); @@ -228,7 +240,7 @@ public class Session return receiver; } - public Sender createTemporaryQueueSender() throws Sender.SenderCreationException + public Sender createTemporaryQueueSender() throws Sender.SenderCreationException, ConnectionClosedException { Target target = new Target(); target.setDynamic(true); @@ -351,4 +363,22 @@ public class Session { return _connection; } + + public void awaitActive() + { + synchronized(getEndpoint().getLock()) + { + while(!getEndpoint().isEnded() && !getEndpoint().isActive()) + { + try + { + getEndpoint().getLock().wait(); + } + catch (InterruptedException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + } + } } diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java index 6977a40902..08ab14f123 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java @@ -1,188 +1,188 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class AbstractDescribedTypeWriter implements ValueWriter -{ - private int _length; - private Registry _registry; - private static final int LARGE_COMPOUND_THRESHOLD_COUNT = 10; - private ValueWriter _delegate; - private static final byte DESCRIBED_TYPE = (byte)0; - - public AbstractDescribedTypeWriter(final Registry registry) - { - _registry = registry; - } - - enum State { - FORMAT_CODE, - DESCRIPTOR, - DESCRIBED, - DONE - } - - private State _state = State.FORMAT_CODE; - - public int writeToBuffer(ByteBuffer buffer) - { - final int length = _length; - - if(length == -1) - { - writeFirstPass(buffer); - } - else - { - - State state = _state; - - switch(state) - { - case FORMAT_CODE: - if(buffer.hasRemaining()) - { - buffer.put(DESCRIBED_TYPE); - state = State.DESCRIPTOR; - _delegate = createDescriptorWriter(); - } - else - { - break; - } - - case DESCRIPTOR: - if(buffer.hasRemaining()) - { - _delegate.writeToBuffer(buffer); - if(_delegate.isComplete()) - { - state = State.DESCRIBED; - _delegate = createDescribedWriter(); - } - else - { - break; - } - } - case DESCRIBED: - if(buffer.hasRemaining()) - { - _delegate.writeToBuffer(buffer); - if(_delegate.isComplete()) - { - state = State.DONE; - _delegate = null; - } - else - { - break; - } - } - - } - - _state = state; - - } - - return _length; - } - - private void writeFirstPass(ByteBuffer buffer) - { - - int length = 1; - State state = State.FORMAT_CODE; - - ValueWriter descriptorWriter = createDescriptorWriter(); - if(buffer.hasRemaining()) - { - buffer.put(DESCRIBED_TYPE); - state = State.DESCRIPTOR; - _delegate = descriptorWriter; - } - length += descriptorWriter.writeToBuffer(buffer); - - ValueWriter describedWriter = createDescribedWriter(); - - if(descriptorWriter.isComplete()) - { - state = State.DESCRIBED; - _delegate = describedWriter; - } - - length += describedWriter.writeToBuffer(buffer); - - if(describedWriter.isComplete()) - { - _delegate = null; - state = State.DONE; - } - - _state = state; - _length = length; - } - - public void setValue(V value) - { - _length = -1; - _delegate = null; - _state = State.FORMAT_CODE; - onSetValue(value); - } - - public void setRegistry(Registry registry) - { - _registry = registry; - } - - protected Registry getRegistry() - { - return _registry; - } - - protected abstract void onSetValue(final V value); - - protected abstract void clear(); - - protected abstract ValueWriter createDescribedWriter(); - - protected abstract Object getDescriptor(); - - protected final ValueWriter createDescriptorWriter() - { - return getRegistry().getValueWriter(getDescriptor()); - } - - public boolean isComplete() - { - return _state == State.DONE; - } - - public boolean isCacheable() - { - return false; - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class AbstractDescribedTypeWriter implements ValueWriter +{ + private int _length; + private Registry _registry; + private static final int LARGE_COMPOUND_THRESHOLD_COUNT = 10; + private ValueWriter _delegate; + private static final byte DESCRIBED_TYPE = (byte)0; + + public AbstractDescribedTypeWriter(final Registry registry) + { + _registry = registry; + } + + enum State { + FORMAT_CODE, + DESCRIPTOR, + DESCRIBED, + DONE + } + + private State _state = State.FORMAT_CODE; + + public int writeToBuffer(ByteBuffer buffer) + { + final int length = _length; + + if(length == -1) + { + writeFirstPass(buffer); + } + else + { + + State state = _state; + + switch(state) + { + case FORMAT_CODE: + if(buffer.hasRemaining()) + { + buffer.put(DESCRIBED_TYPE); + state = State.DESCRIPTOR; + _delegate = createDescriptorWriter(); + } + else + { + break; + } + + case DESCRIPTOR: + if(buffer.hasRemaining()) + { + _delegate.writeToBuffer(buffer); + if(_delegate.isComplete()) + { + state = State.DESCRIBED; + _delegate = createDescribedWriter(); + } + else + { + break; + } + } + case DESCRIBED: + if(buffer.hasRemaining()) + { + _delegate.writeToBuffer(buffer); + if(_delegate.isComplete()) + { + state = State.DONE; + _delegate = null; + } + else + { + break; + } + } + + } + + _state = state; + + } + + return _length; + } + + private void writeFirstPass(ByteBuffer buffer) + { + + int length = 1; + State state = State.FORMAT_CODE; + + ValueWriter descriptorWriter = createDescriptorWriter(); + if(buffer.hasRemaining()) + { + buffer.put(DESCRIBED_TYPE); + state = State.DESCRIPTOR; + _delegate = descriptorWriter; + } + length += descriptorWriter.writeToBuffer(buffer); + + ValueWriter describedWriter = createDescribedWriter(); + + if(descriptorWriter.isComplete()) + { + state = State.DESCRIBED; + _delegate = describedWriter; + } + + length += describedWriter.writeToBuffer(buffer); + + if(describedWriter.isComplete()) + { + _delegate = null; + state = State.DONE; + } + + _state = state; + _length = length; + } + + public void setValue(V value) + { + _length = -1; + _delegate = null; + _state = State.FORMAT_CODE; + onSetValue(value); + } + + public void setRegistry(Registry registry) + { + _registry = registry; + } + + protected Registry getRegistry() + { + return _registry; + } + + protected abstract void onSetValue(final V value); + + protected abstract void clear(); + + protected abstract ValueWriter createDescribedWriter(); + + protected abstract Object getDescriptor(); + + protected final ValueWriter createDescriptorWriter() + { + return getRegistry().getValueWriter(getDescriptor()); + } + + public boolean isComplete() + { + return _state == State.DONE; + } + + public boolean isCacheable() + { + return false; + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java index 655b1f2164..9e8acaaeeb 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java @@ -1,41 +1,41 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -public abstract class AbstractListWriter extends CompoundWriter -{ - public AbstractListWriter(final Registry registry) - { - super(registry); - } - - @Override - protected byte getFourOctetEncodingCode() - { - return (byte)0xd0; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return (byte)0xc0; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +public abstract class AbstractListWriter extends CompoundWriter +{ + public AbstractListWriter(final Registry registry) + { + super(registry); + } + + @Override + protected byte getFourOctetEncodingCode() + { + return (byte)0xd0; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return (byte)0xc0; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java index 0fa29b5210..b4390c3ef3 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java @@ -1,95 +1,95 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public abstract class AbstractMapWriter extends CompoundWriter -{ - private boolean onKey; - - public AbstractMapWriter(Registry registry) - { - super(registry); - } - - @Override - protected byte getFourOctetEncodingCode() - { - return (byte)0xd1; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return (byte)0xc1; - } - - @Override - protected final int getCount() - { - return 2 * getMapCount(); - } - - protected abstract int getMapCount(); - - @Override - protected final boolean hasNext() - { - return onKey || hasMapNext(); - } - - protected abstract boolean hasMapNext(); - - @Override - protected final Object next() - { - if(onKey = !onKey) - { - return nextKey(); - } - else - { - return nextValue(); - } - } - - protected abstract Object nextValue(); - - protected abstract Object nextKey(); - - @Override - protected final void clear() - { - onKey = false; - onClear(); - } - - protected abstract void onClear(); - - @Override - protected final void reset() - { - onKey = false; - onReset(); - } - - protected abstract void onReset(); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public abstract class AbstractMapWriter extends CompoundWriter +{ + private boolean onKey; + + public AbstractMapWriter(Registry registry) + { + super(registry); + } + + @Override + protected byte getFourOctetEncodingCode() + { + return (byte)0xd1; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return (byte)0xc1; + } + + @Override + protected final int getCount() + { + return 2 * getMapCount(); + } + + protected abstract int getMapCount(); + + @Override + protected final boolean hasNext() + { + return onKey || hasMapNext(); + } + + protected abstract boolean hasMapNext(); + + @Override + protected final Object next() + { + if(onKey = !onKey) + { + return nextKey(); + } + else + { + return nextValue(); + } + } + + protected abstract Object nextValue(); + + protected abstract Object nextKey(); + + @Override + protected final void clear() + { + onKey = false; + onClear(); + } + + protected abstract void onClear(); + + @Override + protected final void reset() + { + onKey = false; + onReset(); + } + + protected abstract void onReset(); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java index 68239ad143..cc9cc27b56 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java @@ -1,113 +1,113 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.transport.AmqpError; - -import java.lang.reflect.Array; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -public abstract class ArrayTypeConstructor implements TypeConstructor -{ - - - - public Object[] construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException - { - int size = read(in); - if(in.remaining() < size) - { - throw new AmqpErrorException(AmqpError.DECODE_ERROR, - "Insufficient data to decode array - requires %d octects, only %d remaining.", - size, in.remaining()); - } - ByteBuffer dup = in.slice(); - dup.limit(size); - in.position(in.position()+size); - int count = read(dup); - TypeConstructor t = handler.readConstructor(dup); - List rval = new ArrayList(count); - for(int i = 0; i < count; i++) - { - rval.add(t.construct(dup, handler)); - } - if(dup.hasRemaining()) - { - throw new AmqpErrorException(AmqpError.DECODE_ERROR, - "Array incorrectly encoded, %d bytes remaining after decoding %d elements", - dup.remaining(), count); - } - if(rval.size() == 0) - { - return null; - } - else - { - - - return rval.toArray((Object[])Array.newInstance(rval.get(0).getClass(), rval.size())); - } - } - - - abstract int read(ByteBuffer in) throws AmqpErrorException; - - - private static final ArrayTypeConstructor ONE_BYTE_SIZE_ARRAY = new ArrayTypeConstructor() - { - - @Override int read(final ByteBuffer in) throws AmqpErrorException - { - if(!in.hasRemaining()) - { - throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data to decode array"); - } - return ((int)in.get()) & 0xff; - } - - }; - - private static final ArrayTypeConstructor FOUR_BYTE_SIZE_ARRAY = new ArrayTypeConstructor() - { - - @Override int read(final ByteBuffer in) throws AmqpErrorException - { - if(in.remaining()<4) - { - throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data to decode array"); - } - return in.getInt(); - } - - }; - - public static ArrayTypeConstructor getOneByteSizeTypeConstructor() - { - return ONE_BYTE_SIZE_ARRAY; - } - - public static ArrayTypeConstructor getFourByteSizeTypeConstructor() - { - return FOUR_BYTE_SIZE_ARRAY; - } - +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.transport.AmqpError; + +import java.lang.reflect.Array; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +public abstract class ArrayTypeConstructor implements TypeConstructor +{ + + + + public Object[] construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException + { + int size = read(in); + if(in.remaining() < size) + { + throw new AmqpErrorException(AmqpError.DECODE_ERROR, + "Insufficient data to decode array - requires %d octects, only %d remaining.", + size, in.remaining()); + } + ByteBuffer dup = in.slice(); + dup.limit(size); + in.position(in.position()+size); + int count = read(dup); + TypeConstructor t = handler.readConstructor(dup); + List rval = new ArrayList(count); + for(int i = 0; i < count; i++) + { + rval.add(t.construct(dup, handler)); + } + if(dup.hasRemaining()) + { + throw new AmqpErrorException(AmqpError.DECODE_ERROR, + "Array incorrectly encoded, %d bytes remaining after decoding %d elements", + dup.remaining(), count); + } + if(rval.size() == 0) + { + return null; + } + else + { + + + return rval.toArray((Object[])Array.newInstance(rval.get(0).getClass(), rval.size())); + } + } + + + abstract int read(ByteBuffer in) throws AmqpErrorException; + + + private static final ArrayTypeConstructor ONE_BYTE_SIZE_ARRAY = new ArrayTypeConstructor() + { + + @Override int read(final ByteBuffer in) throws AmqpErrorException + { + if(!in.hasRemaining()) + { + throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data to decode array"); + } + return ((int)in.get()) & 0xff; + } + + }; + + private static final ArrayTypeConstructor FOUR_BYTE_SIZE_ARRAY = new ArrayTypeConstructor() + { + + @Override int read(final ByteBuffer in) throws AmqpErrorException + { + if(in.remaining()<4) + { + throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data to decode array"); + } + return in.getInt(); + } + + }; + + public static ArrayTypeConstructor getOneByteSizeTypeConstructor() + { + return ONE_BYTE_SIZE_ARRAY; + } + + public static ArrayTypeConstructor getFourByteSizeTypeConstructor() + { + return FOUR_BYTE_SIZE_ARRAY; + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java index 7766a486f0..4bf083456e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java @@ -1,82 +1,82 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class ArrayWriter implements ValueWriter -{ - private Object[] _list; - private int _position = 0; - private final Registry _registry; - private ValueWriter valueWriter; - - public ArrayWriter(final Registry registry) - { - _registry = registry; - } - - - protected void onSetValue(final Object[] value) - { - - Class clazz = value.getClass().getComponentType(); - //valueWriter = _registry.getValueWriterByClass(clazz); - - - } - - - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ArrayWriter(registry); - } - }; - - public static void register(Registry registry) - { - //registry.register(List.class, FACTORY); - } - - public int writeToBuffer(final ByteBuffer buffer) - { - return 0; //TODO change body of implemented methods use File | Settings | File Templates. - } - - public void setValue(final Object[] frameBody) - { - //TODO change body of implemented methods use File | Settings | File Templates. - } - - public boolean isComplete() - { - return false; //TODO change body of implemented methods use File | Settings | File Templates. - } - - public boolean isCacheable() - { - return false; //TODO change body of implemented methods use File | Settings | File Templates. - } +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class ArrayWriter implements ValueWriter +{ + private Object[] _list; + private int _position = 0; + private final Registry _registry; + private ValueWriter valueWriter; + + public ArrayWriter(final Registry registry) + { + _registry = registry; + } + + + protected void onSetValue(final Object[] value) + { + + Class clazz = value.getClass().getComponentType(); + //valueWriter = _registry.getValueWriterByClass(clazz); + + + } + + + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ArrayWriter(registry); + } + }; + + public static void register(Registry registry) + { + //registry.register(List.class, FACTORY); + } + + public int writeToBuffer(final ByteBuffer buffer) + { + return 0; //TODO change body of implemented methods use File | Settings | File Templates. + } + + public void setValue(final Object[] frameBody) + { + //TODO change body of implemented methods use File | Settings | File Templates. + } + + public boolean isComplete() + { + return false; //TODO change body of implemented methods use File | Settings | File Templates. + } + + public boolean isCacheable() + { + return false; //TODO change body of implemented methods use File | Settings | File Templates. + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java index 4fc7f8e919..c8af187a5f 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java @@ -18,71 +18,71 @@ * under the License. * */ -package org.apache.qpid.amqp_1_0.codec; - - -final class BinaryString -{ - - private byte[] _data; - private int _offset; - private int _size; - private int _hashCode; - - BinaryString(final byte[] data, final int offset, final int size) - { - - setData(data, offset, size); - } - - BinaryString() - { - } - - void setData(byte[] data, int offset, int size) - { - _data = data; - _offset = offset; - _size = size; - int hc = 0; - for (int i = 0; i < size; i++) - { - hc = 31*hc + (0xFF & data[offset + i]); - } - _hashCode = hc; - } - - - public final int hashCode() - { - return _hashCode; - } - - public final boolean equals(Object o) - { - BinaryString buf = (BinaryString) o; - final int size = _size; - if (size != buf._size) - { - return false; - } - - final byte[] myData = _data; - final byte[] theirData = buf._data; - int myOffset = _offset; - int theirOffset = buf._offset; - final int myLimit = myOffset + size; - - while(myOffset < myLimit) - { - if (myData[myOffset++] != theirData[theirOffset++]) - { - return false; - } - } - - return true; - } - - -} +package org.apache.qpid.amqp_1_0.codec; + + +final class BinaryString +{ + + private byte[] _data; + private int _offset; + private int _size; + private int _hashCode; + + BinaryString(final byte[] data, final int offset, final int size) + { + + setData(data, offset, size); + } + + BinaryString() + { + } + + void setData(byte[] data, int offset, int size) + { + _data = data; + _offset = offset; + _size = size; + int hc = 0; + for (int i = 0; i < size; i++) + { + hc = 31*hc + (0xFF & data[offset + i]); + } + _hashCode = hc; + } + + + public final int hashCode() + { + return _hashCode; + } + + public final boolean equals(Object o) + { + BinaryString buf = (BinaryString) o; + final int size = _size; + if (size != buf._size) + { + return false; + } + + final byte[] myData = _data; + final byte[] theirData = buf._data; + int myOffset = _offset; + int theirOffset = buf._offset; + final int myLimit = myOffset + size; + + while(myOffset < myLimit) + { + if (myData[myOffset++] != theirData[theirOffset++]) + { + return false; + } + } + + return true; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java index e83718d88d..4c5a9d9830 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java @@ -1,80 +1,80 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.Binary; - -import java.nio.ByteBuffer; - -public class BinaryTypeConstructor extends VariableWidthTypeConstructor -{ - private static final BinaryTypeConstructor INSTANCE_1 = new BinaryTypeConstructor(1); - private static final BinaryTypeConstructor INSTANCE_4 = new BinaryTypeConstructor(4); - - public static BinaryTypeConstructor getInstance(int i) - { - return i == 1 ? INSTANCE_1 : INSTANCE_4; - } - - - private BinaryTypeConstructor(int size) - { - super(size); - } - - @Override - public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException - { - int size; - - if(getSize() == 1) - { - size = in.get() & 0xFF; - } - else - { - size = in.getInt(); - } - - ByteBuffer inDup = in.slice(); - inDup.limit(inDup.position()+size); - - Binary binary; -/* if(isCopy && inDup.hasArray()) - { - binary= new Binary(inDup.array(), inDup.arrayOffset()+inDup.position(),size); - } - else - {*/ - byte[] buf = new byte[size]; - inDup.get(buf); - binary = new Binary(buf); - /* }*/ - - in.position(in.position()+size); - - - return binary; - - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.Binary; + +import java.nio.ByteBuffer; + +public class BinaryTypeConstructor extends VariableWidthTypeConstructor +{ + private static final BinaryTypeConstructor INSTANCE_1 = new BinaryTypeConstructor(1); + private static final BinaryTypeConstructor INSTANCE_4 = new BinaryTypeConstructor(4); + + public static BinaryTypeConstructor getInstance(int i) + { + return i == 1 ? INSTANCE_1 : INSTANCE_4; + } + + + private BinaryTypeConstructor(int size) + { + super(size); + } + + @Override + public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException + { + int size; + + if(getSize() == 1) + { + size = in.get() & 0xFF; + } + else + { + size = in.getInt(); + } + + ByteBuffer inDup = in.slice(); + inDup.limit(inDup.position()+size); + + Binary binary; +/* if(isCopy && inDup.hasArray()) + { + binary= new Binary(inDup.array(), inDup.arrayOffset()+inDup.position(),size); + } + else + {*/ + byte[] buf = new byte[size]; + inDup.get(buf); + binary = new Binary(buf); + /* }*/ + + in.position(in.position()+size); + + + return binary; + + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java index 8ab4569646..835b2567c1 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java @@ -1,75 +1,75 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.Binary; - -public class BinaryWriter extends SimpleVariableWidthWriter -{ - private int _offset; - private int _length; - - @Override - protected byte getFourOctetEncodingCode() - { - return (byte)0xb0; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return (byte)0xa0; - } - - @Override - protected byte[] getByteArray(Binary value) - { - _offset = value.getArrayOffset(); - _length = value.getLength(); - return value.getArray(); - } - - @Override - protected int getOffset() - { - return _offset; - } - - @Override protected int getLength() - { - return _length; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new BinaryWriter(); - } - }; - - public static void register(Registry registry) - { - registry.register(Binary.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.Binary; + +public class BinaryWriter extends SimpleVariableWidthWriter +{ + private int _offset; + private int _length; + + @Override + protected byte getFourOctetEncodingCode() + { + return (byte)0xb0; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return (byte)0xa0; + } + + @Override + protected byte[] getByteArray(Binary value) + { + _offset = value.getArrayOffset(); + _length = value.getLength(); + return value.getArray(); + } + + @Override + protected int getOffset() + { + return _offset; + } + + @Override protected int getLength() + { + return _length; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new BinaryWriter(); + } + }; + + public static void register(Registry registry) + { + registry.register(Binary.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java index fb4449fb2c..9c3d519972 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java @@ -1,70 +1,70 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class BooleanWriter implements ValueWriter -{ - private boolean _complete = true; - private boolean _value; - - public int writeToBuffer(ByteBuffer buffer) - { - if(!_complete & buffer.hasRemaining()) - { - buffer.put(_value ? (byte)0x41 : (byte)0x42); - _complete = true; - } - return 1; - } - - public void setValue(Boolean value) - { - _complete = false; - _value = value.booleanValue(); - } - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _complete; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new BooleanWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Boolean.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class BooleanWriter implements ValueWriter +{ + private boolean _complete = true; + private boolean _value; + + public int writeToBuffer(ByteBuffer buffer) + { + if(!_complete & buffer.hasRemaining()) + { + buffer.put(_value ? (byte)0x41 : (byte)0x42); + _complete = true; + } + return 1; + } + + public void setValue(Boolean value) + { + _complete = false; + _value = value.booleanValue(); + } + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _complete; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new BooleanWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Boolean.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java index 662b4085db..539e000050 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java @@ -1,66 +1,66 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public class ByteArrayWriter extends SimpleVariableWidthWriter -{ - - @Override - protected byte getFourOctetEncodingCode() - { - return (byte)0xb0; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return (byte)0xa0; - } - - @Override - protected byte[] getByteArray(byte[] value) - { - return value; - } - - - @Override - protected int getOffset() - { - return 0; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ByteArrayWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(byte[].class, FACTORY); - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public class ByteArrayWriter extends SimpleVariableWidthWriter +{ + + @Override + protected byte getFourOctetEncodingCode() + { + return (byte)0xb0; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return (byte)0xa0; + } + + @Override + protected byte[] getByteArray(byte[] value) + { + return value; + } + + + @Override + protected int getOffset() + { + return 0; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ByteArrayWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(byte[].class, FACTORY); + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java index 41bd20c0a2..81cd14b2d9 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java @@ -1,75 +1,75 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class ByteBufferWriter extends SimpleVariableWidthWriter -{ - - @Override - protected byte getFourOctetEncodingCode() - { - return (byte)0xb0; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return (byte)0xa0; - } - - @Override - protected byte[] getByteArray(ByteBuffer value) - { - if(value.hasArray() && value.arrayOffset() == 0 && value.remaining() == value.array().length) - { - return value.array(); - } - else - { - byte[] copy = new byte[value.remaining()]; - value.duplicate().get(copy); - return copy; - } - } - - @Override - protected int getOffset() - { - return 0; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ByteBufferWriter(); - } - }; - - public static void register(Registry registry) - { - registry.register(ByteBuffer.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class ByteBufferWriter extends SimpleVariableWidthWriter +{ + + @Override + protected byte getFourOctetEncodingCode() + { + return (byte)0xb0; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return (byte)0xa0; + } + + @Override + protected byte[] getByteArray(ByteBuffer value) + { + if(value.hasArray() && value.arrayOffset() == 0 && value.remaining() == value.array().length) + { + return value.array(); + } + else + { + byte[] copy = new byte[value.remaining()]; + value.duplicate().get(copy); + return copy; + } + } + + @Override + protected int getOffset() + { + return 0; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ByteBufferWriter(); + } + }; + + public static void register(Registry registry) + { + registry.register(ByteBuffer.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java index 03db2c568c..02278597f6 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java @@ -1,59 +1,59 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class ByteTypeConstructor implements TypeConstructor -{ - private static final ByteTypeConstructor INSTANCE = new ByteTypeConstructor(); - - public static ByteTypeConstructor getInstance() - { - return INSTANCE; - } - - private ByteTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - return in.get(); - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct byte: insufficient input data"); - throw new AmqpErrorException(error); - - } - - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class ByteTypeConstructor implements TypeConstructor +{ + private static final ByteTypeConstructor INSTANCE = new ByteTypeConstructor(); + + public static ByteTypeConstructor getInstance() + { + return INSTANCE; + } + + private ByteTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + return in.get(); + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct byte: insufficient input data"); + throw new AmqpErrorException(error); + + } + + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java index 6155de4d2a..275ae9239b 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java @@ -1,90 +1,90 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class ByteWriter implements ValueWriter -{ - private int _written = 2; - private byte _value; - - public int writeToBuffer(ByteBuffer buffer) - { - - switch(_written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put((byte)0x51); - } - else - { - break; - } - case 1: - if(buffer.hasRemaining()) - { - buffer.put(_value); - _written = 2; - } - else - { - _written = 1; - } - - } - - return 2; - } - - public void setValue(Byte value) - { - _written = 0; - _value = value.byteValue(); - } - - public boolean isComplete() - { - return _written == 2; - } - - public boolean isCacheable() - { - return true; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ByteWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Byte.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class ByteWriter implements ValueWriter +{ + private int _written = 2; + private byte _value; + + public int writeToBuffer(ByteBuffer buffer) + { + + switch(_written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put((byte)0x51); + } + else + { + break; + } + case 1: + if(buffer.hasRemaining()) + { + buffer.put(_value); + _written = 2; + } + else + { + _written = 1; + } + + } + + return 2; + } + + public void setValue(Byte value) + { + _written = 0; + _value = value.byteValue(); + } + + public boolean isComplete() + { + return _written == 2; + } + + public boolean isCacheable() + { + return true; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ByteWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Byte.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java index 6a2ce2d725..d661838350 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java @@ -1,67 +1,67 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.*; - -import java.nio.ByteBuffer; - -public class CharTypeConstructor implements TypeConstructor -{ - private static final CharTypeConstructor INSTANCE = new CharTypeConstructor(); - - - public static CharTypeConstructor getInstance() - { - return INSTANCE; - } - - private CharTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=4) - { - int codePoint = in.getInt(); - char[] chars = Character.toChars(codePoint); - if(chars.length == 1) - { - return chars[0]; - } - else - { - return chars; - } - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct char: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.*; + +import java.nio.ByteBuffer; + +public class CharTypeConstructor implements TypeConstructor +{ + private static final CharTypeConstructor INSTANCE = new CharTypeConstructor(); + + + public static CharTypeConstructor getInstance() + { + return INSTANCE; + } + + private CharTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=4) + { + int codePoint = in.getInt(); + char[] chars = Character.toChars(codePoint); + if(chars.length == 1) + { + return chars[0]; + } + else + { + return chars; + } + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct char: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java index 05f6e28d2f..c3fbdf76dd 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java @@ -1,53 +1,53 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public class CharWriter extends FixedFourWriter -{ - private static final byte FORMAT_CODE = (byte)0x73; - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - @Override - int convertValueToInt(Character value) - { - return (int) value.charValue(); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new CharWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Character.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public class CharWriter extends FixedFourWriter +{ + private static final byte FORMAT_CODE = (byte)0x73; + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + @Override + int convertValueToInt(Character value) + { + return (int) value.charValue(); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new CharWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Character.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java index 5625797f74..abdcee65d7 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java @@ -1,36 +1,36 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -public interface CompoundTypeAssembler -{ - - public static interface Factory - { - CompoundTypeAssembler newInstance(); - } - - void init(int count) throws AmqpErrorException; - void addItem(Object obj) throws AmqpErrorException; - Object complete() throws AmqpErrorException; -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; + +public interface CompoundTypeAssembler +{ + + public static interface Factory + { + CompoundTypeAssembler newInstance(); + } + + void init(int count) throws AmqpErrorException; + void addItem(Object obj) throws AmqpErrorException; + Object complete() throws AmqpErrorException; +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java index fc4fcdf9ee..28bf1a577c 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java @@ -1,192 +1,192 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Formatter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class CompoundTypeConstructor extends VariableWidthTypeConstructor -{ - private final CompoundTypeAssembler.Factory _assemblerFactory; - - public static final CompoundTypeAssembler.Factory LIST_ASSEMBLER_FACTORY = - new CompoundTypeAssembler.Factory() - { - - public CompoundTypeAssembler newInstance() - { - return new ListAssembler(); - } - }; - - - - private static class ListAssembler implements CompoundTypeAssembler - { - private List _list; - - public void init(final int count) throws AmqpErrorException - { - _list = new ArrayList(count); - } - - public void addItem(final Object obj) throws AmqpErrorException - { - _list.add(obj); - } - - public Object complete() throws AmqpErrorException - { - return _list; - } - - @Override - public String toString() - { - return "ListAssembler{" + - "_list=" + _list + - '}'; - } - } - - - public static final CompoundTypeAssembler.Factory MAP_ASSEMBLER_FACTORY = - new CompoundTypeAssembler.Factory() - { - - public CompoundTypeAssembler newInstance() - { - return new MapAssembler(); - } - }; - - private static class MapAssembler implements CompoundTypeAssembler - { - private Map _map; - private Object _lastKey; - private static final Object NOT_A_KEY = new Object(); - - - public void init(final int count) throws AmqpErrorException - { - // Can't have an odd number of elements in a map - if((count & 0x1) == 1) - { - Error error = new Error(); - error.setCondition(AmqpError.DECODE_ERROR); - Formatter formatter = new Formatter(); - formatter.format("map cannot have odd number of elements: %d", count); - error.setDescription(formatter.toString()); - throw new AmqpErrorException(error); - } - _map = new HashMap(count); - _lastKey = NOT_A_KEY; - } - - public void addItem(final Object obj) throws AmqpErrorException - { - if(_lastKey != NOT_A_KEY) - { - if(_map.put(_lastKey, obj) != null) - { - Error error = new Error(); - error.setCondition(AmqpError.DECODE_ERROR); - Formatter formatter = new Formatter(); - formatter.format("map cannot have duplicate keys: %s has values (%s, %s)", _lastKey, _map.get(_lastKey), obj); - error.setDescription(formatter.toString()); - - throw new AmqpErrorException(error); - } - _lastKey = NOT_A_KEY; - } - else - { - _lastKey = obj; - } - - } - - public Object complete() throws AmqpErrorException - { - return _map; - } - } - - - public static CompoundTypeConstructor getInstance(int i, - CompoundTypeAssembler.Factory assemblerFactory) - { - return new CompoundTypeConstructor(i, assemblerFactory); - } - - - private CompoundTypeConstructor(int size, - final CompoundTypeAssembler.Factory assemblerFactory) - { - super(size); - _assemblerFactory = assemblerFactory; - } - - @Override - public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler delegate) throws AmqpErrorException - { - int size; - int count; - - if(getSize() == 1) - { - size = in.get() & 0xFF; - count = in.get() & 0xFF; - } - else - { - size = in.getInt(); - count = in.getInt(); - } - - ByteBuffer data; - ByteBuffer inDup = in.slice(); - - inDup.limit(size-getSize()); - - CompoundTypeAssembler assembler = _assemblerFactory.newInstance(); - - assembler.init(count); - - for(int i = 0; i < count; i++) - { - assembler.addItem(delegate.parse(in)); - } - - return assembler.complete(); - - } - - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Formatter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CompoundTypeConstructor extends VariableWidthTypeConstructor +{ + private final CompoundTypeAssembler.Factory _assemblerFactory; + + public static final CompoundTypeAssembler.Factory LIST_ASSEMBLER_FACTORY = + new CompoundTypeAssembler.Factory() + { + + public CompoundTypeAssembler newInstance() + { + return new ListAssembler(); + } + }; + + + + private static class ListAssembler implements CompoundTypeAssembler + { + private List _list; + + public void init(final int count) throws AmqpErrorException + { + _list = new ArrayList(count); + } + + public void addItem(final Object obj) throws AmqpErrorException + { + _list.add(obj); + } + + public Object complete() throws AmqpErrorException + { + return _list; + } + + @Override + public String toString() + { + return "ListAssembler{" + + "_list=" + _list + + '}'; + } + } + + + public static final CompoundTypeAssembler.Factory MAP_ASSEMBLER_FACTORY = + new CompoundTypeAssembler.Factory() + { + + public CompoundTypeAssembler newInstance() + { + return new MapAssembler(); + } + }; + + private static class MapAssembler implements CompoundTypeAssembler + { + private Map _map; + private Object _lastKey; + private static final Object NOT_A_KEY = new Object(); + + + public void init(final int count) throws AmqpErrorException + { + // Can't have an odd number of elements in a map + if((count & 0x1) == 1) + { + Error error = new Error(); + error.setCondition(AmqpError.DECODE_ERROR); + Formatter formatter = new Formatter(); + formatter.format("map cannot have odd number of elements: %d", count); + error.setDescription(formatter.toString()); + throw new AmqpErrorException(error); + } + _map = new HashMap(count); + _lastKey = NOT_A_KEY; + } + + public void addItem(final Object obj) throws AmqpErrorException + { + if(_lastKey != NOT_A_KEY) + { + if(_map.put(_lastKey, obj) != null) + { + Error error = new Error(); + error.setCondition(AmqpError.DECODE_ERROR); + Formatter formatter = new Formatter(); + formatter.format("map cannot have duplicate keys: %s has values (%s, %s)", _lastKey, _map.get(_lastKey), obj); + error.setDescription(formatter.toString()); + + throw new AmqpErrorException(error); + } + _lastKey = NOT_A_KEY; + } + else + { + _lastKey = obj; + } + + } + + public Object complete() throws AmqpErrorException + { + return _map; + } + } + + + public static CompoundTypeConstructor getInstance(int i, + CompoundTypeAssembler.Factory assemblerFactory) + { + return new CompoundTypeConstructor(i, assemblerFactory); + } + + + private CompoundTypeConstructor(int size, + final CompoundTypeAssembler.Factory assemblerFactory) + { + super(size); + _assemblerFactory = assemblerFactory; + } + + @Override + public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler delegate) throws AmqpErrorException + { + int size; + int count; + + if(getSize() == 1) + { + size = in.get() & 0xFF; + count = in.get() & 0xFF; + } + else + { + size = in.getInt(); + count = in.getInt(); + } + + ByteBuffer data; + ByteBuffer inDup = in.slice(); + + inDup.limit(size-getSize()); + + CompoundTypeAssembler assembler = _assemblerFactory.newInstance(); + + assembler.init(count); + + for(int i = 0; i < count; i++) + { + assembler.addItem(delegate.parse(in)); + } + + return assembler.complete(); + + } + + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java index 39dce2b448..73c4d4bd95 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java @@ -1,420 +1,420 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; -import java.util.HashMap; -import java.util.Map; - -public abstract class CompoundWriter implements ValueWriter -{ - private int _length; - private Registry _registry; - private static final int LARGE_COMPOUND_THRESHOLD_COUNT = 25; - private ValueWriter _delegate; - private Map _writerCache = new HashMap(); - - public CompoundWriter(final Registry registry) - { - _registry = registry; - } - - enum State { - FORMAT_CODE, - SIZE_0, - SIZE_1, - SIZE_2, - SIZE_3, - COUNT_0, - COUNT_1, - COUNT_2, - COUNT_3, - DELEGATING, - DONE - } - - private State _state = State.FORMAT_CODE; - - public int writeToBuffer(ByteBuffer buffer) - { - return writeToBuffer(buffer, false); - } - - public int writeToBuffer(ByteBuffer buffer, boolean large) - { - final int length = _length; - - if(length == -1) - { - writeFirstPass(buffer, (large || getCount() > LARGE_COMPOUND_THRESHOLD_COUNT) ? 4 : 1); - if(_delegate != null && _delegate.isComplete()) - { - _delegate = null; - } - } - else - { - // - - final int size = (length & 0xFFFFFF00) == 0 ? 1 : 4; - final int count = getCount(); - final int typeLength = length - (1+size); - - State state = _state; - - switch(state) - { - case FORMAT_CODE: - if(buffer.hasRemaining()) - { - buffer.put(size == 1 ? getSingleOctetEncodingCode(): getFourOctetEncodingCode()); - state = State.SIZE_0; - } - else - { - break; - } - - case SIZE_0: - if(size == 4) - { - if(buffer.remaining()>=4) - { - buffer.putInt(typeLength); - state = State.COUNT_0; - } - } - else if(size == 1) - { - if(buffer.hasRemaining()) - { - buffer.put((byte)(typeLength)); - state = State.COUNT_0; - } - else - { - break; - } - - } - case SIZE_1: - case SIZE_2: - if(state != State.COUNT_0 && buffer.remaining() >= 2) - { - buffer.putShort((short)(((typeLength) >> ((3-state.ordinal())<<3)) & 0xFFFF )); - state = (state == State.SIZE_0) - ? State.SIZE_2 - : (state == State.SIZE_1) - ? State.SIZE_3 - : State.COUNT_0; - } - case SIZE_3: - if(state != State.COUNT_0 && buffer.hasRemaining()) - { - buffer.put((byte)(((typeLength) >> ((4-state.ordinal())<<3)) & 0xFF )); - state = (state == State.SIZE_0) - ? State.SIZE_1 - : (state == State.SIZE_1) - ? State.SIZE_2 - : (state == State.SIZE_2) - ? State.SIZE_3 - : State.COUNT_0; - } - case COUNT_0: - if(size == 4) - { - if(buffer.remaining()>=4) - { - buffer.putInt(count); - state = State.DELEGATING; - } - } - else if(size == 1) - { - if(buffer.hasRemaining()) - { - buffer.put((byte)count); - state = State.DELEGATING; - } - else - { - break; - } - - } - - case COUNT_1: - case COUNT_2: - if(state != State.DELEGATING && buffer.remaining() >= 2) - { - buffer.putShort((short)((count >> ((7-state.ordinal())<<3)) & 0xFFFF )); - state = state == State.COUNT_0 - ? State.COUNT_2 - : state == State.COUNT_1 - ? State.COUNT_3 - : State.DELEGATING; - } - case COUNT_3: - if(state != State.DELEGATING && buffer.hasRemaining()) - { - buffer.put((byte)((count >> ((8-state.ordinal())<<3)) & 0xFF )); - state = state == State.COUNT_0 - ? State.COUNT_1 - : state == State.COUNT_1 - ? State.COUNT_2 - : state == State.COUNT_2 - ? State.COUNT_3 - : State.DELEGATING; - } - case DELEGATING: - while(state == State.DELEGATING && buffer.hasRemaining()) - { - if(_delegate == null || _delegate.isComplete()) - { - if(hasNext()) - { - Object val = next(); - _delegate = _registry.getValueWriter(val); - } - else - { - state = State.DONE; - break; - } - } - _delegate.writeToBuffer(buffer); - } - } - - _state = state; - - } - - return _length; - } - - private void writeFirstPass(ByteBuffer buffer, int size) - { - - State state = State.FORMAT_CODE; - /*ByteBuffer origBuffer = buffer; - buffer = buffer.duplicate();*/ - int origPosition = buffer.position(); - int length ; - - - if(size == 4) - { - if(buffer.hasRemaining()) - { - buffer.put(getFourOctetEncodingCode()); - - // Skip the size - we will come back and patch this - if(buffer.remaining() >= 4 ) - { - buffer.position(buffer.position()+4); - state = State.COUNT_0; - } - else - { - state = State.values()[buffer.remaining()+1]; - buffer.position(buffer.limit()); - } - - - switch(buffer.remaining()) - { - case 0: - break; - case 1: - buffer.put((byte)((getCount() >> 24) & 0xFF)); - state = State.COUNT_1; - break; - case 2: - buffer.putShort((short)((getCount() >> 16) & 0xFFFF)); - state = State.COUNT_2; - break; - case 3: - buffer.putShort((short)((getCount() >> 16) & 0xFFFF)); - buffer.put((byte)((getCount() >> 8) & 0xFF)); - state = State.COUNT_3; - break; - default: - buffer.putInt(getCount()); - state = State.DELEGATING; - } - - - - } - length = 9; - - - - } - else - { - if(buffer.hasRemaining()) - { - buffer.put(getSingleOctetEncodingCode()); - if(buffer.hasRemaining()) - { - // Size - we will come back and patch this - buffer.put((byte) 0); - - if(buffer.hasRemaining()) - { - buffer.put((byte)getCount()); - state = State.DELEGATING; - } - else - { - state = State.COUNT_0; - } - } - else - { - state = State.SIZE_0; - } - } - length = 3; - - } - - - int iterPos = -1; - for(int i = 0; i < getCount(); i++) - { - Object val = next(); - ValueWriter writer = _registry.getValueWriter(val, _writerCache); - if(writer == null) - { - // TODO - System.out.println("no writer for " + val); - } - length += writer.writeToBuffer(buffer); - if(iterPos == -1 && !writer.isComplete()) - { - iterPos = i; - _delegate = writer; - } - - if(size == 1 && length > 255) - { - reset(); - buffer.position(origPosition); - writeFirstPass(buffer, 4); - return; - } - - } - - // TODO - back-patch size - if(buffer.limit() - origPosition >= 2) - { - buffer.position(origPosition+1); - if(size == 1) - { - buffer.put((byte)((length & 0xFF)-2)); - } - else - { - switch(buffer.remaining()) - { - case 1: - buffer.put((byte)(((length-5) >> 24) & 0xFF)); - break; - case 2: - buffer.putShort((short)(((length-5) >> 16) & 0xFFFF)); - break; - case 3: - buffer.putShort((short)(((length-5) >> 16) & 0xFFFF)); - buffer.put((byte)(((length-5) >> 8) & 0xFF)); - break; - default: - buffer.putInt(length-5); - } - } - } - - if(buffer.limit() - origPosition >= length) - { - buffer.position(origPosition+length); - state = State.DONE; - } - else - { - reset(); - while(iterPos-- >= 0) - { - next(); - } - buffer.position(buffer.limit()); - } - _state = state; - _length = length; - } - - protected abstract byte getFourOctetEncodingCode(); - - protected abstract byte getSingleOctetEncodingCode(); - - public void setValue(V value) - { - _length = -1; - _delegate = null; - _state = State.FORMAT_CODE; - onSetValue(value); - } - - public void setRegistry(Registry registry) - { - _registry = registry; - } - - public Registry getRegistry() - { - return _registry; - } - - protected abstract void onSetValue(final V value); - - protected abstract int getCount(); - - protected abstract boolean hasNext(); - - protected abstract Object next(); - - protected abstract void clear(); - - protected abstract void reset(); - - public boolean isCacheable() - { - return false; - } - - public boolean isComplete() - { - return _state == State.DONE; - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; + +public abstract class CompoundWriter implements ValueWriter +{ + private int _length; + private Registry _registry; + private static final int LARGE_COMPOUND_THRESHOLD_COUNT = 25; + private ValueWriter _delegate; + private Map _writerCache = new HashMap(); + + public CompoundWriter(final Registry registry) + { + _registry = registry; + } + + enum State { + FORMAT_CODE, + SIZE_0, + SIZE_1, + SIZE_2, + SIZE_3, + COUNT_0, + COUNT_1, + COUNT_2, + COUNT_3, + DELEGATING, + DONE + } + + private State _state = State.FORMAT_CODE; + + public int writeToBuffer(ByteBuffer buffer) + { + return writeToBuffer(buffer, false); + } + + public int writeToBuffer(ByteBuffer buffer, boolean large) + { + final int length = _length; + + if(length == -1) + { + writeFirstPass(buffer, (large || getCount() > LARGE_COMPOUND_THRESHOLD_COUNT) ? 4 : 1); + if(_delegate != null && _delegate.isComplete()) + { + _delegate = null; + } + } + else + { + // + + final int size = (length & 0xFFFFFF00) == 0 ? 1 : 4; + final int count = getCount(); + final int typeLength = length - (1+size); + + State state = _state; + + switch(state) + { + case FORMAT_CODE: + if(buffer.hasRemaining()) + { + buffer.put(size == 1 ? getSingleOctetEncodingCode(): getFourOctetEncodingCode()); + state = State.SIZE_0; + } + else + { + break; + } + + case SIZE_0: + if(size == 4) + { + if(buffer.remaining()>=4) + { + buffer.putInt(typeLength); + state = State.COUNT_0; + } + } + else if(size == 1) + { + if(buffer.hasRemaining()) + { + buffer.put((byte)(typeLength)); + state = State.COUNT_0; + } + else + { + break; + } + + } + case SIZE_1: + case SIZE_2: + if(state != State.COUNT_0 && buffer.remaining() >= 2) + { + buffer.putShort((short)(((typeLength) >> ((3-state.ordinal())<<3)) & 0xFFFF )); + state = (state == State.SIZE_0) + ? State.SIZE_2 + : (state == State.SIZE_1) + ? State.SIZE_3 + : State.COUNT_0; + } + case SIZE_3: + if(state != State.COUNT_0 && buffer.hasRemaining()) + { + buffer.put((byte)(((typeLength) >> ((4-state.ordinal())<<3)) & 0xFF )); + state = (state == State.SIZE_0) + ? State.SIZE_1 + : (state == State.SIZE_1) + ? State.SIZE_2 + : (state == State.SIZE_2) + ? State.SIZE_3 + : State.COUNT_0; + } + case COUNT_0: + if(size == 4) + { + if(buffer.remaining()>=4) + { + buffer.putInt(count); + state = State.DELEGATING; + } + } + else if(size == 1) + { + if(buffer.hasRemaining()) + { + buffer.put((byte)count); + state = State.DELEGATING; + } + else + { + break; + } + + } + + case COUNT_1: + case COUNT_2: + if(state != State.DELEGATING && buffer.remaining() >= 2) + { + buffer.putShort((short)((count >> ((7-state.ordinal())<<3)) & 0xFFFF )); + state = state == State.COUNT_0 + ? State.COUNT_2 + : state == State.COUNT_1 + ? State.COUNT_3 + : State.DELEGATING; + } + case COUNT_3: + if(state != State.DELEGATING && buffer.hasRemaining()) + { + buffer.put((byte)((count >> ((8-state.ordinal())<<3)) & 0xFF )); + state = state == State.COUNT_0 + ? State.COUNT_1 + : state == State.COUNT_1 + ? State.COUNT_2 + : state == State.COUNT_2 + ? State.COUNT_3 + : State.DELEGATING; + } + case DELEGATING: + while(state == State.DELEGATING && buffer.hasRemaining()) + { + if(_delegate == null || _delegate.isComplete()) + { + if(hasNext()) + { + Object val = next(); + _delegate = _registry.getValueWriter(val); + } + else + { + state = State.DONE; + break; + } + } + _delegate.writeToBuffer(buffer); + } + } + + _state = state; + + } + + return _length; + } + + private void writeFirstPass(ByteBuffer buffer, int size) + { + + State state = State.FORMAT_CODE; + /*ByteBuffer origBuffer = buffer; + buffer = buffer.duplicate();*/ + int origPosition = buffer.position(); + int length ; + + + if(size == 4) + { + if(buffer.hasRemaining()) + { + buffer.put(getFourOctetEncodingCode()); + + // Skip the size - we will come back and patch this + if(buffer.remaining() >= 4 ) + { + buffer.position(buffer.position()+4); + state = State.COUNT_0; + } + else + { + state = State.values()[buffer.remaining()+1]; + buffer.position(buffer.limit()); + } + + + switch(buffer.remaining()) + { + case 0: + break; + case 1: + buffer.put((byte)((getCount() >> 24) & 0xFF)); + state = State.COUNT_1; + break; + case 2: + buffer.putShort((short)((getCount() >> 16) & 0xFFFF)); + state = State.COUNT_2; + break; + case 3: + buffer.putShort((short)((getCount() >> 16) & 0xFFFF)); + buffer.put((byte)((getCount() >> 8) & 0xFF)); + state = State.COUNT_3; + break; + default: + buffer.putInt(getCount()); + state = State.DELEGATING; + } + + + + } + length = 9; + + + + } + else + { + if(buffer.hasRemaining()) + { + buffer.put(getSingleOctetEncodingCode()); + if(buffer.hasRemaining()) + { + // Size - we will come back and patch this + buffer.put((byte) 0); + + if(buffer.hasRemaining()) + { + buffer.put((byte)getCount()); + state = State.DELEGATING; + } + else + { + state = State.COUNT_0; + } + } + else + { + state = State.SIZE_0; + } + } + length = 3; + + } + + + int iterPos = -1; + for(int i = 0; i < getCount(); i++) + { + Object val = next(); + ValueWriter writer = _registry.getValueWriter(val, _writerCache); + if(writer == null) + { + // TODO + System.out.println("no writer for " + val); + } + length += writer.writeToBuffer(buffer); + if(iterPos == -1 && !writer.isComplete()) + { + iterPos = i; + _delegate = writer; + } + + if(size == 1 && length > 255) + { + reset(); + buffer.position(origPosition); + writeFirstPass(buffer, 4); + return; + } + + } + + // TODO - back-patch size + if(buffer.limit() - origPosition >= 2) + { + buffer.position(origPosition+1); + if(size == 1) + { + buffer.put((byte)((length & 0xFF)-2)); + } + else + { + switch(buffer.remaining()) + { + case 1: + buffer.put((byte)(((length-5) >> 24) & 0xFF)); + break; + case 2: + buffer.putShort((short)(((length-5) >> 16) & 0xFFFF)); + break; + case 3: + buffer.putShort((short)(((length-5) >> 16) & 0xFFFF)); + buffer.put((byte)(((length-5) >> 8) & 0xFF)); + break; + default: + buffer.putInt(length-5); + } + } + } + + if(buffer.limit() - origPosition >= length) + { + buffer.position(origPosition+length); + state = State.DONE; + } + else + { + reset(); + while(iterPos-- >= 0) + { + next(); + } + buffer.position(buffer.limit()); + } + _state = state; + _length = length; + } + + protected abstract byte getFourOctetEncodingCode(); + + protected abstract byte getSingleOctetEncodingCode(); + + public void setValue(V value) + { + _length = -1; + _delegate = null; + _state = State.FORMAT_CODE; + onSetValue(value); + } + + public void setRegistry(Registry registry) + { + _registry = registry; + } + + public Registry getRegistry() + { + return _registry; + } + + protected abstract void onSetValue(final V value); + + protected abstract int getCount(); + + protected abstract boolean hasNext(); + + protected abstract Object next(); + + protected abstract void clear(); + + protected abstract void reset(); + + public boolean isCacheable() + { + return false; + } + + public boolean isComplete() + { + return _state == State.DONE; + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java index 48b2045298..5da6bf7e58 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java @@ -1,70 +1,70 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.LineNumberReader; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class DefaultDescribedTypeConstructor extends DescribedTypeConstructor -{ - private Object _descriptor; - - public DefaultDescribedTypeConstructor(final Object descriptor) - { - _descriptor = descriptor; - } - - public Object construct(final Object underlying) - { - return new DescribedType(_descriptor, underlying); - } - - - public static void main(String[] args) throws IOException, ParseException - { - LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in)); - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"); - String line; - Pattern pattern = Pattern.compile("^\\d+ (\\d{4}-\\d{2}-\\d{2} \\d\\d:\\d\\d:\\d\\d,\\d\\d\\d)"); - - long prevTime = Long.MAX_VALUE; - - while((line = reader.readLine()) != null) - { - Matcher m = pattern.matcher(line); - if(m.matches()) - { - String timeStr = m.group(1); - long time = df.parse(timeStr).getTime(); - if(time - prevTime > 20000) - { - System.out.println(df.format(prevTime) + " - " + df.format(time)); - } - prevTime = time; - } - } - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class DefaultDescribedTypeConstructor extends DescribedTypeConstructor +{ + private Object _descriptor; + + public DefaultDescribedTypeConstructor(final Object descriptor) + { + _descriptor = descriptor; + } + + public Object construct(final Object underlying) + { + return new DescribedType(_descriptor, underlying); + } + + + public static void main(String[] args) throws IOException, ParseException + { + LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in)); + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"); + String line; + Pattern pattern = Pattern.compile("^\\d+ (\\d{4}-\\d{2}-\\d{2} \\d\\d:\\d\\d:\\d\\d,\\d\\d\\d)"); + + long prevTime = Long.MAX_VALUE; + + while((line = reader.readLine()) != null) + { + Matcher m = pattern.matcher(line); + if(m.matches()) + { + String timeStr = m.group(1); + long time = df.parse(timeStr).getTime(); + if(time - prevTime > 20000) + { + System.out.println(df.format(prevTime) + " - " + df.format(time)); + } + prevTime = time; + } + } + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java index b11530d94f..87ef8456b3 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java @@ -1,52 +1,52 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class DelegatingValueWriter implements ValueWriter -{ - private ValueWriter _delegate; - private Registry _registry; - - - protected DelegatingValueWriter(final Registry registry) - { - _registry = registry; - } - - public int writeToBuffer(final ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(final V frameBody) - { - _delegate = _registry.getValueWriter(getUnderlyingValue(frameBody)); - } - - protected abstract Object getUnderlyingValue(final V frameBody); - - public boolean isComplete() - { - return _delegate.isComplete(); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class DelegatingValueWriter implements ValueWriter +{ + private ValueWriter _delegate; + private Registry _registry; + + + protected DelegatingValueWriter(final Registry registry) + { + _registry = registry; + } + + public int writeToBuffer(final ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(final V frameBody) + { + _delegate = _registry.getValueWriter(getUnderlyingValue(frameBody)); + } + + protected abstract Object getUnderlyingValue(final V frameBody); + + public boolean isComplete() + { + return _delegate.isComplete(); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java index 2f171c49b2..d0f8dfffcc 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java @@ -1,85 +1,85 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -public class DescribedType -{ - private final Object _descriptor; - private final Object _described; - - public DescribedType(final Object descriptor, final Object described) - { - _descriptor = descriptor; - _described = described; - } - - public Object getDescriptor() - { - return _descriptor; - } - - public Object getDescribed() - { - return _described; - } - - @Override - public boolean equals(final Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - final DescribedType that = (DescribedType) o; - - if (_described != null ? !_described.equals(that._described) : that._described != null) - { - return false; - } - if (_descriptor != null ? !_descriptor.equals(that._descriptor) : that._descriptor != null) - { - return false; - } - - return true; - } - - @Override - public int hashCode() - { - int result = _descriptor != null ? _descriptor.hashCode() : 0; - result = 31 * result + (_described != null ? _described.hashCode() : 0); - return result; - } - - @Override - public String toString() - { - return "DescribedType{"+ _descriptor + - ", " + _described + - '}'; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +public class DescribedType +{ + private final Object _descriptor; + private final Object _described; + + public DescribedType(final Object descriptor, final Object described) + { + _descriptor = descriptor; + _described = described; + } + + public Object getDescriptor() + { + return _descriptor; + } + + public Object getDescribed() + { + return _described; + } + + @Override + public boolean equals(final Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + final DescribedType that = (DescribedType) o; + + if (_described != null ? !_described.equals(that._described) : that._described != null) + { + return false; + } + if (_descriptor != null ? !_descriptor.equals(that._descriptor) : that._descriptor != null) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = _descriptor != null ? _descriptor.hashCode() : 0; + result = 31 * result + (_described != null ? _described.hashCode() : 0); + return result; + } + + @Override + public String toString() + { + return "DescribedType{"+ _descriptor + + ", " + _described + + '}'; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java index 4093583441..e6746474bc 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java @@ -1,41 +1,41 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -import java.nio.ByteBuffer; - -public abstract class DescribedTypeConstructor -{ - public TypeConstructor construct(final TypeConstructor describedConstructor) throws AmqpErrorException - { - return new TypeConstructor() - { - public T construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException - { - return DescribedTypeConstructor.this.construct(describedConstructor.construct(in, handler)); - } - }; - } - - public abstract T construct(Object underlying); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; + +import java.nio.ByteBuffer; + +public abstract class DescribedTypeConstructor +{ + public TypeConstructor construct(final TypeConstructor describedConstructor) throws AmqpErrorException + { + return new TypeConstructor() + { + public T construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException + { + return DescribedTypeConstructor.this.construct(describedConstructor.construct(in, handler)); + } + }; + } + + public abstract T construct(Object underlying); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java index 38cfa0f5a7..f7b611c3a1 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java @@ -1,35 +1,35 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public interface DescribedTypeConstructorRegistry -{ - public static interface Source - { - public DescribedTypeConstructorRegistry getDescribedTypeRegistry(); - } - - void register(Object descriptor, DescribedTypeConstructor constructor); - - DescribedTypeConstructor getConstructor(Object descriptor); - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public interface DescribedTypeConstructorRegistry +{ + public static interface Source + { + public DescribedTypeConstructorRegistry getDescribedTypeRegistry(); + } + + void register(Object descriptor, DescribedTypeConstructor constructor); + + DescribedTypeConstructor getConstructor(Object descriptor); + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java index 439ad73875..ab2550face 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class DoubleTypeConstructor implements TypeConstructor -{ - private static final DoubleTypeConstructor INSTANCE = new DoubleTypeConstructor(); - - - public static DoubleTypeConstructor getInstance() - { - return INSTANCE; - } - - private DoubleTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=8) - { - return in.getDouble(); - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct double: insufficient input data"); - throw new AmqpErrorException(error); - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class DoubleTypeConstructor implements TypeConstructor +{ + private static final DoubleTypeConstructor INSTANCE = new DoubleTypeConstructor(); + + + public static DoubleTypeConstructor getInstance() + { + return INSTANCE; + } + + private DoubleTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=8) + { + return in.getDouble(); + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct double: insufficient input data"); + throw new AmqpErrorException(error); + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java index 372e739a51..6c17528cf8 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java @@ -1,54 +1,54 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public class DoubleWriter extends FixedEightWriter -{ - private static final byte FORMAT_CODE = (byte) 0x82; - - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - @Override - long convertValueToLong(Double value) - { - return Double.doubleToLongBits(value.doubleValue()); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new DoubleWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Double.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public class DoubleWriter extends FixedEightWriter +{ + private static final byte FORMAT_CODE = (byte) 0x82; + + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + @Override + long convertValueToLong(Double value) + { + return Double.doubleToLongBits(value.doubleValue()); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new DoubleWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Double.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java index c9cc0b72c3..2a68af9fae 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java @@ -1,108 +1,108 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class FixedEightWriter implements ValueWriter -{ - private int _written = 9; - private long _value; - - public final int writeToBuffer(ByteBuffer buffer) - { - int remaining = buffer.remaining(); - int written = _written; - switch(written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put(getFormatCode()); - remaining--; - written = 1; - } - else - { - break; - } - case 1: - if(remaining>=8) - { - buffer.putLong(_value); - written = 9; - break; - } - case 2: - case 3: - case 4: - case 5: - if(remaining >= 4) - { - buffer.putInt((int)((_value >> ((5-written)<<3)) & 0xFFFFFFFF )); - remaining-=4; - written+=4; - } - case 6: - case 7: - if(remaining >= 2 && written <= 7) - { - buffer.putShort((short)((_value >> ((7-written)<<3)) & 0xFFFF )); - remaining -= 2; - written += 2; - } - case 8: - if(remaining >=1 && written != 9) - { - buffer.put((byte)((_value >> ((8-written)<<3)) & 0xFF )); - written++; - } - - - } - _written = written; - - return 9; - } - - abstract byte getFormatCode(); - - public final void setValue(T value) - { - _written = 0; - _value = convertValueToLong(value); - } - - abstract long convertValueToLong(T value); - - public boolean isCacheable() - { - return true; - } - - public final boolean isComplete() - { - return _written == 9; - } - - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class FixedEightWriter implements ValueWriter +{ + private int _written = 9; + private long _value; + + public final int writeToBuffer(ByteBuffer buffer) + { + int remaining = buffer.remaining(); + int written = _written; + switch(written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put(getFormatCode()); + remaining--; + written = 1; + } + else + { + break; + } + case 1: + if(remaining>=8) + { + buffer.putLong(_value); + written = 9; + break; + } + case 2: + case 3: + case 4: + case 5: + if(remaining >= 4) + { + buffer.putInt((int)((_value >> ((5-written)<<3)) & 0xFFFFFFFF )); + remaining-=4; + written+=4; + } + case 6: + case 7: + if(remaining >= 2 && written <= 7) + { + buffer.putShort((short)((_value >> ((7-written)<<3)) & 0xFFFF )); + remaining -= 2; + written += 2; + } + case 8: + if(remaining >=1 && written != 9) + { + buffer.put((byte)((_value >> ((8-written)<<3)) & 0xFF )); + written++; + } + + + } + _written = written; + + return 9; + } + + abstract byte getFormatCode(); + + public final void setValue(T value) + { + _written = 0; + _value = convertValueToLong(value); + } + + abstract long convertValueToLong(T value); + + public boolean isCacheable() + { + return true; + } + + public final boolean isComplete() + { + return _written == 9; + } + + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java index 164a869299..eeacef7cd9 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java @@ -1,122 +1,122 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class FixedFourWriter implements ValueWriter -{ - private int _written = 5; - private int _value; - - public final int writeToBuffer(ByteBuffer buffer) - { - int remaining = buffer.remaining(); - int written = _written; - switch(written) - { - case 0: - if(remaining-- != 0) - { - buffer.put(getFormatCode()); - written = 1; - } - else - { - break; - } - case 1: - if(remaining>=4) - { - buffer.putInt(_value); - written = 5; - break; - } - else if(remaining-- != 0) - { - buffer.put((byte)((_value >> 24)&0xFF)); - written = 2; - } - else - { - break; - } - case 2: - if(remaining-- != 0) - { - buffer.put((byte)((_value >> 16)&0xFF)); - written = 3; - } - else - { - break; - } - case 3: - if(remaining-- != 0) - { - buffer.put((byte)((_value >> 8)&0xFF)); - written = 4; - } - else - { - break; - } - case 4: - if(remaining-- != 0) - { - buffer.put((byte)(_value&0xFF)); - written = 5; - } - - } - _written = written; - - return 5; - } - - abstract byte getFormatCode(); - - public final void setValue(T value) - { - if(_written==1) - { - // TODO - remove - System.out.println("Remove"); - } - _written = 0; - _value = convertValueToInt(value); - } - - abstract int convertValueToInt(T value); - - public boolean isCacheable() - { - return true; - } - - public final boolean isComplete() - { - return _written == 5; - } - - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class FixedFourWriter implements ValueWriter +{ + private int _written = 5; + private int _value; + + public final int writeToBuffer(ByteBuffer buffer) + { + int remaining = buffer.remaining(); + int written = _written; + switch(written) + { + case 0: + if(remaining-- != 0) + { + buffer.put(getFormatCode()); + written = 1; + } + else + { + break; + } + case 1: + if(remaining>=4) + { + buffer.putInt(_value); + written = 5; + break; + } + else if(remaining-- != 0) + { + buffer.put((byte)((_value >> 24)&0xFF)); + written = 2; + } + else + { + break; + } + case 2: + if(remaining-- != 0) + { + buffer.put((byte)((_value >> 16)&0xFF)); + written = 3; + } + else + { + break; + } + case 3: + if(remaining-- != 0) + { + buffer.put((byte)((_value >> 8)&0xFF)); + written = 4; + } + else + { + break; + } + case 4: + if(remaining-- != 0) + { + buffer.put((byte)(_value&0xFF)); + written = 5; + } + + } + _written = written; + + return 5; + } + + abstract byte getFormatCode(); + + public final void setValue(T value) + { + if(_written==1) + { + // TODO - remove + System.out.println("Remove"); + } + _written = 0; + _value = convertValueToInt(value); + } + + abstract int convertValueToInt(T value); + + public boolean isCacheable() + { + return true; + } + + public final boolean isComplete() + { + return _written == 5; + } + + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java index 20334595db..c71572ac45 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java @@ -1,150 +1,150 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class FixedSixteenWriter implements ValueWriter -{ - private int _written = 17; - private long _msb; - private long _lsb; - - public final int writeToBuffer(ByteBuffer buffer) - { - int remaining = buffer.remaining(); - int written = _written; - switch(written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put(getFormatCode()); - remaining--; - written = 1; - } - else - { - break; - } - case 1: - if(remaining>=8) - { - buffer.putLong(_msb); - written = 9; - break; - } - case 2: - case 3: - case 4: - case 5: - if(remaining >= 4) - { - buffer.putInt((int)((_msb >> ((5-written)<<3)) & 0xFFFFFFFF )); - remaining-=4; - written+=4; - } - case 6: - case 7: - if(remaining >= 2 && written <= 7) - { - buffer.putShort((short)((_msb >> ((7-written)<<3)) & 0xFFFF )); - remaining -= 2; - written += 2; - } - case 8: - if(remaining >=1 && written != 9) - { - buffer.put((byte)((_msb >> ((8-written)<<3)) & 0xFF )); - written++; - } - - - } - if(remaining != 0) - { - switch(written) - { - case 9: - if(remaining>=8) - { - buffer.putLong(_lsb); - written = 17; - break; - } - case 10: - case 11: - case 12: - case 13: - if(remaining >= 4) - { - buffer.putInt((int)((_lsb >> ((13-written)<<3)) & 0xFFFFFFFF )); - remaining-=4; - written+=4; - } - case 14: - case 15: - if(remaining >= 2 && written <= 15) - { - buffer.putShort((short)((_lsb >> ((15-written)<<3)) & 0xFFFF )); - remaining -= 2; - written += 2; - } - case 16: - if(remaining >=1 && written != 17) - { - buffer.put((byte)((_msb >> ((16-written)<<3)) & 0xFF )); - written++; - } - } - - } - - _written = written; - - return 17; - } - - abstract byte getFormatCode(); - - public final void setValue(T value) - { - _written = 0; - _msb = convertValueToMSB(value); - _lsb = convertValueToLSB(value); - } - - abstract long convertValueToMSB(T value); - abstract long convertValueToLSB(T value); - - public boolean isCacheable() - { - return true; - } - - public final boolean isComplete() - { - return _written == 17; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class FixedSixteenWriter implements ValueWriter +{ + private int _written = 17; + private long _msb; + private long _lsb; + + public final int writeToBuffer(ByteBuffer buffer) + { + int remaining = buffer.remaining(); + int written = _written; + switch(written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put(getFormatCode()); + remaining--; + written = 1; + } + else + { + break; + } + case 1: + if(remaining>=8) + { + buffer.putLong(_msb); + written = 9; + break; + } + case 2: + case 3: + case 4: + case 5: + if(remaining >= 4) + { + buffer.putInt((int)((_msb >> ((5-written)<<3)) & 0xFFFFFFFF )); + remaining-=4; + written+=4; + } + case 6: + case 7: + if(remaining >= 2 && written <= 7) + { + buffer.putShort((short)((_msb >> ((7-written)<<3)) & 0xFFFF )); + remaining -= 2; + written += 2; + } + case 8: + if(remaining >=1 && written != 9) + { + buffer.put((byte)((_msb >> ((8-written)<<3)) & 0xFF )); + written++; + } + + + } + if(remaining != 0) + { + switch(written) + { + case 9: + if(remaining>=8) + { + buffer.putLong(_lsb); + written = 17; + break; + } + case 10: + case 11: + case 12: + case 13: + if(remaining >= 4) + { + buffer.putInt((int)((_lsb >> ((13-written)<<3)) & 0xFFFFFFFF )); + remaining-=4; + written+=4; + } + case 14: + case 15: + if(remaining >= 2 && written <= 15) + { + buffer.putShort((short)((_lsb >> ((15-written)<<3)) & 0xFFFF )); + remaining -= 2; + written += 2; + } + case 16: + if(remaining >=1 && written != 17) + { + buffer.put((byte)((_msb >> ((16-written)<<3)) & 0xFF )); + written++; + } + } + + } + + _written = written; + + return 17; + } + + abstract byte getFormatCode(); + + public final void setValue(T value) + { + _written = 0; + _msb = convertValueToMSB(value); + _lsb = convertValueToLSB(value); + } + + abstract long convertValueToMSB(T value); + abstract long convertValueToLSB(T value); + + public boolean isCacheable() + { + return true; + } + + public final boolean isComplete() + { + return _written == 17; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java index f6da0490a6..a45523c2b2 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java @@ -1,96 +1,96 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class FixedTwoWriter implements ValueWriter -{ - private int _written = 3; - private short _value; - - public int writeToBuffer(ByteBuffer buffer) - { - - switch(_written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put(getFormatCode()); - } - else - { - break; - } - case 1: - - if(buffer.remaining()>1) - { - buffer.putShort(_value); - _written = 3; - } - else if(buffer.hasRemaining()) - { - buffer.put((byte) (0xFF & (_value >> 8))); - _written = 2; - } - else - { - _written = 1; - } - break; - case 2: - if(buffer.hasRemaining()) - { - buffer.put((byte)(0xFF & _value)); - } - - - } - - return 3; - } - - - public final void setValue(T value) - { - _written = 0; - _value = convertValueToShort(value); - } - - abstract short convertValueToShort(T value); - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _written == 3; - } - - abstract byte getFormatCode(); - - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class FixedTwoWriter implements ValueWriter +{ + private int _written = 3; + private short _value; + + public int writeToBuffer(ByteBuffer buffer) + { + + switch(_written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put(getFormatCode()); + } + else + { + break; + } + case 1: + + if(buffer.remaining()>1) + { + buffer.putShort(_value); + _written = 3; + } + else if(buffer.hasRemaining()) + { + buffer.put((byte) (0xFF & (_value >> 8))); + _written = 2; + } + else + { + _written = 1; + } + break; + case 2: + if(buffer.hasRemaining()) + { + buffer.put((byte)(0xFF & _value)); + } + + + } + + return 3; + } + + + public final void setValue(T value) + { + _written = 0; + _value = convertValueToShort(value); + } + + abstract short convertValueToShort(T value); + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _written == 3; + } + + abstract byte getFormatCode(); + + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java index 200fead74e..58b0b114f1 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class FloatTypeConstructor implements TypeConstructor -{ - private static final FloatTypeConstructor INSTANCE = new FloatTypeConstructor(); - - - public static FloatTypeConstructor getInstance() - { - return INSTANCE; - } - - private FloatTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=4) - { - return in.getFloat(); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct float: insufficient input data"); - throw new AmqpErrorException(error); - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class FloatTypeConstructor implements TypeConstructor +{ + private static final FloatTypeConstructor INSTANCE = new FloatTypeConstructor(); + + + public static FloatTypeConstructor getInstance() + { + return INSTANCE; + } + + private FloatTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=4) + { + return in.getFloat(); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct float: insufficient input data"); + throw new AmqpErrorException(error); + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java index 823e33c3f8..44d9fba737 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java @@ -1,54 +1,54 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public class FloatWriter extends FixedFourWriter -{ - private static final byte FORMAT_CODE = (byte)0x72; - - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - @Override - int convertValueToInt(Float value) - { - return Float.floatToIntBits(value.floatValue()); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new FloatWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Float.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public class FloatWriter extends FixedFourWriter +{ + private static final byte FORMAT_CODE = (byte)0x72; + + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + @Override + int convertValueToInt(Float value) + { + return Float.floatToIntBits(value.floatValue()); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new FloatWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Float.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java index 95e327852b..f1d33c9b38 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java @@ -1,260 +1,260 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.framing.AMQFrame; - -import java.nio.ByteBuffer; - -public class FrameWriter implements ValueWriter -{ - private Registry _registry; - private AMQFrame _frame; - private State _state = State.DONE; - private ValueWriter _typeWriter; - private int _size = -1; - private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {}; - private ByteBuffer _payload; - - enum State - { - SIZE_0, - SIZE_1, - SIZE_2, - SIZE_3, - DOFF, - TYPE, - CHANNEL_0, - CHANNEL_1, - DELEGATE, - PAYLOAD, - DONE - } - - public FrameWriter(final Registry registry) - { - _registry = registry; - } - - public boolean isComplete() - { - return _state == State.DONE; - } - - public boolean isCacheable() - { - return false; - } - - public int writeToBuffer(ByteBuffer buffer) - { - int remaining; - - - - while((remaining = buffer.remaining()) != 0 && _state != State.DONE) - { - switch(_state) - { - case SIZE_0: - - int payloadLength = _payload == null ? 0 : _payload.remaining(); - - if(_typeWriter!=null) - { - _typeWriter.setValue(_frame.getFrameBody()); - - - _size = _typeWriter.writeToBuffer(remaining > 8 - ? (ByteBuffer)buffer.duplicate().position(buffer.position()+8) - : ByteBuffer.wrap(EMPTY_BYTE_ARRAY)) + 8 + payloadLength; - } - else - { - _size = 8 + payloadLength; - } - if(remaining >= 4) - { - buffer.putInt(_size); - - if(remaining >= 8) - { - buffer.put((byte)2); // DOFF - buffer.put(_frame.getFrameType()); // AMQP Frame Type - buffer.putShort(_frame.getChannel()); - - if(_size - payloadLength > remaining) - { - buffer.position(buffer.limit()); - _state = State.DELEGATE; - } - else if(_size > remaining ) - { - buffer.position(buffer.position()+_size-8-payloadLength); - if(payloadLength > 0) - { - - ByteBuffer dup = _payload.slice(); - int payloadUsed = buffer.remaining(); - dup.limit(payloadUsed); - buffer.put(dup); - _payload.position(_payload.position()+payloadUsed); - } - _state = State.PAYLOAD; - } - else - { - - buffer.position(buffer.position()+_size-8-payloadLength); - if(payloadLength > 0) - { - buffer.put(_payload); - } - _state = State.DONE; - } - - } - else - { - _state = State.DOFF; - } - break; - } - else - { - buffer.put((byte)((_size >> 24) & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.SIZE_1; - break; - } - } - - case SIZE_1: - buffer.put((byte)((_size >> 16) & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.SIZE_2; - break; - } - case SIZE_2: - buffer.put((byte)((_size >> 8) & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.SIZE_3; - break; - } - case SIZE_3: - buffer.put((byte)(_size & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.DOFF; - break; - } - case DOFF: - buffer.put((byte)2); // Always 2 (8 bytes) - if(!buffer.hasRemaining()) - { - _state = State.TYPE; - break; - } - case TYPE: - buffer.put((byte)0); - if(!buffer.hasRemaining()) - { - _state = State.CHANNEL_0; - break; - } - case CHANNEL_0: - buffer.put((byte)((_frame.getChannel() >> 8) & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.CHANNEL_1; - break; - } - case CHANNEL_1: - buffer.put((byte)(_frame.getChannel() & 0xFF)); - if(!buffer.hasRemaining()) - { - _state = State.DELEGATE; - break; - } - case DELEGATE: - _typeWriter.writeToBuffer(buffer); - if(_typeWriter.isComplete()) - { - _state = State.PAYLOAD; - _frame = null; - _typeWriter = null; - } - else - { - break; - } - case PAYLOAD: - if(_payload == null || _payload.remaining() == 0) - { - _state = State.DONE; - _frame = null; - _typeWriter = null; - _payload = null; - - } - else if(buffer.hasRemaining()) - { - buffer.put(_payload); - if(_payload.remaining() == 0) - { - _state = State.DONE; - _frame = null; - _typeWriter = null; - _payload = null; - } - } - - } - } - if(_size == -1) - { - _size = _typeWriter.writeToBuffer(ByteBuffer.wrap(EMPTY_BYTE_ARRAY)) + 8 + (_payload == null ? 0 : _payload.remaining()); - } - return _size; - } - - public void setValue(AMQFrame frame) - { - _frame = frame; - _state = State.SIZE_0; - _size = -1; - _payload = null; - final Object frameBody = frame.getFrameBody(); - if(frameBody!=null) - { - _typeWriter = _registry.getValueWriter(frameBody); - } - else - { - _typeWriter = null; - } - _payload = frame.getPayload() == null ? null : frame.getPayload().duplicate(); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.framing.AMQFrame; + +import java.nio.ByteBuffer; + +public class FrameWriter implements ValueWriter +{ + private Registry _registry; + private AMQFrame _frame; + private State _state = State.DONE; + private ValueWriter _typeWriter; + private int _size = -1; + private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {}; + private ByteBuffer _payload; + + enum State + { + SIZE_0, + SIZE_1, + SIZE_2, + SIZE_3, + DOFF, + TYPE, + CHANNEL_0, + CHANNEL_1, + DELEGATE, + PAYLOAD, + DONE + } + + public FrameWriter(final Registry registry) + { + _registry = registry; + } + + public boolean isComplete() + { + return _state == State.DONE; + } + + public boolean isCacheable() + { + return false; + } + + public int writeToBuffer(ByteBuffer buffer) + { + int remaining; + + + + while((remaining = buffer.remaining()) != 0 && _state != State.DONE) + { + switch(_state) + { + case SIZE_0: + + int payloadLength = _payload == null ? 0 : _payload.remaining(); + + if(_typeWriter!=null) + { + _typeWriter.setValue(_frame.getFrameBody()); + + + _size = _typeWriter.writeToBuffer(remaining > 8 + ? (ByteBuffer)buffer.duplicate().position(buffer.position()+8) + : ByteBuffer.wrap(EMPTY_BYTE_ARRAY)) + 8 + payloadLength; + } + else + { + _size = 8 + payloadLength; + } + if(remaining >= 4) + { + buffer.putInt(_size); + + if(remaining >= 8) + { + buffer.put((byte)2); // DOFF + buffer.put(_frame.getFrameType()); // AMQP Frame Type + buffer.putShort(_frame.getChannel()); + + if(_size - payloadLength > remaining) + { + buffer.position(buffer.limit()); + _state = State.DELEGATE; + } + else if(_size > remaining ) + { + buffer.position(buffer.position()+_size-8-payloadLength); + if(payloadLength > 0) + { + + ByteBuffer dup = _payload.slice(); + int payloadUsed = buffer.remaining(); + dup.limit(payloadUsed); + buffer.put(dup); + _payload.position(_payload.position()+payloadUsed); + } + _state = State.PAYLOAD; + } + else + { + + buffer.position(buffer.position()+_size-8-payloadLength); + if(payloadLength > 0) + { + buffer.put(_payload); + } + _state = State.DONE; + } + + } + else + { + _state = State.DOFF; + } + break; + } + else + { + buffer.put((byte)((_size >> 24) & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.SIZE_1; + break; + } + } + + case SIZE_1: + buffer.put((byte)((_size >> 16) & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.SIZE_2; + break; + } + case SIZE_2: + buffer.put((byte)((_size >> 8) & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.SIZE_3; + break; + } + case SIZE_3: + buffer.put((byte)(_size & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.DOFF; + break; + } + case DOFF: + buffer.put((byte)2); // Always 2 (8 bytes) + if(!buffer.hasRemaining()) + { + _state = State.TYPE; + break; + } + case TYPE: + buffer.put((byte)0); + if(!buffer.hasRemaining()) + { + _state = State.CHANNEL_0; + break; + } + case CHANNEL_0: + buffer.put((byte)((_frame.getChannel() >> 8) & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.CHANNEL_1; + break; + } + case CHANNEL_1: + buffer.put((byte)(_frame.getChannel() & 0xFF)); + if(!buffer.hasRemaining()) + { + _state = State.DELEGATE; + break; + } + case DELEGATE: + _typeWriter.writeToBuffer(buffer); + if(_typeWriter.isComplete()) + { + _state = State.PAYLOAD; + _frame = null; + _typeWriter = null; + } + else + { + break; + } + case PAYLOAD: + if(_payload == null || _payload.remaining() == 0) + { + _state = State.DONE; + _frame = null; + _typeWriter = null; + _payload = null; + + } + else if(buffer.hasRemaining()) + { + buffer.put(_payload); + if(_payload.remaining() == 0) + { + _state = State.DONE; + _frame = null; + _typeWriter = null; + _payload = null; + } + } + + } + } + if(_size == -1) + { + _size = _typeWriter.writeToBuffer(ByteBuffer.wrap(EMPTY_BYTE_ARRAY)) + 8 + (_payload == null ? 0 : _payload.remaining()); + } + return _size; + } + + public void setValue(AMQFrame frame) + { + _frame = frame; + _state = State.SIZE_0; + _size = -1; + _payload = null; + final Object frameBody = frame.getFrameBody(); + if(frameBody!=null) + { + _typeWriter = _registry.getValueWriter(frameBody); + } + else + { + _typeWriter = null; + } + _payload = frame.getPayload() == null ? null : frame.getPayload().duplicate(); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java index b3e774de5c..4eef4933d4 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class IntTypeConstructor implements TypeConstructor -{ - private static final IntTypeConstructor INSTANCE = new IntTypeConstructor(); - - - public static IntTypeConstructor getInstance() - { - return INSTANCE; - } - - private IntTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=4) - { - return in.getInt(); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct int: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class IntTypeConstructor implements TypeConstructor +{ + private static final IntTypeConstructor INSTANCE = new IntTypeConstructor(); + + + public static IntTypeConstructor getInstance() + { + return INSTANCE; + } + + private IntTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=4) + { + return in.getInt(); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct int: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java index 91c3151494..271ea8a23b 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java @@ -1,106 +1,106 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class IntegerWriter implements ValueWriter -{ - private static final byte EIGHT_BYTE_FORMAT_CODE = (byte)0x71; - private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x54; - - private ValueWriter _delegate; - - private final FixedFourWriter _eightByteWriter = new FixedFourWriter() - { - - @Override - byte getFormatCode() - { - return EIGHT_BYTE_FORMAT_CODE; - } - - @Override - int convertValueToInt(Integer value) - { - return value.intValue(); - } - }; - - private final ValueWriter _oneByteWriter = new FixedOneWriter() - { - - @Override protected byte getFormatCode() - { - return ONE_BYTE_FORMAT_CODE; - } - - @Override protected byte convertToByte(final Integer value) - { - return value.byteValue(); - } - }; - - - public int writeToBuffer(final ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(final Integer i) - { - if(i >= -128 && i <= 127) - { - _delegate = _oneByteWriter; - } - else - { - _delegate = _eightByteWriter; - } - _delegate.setValue(i); - } - - public boolean isComplete() - { - return _delegate.isComplete(); - } - - public boolean isCacheable() - { - return false; - } - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new IntegerWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Integer.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class IntegerWriter implements ValueWriter +{ + private static final byte EIGHT_BYTE_FORMAT_CODE = (byte)0x71; + private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x54; + + private ValueWriter _delegate; + + private final FixedFourWriter _eightByteWriter = new FixedFourWriter() + { + + @Override + byte getFormatCode() + { + return EIGHT_BYTE_FORMAT_CODE; + } + + @Override + int convertValueToInt(Integer value) + { + return value.intValue(); + } + }; + + private final ValueWriter _oneByteWriter = new FixedOneWriter() + { + + @Override protected byte getFormatCode() + { + return ONE_BYTE_FORMAT_CODE; + } + + @Override protected byte convertToByte(final Integer value) + { + return value.byteValue(); + } + }; + + + public int writeToBuffer(final ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(final Integer i) + { + if(i >= -128 && i <= 127) + { + _delegate = _oneByteWriter; + } + else + { + _delegate = _eightByteWriter; + } + _delegate.setValue(i); + } + + public boolean isComplete() + { + return _delegate.isComplete(); + } + + public boolean isCacheable() + { + return false; + } + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new IntegerWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Integer.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java index 3e0164705f..a726c268fd 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java @@ -1,172 +1,172 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; -import java.util.List; - -public class ListWriter implements ValueWriter -{ - private static class NonEmptyListWriter extends AbstractListWriter - { - private List _list; - private int _position = 0; - - public NonEmptyListWriter(final Registry registry) - { - super(registry); - } - - @Override - protected void onSetValue(final List value) - { - _list = value; - _position = 0; - - } - - @Override - protected int getCount() - { - return _list.size(); - } - - @Override - protected boolean hasNext() - { - return _position < getCount(); - } - - @Override - protected Object next() - { - return _list.get(_position++); - } - - @Override - protected void clear() - { - _list = null; - _position = 0; - } - - @Override - protected void reset() - { - _position = 0; - } - - } - - private final NonEmptyListWriter _nonEmptyListWriter; - private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x45; - - private final ValueWriter _emptyListWriter = new EmptyListValueWriter(); - - - private ValueWriter _delegate; - - public ListWriter(final Registry registry) - { - _nonEmptyListWriter = new NonEmptyListWriter(registry); - - } - - - public int writeToBuffer(ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(List frameBody) - { - if(frameBody.isEmpty()) - { - _delegate = _emptyListWriter; - } - else - { - _delegate = _nonEmptyListWriter; - } - _delegate.setValue(frameBody); - } - - public boolean isComplete() - { - return _delegate.isComplete(); - } - - public boolean isCacheable() - { - return false; - } - - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ListWriter(registry); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(List.class, FACTORY); - } - - public static class EmptyListValueWriter implements ValueWriter - { - private boolean _complete; - - - public int writeToBuffer(ByteBuffer buffer) - { - - if(!_complete && buffer.hasRemaining()) - { - buffer.put(ZERO_BYTE_FORMAT_CODE); - _complete = true; - } - - return 1; - } - - public void setValue(List list) - { - _complete = false; - } - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _complete; - } - - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; +import java.util.List; + +public class ListWriter implements ValueWriter +{ + private static class NonEmptyListWriter extends AbstractListWriter + { + private List _list; + private int _position = 0; + + public NonEmptyListWriter(final Registry registry) + { + super(registry); + } + + @Override + protected void onSetValue(final List value) + { + _list = value; + _position = 0; + + } + + @Override + protected int getCount() + { + return _list.size(); + } + + @Override + protected boolean hasNext() + { + return _position < getCount(); + } + + @Override + protected Object next() + { + return _list.get(_position++); + } + + @Override + protected void clear() + { + _list = null; + _position = 0; + } + + @Override + protected void reset() + { + _position = 0; + } + + } + + private final NonEmptyListWriter _nonEmptyListWriter; + private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x45; + + private final ValueWriter _emptyListWriter = new EmptyListValueWriter(); + + + private ValueWriter _delegate; + + public ListWriter(final Registry registry) + { + _nonEmptyListWriter = new NonEmptyListWriter(registry); + + } + + + public int writeToBuffer(ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(List frameBody) + { + if(frameBody.isEmpty()) + { + _delegate = _emptyListWriter; + } + else + { + _delegate = _nonEmptyListWriter; + } + _delegate.setValue(frameBody); + } + + public boolean isComplete() + { + return _delegate.isComplete(); + } + + public boolean isCacheable() + { + return false; + } + + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ListWriter(registry); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(List.class, FACTORY); + } + + public static class EmptyListValueWriter implements ValueWriter + { + private boolean _complete; + + + public int writeToBuffer(ByteBuffer buffer) + { + + if(!_complete && buffer.hasRemaining()) + { + buffer.put(ZERO_BYTE_FORMAT_CODE); + _complete = true; + } + + return 1; + } + + public void setValue(List list) + { + _complete = false; + } + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _complete; + } + + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java index b9a4509a04..edfa9f600f 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class LongTypeConstructor implements TypeConstructor -{ - private static final LongTypeConstructor INSTANCE = new LongTypeConstructor(); - - - public static LongTypeConstructor getInstance() - { - return INSTANCE; - } - - private LongTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=8) - { - return in.getLong(); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct long: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class LongTypeConstructor implements TypeConstructor +{ + private static final LongTypeConstructor INSTANCE = new LongTypeConstructor(); + + + public static LongTypeConstructor getInstance() + { + return INSTANCE; + } + + private LongTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=8) + { + return in.getLong(); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct long: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java index 984775cc74..56b9ac70a5 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java @@ -1,111 +1,111 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class LongWriter implements ValueWriter -{ - private static final byte EIGHT_BYTE_FORMAT_CODE = (byte) 0x81; - - - private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x55; - - private ValueWriter _delegate; - - private final FixedEightWriter _eightByteWriter = new FixedEightWriter() - { - - @Override - byte getFormatCode() - { - return EIGHT_BYTE_FORMAT_CODE; - } - - @Override - long convertValueToLong(Long value) - { - return value; - } - - }; - - private final ValueWriter _oneByteWriter = new FixedOneWriter() - { - - @Override protected byte getFormatCode() - { - return ONE_BYTE_FORMAT_CODE; - } - - @Override protected byte convertToByte(final Long value) - { - return value.byteValue(); - } - }; - - public int writeToBuffer(final ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(final Long l) - { - if(l >= -128 && l <= 127) - { - _delegate = _oneByteWriter; - } - else - { - _delegate = _eightByteWriter; - } - _delegate.setValue(l); - } - - public boolean isComplete() - { - return _delegate.isComplete(); - } - - public boolean isCacheable() - { - return false; - } - - - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new LongWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Long.class, FACTORY); - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class LongWriter implements ValueWriter +{ + private static final byte EIGHT_BYTE_FORMAT_CODE = (byte) 0x81; + + + private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x55; + + private ValueWriter _delegate; + + private final FixedEightWriter _eightByteWriter = new FixedEightWriter() + { + + @Override + byte getFormatCode() + { + return EIGHT_BYTE_FORMAT_CODE; + } + + @Override + long convertValueToLong(Long value) + { + return value; + } + + }; + + private final ValueWriter _oneByteWriter = new FixedOneWriter() + { + + @Override protected byte getFormatCode() + { + return ONE_BYTE_FORMAT_CODE; + } + + @Override protected byte convertToByte(final Long value) + { + return value.byteValue(); + } + }; + + public int writeToBuffer(final ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(final Long l) + { + if(l >= -128 && l <= 127) + { + _delegate = _oneByteWriter; + } + else + { + _delegate = _eightByteWriter; + } + _delegate.setValue(l); + } + + public boolean isComplete() + { + return _delegate.isComplete(); + } + + public boolean isCacheable() + { + return false; + } + + + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new LongWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Long.class, FACTORY); + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java index b239d4a397..5f4f6c2d2e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java @@ -1,102 +1,102 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.util.Iterator; -import java.util.Map; - -public class MapWriter extends AbstractMapWriter -{ - private Map _map; - private Object _value; - private Iterator _iterator; - - public MapWriter(final Registry registry) - { - super(registry); - } - - @Override - protected void onSetValue(final Map value) - { - _map = value; - _iterator = value.entrySet().iterator(); - } - - @Override - protected int getMapCount() - { - return _map.size(); - } - - @Override - protected boolean hasMapNext() - { - return _iterator.hasNext(); - } - - @Override - protected Object nextKey() - { - Map.Entry entry = _iterator.next(); - _value = entry.getValue(); - return entry.getKey(); - } - @Override - protected Object nextValue() - { - Object value = _value; - _value = null; - return value; - } - - - @Override - protected void onClear() - { - _map = null; - _iterator = null; - _value = null; - } - - @Override - protected void onReset() - { - _iterator = _map.entrySet().iterator(); - _value = null; - } - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new MapWriter(registry); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Map.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.util.Iterator; +import java.util.Map; + +public class MapWriter extends AbstractMapWriter +{ + private Map _map; + private Object _value; + private Iterator _iterator; + + public MapWriter(final Registry registry) + { + super(registry); + } + + @Override + protected void onSetValue(final Map value) + { + _map = value; + _iterator = value.entrySet().iterator(); + } + + @Override + protected int getMapCount() + { + return _map.size(); + } + + @Override + protected boolean hasMapNext() + { + return _iterator.hasNext(); + } + + @Override + protected Object nextKey() + { + Map.Entry entry = _iterator.next(); + _value = entry.getValue(); + return entry.getKey(); + } + @Override + protected Object nextValue() + { + Object value = _value; + _value = null; + return value; + } + + + @Override + protected void onClear() + { + _map = null; + _iterator = null; + _value = null; + } + + @Override + protected void onReset() + { + _iterator = _map.entrySet().iterator(); + _value = null; + } + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new MapWriter(registry); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Map.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/NullWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/NullWriter.java index 690a2222ab..cdc0ea3386 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/NullWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/NullWriter.java @@ -1,70 +1,70 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public class NullWriter implements ValueWriter -{ - private boolean _complete = true; - - public int writeToBuffer(ByteBuffer buffer) - { - - if(!_complete && buffer.hasRemaining()) - { - buffer.put((byte)0x40); - _complete = true; - } - - return 1; - } - - public void setValue(Void frameBody) - { - _complete = false; - } - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _complete; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new NullWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Void.TYPE, FACTORY); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public class NullWriter implements ValueWriter +{ + private boolean _complete = true; + + public int writeToBuffer(ByteBuffer buffer) + { + + if(!_complete && buffer.hasRemaining()) + { + buffer.put((byte)0x40); + _complete = true; + } + + return 1; + } + + public void setValue(Void frameBody) + { + _complete = false; + } + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _complete; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new NullWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Void.TYPE, FACTORY); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHandler.java index a954c6db50..e792cb45a8 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHandler.java @@ -1,30 +1,30 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public interface ProtocolHandler -{ - ProtocolHandler parse(ByteBuffer in); - - boolean isDone(); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public interface ProtocolHandler +{ + ProtocolHandler parse(ByteBuffer in); + + boolean isDone(); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHeaderHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHeaderHandler.java index f72fea56b2..504f3bdfa2 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHeaderHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ProtocolHeaderHandler.java @@ -1,146 +1,146 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.framing.AMQPProtocolHeaderHandler; -import org.apache.qpid.amqp_1_0.framing.SASLProtocolHeaderHandler; - -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; - -import java.nio.ByteBuffer; - -public class ProtocolHeaderHandler implements ProtocolHandler -{ - private final ConnectionEndpoint _connection; - private ProtocolHandler[] _protocolHandlers = new ProtocolHandler[4]; - private boolean _done; - - enum State { - AWAITING_A, - AWAITING_M, - AWAITING_Q, - AWAITING_P, - AWAITING_PROTOCOL_ID, - ERROR - } - - private State _state = State.AWAITING_A; - - public ProtocolHeaderHandler(final ConnectionEndpoint connection) - { - _connection = connection; - _protocolHandlers[0] = new AMQPProtocolHeaderHandler(connection); - _protocolHandlers[1] = null ; // historic apache.qpid.amqp_1_0 - _protocolHandlers[2] = null ; // TLS - _protocolHandlers[3] = new SASLProtocolHeaderHandler(connection); // SASL - - - } - - public ProtocolHandler parse(final ByteBuffer in) - { - if(!in.hasRemaining()) - { - return this; - } - - switch(_state) - { - case AWAITING_A: - if(transition(in, (byte)'A', State.AWAITING_M)) - { - break; - } - case AWAITING_M: - if(transition(in, (byte)'M', State.AWAITING_Q)) - { - break; - } - case AWAITING_Q: - if(transition(in, (byte)'Q', State.AWAITING_P)) - { - break; - } - - case AWAITING_P: - if(transition(in, (byte)'P', State.AWAITING_PROTOCOL_ID)) - { - break; - } - case AWAITING_PROTOCOL_ID: - int protocolId = ((int) in.get()) & 0xff; - ProtocolHandler delegate; - - try - { - delegate = _protocolHandlers[protocolId]; - } - catch(IndexOutOfBoundsException e) - { - delegate = null; - } - - if(delegate == null) - { - _state = State.ERROR; - } - else - { - return delegate.parse(in); - } - } - if(_state == State.ERROR) - { - _connection.invalidHeaderReceived(); - _done = true; - } - return this; - - } - - boolean transition(ByteBuffer in, byte expected, State next) - { - byte b = in.get(); - if(b == expected) - { - if(!in.hasRemaining()) - { - _state = next; - return true; - } - else - { - return false; - } - } - else - { - _state = State.ERROR; - return true; - } - } - - - public boolean isDone() - { - return _done; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.framing.AMQPProtocolHeaderHandler; +import org.apache.qpid.amqp_1_0.framing.SASLProtocolHeaderHandler; + +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; + +import java.nio.ByteBuffer; + +public class ProtocolHeaderHandler implements ProtocolHandler +{ + private final ConnectionEndpoint _connection; + private ProtocolHandler[] _protocolHandlers = new ProtocolHandler[4]; + private boolean _done; + + enum State { + AWAITING_A, + AWAITING_M, + AWAITING_Q, + AWAITING_P, + AWAITING_PROTOCOL_ID, + ERROR + } + + private State _state = State.AWAITING_A; + + public ProtocolHeaderHandler(final ConnectionEndpoint connection) + { + _connection = connection; + _protocolHandlers[0] = new AMQPProtocolHeaderHandler(connection); + _protocolHandlers[1] = null ; // historic apache.qpid.amqp_1_0 + _protocolHandlers[2] = null ; // TLS + _protocolHandlers[3] = new SASLProtocolHeaderHandler(connection); // SASL + + + } + + public ProtocolHandler parse(final ByteBuffer in) + { + if(!in.hasRemaining()) + { + return this; + } + + switch(_state) + { + case AWAITING_A: + if(transition(in, (byte)'A', State.AWAITING_M)) + { + break; + } + case AWAITING_M: + if(transition(in, (byte)'M', State.AWAITING_Q)) + { + break; + } + case AWAITING_Q: + if(transition(in, (byte)'Q', State.AWAITING_P)) + { + break; + } + + case AWAITING_P: + if(transition(in, (byte)'P', State.AWAITING_PROTOCOL_ID)) + { + break; + } + case AWAITING_PROTOCOL_ID: + int protocolId = ((int) in.get()) & 0xff; + ProtocolHandler delegate; + + try + { + delegate = _protocolHandlers[protocolId]; + } + catch(IndexOutOfBoundsException e) + { + delegate = null; + } + + if(delegate == null) + { + _state = State.ERROR; + } + else + { + return delegate.parse(in); + } + } + if(_state == State.ERROR) + { + _connection.invalidHeaderReceived(); + _done = true; + } + return this; + + } + + boolean transition(ByteBuffer in, byte expected, State next) + { + byte b = in.get(); + if(b == expected) + { + if(!in.hasRemaining()) + { + _state = next; + return true; + } + else + { + return false; + } + } + else + { + _state = State.ERROR; + return true; + } + } + + + public boolean isDone() + { + return _done; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/RestrictedTypeValueWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/RestrictedTypeValueWriter.java index cbc8277dc8..89821984f8 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/RestrictedTypeValueWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/RestrictedTypeValueWriter.java @@ -1,55 +1,55 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.RestrictedType; - -public class RestrictedTypeValueWriter extends DelegatingValueWriter> -{ - public RestrictedTypeValueWriter(final Registry registry) - { - super(registry); - } - - @Override - protected Object getUnderlyingValue(final RestrictedType restrictedType) - { - return restrictedType.getValue(); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new RestrictedTypeValueWriter(registry); - } - }; - - public boolean isCacheable() - { - return true; - } - - public static void register(ValueWriter.Registry registry, Class clazz) - { - registry.register(clazz, FACTORY); - }} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.RestrictedType; + +public class RestrictedTypeValueWriter extends DelegatingValueWriter> +{ + public RestrictedTypeValueWriter(final Registry registry) + { + super(registry); + } + + @Override + protected Object getUnderlyingValue(final RestrictedType restrictedType) + { + return restrictedType.getValue(); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new RestrictedTypeValueWriter(registry); + } + }; + + public boolean isCacheable() + { + return true; + } + + public static void register(ValueWriter.Registry registry, Class clazz) + { + registry.register(clazz, FACTORY); + }} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortTypeConstructor.java index 648ed36c69..de86dbaf64 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortTypeConstructor.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class ShortTypeConstructor implements TypeConstructor -{ - private static final ShortTypeConstructor INSTANCE = new ShortTypeConstructor(); - - - public static ShortTypeConstructor getInstance() - { - return INSTANCE; - } - - private ShortTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=2) - { - return in.getShort(); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct short: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class ShortTypeConstructor implements TypeConstructor +{ + private static final ShortTypeConstructor INSTANCE = new ShortTypeConstructor(); + + + public static ShortTypeConstructor getInstance() + { + return INSTANCE; + } + + private ShortTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=2) + { + return in.getShort(); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct short: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortWriter.java index 52ec577c3e..005cd17ae2 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ShortWriter.java @@ -1,55 +1,55 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -public class ShortWriter extends FixedTwoWriter -{ - - private static final byte FORMAT_CODE = (byte)0x61; - - - @Override - short convertValueToShort(Short value) - { - return value.shortValue(); - } - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new ShortWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Short.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +public class ShortWriter extends FixedTwoWriter +{ + + private static final byte FORMAT_CODE = (byte)0x61; + + + @Override + short convertValueToShort(Short value) + { + return value.shortValue(); + } + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new ShortWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Short.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SimpleVariableWidthWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SimpleVariableWidthWriter.java index bd14483361..415b5973a5 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SimpleVariableWidthWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SimpleVariableWidthWriter.java @@ -1,68 +1,68 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class SimpleVariableWidthWriter extends VariableWidthWriter -{ - private byte[] _buf; - - - public void setValue(V value) - { - _buf = getByteArray(value); - super.setValue(value); - } - - protected int getLength() - { - return _buf.length; - } - - protected void writeBytes(ByteBuffer buf, int offset, int length) - { - buf.put(_buf, getOffset()+offset, length); - } - - @Override - protected void clearValue() - { - _buf = null; - } - - @Override - protected boolean hasValue() - { - return _buf != null; - } - - public boolean isCacheable() - { - return true; - } - - protected abstract byte[] getByteArray(V value); - - protected abstract int getOffset(); - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class SimpleVariableWidthWriter extends VariableWidthWriter +{ + private byte[] _buf; + + + public void setValue(V value) + { + _buf = getByteArray(value); + super.setValue(value); + } + + protected int getLength() + { + return _buf.length; + } + + protected void writeBytes(ByteBuffer buf, int offset, int length) + { + buf.put(_buf, getOffset()+offset, length); + } + + @Override + protected void clearValue() + { + _buf = null; + } + + @Override + protected boolean hasValue() + { + return _buf != null; + } + + public boolean isCacheable() + { + return true; + } + + protected abstract byte[] getByteArray(V value); + + protected abstract int getOffset(); + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallIntConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallIntConstructor.java index 07fa853dce..652285c0da 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallIntConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallIntConstructor.java @@ -1,58 +1,58 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class SmallIntConstructor implements TypeConstructor -{ - private static final SmallIntConstructor INSTANCE = new SmallIntConstructor(); - - - public static SmallIntConstructor getInstance() - { - return INSTANCE; - } - - private SmallIntConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - byte b = in.get(); - return (int) b; - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct int: insufficient input data"); - throw new AmqpErrorException(error); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class SmallIntConstructor implements TypeConstructor +{ + private static final SmallIntConstructor INSTANCE = new SmallIntConstructor(); + + + public static SmallIntConstructor getInstance() + { + return INSTANCE; + } + + private SmallIntConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + byte b = in.get(); + return (int) b; + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct int: insufficient input data"); + throw new AmqpErrorException(error); + } + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallLongConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallLongConstructor.java index 013b9bef64..e7258b30e9 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallLongConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallLongConstructor.java @@ -1,58 +1,58 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class SmallLongConstructor implements TypeConstructor -{ - private static final SmallLongConstructor INSTANCE = new SmallLongConstructor(); - - - public static SmallLongConstructor getInstance() - { - return INSTANCE; - } - - private SmallLongConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - byte b = in.get(); - return (long) b; - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct long: insufficient input data"); - throw new AmqpErrorException(error); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class SmallLongConstructor implements TypeConstructor +{ + private static final SmallLongConstructor INSTANCE = new SmallLongConstructor(); + + + public static SmallLongConstructor getInstance() + { + return INSTANCE; + } + + private SmallLongConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + byte b = in.get(); + return (long) b; + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct long: insufficient input data"); + throw new AmqpErrorException(error); + } + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallUIntConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallUIntConstructor.java index b3a9e3ef19..fd2f41fa59 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallUIntConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallUIntConstructor.java @@ -1,58 +1,58 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.UnsignedInteger; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class SmallUIntConstructor implements TypeConstructor -{ - private static final SmallUIntConstructor INSTANCE = new SmallUIntConstructor(); - - - public static SmallUIntConstructor getInstance() - { - return INSTANCE; - } - - private SmallUIntConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - byte b = in.get(); - return UnsignedInteger.valueOf(((int) b) & 0xff); - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct uint: insufficient input data"); - throw new AmqpErrorException(error); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.UnsignedInteger; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class SmallUIntConstructor implements TypeConstructor +{ + private static final SmallUIntConstructor INSTANCE = new SmallUIntConstructor(); + + + public static SmallUIntConstructor getInstance() + { + return INSTANCE; + } + + private SmallUIntConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + byte b = in.get(); + return UnsignedInteger.valueOf(((int) b) & 0xff); + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct uint: insufficient input data"); + throw new AmqpErrorException(error); + } + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallULongConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallULongConstructor.java index 38cd0b65ac..2b49335e3e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallULongConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SmallULongConstructor.java @@ -1,59 +1,59 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.UnsignedLong; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; - -public class SmallULongConstructor implements TypeConstructor -{ - private static final SmallULongConstructor INSTANCE = new SmallULongConstructor(); - - - public static SmallULongConstructor getInstance() - { - return INSTANCE; - } - - private SmallULongConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - byte b = in.get(); - return UnsignedLong.valueOf(((long) b) & 0xffL); - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct ulong: insufficient input data"); - throw new AmqpErrorException(error); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.UnsignedLong; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; + +public class SmallULongConstructor implements TypeConstructor +{ + private static final SmallULongConstructor INSTANCE = new SmallULongConstructor(); + + + public static SmallULongConstructor getInstance() + { + return INSTANCE; + } + + private SmallULongConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + byte b = in.get(); + return UnsignedLong.valueOf(((long) b) & 0xffL); + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct ulong: insufficient input data"); + throw new AmqpErrorException(error); + } + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringTypeConstructor.java index 73b85d4163..e89f038f23 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringTypeConstructor.java @@ -1,132 +1,132 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.util.LinkedHashMap; -import java.util.Map; - -public class StringTypeConstructor extends VariableWidthTypeConstructor -{ - private Charset _charSet; - - private BinaryString _defaultBinaryString = new BinaryString(); - private ValueCache _cachedValues = new ValueCache(10); - - private static final class ValueCache extends LinkedHashMap - { - private final int _cacheSize; - - public ValueCache(int cacheSize) - { - _cacheSize = cacheSize; - } - - @Override - protected boolean removeEldestEntry(Map.Entry eldest) - { - return size() > _cacheSize; - } - - public boolean isFull() - { - return size() == _cacheSize; - } - } - - - public static StringTypeConstructor getInstance(int i, Charset c) - { - return new StringTypeConstructor(i, c); - } - - - private StringTypeConstructor(int size, Charset c) - { - super(size); - _charSet = c; - } - - @Override - public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException - { - int size; - - if(getSize() == 1) - { - size = in.get() & 0xFF; - } - else - { - size = in.getInt(); - } - - int origPosition = in.position(); - _defaultBinaryString.setData(in.array(), in.arrayOffset()+ origPosition, size); - - BinaryString binaryStr = _defaultBinaryString; - - boolean isFull = _cachedValues.isFull(); - - String str = isFull ? _cachedValues.remove(binaryStr) : _cachedValues.get(binaryStr); - - if(str == null) - { - - ByteBuffer dup = in.duplicate(); - try - { - dup.limit(dup.position()+size); - } - catch(IllegalArgumentException e) - { - throw new IllegalArgumentException("position: " + dup.position() + "size: " + size + " capacity: " + dup.capacity()); - } - CharBuffer charBuf = _charSet.decode(dup); - - str = charBuf.toString(); - - byte[] data = new byte[size]; - in.get(data); - binaryStr = new BinaryString(data, 0, size); - - _cachedValues.put(binaryStr, str); - } - else if(isFull) - { - byte[] data = new byte[size]; - in.get(data); - binaryStr = new BinaryString(data, 0, size); - - _cachedValues.put(binaryStr, str); - } - - in.position(origPosition+size); - - return str; - - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.util.LinkedHashMap; +import java.util.Map; + +public class StringTypeConstructor extends VariableWidthTypeConstructor +{ + private Charset _charSet; + + private BinaryString _defaultBinaryString = new BinaryString(); + private ValueCache _cachedValues = new ValueCache(10); + + private static final class ValueCache extends LinkedHashMap + { + private final int _cacheSize; + + public ValueCache(int cacheSize) + { + _cacheSize = cacheSize; + } + + @Override + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() > _cacheSize; + } + + public boolean isFull() + { + return size() == _cacheSize; + } + } + + + public static StringTypeConstructor getInstance(int i, Charset c) + { + return new StringTypeConstructor(i, c); + } + + + private StringTypeConstructor(int size, Charset c) + { + super(size); + _charSet = c; + } + + @Override + public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException + { + int size; + + if(getSize() == 1) + { + size = in.get() & 0xFF; + } + else + { + size = in.getInt(); + } + + int origPosition = in.position(); + _defaultBinaryString.setData(in.array(), in.arrayOffset()+ origPosition, size); + + BinaryString binaryStr = _defaultBinaryString; + + boolean isFull = _cachedValues.isFull(); + + String str = isFull ? _cachedValues.remove(binaryStr) : _cachedValues.get(binaryStr); + + if(str == null) + { + + ByteBuffer dup = in.duplicate(); + try + { + dup.limit(dup.position()+size); + } + catch(IllegalArgumentException e) + { + throw new IllegalArgumentException("position: " + dup.position() + "size: " + size + " capacity: " + dup.capacity()); + } + CharBuffer charBuf = _charSet.decode(dup); + + str = charBuf.toString(); + + byte[] data = new byte[size]; + in.get(data); + binaryStr = new BinaryString(data, 0, size); + + _cachedValues.put(binaryStr, str); + } + else if(isFull) + { + byte[] data = new byte[size]; + in.get(data); + binaryStr = new BinaryString(data, 0, size); + + _cachedValues.put(binaryStr, str); + } + + in.position(origPosition+size); + + return str; + + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringWriter.java index 39f759e54d..872d12070b 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/StringWriter.java @@ -1,146 +1,146 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.util.LinkedHashMap; -import java.util.Map; - -public class StringWriter extends SimpleVariableWidthWriter -{ - private static final Charset ENCODING_CHARSET; - private static final byte ONE_BYTE_CODE; - private static final byte FOUR_BYTE_CODE; - static - { - Charset defaultCharset = Charset.defaultCharset(); - if(defaultCharset.name().equals("UTF-16") || defaultCharset.name().equals("UTF-16BE")) - { - ENCODING_CHARSET = defaultCharset; - ONE_BYTE_CODE = (byte) 0xa2; - FOUR_BYTE_CODE = (byte) 0xb2; - } - else - { - ENCODING_CHARSET = Charset.forName("UTF-8"); - ONE_BYTE_CODE = (byte) 0xa1; - FOUR_BYTE_CODE = (byte) 0xb1; - } - } - - private static final class ValueCache extends LinkedHashMap - { - private final int _cacheSize; - - public ValueCache(int cacheSize) - { - _cacheSize = cacheSize; - } - - @Override - protected boolean removeEldestEntry(Map.Entry eldest) - { - return size() > _cacheSize; - } - - public boolean isFull() - { - return size() == _cacheSize; - } - } - - private final ValueCache _cachedEncodings = new ValueCache(10); - - - @Override - protected byte getFourOctetEncodingCode() - { - return FOUR_BYTE_CODE; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return ONE_BYTE_CODE; - } - - @Override - protected byte[] getByteArray(String value) - { - - byte[] encoding; - boolean isFull = _cachedEncodings.isFull(); - if(isFull) - { - encoding = _cachedEncodings.remove(value); - } - else - { - encoding = _cachedEncodings.get(value); - } - - - if(encoding == null) - { - ByteBuffer buf = ENCODING_CHARSET.encode(value); - if(buf.hasArray() && buf.arrayOffset() == 0 && buf.limit()==buf.capacity()) - { - encoding = buf.array(); - } - else - { - byte[] bufArray = new byte[buf.limit()-buf.position()]; - buf.get(bufArray); - encoding = bufArray; - } - _cachedEncodings.put(value,encoding); - - } - else if(isFull) - { - _cachedEncodings.put(value,encoding); - } - - return encoding; - } - - @Override - protected int getOffset() - { - return 0; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new StringWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(String.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.LinkedHashMap; +import java.util.Map; + +public class StringWriter extends SimpleVariableWidthWriter +{ + private static final Charset ENCODING_CHARSET; + private static final byte ONE_BYTE_CODE; + private static final byte FOUR_BYTE_CODE; + static + { + Charset defaultCharset = Charset.defaultCharset(); + if(defaultCharset.name().equals("UTF-16") || defaultCharset.name().equals("UTF-16BE")) + { + ENCODING_CHARSET = defaultCharset; + ONE_BYTE_CODE = (byte) 0xa2; + FOUR_BYTE_CODE = (byte) 0xb2; + } + else + { + ENCODING_CHARSET = Charset.forName("UTF-8"); + ONE_BYTE_CODE = (byte) 0xa1; + FOUR_BYTE_CODE = (byte) 0xb1; + } + } + + private static final class ValueCache extends LinkedHashMap + { + private final int _cacheSize; + + public ValueCache(int cacheSize) + { + _cacheSize = cacheSize; + } + + @Override + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() > _cacheSize; + } + + public boolean isFull() + { + return size() == _cacheSize; + } + } + + private final ValueCache _cachedEncodings = new ValueCache(10); + + + @Override + protected byte getFourOctetEncodingCode() + { + return FOUR_BYTE_CODE; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return ONE_BYTE_CODE; + } + + @Override + protected byte[] getByteArray(String value) + { + + byte[] encoding; + boolean isFull = _cachedEncodings.isFull(); + if(isFull) + { + encoding = _cachedEncodings.remove(value); + } + else + { + encoding = _cachedEncodings.get(value); + } + + + if(encoding == null) + { + ByteBuffer buf = ENCODING_CHARSET.encode(value); + if(buf.hasArray() && buf.arrayOffset() == 0 && buf.limit()==buf.capacity()) + { + encoding = buf.array(); + } + else + { + byte[] bufArray = new byte[buf.limit()-buf.position()]; + buf.get(bufArray); + encoding = bufArray; + } + _cachedEncodings.put(value,encoding); + + } + else if(isFull) + { + _cachedEncodings.put(value,encoding); + } + + return encoding; + } + + @Override + protected int getOffset() + { + return 0; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new StringWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(String.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolTypeConstructor.java index ce0f5cb26b..4049a40448 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolTypeConstructor.java @@ -1,109 +1,109 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.Symbol; - -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.util.concurrent.ConcurrentHashMap; - -public class SymbolTypeConstructor extends VariableWidthTypeConstructor -{ - private static final Charset ASCII = Charset.forName("US-ASCII"); - - private BinaryString _defaultBinaryString = new BinaryString(); - - private static final ConcurrentHashMap SYMBOL_MAP = - new ConcurrentHashMap(2048); - - public static SymbolTypeConstructor getInstance(int i) - { - return new SymbolTypeConstructor(i); - } - - - private SymbolTypeConstructor(int size) - { - super(size); - } - - @Override - public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException - { - int size; - - if(getSize() == 1) - { - size = in.get() & 0xFF; - } - else - { - size = in.getInt(); - } - - _defaultBinaryString.setData(in.array(), in.arrayOffset()+in.position(), size); - - BinaryString binaryStr = _defaultBinaryString; - - Symbol symbolVal = SYMBOL_MAP.get(binaryStr); - if(symbolVal == null) - { - ByteBuffer dup = in.duplicate(); - try - { - dup.limit(in.position()+size); - } - catch (IllegalArgumentException e) - { - System.err.println("in.position(): " + in.position()); - System.err.println("size: " + size); - System.err.println("dup.position(): " + dup.position()); - System.err.println("dup.capacity(): " + dup.capacity()); - System.err.println("dup.limit(): " + dup.limit()); - throw e; - - } - CharBuffer charBuf = ASCII.decode(dup); - - - symbolVal = Symbol.getSymbol(charBuf.toString()); - - - - - byte[] data = new byte[size]; - in.get(data); - binaryStr = new BinaryString(data, 0, size); - SYMBOL_MAP.putIfAbsent(binaryStr, symbolVal); - } - else - { - in.position(in.position()+size); - } - - return symbolVal; - - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.Symbol; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.util.concurrent.ConcurrentHashMap; + +public class SymbolTypeConstructor extends VariableWidthTypeConstructor +{ + private static final Charset ASCII = Charset.forName("US-ASCII"); + + private BinaryString _defaultBinaryString = new BinaryString(); + + private static final ConcurrentHashMap SYMBOL_MAP = + new ConcurrentHashMap(2048); + + public static SymbolTypeConstructor getInstance(int i) + { + return new SymbolTypeConstructor(i); + } + + + private SymbolTypeConstructor(int size) + { + super(size); + } + + @Override + public Object construct(final ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException + { + int size; + + if(getSize() == 1) + { + size = in.get() & 0xFF; + } + else + { + size = in.getInt(); + } + + _defaultBinaryString.setData(in.array(), in.arrayOffset()+in.position(), size); + + BinaryString binaryStr = _defaultBinaryString; + + Symbol symbolVal = SYMBOL_MAP.get(binaryStr); + if(symbolVal == null) + { + ByteBuffer dup = in.duplicate(); + try + { + dup.limit(in.position()+size); + } + catch (IllegalArgumentException e) + { + System.err.println("in.position(): " + in.position()); + System.err.println("size: " + size); + System.err.println("dup.position(): " + dup.position()); + System.err.println("dup.capacity(): " + dup.capacity()); + System.err.println("dup.limit(): " + dup.limit()); + throw e; + + } + CharBuffer charBuf = ASCII.decode(dup); + + + symbolVal = Symbol.getSymbol(charBuf.toString()); + + + + + byte[] data = new byte[size]; + in.get(data); + binaryStr = new BinaryString(data, 0, size); + SYMBOL_MAP.putIfAbsent(binaryStr, symbolVal); + } + else + { + in.position(in.position()+size); + } + + return symbolVal; + + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolWriter.java index ae43c7ebd2..0498e0ada6 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/SymbolWriter.java @@ -1,102 +1,102 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.Symbol; - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; - -public class SymbolWriter extends VariableWidthWriter -{ - private static final Charset ENCODING_CHARSET = Charset.forName("US-ASCII"); - public static final byte LARGE_ENCODING_CODE = (byte) 0xb3; - public static final byte SMALL_ENCODING_CODE = (byte) 0xa3; - private Symbol _value; - - - @Override - protected byte getFourOctetEncodingCode() - { - return LARGE_ENCODING_CODE; - } - - @Override - protected byte getSingleOctetEncodingCode() - { - return SMALL_ENCODING_CODE; - } - - @Override - public void setValue(Symbol value) - { - _value = value; - super.setValue(value); - } - - public boolean isCacheable() - { - return true; - } - - @Override - protected void clearValue() - { - _value = null; - } - - @Override - protected boolean hasValue() - { - return _value != null; - } - - @Override - protected int getLength() - { - return _value.length(); - } - - @Override - protected void writeBytes(ByteBuffer buf, int offset, int length) - { - int end = offset + length; - for(int i = offset; i < end; i++) - { - buf.put((byte)_value.charAt(i)); - } - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new SymbolWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Symbol.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.Symbol; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +public class SymbolWriter extends VariableWidthWriter +{ + private static final Charset ENCODING_CHARSET = Charset.forName("US-ASCII"); + public static final byte LARGE_ENCODING_CODE = (byte) 0xb3; + public static final byte SMALL_ENCODING_CODE = (byte) 0xa3; + private Symbol _value; + + + @Override + protected byte getFourOctetEncodingCode() + { + return LARGE_ENCODING_CODE; + } + + @Override + protected byte getSingleOctetEncodingCode() + { + return SMALL_ENCODING_CODE; + } + + @Override + public void setValue(Symbol value) + { + _value = value; + super.setValue(value); + } + + public boolean isCacheable() + { + return true; + } + + @Override + protected void clearValue() + { + _value = null; + } + + @Override + protected boolean hasValue() + { + return _value != null; + } + + @Override + protected int getLength() + { + return _value.length(); + } + + @Override + protected void writeBytes(ByteBuffer buf, int offset, int length) + { + int end = offset + length; + for(int i = offset; i < end; i++) + { + buf.put((byte)_value.charAt(i)); + } + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new SymbolWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Symbol.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampTypeConstructor.java index d826e0ee56..0dc8ab99ef 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampTypeConstructor.java @@ -1,60 +1,60 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; -import java.util.Date; - -public class TimestampTypeConstructor implements TypeConstructor -{ - private static final TimestampTypeConstructor INSTANCE = new TimestampTypeConstructor(); - - - public static TimestampTypeConstructor getInstance() - { - return INSTANCE; - } - - private TimestampTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=8) - { - long l = in.getLong(); - return new Date(l); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct timestamp: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; +import java.util.Date; + +public class TimestampTypeConstructor implements TypeConstructor +{ + private static final TimestampTypeConstructor INSTANCE = new TimestampTypeConstructor(); + + + public static TimestampTypeConstructor getInstance() + { + return INSTANCE; + } + + private TimestampTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=8) + { + long l = in.getLong(); + return new Date(l); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct timestamp: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampWriter.java index 153c51b7c7..dd8eccbdfb 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TimestampWriter.java @@ -1,57 +1,57 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.util.Date; - -public class TimestampWriter extends FixedEightWriter -{ - private static final byte FORMAT_CODE = (byte) 0x83; - - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - @Override - long convertValueToLong(Date value) - { - return value.getTime(); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new TimestampWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(Date.class, FACTORY); - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.util.Date; + +public class TimestampWriter extends FixedEightWriter +{ + private static final byte FORMAT_CODE = (byte) 0x83; + + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + @Override + long convertValueToLong(Date value) + { + return value.getTime(); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new TimestampWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(Date.class, FACTORY); + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TypeConstructor.java index 8b433e4b20..bd29be78f6 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/TypeConstructor.java @@ -1,32 +1,32 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -public interface TypeConstructor -{ - - public T construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException; - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; + +public interface TypeConstructor +{ + + public T construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException; + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UByteTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UByteTypeConstructor.java index 7ff5abc558..904c5c8d46 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UByteTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UByteTypeConstructor.java @@ -1,59 +1,59 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class UByteTypeConstructor implements TypeConstructor -{ - private static final UByteTypeConstructor INSTANCE = new UByteTypeConstructor(); - - - public static UByteTypeConstructor getInstance() - { - return INSTANCE; - } - - private UByteTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.hasRemaining()) - { - byte b = in.get(); - return UnsignedByte.valueOf(b); - } - else - { - Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct ubyte: insufficient input data"); - throw new AmqpErrorException(error); - } - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class UByteTypeConstructor implements TypeConstructor +{ + private static final UByteTypeConstructor INSTANCE = new UByteTypeConstructor(); + + + public static UByteTypeConstructor getInstance() + { + return INSTANCE; + } + + private UByteTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.hasRemaining()) + { + byte b = in.get(); + return UnsignedByte.valueOf(b); + } + else + { + Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct ubyte: insufficient input data"); + throw new AmqpErrorException(error); + } + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UIntTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UIntTypeConstructor.java index fc0c433d7c..1dbba651a2 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UIntTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UIntTypeConstructor.java @@ -1,59 +1,59 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class UIntTypeConstructor implements TypeConstructor -{ - private static final UIntTypeConstructor INSTANCE = new UIntTypeConstructor(); - - - public static UIntTypeConstructor getInstance() - { - return INSTANCE; - } - - private UIntTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=4) - { - final int i = in.getInt(); - return UnsignedInteger.valueOf(i); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct uint: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class UIntTypeConstructor implements TypeConstructor +{ + private static final UIntTypeConstructor INSTANCE = new UIntTypeConstructor(); + + + public static UIntTypeConstructor getInstance() + { + return INSTANCE; + } + + private UIntTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=4) + { + final int i = in.getInt(); + return UnsignedInteger.valueOf(i); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct uint: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ULongTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ULongTypeConstructor.java index 550a61b4fa..61f9922da6 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ULongTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ULongTypeConstructor.java @@ -1,61 +1,61 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class ULongTypeConstructor implements TypeConstructor -{ - private static final ULongTypeConstructor INSTANCE = new ULongTypeConstructor(); - - - public static ULongTypeConstructor getInstance() - { - return INSTANCE; - } - - private ULongTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=8) - { - long l = in.getLong(); - - return UnsignedLong.valueOf(l); - - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct ulong: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class ULongTypeConstructor implements TypeConstructor +{ + private static final ULongTypeConstructor INSTANCE = new ULongTypeConstructor(); + + + public static ULongTypeConstructor getInstance() + { + return INSTANCE; + } + + private ULongTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=8) + { + long l = in.getLong(); + + return UnsignedLong.valueOf(l); + + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new org.apache.qpid.amqp_1_0.type.transport.Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct ulong: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UShortTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UShortTypeConstructor.java index 6cf7cb5dca..a43c886549 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UShortTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UShortTypeConstructor.java @@ -1,60 +1,60 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; - -public class UShortTypeConstructor implements TypeConstructor -{ - private static final UShortTypeConstructor INSTANCE = new UShortTypeConstructor(); - - - public static UShortTypeConstructor getInstance() - { - return INSTANCE; - } - - private UShortTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=2) - { - short s = in.getShort(); - return UnsignedShort.valueOf(s); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct ushort: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; + +public class UShortTypeConstructor implements TypeConstructor +{ + private static final UShortTypeConstructor INSTANCE = new UShortTypeConstructor(); + + + public static UShortTypeConstructor getInstance() + { + return INSTANCE; + } + + private UShortTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=2) + { + short s = in.getShort(); + return UnsignedShort.valueOf(s); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct ushort: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDTypeConstructor.java index c34a8fca65..ed9eaeabcb 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDTypeConstructor.java @@ -1,62 +1,62 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; -import java.util.UUID; - -public class UUIDTypeConstructor implements TypeConstructor -{ - private static final UUIDTypeConstructor INSTANCE = new UUIDTypeConstructor(); - - - public static UUIDTypeConstructor getInstance() - { - return INSTANCE; - } - - private UUIDTypeConstructor() - { - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - if(in.remaining()>=16) - { - long msb = in.getLong(); - long lsb = in.getLong(); - return new UUID(msb, lsb); - } - else - { - org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("Cannot construct UUID: insufficient input data"); - throw new AmqpErrorException(error); - - } - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; +import java.util.UUID; + +public class UUIDTypeConstructor implements TypeConstructor +{ + private static final UUIDTypeConstructor INSTANCE = new UUIDTypeConstructor(); + + + public static UUIDTypeConstructor getInstance() + { + return INSTANCE; + } + + private UUIDTypeConstructor() + { + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + if(in.remaining()>=16) + { + long msb = in.getLong(); + long lsb = in.getLong(); + return new UUID(msb, lsb); + } + else + { + org.apache.qpid.amqp_1_0.type.transport.Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("Cannot construct UUID: insufficient input data"); + throw new AmqpErrorException(error); + + } + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDWriter.java index b900fd63ce..7ca6a80633 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UUIDWriter.java @@ -1,63 +1,63 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.util.UUID; - -public class UUIDWriter extends FixedSixteenWriter -{ - private static final byte FORMAT_CODE = (byte) 0x98; - - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - @Override - long convertValueToMSB(UUID value) - { - return value.getMostSignificantBits(); - } - - @Override - long convertValueToLSB(UUID value) - { - return value.getLeastSignificantBits(); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new UUIDWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(UUID.class, FACTORY); - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.util.UUID; + +public class UUIDWriter extends FixedSixteenWriter +{ + private static final byte FORMAT_CODE = (byte) 0x98; + + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + @Override + long convertValueToMSB(UUID value) + { + return value.getMostSignificantBits(); + } + + @Override + long convertValueToLSB(UUID value) + { + return value.getLeastSignificantBits(); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new UUIDWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(UUID.class, FACTORY); + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedByteWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedByteWriter.java index aedf3811d9..afecc993b3 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedByteWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedByteWriter.java @@ -1,92 +1,92 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.UnsignedByte; - -import java.nio.ByteBuffer; - -public class UnsignedByteWriter implements ValueWriter -{ - private int _written; - private byte _value; - - public int writeToBuffer(ByteBuffer buffer) - { - - switch(_written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put((byte)0x50); - } - else - { - break; - } - case 1: - if(buffer.hasRemaining()) - { - buffer.put(_value); - _written = 2; - } - else - { - _written = 1; - } - - } - - return 2; - } - - public void setValue(UnsignedByte value) - { - _written = 0; - _value = value.byteValue(); - } - - public boolean isComplete() - { - return _written == 2; - } - - public boolean isCacheable() - { - return true; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new UnsignedByteWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(UnsignedByte.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.UnsignedByte; + +import java.nio.ByteBuffer; + +public class UnsignedByteWriter implements ValueWriter +{ + private int _written; + private byte _value; + + public int writeToBuffer(ByteBuffer buffer) + { + + switch(_written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put((byte)0x50); + } + else + { + break; + } + case 1: + if(buffer.hasRemaining()) + { + buffer.put(_value); + _written = 2; + } + else + { + _written = 1; + } + + } + + return 2; + } + + public void setValue(UnsignedByte value) + { + _written = 0; + _value = value.byteValue(); + } + + public boolean isComplete() + { + return _written == 2; + } + + public boolean isCacheable() + { + return true; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new UnsignedByteWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(UnsignedByte.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedIntegerWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedIntegerWriter.java index 9517bd9c6e..7e4e6be075 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedIntegerWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedIntegerWriter.java @@ -1,148 +1,148 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.UnsignedInteger; - -import java.nio.ByteBuffer; - -public class UnsignedIntegerWriter implements ValueWriter -{ - private static final byte EIGHT_BYTE_FORMAT_CODE = (byte)0x70; - private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x52; - private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x43; - - private ValueWriter _delegate; - - private final FixedFourWriter _eightByteWriter = new FixedFourWriter() - { - - @Override - byte getFormatCode() - { - return EIGHT_BYTE_FORMAT_CODE; - } - - @Override - int convertValueToInt(UnsignedInteger value) - { - return value.intValue(); - } - }; - - private final ValueWriter _oneByteWriter = new FixedOneWriter() - { - - @Override protected byte getFormatCode() - { - return ONE_BYTE_FORMAT_CODE; - } - - @Override protected byte convertToByte(final UnsignedInteger value) - { - return value.byteValue(); - } - }; - - private final ValueWriter _zeroByteWriter = new ValueWriter() - { - private boolean _complete; - - - public int writeToBuffer(ByteBuffer buffer) - { - - if(!_complete && buffer.hasRemaining()) - { - buffer.put(ZERO_BYTE_FORMAT_CODE); - _complete = true; - } - - return 1; - } - - public void setValue(UnsignedInteger uint) - { - _complete = false; - } - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _complete; - } - - }; - - - - public int writeToBuffer(final ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(final UnsignedInteger uint) - { - if(uint.equals(UnsignedInteger.ZERO)) - { - _delegate = _zeroByteWriter; - } - else if(uint.compareTo(UnsignedInteger.valueOf(256))<0) - { - _delegate = _oneByteWriter; - } - else - { - _delegate = _eightByteWriter; - } - _delegate.setValue(uint); - } - - public boolean isComplete() - { - return _delegate.isComplete(); - } - - public boolean isCacheable() - { - return false; - } - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new UnsignedIntegerWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(UnsignedInteger.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.UnsignedInteger; + +import java.nio.ByteBuffer; + +public class UnsignedIntegerWriter implements ValueWriter +{ + private static final byte EIGHT_BYTE_FORMAT_CODE = (byte)0x70; + private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x52; + private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x43; + + private ValueWriter _delegate; + + private final FixedFourWriter _eightByteWriter = new FixedFourWriter() + { + + @Override + byte getFormatCode() + { + return EIGHT_BYTE_FORMAT_CODE; + } + + @Override + int convertValueToInt(UnsignedInteger value) + { + return value.intValue(); + } + }; + + private final ValueWriter _oneByteWriter = new FixedOneWriter() + { + + @Override protected byte getFormatCode() + { + return ONE_BYTE_FORMAT_CODE; + } + + @Override protected byte convertToByte(final UnsignedInteger value) + { + return value.byteValue(); + } + }; + + private final ValueWriter _zeroByteWriter = new ValueWriter() + { + private boolean _complete; + + + public int writeToBuffer(ByteBuffer buffer) + { + + if(!_complete && buffer.hasRemaining()) + { + buffer.put(ZERO_BYTE_FORMAT_CODE); + _complete = true; + } + + return 1; + } + + public void setValue(UnsignedInteger uint) + { + _complete = false; + } + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _complete; + } + + }; + + + + public int writeToBuffer(final ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(final UnsignedInteger uint) + { + if(uint.equals(UnsignedInteger.ZERO)) + { + _delegate = _zeroByteWriter; + } + else if(uint.compareTo(UnsignedInteger.valueOf(256))<0) + { + _delegate = _oneByteWriter; + } + else + { + _delegate = _eightByteWriter; + } + _delegate.setValue(uint); + } + + public boolean isComplete() + { + return _delegate.isComplete(); + } + + public boolean isCacheable() + { + return false; + } + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new UnsignedIntegerWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(UnsignedInteger.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedLongWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedLongWriter.java index 4345187d61..93e0fea740 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedLongWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedLongWriter.java @@ -1,152 +1,152 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.UnsignedLong; - -import java.nio.ByteBuffer; - -public class UnsignedLongWriter implements ValueWriter -{ - - - private static final byte EIGHT_BYTE_FORMAT_CODE = (byte) 0x80; - private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x53; - private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x44; - - private ValueWriter _delegate; - - private final FixedEightWriter _eightByteWriter = new FixedEightWriter() - { - - @Override - byte getFormatCode() - { - return EIGHT_BYTE_FORMAT_CODE; - } - - @Override - long convertValueToLong(UnsignedLong value) - { - return value.longValue(); - } - - }; - - private final ValueWriter _oneByteWriter = new FixedOneWriter() - { - - @Override protected byte getFormatCode() - { - return ONE_BYTE_FORMAT_CODE; - } - - @Override protected byte convertToByte(final UnsignedLong value) - { - return value.byteValue(); - } - }; - - private final ValueWriter _zeroByteWriter = new ValueWriter() - { - private boolean _complete; - - - public int writeToBuffer(ByteBuffer buffer) - { - - if(!_complete && buffer.hasRemaining()) - { - buffer.put(ZERO_BYTE_FORMAT_CODE); - _complete = true; - } - - return 1; - } - - public void setValue(UnsignedLong ulong) - { - _complete = false; - } - - public boolean isCacheable() - { - return true; - } - - public boolean isComplete() - { - return _complete; - } - - }; - - - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new UnsignedLongWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(UnsignedLong.class, FACTORY); - } - - public int writeToBuffer(final ByteBuffer buffer) - { - return _delegate.writeToBuffer(buffer); - } - - public void setValue(final UnsignedLong ulong) - { - if(ulong.equals(UnsignedLong.ZERO)) - { - _delegate = _zeroByteWriter; - } - else if(ulong.compareTo(UnsignedLong.valueOf(256))<0) - { - _delegate = _oneByteWriter; - } - else - { - _delegate = _eightByteWriter; - } - - _delegate.setValue(ulong); - } - - public boolean isComplete() - { - return _delegate.isComplete(); - } - - public boolean isCacheable() - { - return false; - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.UnsignedLong; + +import java.nio.ByteBuffer; + +public class UnsignedLongWriter implements ValueWriter +{ + + + private static final byte EIGHT_BYTE_FORMAT_CODE = (byte) 0x80; + private static final byte ONE_BYTE_FORMAT_CODE = (byte) 0x53; + private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x44; + + private ValueWriter _delegate; + + private final FixedEightWriter _eightByteWriter = new FixedEightWriter() + { + + @Override + byte getFormatCode() + { + return EIGHT_BYTE_FORMAT_CODE; + } + + @Override + long convertValueToLong(UnsignedLong value) + { + return value.longValue(); + } + + }; + + private final ValueWriter _oneByteWriter = new FixedOneWriter() + { + + @Override protected byte getFormatCode() + { + return ONE_BYTE_FORMAT_CODE; + } + + @Override protected byte convertToByte(final UnsignedLong value) + { + return value.byteValue(); + } + }; + + private final ValueWriter _zeroByteWriter = new ValueWriter() + { + private boolean _complete; + + + public int writeToBuffer(ByteBuffer buffer) + { + + if(!_complete && buffer.hasRemaining()) + { + buffer.put(ZERO_BYTE_FORMAT_CODE); + _complete = true; + } + + return 1; + } + + public void setValue(UnsignedLong ulong) + { + _complete = false; + } + + public boolean isCacheable() + { + return true; + } + + public boolean isComplete() + { + return _complete; + } + + }; + + + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new UnsignedLongWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(UnsignedLong.class, FACTORY); + } + + public int writeToBuffer(final ByteBuffer buffer) + { + return _delegate.writeToBuffer(buffer); + } + + public void setValue(final UnsignedLong ulong) + { + if(ulong.equals(UnsignedLong.ZERO)) + { + _delegate = _zeroByteWriter; + } + else if(ulong.compareTo(UnsignedLong.valueOf(256))<0) + { + _delegate = _oneByteWriter; + } + else + { + _delegate = _eightByteWriter; + } + + _delegate.setValue(ulong); + } + + public boolean isComplete() + { + return _delegate.isComplete(); + } + + public boolean isCacheable() + { + return false; + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedShortWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedShortWriter.java index 63d8bf0d7b..56f1a37bd6 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedShortWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/UnsignedShortWriter.java @@ -1,56 +1,56 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.UnsignedShort; - -public class UnsignedShortWriter extends FixedTwoWriter -{ - private static final byte FORMAT_CODE = (byte)0x60; - - - @Override - short convertValueToShort(UnsignedShort value) - { - return value.shortValue(); - } - - @Override - byte getFormatCode() - { - return FORMAT_CODE; - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new UnsignedShortWriter(); - } - }; - - public static void register(ValueWriter.Registry registry) - { - registry.register(UnsignedShort.class, FACTORY); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.UnsignedShort; + +public class UnsignedShortWriter extends FixedTwoWriter +{ + private static final byte FORMAT_CODE = (byte)0x60; + + + @Override + short convertValueToShort(UnsignedShort value) + { + return value.shortValue(); + } + + @Override + byte getFormatCode() + { + return FORMAT_CODE; + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new UnsignedShortWriter(); + } + }; + + public static void register(ValueWriter.Registry registry) + { + registry.register(UnsignedShort.class, FACTORY); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueHandler.java index 57351a91d7..2bac799c55 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueHandler.java @@ -1,159 +1,159 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.transport.AmqpError; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; - -public class ValueHandler implements DescribedTypeConstructorRegistry.Source -{ - private static final byte DESCRIBED_TYPE = (byte)0; - - private final DescribedTypeConstructorRegistry _describedTypeConstructorRegistry; - - - private static final TypeConstructor[][] TYPE_CONSTRUCTORS = - { - {}, - {}, - {}, - {}, - { NullTypeConstructor.getInstance(), BooleanConstructor.getTrueInstance(), - BooleanConstructor.getFalseInstance(), ZeroUIntConstructor.getInstance(), - ZeroULongConstructor.getInstance(), ZeroListConstructor.getInstance() }, - { UByteTypeConstructor.getInstance(), ByteTypeConstructor.getInstance(), - SmallUIntConstructor.getInstance(), SmallULongConstructor.getInstance(), - SmallIntConstructor.getInstance(), SmallLongConstructor.getInstance(), - BooleanConstructor.getByteInstance()}, - { UShortTypeConstructor.getInstance(), ShortTypeConstructor.getInstance() }, - { UIntTypeConstructor.getInstance(), IntTypeConstructor.getInstance(), - FloatTypeConstructor.getInstance(), CharTypeConstructor.getInstance(), - DecimalConstructor.getDecimal32Instance()}, - { ULongTypeConstructor.getInstance(), LongTypeConstructor.getInstance(), - DoubleTypeConstructor.getInstance(), TimestampTypeConstructor.getInstance(), - DecimalConstructor.getDecimal64Instance()}, - { null, null, - null, null, - DecimalConstructor.getDecimal128Instance(), null, - null, null, - UUIDTypeConstructor.getInstance() }, - { BinaryTypeConstructor.getInstance(1), - StringTypeConstructor.getInstance(1, Charset.forName("UTF8")), - StringTypeConstructor.getInstance(1, Charset.forName("UTF16")), - SymbolTypeConstructor.getInstance(1) }, - { BinaryTypeConstructor.getInstance(4), - StringTypeConstructor.getInstance(4, Charset.forName("UTF8")), - StringTypeConstructor.getInstance(4, Charset.forName("UTF16")), - SymbolTypeConstructor.getInstance(4) }, - { CompoundTypeConstructor.getInstance(1, CompoundTypeConstructor.LIST_ASSEMBLER_FACTORY), - CompoundTypeConstructor.getInstance(1, CompoundTypeConstructor.MAP_ASSEMBLER_FACTORY) }, - { CompoundTypeConstructor.getInstance(4, CompoundTypeConstructor.LIST_ASSEMBLER_FACTORY), - CompoundTypeConstructor.getInstance(4, CompoundTypeConstructor.MAP_ASSEMBLER_FACTORY) }, - { - ArrayTypeConstructor.getOneByteSizeTypeConstructor() - }, - { - ArrayTypeConstructor.getFourByteSizeTypeConstructor() - } - }; - - - public ValueHandler(DescribedTypeConstructorRegistry registry) - { - _describedTypeConstructorRegistry = registry; - } - - public Object parse(final ByteBuffer in) throws AmqpErrorException - { - TypeConstructor constructor = readConstructor(in); - return constructor.construct(in, this); - } - - - public TypeConstructor readConstructor(ByteBuffer in) throws AmqpErrorException - { - if(!in.hasRemaining()) - { - throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data - expected type, no data remaining"); - } - byte formatCode = in.get(); - - if(formatCode == DESCRIBED_TYPE) - { - Object descriptor = parse(in); - DescribedTypeConstructor describedTypeConstructor = _describedTypeConstructorRegistry.getConstructor(descriptor); - if(describedTypeConstructor==null) - { - describedTypeConstructor=new DefaultDescribedTypeConstructor(descriptor); - } - TypeConstructor typeConstructor = readConstructor(in); - - return describedTypeConstructor.construct(typeConstructor); - - } - else - { - int subCategory = (formatCode >> 4) & 0x0F; - int subtype = formatCode & 0x0F; - - TypeConstructor tc; - try - { - tc = TYPE_CONSTRUCTORS[subCategory][subtype]; - } - catch(IndexOutOfBoundsException e) - { - tc = null; - } - - if(tc == null) - { - throw new AmqpErrorException(ConnectionError.FRAMING_ERROR,"Unknown type format-code 0x%02x", formatCode); - } - - return tc; - } - } - - - - - - @Override - public String toString() - { - return "ValueHandler{" + - ", _describedTypeConstructorRegistry=" + _describedTypeConstructorRegistry + - '}'; - } - - - public DescribedTypeConstructorRegistry getDescribedTypeRegistry() - { - return _describedTypeConstructorRegistry; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.transport.AmqpError; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +public class ValueHandler implements DescribedTypeConstructorRegistry.Source +{ + private static final byte DESCRIBED_TYPE = (byte)0; + + private final DescribedTypeConstructorRegistry _describedTypeConstructorRegistry; + + + private static final TypeConstructor[][] TYPE_CONSTRUCTORS = + { + {}, + {}, + {}, + {}, + { NullTypeConstructor.getInstance(), BooleanConstructor.getTrueInstance(), + BooleanConstructor.getFalseInstance(), ZeroUIntConstructor.getInstance(), + ZeroULongConstructor.getInstance(), ZeroListConstructor.getInstance() }, + { UByteTypeConstructor.getInstance(), ByteTypeConstructor.getInstance(), + SmallUIntConstructor.getInstance(), SmallULongConstructor.getInstance(), + SmallIntConstructor.getInstance(), SmallLongConstructor.getInstance(), + BooleanConstructor.getByteInstance()}, + { UShortTypeConstructor.getInstance(), ShortTypeConstructor.getInstance() }, + { UIntTypeConstructor.getInstance(), IntTypeConstructor.getInstance(), + FloatTypeConstructor.getInstance(), CharTypeConstructor.getInstance(), + DecimalConstructor.getDecimal32Instance()}, + { ULongTypeConstructor.getInstance(), LongTypeConstructor.getInstance(), + DoubleTypeConstructor.getInstance(), TimestampTypeConstructor.getInstance(), + DecimalConstructor.getDecimal64Instance()}, + { null, null, + null, null, + DecimalConstructor.getDecimal128Instance(), null, + null, null, + UUIDTypeConstructor.getInstance() }, + { BinaryTypeConstructor.getInstance(1), + StringTypeConstructor.getInstance(1, Charset.forName("UTF8")), + StringTypeConstructor.getInstance(1, Charset.forName("UTF16")), + SymbolTypeConstructor.getInstance(1) }, + { BinaryTypeConstructor.getInstance(4), + StringTypeConstructor.getInstance(4, Charset.forName("UTF8")), + StringTypeConstructor.getInstance(4, Charset.forName("UTF16")), + SymbolTypeConstructor.getInstance(4) }, + { CompoundTypeConstructor.getInstance(1, CompoundTypeConstructor.LIST_ASSEMBLER_FACTORY), + CompoundTypeConstructor.getInstance(1, CompoundTypeConstructor.MAP_ASSEMBLER_FACTORY) }, + { CompoundTypeConstructor.getInstance(4, CompoundTypeConstructor.LIST_ASSEMBLER_FACTORY), + CompoundTypeConstructor.getInstance(4, CompoundTypeConstructor.MAP_ASSEMBLER_FACTORY) }, + { + ArrayTypeConstructor.getOneByteSizeTypeConstructor() + }, + { + ArrayTypeConstructor.getFourByteSizeTypeConstructor() + } + }; + + + public ValueHandler(DescribedTypeConstructorRegistry registry) + { + _describedTypeConstructorRegistry = registry; + } + + public Object parse(final ByteBuffer in) throws AmqpErrorException + { + TypeConstructor constructor = readConstructor(in); + return constructor.construct(in, this); + } + + + public TypeConstructor readConstructor(ByteBuffer in) throws AmqpErrorException + { + if(!in.hasRemaining()) + { + throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data - expected type, no data remaining"); + } + byte formatCode = in.get(); + + if(formatCode == DESCRIBED_TYPE) + { + Object descriptor = parse(in); + DescribedTypeConstructor describedTypeConstructor = _describedTypeConstructorRegistry.getConstructor(descriptor); + if(describedTypeConstructor==null) + { + describedTypeConstructor=new DefaultDescribedTypeConstructor(descriptor); + } + TypeConstructor typeConstructor = readConstructor(in); + + return describedTypeConstructor.construct(typeConstructor); + + } + else + { + int subCategory = (formatCode >> 4) & 0x0F; + int subtype = formatCode & 0x0F; + + TypeConstructor tc; + try + { + tc = TYPE_CONSTRUCTORS[subCategory][subtype]; + } + catch(IndexOutOfBoundsException e) + { + tc = null; + } + + if(tc == null) + { + throw new AmqpErrorException(ConnectionError.FRAMING_ERROR,"Unknown type format-code 0x%02x", formatCode); + } + + return tc; + } + } + + + + + + @Override + public String toString() + { + return "ValueHandler{" + + ", _describedTypeConstructorRegistry=" + _describedTypeConstructorRegistry + + '}'; + } + + + public DescribedTypeConstructorRegistry getDescribedTypeRegistry() + { + return _describedTypeConstructorRegistry; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueProducingProtocolHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueProducingProtocolHandler.java index 48db222aa0..a8563352e7 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueProducingProtocolHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueProducingProtocolHandler.java @@ -1,31 +1,31 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.transport.Error; - -public interface ValueProducingProtocolHandler extends ProtocolHandler -{ - Object getValue(); - public boolean hasError(); - public Error getError(); - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.transport.Error; + +public interface ValueProducingProtocolHandler extends ProtocolHandler +{ + Object getValue(); + public boolean hasError(); + public Error getError(); + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueWriter.java index 7918397994..65f33aa4ad 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ValueWriter.java @@ -1,58 +1,58 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; -import java.util.Map; - -public interface ValueWriter -{ - - - - public static interface Factory - { - ValueWriter newInstance(Registry registry); - } - - public static interface Registry - { - public static interface Source - { - public Registry getDescribedTypeRegistry(); - } - - ValueWriter getValueWriter(V value); - ValueWriter getValueWriter(V value, Map localCache); - ValueWriter register(Class clazz, ValueWriter.Factory writer); - - } - - - int writeToBuffer(ByteBuffer buffer); - - void setValue(T frameBody); - - boolean isComplete(); - - boolean isCacheable(); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; +import java.util.Map; + +public interface ValueWriter +{ + + + + public static interface Factory + { + ValueWriter newInstance(Registry registry); + } + + public static interface Registry + { + public static interface Source + { + public Registry getDescribedTypeRegistry(); + } + + ValueWriter getValueWriter(V value); + ValueWriter getValueWriter(V value, Map localCache); + ValueWriter register(Class clazz, ValueWriter.Factory writer); + + } + + + int writeToBuffer(ByteBuffer buffer); + + void setValue(T frameBody); + + boolean isComplete(); + + boolean isCacheable(); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthTypeConstructor.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthTypeConstructor.java index 35ce253623..6dbc27aa8d 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthTypeConstructor.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthTypeConstructor.java @@ -1,48 +1,48 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; - -import java.nio.ByteBuffer; - -public abstract class VariableWidthTypeConstructor implements TypeConstructor -{ - protected int _size; - - public VariableWidthTypeConstructor(int size) - { - _size = size; - } - - public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException - { - return construct(in, false, handler); - } - - public int getSize() - { - return _size; - } - - public abstract Object construct(ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException; - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; + +import java.nio.ByteBuffer; + +public abstract class VariableWidthTypeConstructor implements TypeConstructor +{ + protected int _size; + + public VariableWidthTypeConstructor(int size) + { + _size = size; + } + + public Object construct(final ByteBuffer in, ValueHandler handler) throws AmqpErrorException + { + return construct(in, false, handler); + } + + public int getSize() + { + return _size; + } + + public abstract Object construct(ByteBuffer in, boolean isCopy, ValueHandler handler) throws AmqpErrorException; + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthWriter.java index 93b2b5e6d8..33d87b79ac 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/VariableWidthWriter.java @@ -1,169 +1,169 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.codec; - -import java.nio.ByteBuffer; - -public abstract class VariableWidthWriter implements ValueWriter -{ - private int _written; - private int _size; - - public int writeToBuffer(ByteBuffer buffer) - { - - int written = _written; - final int length = getLength(); - boolean singleOctetSize = _size == 1; - if(singleOctetSize) - { - switch(written) - { - case 0: - if(buffer.hasRemaining()) - { - buffer.put(getSingleOctetEncodingCode()); - } - else - { - break; - } - case 1: - if(buffer.hasRemaining()) - { - buffer.put((byte)length); - written = 2; - } - else - { - written = 1; - break; - } - default: - - final int toWrite = 2 + length - written; - if(buffer.remaining() >= toWrite) - { - writeBytes(buffer, written-2,toWrite); - written = length + 2; - clearValue(); - } - else - { - final int remaining = buffer.remaining(); - - writeBytes(buffer, written-2, remaining); - written += remaining; - } - - } - } - else - { - - int remaining = buffer.remaining(); - - switch(written) - { - - case 0: - if(buffer.hasRemaining()) - { - buffer.put(getFourOctetEncodingCode()); - remaining--; - written = 1; - } - else - { - break; - } - case 1: - if(remaining >= 4) - { - buffer.putInt(length); - remaining-=4; - written+=4; - } - case 2: - case 3: - if(remaining >= 2 && written <= 3) - { - buffer.putShort((short)((length >> ((3-written)<<3)) & 0xFFFF )); - remaining -= 2; - written += 2; - } - case 4: - if(remaining >=1 && written <=4) - { - buffer.put((byte)((length>> ((4-written)<<3)) & 0xFF )); - written++; - } - - default: - - final int toWrite = 5 + length - written; - if(buffer.remaining() >= toWrite) - { - writeBytes(buffer, written-5,toWrite); - written = length + 5; - clearValue(); - } - else if(buffer.hasRemaining()) - { - written += buffer.remaining(); - writeBytes(buffer, written-5, buffer.remaining()); - } - - } - - } - - _written = written; - return 1 + _size + length; - } - - protected abstract void clearValue(); - - protected abstract boolean hasValue(); - - protected abstract byte getFourOctetEncodingCode(); - - protected abstract byte getSingleOctetEncodingCode(); - - public void setValue(V value) - { - _written = 0; - _size = (getLength() & 0xFFFFFF00) == 0 ? 1 : 4; - } - - protected abstract int getLength(); - - protected abstract void writeBytes(ByteBuffer buf, int offset, int length); - - - public boolean isComplete() - { - return !hasValue() || _written == getLength() + _size + 1; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.codec; + +import java.nio.ByteBuffer; + +public abstract class VariableWidthWriter implements ValueWriter +{ + private int _written; + private int _size; + + public int writeToBuffer(ByteBuffer buffer) + { + + int written = _written; + final int length = getLength(); + boolean singleOctetSize = _size == 1; + if(singleOctetSize) + { + switch(written) + { + case 0: + if(buffer.hasRemaining()) + { + buffer.put(getSingleOctetEncodingCode()); + } + else + { + break; + } + case 1: + if(buffer.hasRemaining()) + { + buffer.put((byte)length); + written = 2; + } + else + { + written = 1; + break; + } + default: + + final int toWrite = 2 + length - written; + if(buffer.remaining() >= toWrite) + { + writeBytes(buffer, written-2,toWrite); + written = length + 2; + clearValue(); + } + else + { + final int remaining = buffer.remaining(); + + writeBytes(buffer, written-2, remaining); + written += remaining; + } + + } + } + else + { + + int remaining = buffer.remaining(); + + switch(written) + { + + case 0: + if(buffer.hasRemaining()) + { + buffer.put(getFourOctetEncodingCode()); + remaining--; + written = 1; + } + else + { + break; + } + case 1: + if(remaining >= 4) + { + buffer.putInt(length); + remaining-=4; + written+=4; + } + case 2: + case 3: + if(remaining >= 2 && written <= 3) + { + buffer.putShort((short)((length >> ((3-written)<<3)) & 0xFFFF )); + remaining -= 2; + written += 2; + } + case 4: + if(remaining >=1 && written <=4) + { + buffer.put((byte)((length>> ((4-written)<<3)) & 0xFF )); + written++; + } + + default: + + final int toWrite = 5 + length - written; + if(buffer.remaining() >= toWrite) + { + writeBytes(buffer, written-5,toWrite); + written = length + 5; + clearValue(); + } + else if(buffer.hasRemaining()) + { + written += buffer.remaining(); + writeBytes(buffer, written-5, buffer.remaining()); + } + + } + + } + + _written = written; + return 1 + _size + length; + } + + protected abstract void clearValue(); + + protected abstract boolean hasValue(); + + protected abstract byte getFourOctetEncodingCode(); + + protected abstract byte getSingleOctetEncodingCode(); + + public void setValue(V value) + { + _written = 0; + _size = (getLength() & 0xFFFFFF00) == 0 ? 1 : 4; + } + + protected abstract int getLength(); + + protected abstract void writeBytes(ByteBuffer buf, int offset, int length); + + + public boolean isComplete() + { + return !hasValue() || _written == getLength() + _size + 1; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/WrapperTypeValueWriter.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/WrapperTypeValueWriter.java index 85a3196f6c..c6edc51671 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/WrapperTypeValueWriter.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/WrapperTypeValueWriter.java @@ -1,55 +1,55 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.codec; - -import org.apache.qpid.amqp_1_0.type.WrapperType; - -public class WrapperTypeValueWriter extends DelegatingValueWriter -{ - public WrapperTypeValueWriter(final Registry registry) - { - super(registry); - } - - @Override - protected Object getUnderlyingValue(final WrapperType wrapperType) - { - return wrapperType.getValue(); - } - - private static Factory FACTORY = new Factory() - { - - public ValueWriter newInstance(Registry registry) - { - return new WrapperTypeValueWriter(registry); - } - }; - - public boolean isCacheable() - { - return true; - } - - public static void register(Registry registry, Class clazz) - { - registry.register(clazz, FACTORY); - }} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.codec; + +import org.apache.qpid.amqp_1_0.type.WrapperType; + +public class WrapperTypeValueWriter extends DelegatingValueWriter +{ + public WrapperTypeValueWriter(final Registry registry) + { + super(registry); + } + + @Override + protected Object getUnderlyingValue(final WrapperType wrapperType) + { + return wrapperType.getValue(); + } + + private static Factory FACTORY = new Factory() + { + + public ValueWriter newInstance(Registry registry) + { + return new WrapperTypeValueWriter(registry); + } + }; + + public boolean isCacheable() + { + return true; + } + + public static void register(Registry registry, Class clazz) + { + registry.register(clazz, FACTORY); + }} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQFrame.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQFrame.java index 9684e290f4..7950d9de7c 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQFrame.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQFrame.java @@ -1,76 +1,76 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.type.Binary; -import org.apache.qpid.amqp_1_0.type.FrameBody; - -import java.nio.ByteBuffer; - -public abstract class AMQFrame -{ - private T _frameBody; - private ByteBuffer _payload; - - AMQFrame(T frameBody) - { - _frameBody = frameBody; - } - - protected AMQFrame(T frameBody, ByteBuffer payload) - { - _frameBody = frameBody; - _payload = payload; - } - - public ByteBuffer getPayload() - { - return _payload; - } - - public static TransportFrame createAMQFrame(short channel, FrameBody frameBody) - { - return createAMQFrame(channel, frameBody, null); - } - - public static TransportFrame createAMQFrame(short channel, FrameBody frameBody, ByteBuffer payload) - { - return new TransportFrame(channel, frameBody, payload); - } - - abstract public short getChannel(); - - abstract public byte getFrameType(); - - public T getFrameBody() - { - return _frameBody; - } - - @Override - public String toString() - { - return "AMQFrame{" + - "frameBody=" + _frameBody + - '}'; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.type.Binary; +import org.apache.qpid.amqp_1_0.type.FrameBody; + +import java.nio.ByteBuffer; + +public abstract class AMQFrame +{ + private T _frameBody; + private ByteBuffer _payload; + + AMQFrame(T frameBody) + { + _frameBody = frameBody; + } + + protected AMQFrame(T frameBody, ByteBuffer payload) + { + _frameBody = frameBody; + _payload = payload; + } + + public ByteBuffer getPayload() + { + return _payload; + } + + public static TransportFrame createAMQFrame(short channel, FrameBody frameBody) + { + return createAMQFrame(channel, frameBody, null); + } + + public static TransportFrame createAMQFrame(short channel, FrameBody frameBody, ByteBuffer payload) + { + return new TransportFrame(channel, frameBody, payload); + } + + abstract public short getChannel(); + + abstract public byte getFrameType(); + + public T getFrameBody() + { + return _frameBody; + } + + @Override + public String toString() + { + return "AMQFrame{" + + "frameBody=" + _frameBody + + '}'; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQPProtocolHeaderHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQPProtocolHeaderHandler.java index 007df77f55..ec035bd60d 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQPProtocolHeaderHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/AMQPProtocolHeaderHandler.java @@ -1,85 +1,85 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; - -import java.nio.ByteBuffer; - -public class AMQPProtocolHeaderHandler implements ProtocolHandler -{ - private ConnectionEndpoint _connection; - private static final byte MAJOR_VERSION = (byte) 1; - private static final byte MINOR_VERSION = (byte) 0; - - enum State { - AWAITING_MAJOR, - AWAITING_MINOR, - AWAITING_REVISION, - ERROR - } - - private State _state = State.AWAITING_MAJOR; - - public AMQPProtocolHeaderHandler(final ConnectionEndpoint connection) - { - _connection = connection; - } - - public ProtocolHandler parse(final ByteBuffer in) - { - while(in.hasRemaining() && _state != State.ERROR) - { - switch(_state) - { - case AWAITING_MAJOR: - _state = in.get() == MAJOR_VERSION ? State.AWAITING_MINOR : State.ERROR; - if(!in.hasRemaining()) - { - break; - } - case AWAITING_MINOR: - _state = in.get() == MINOR_VERSION ? State.AWAITING_MINOR : State.ERROR; - if(!in.hasRemaining()) - { - break; - } - case AWAITING_REVISION: - byte revision = in.get(); - _connection.protocolHeaderReceived(MAJOR_VERSION, MINOR_VERSION, revision); - ProtocolHandler handler = new FrameHandler(_connection); - return handler.parse(in); - } - } - if(_state == State.ERROR) - { - _connection.invalidHeaderReceived(); - } - return this; - - } - - public boolean isDone() - { - return _state != State.ERROR && !_connection.closedForInput(); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; + +import java.nio.ByteBuffer; + +public class AMQPProtocolHeaderHandler implements ProtocolHandler +{ + private ConnectionEndpoint _connection; + private static final byte MAJOR_VERSION = (byte) 1; + private static final byte MINOR_VERSION = (byte) 0; + + enum State { + AWAITING_MAJOR, + AWAITING_MINOR, + AWAITING_REVISION, + ERROR + } + + private State _state = State.AWAITING_MAJOR; + + public AMQPProtocolHeaderHandler(final ConnectionEndpoint connection) + { + _connection = connection; + } + + public ProtocolHandler parse(final ByteBuffer in) + { + while(in.hasRemaining() && _state != State.ERROR) + { + switch(_state) + { + case AWAITING_MAJOR: + _state = in.get() == MAJOR_VERSION ? State.AWAITING_MINOR : State.ERROR; + if(!in.hasRemaining()) + { + break; + } + case AWAITING_MINOR: + _state = in.get() == MINOR_VERSION ? State.AWAITING_MINOR : State.ERROR; + if(!in.hasRemaining()) + { + break; + } + case AWAITING_REVISION: + byte revision = in.get(); + _connection.protocolHeaderReceived(MAJOR_VERSION, MINOR_VERSION, revision); + ProtocolHandler handler = new FrameHandler(_connection); + return handler.parse(in); + } + } + if(_state == State.ERROR) + { + _connection.invalidHeaderReceived(); + } + return this; + + } + + public boolean isDone() + { + return _state != State.ERROR && !_connection.closedForInput(); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/ConnectionHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/ConnectionHandler.java index f4cd06f3ef..f391cf3035 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/ConnectionHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/ConnectionHandler.java @@ -1,580 +1,580 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.codec.FrameWriter; -import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; -import org.apache.qpid.amqp_1_0.codec.ProtocolHeaderHandler; -import org.apache.qpid.amqp_1_0.codec.ValueHandler; -import org.apache.qpid.amqp_1_0.codec.ValueWriter; -import org.apache.qpid.amqp_1_0.transport.BytesProcessor; -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; - -import org.apache.qpid.amqp_1_0.transport.FrameOutputHandler; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.Binary; -import org.apache.qpid.amqp_1_0.type.transport.Open; -import org.apache.qpid.amqp_1_0.type.Symbol; -import org.apache.qpid.amqp_1_0.type.UnsignedShort; -import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.Queue; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class ConnectionHandler -{ - private final ConnectionEndpoint _connection; - private ProtocolHandler _delegate; - - private static final Logger FRAME_LOGGER = Logger.getLogger("FRM"); - private static final Logger RAW_LOGGER = Logger.getLogger("RAW"); - - public ConnectionHandler(final ConnectionEndpoint connection) - { - _connection = connection; - _delegate = new ProtocolHeaderHandler(connection); - } - - - public boolean parse(ByteBuffer in) - { - - while(in.hasRemaining() && !isDone()) - { - _delegate = _delegate.parse(in); - - } - return isDone(); - } - - public boolean isDone() - { - return _delegate.isDone(); - } - - - // ---------------------------------------------------------------- - - public static class FrameOutput implements FrameOutputHandler, FrameSource - { - - private static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.wrap(new byte[0]); - private final BlockingQueue> _queue = new ArrayBlockingQueue>(100); - private ConnectionEndpoint _conn; - - private final AMQFrame _endOfFrameMarker = new AMQFrame(null) - { - @Override public short getChannel() - { - throw new UnsupportedOperationException(); - } - - @Override public byte getFrameType() - { - throw new UnsupportedOperationException(); - } - }; - - private boolean _setForClose; - private boolean _closed; - private long _nextHeartbeat; - - public FrameOutput(final ConnectionEndpoint conn) - { - _conn = conn; - } - - public boolean canSend() - { - return _queue.remainingCapacity() != 0; - } - - public void send(AMQFrame frame) - { - send(frame, null); - } - - public void send(final AMQFrame frame, final ByteBuffer payload) - { - synchronized(_conn.getLock()) - { - try - { -// TODO HACK - check frame length - int size = _conn.getDescribedTypeRegistry() - .getValueWriter(frame.getFrameBody()).writeToBuffer(EMPTY_BYTEBUFFER) + 8; - - if(size > _conn.getMaxFrameSize()) - { - throw new OversizeFrameException(frame, size); - } - - while(!_queue.offer(frame)) - { - _conn.getLock().wait(1000L); - - } - _conn.getLock().notifyAll(); - } - catch (InterruptedException e) - { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - } - - - public void close() - { - synchronized (_conn.getLock()) - { - if(!_queue.offer(_endOfFrameMarker)) - { - _setForClose = true; - } - _conn.getLock().notifyAll(); - } - } - - public AMQFrame getNextFrame(final boolean wait) - { - synchronized(_conn.getLock()) - { - long time = System.currentTimeMillis(); - try - { - AMQFrame frame = null; - while(!closed() && (frame = _queue.poll()) == null && wait) - { - _conn.getLock().wait(_conn.getIdleTimeout()/2); - - if(_conn.getIdleTimeout()>0) - { - time = System.currentTimeMillis(); - - if(frame == null && time > _nextHeartbeat) - { - frame = new TransportFrame((short) 0,null); - break; - } - } - } - - - - - if(frame != null) - { - _nextHeartbeat = time + _conn.getIdleTimeout()/2; - - } - if(frame == _endOfFrameMarker) - { - _closed = true; - frame = null; - } - else if(_setForClose && frame != null) - { - _setForClose = !_queue.offer(_endOfFrameMarker); - } - - - if(frame != null && FRAME_LOGGER.isLoggable(Level.FINE)) - { - FRAME_LOGGER.fine("SEND[" + _conn.getRemoteAddress() + "|" + frame.getChannel() + "] : " + frame.getFrameBody()); - } - - _conn.getLock().notifyAll(); - - return frame; - } - catch (InterruptedException e) - { - _conn.setClosedForOutput(true); - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - return null; - } - } - } - - public boolean closed() - { - return _closed; - } - } - - public static interface FrameSource - { - AMQFrame getNextFrame(boolean wait); - boolean closed(); - } - - - public static interface BytesSource - { - void getBytes(BytesProcessor processor, boolean wait); - boolean closed(); - } - - public static class FrameToBytesSourceAdapter implements BytesSource - { - - private final FrameSource _frameSource; - private final FrameWriter _writer; - private static final int BUF_SIZE = 1<<16; - private final byte[] _bytes = new byte[BUF_SIZE]; - private final ByteBuffer _buffer = ByteBuffer.wrap(_bytes); - - public FrameToBytesSourceAdapter(final FrameSource frameSource, ValueWriter.Registry registry) - { - _frameSource = frameSource; - _writer = new FrameWriter(registry); - } - - public void getBytes(final BytesProcessor processor, final boolean wait) - { - - AMQFrame frame; - - if(_buffer.position() == 0 && !_frameSource.closed()) - { - if(!_writer.isComplete()) - { - _writer.writeToBuffer(_buffer); - } - - while(_buffer.hasRemaining()) - { - - if((frame = _frameSource.getNextFrame(wait && _buffer.position()==0)) != null) - { - _writer.setValue(frame); - - try - { - _writer.writeToBuffer(_buffer); - } - catch(RuntimeException e) - { - e.printStackTrace(); - throw e; - } - catch(Error e) - { - e.printStackTrace(); - throw e; - } - - - } - else - { - break; - } - } - _buffer.flip(); - } - if(_buffer.limit() != 0) - { - processor.processBytes(_buffer); - if(_buffer.remaining() == 0) - { - _buffer.clear(); - } - } - } - - public boolean closed() - { - return _buffer.position() == 0 && _frameSource.closed(); - } - } - - - public static class HeaderBytesSource implements BytesSource - { - - private final ByteBuffer _buffer; - private ConnectionEndpoint _conn; - - public HeaderBytesSource(ConnectionEndpoint conn, byte... headerBytes) - { - _conn = conn; - _buffer = ByteBuffer.wrap(headerBytes); - } - - public void getBytes(final BytesProcessor processor, final boolean wait) - { - if(!_conn.closedForOutput()) - { - processor.processBytes(_buffer); - } - } - - public boolean closed() - { - return !_conn.closedForOutput() && !_buffer.hasRemaining(); - } - } - - public static class SequentialBytesSource implements BytesSource - { - private Queue _sources = new LinkedList(); - - public SequentialBytesSource(BytesSource... sources) - { - _sources.addAll(Arrays.asList(sources)); - } - - public synchronized void addSource(BytesSource source) - { - _sources.add(source); - } - - public synchronized void getBytes(final BytesProcessor processor, final boolean wait) - { - BytesSource src = _sources.peek(); - while (src != null && src.closed()) - { - _sources.poll(); - src = _sources.peek(); - } - - if(src != null) - { - src.getBytes(processor, wait); - } - } - - public boolean closed() - { - return _sources.isEmpty(); - } - } - - - public static class BytesOutputHandler implements Runnable, BytesProcessor - { - - private final OutputStream _outputStream; - private BytesSource _bytesSource; - private boolean _closed; - private ConnectionEndpoint _conn; - - public BytesOutputHandler(OutputStream outputStream, BytesSource source, ConnectionEndpoint conn) - { - _outputStream = outputStream; - _bytesSource = source; - _conn = conn; - } - - public void run() - { - - final BytesSource bytesSource = _bytesSource; - - while(!(_closed || bytesSource.closed())) - { - _bytesSource.getBytes(this, true); - } - - } - - public void processBytes(final ByteBuffer buf) - { - try - { - if(RAW_LOGGER.isLoggable(Level.FINE)) - { - Binary bin = new Binary(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position()); - RAW_LOGGER.fine("SEND["+ _conn.getRemoteAddress() +"] : " + bin.toString()); - } - _outputStream.write(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position()); - buf.position(buf.limit()); - } - catch (IOException e) - { - _closed = true; - e.printStackTrace(); //TODO - } - } - } - - - public static class OutputHandler implements Runnable - { - - - - private final OutputStream _outputStream; - private FrameSource _frameSource; - - private static final int BUF_SIZE = 1<<16; - private ValueWriter.Registry _registry; - - - public OutputHandler(OutputStream outputStream, FrameSource source, ValueWriter.Registry registry) - { - _outputStream = outputStream; - _frameSource = source; - _registry = registry; - } - - public void run() - { - int i=0; - - - try - { - - byte[] buffer = new byte[BUF_SIZE]; - ByteBuffer buf = ByteBuffer.wrap(buffer); - - buf.put((byte)'A'); - buf.put((byte)'M'); - buf.put((byte)'Q'); - buf.put((byte)'P'); - buf.put((byte) 0); - buf.put((byte) 1); - buf.put((byte) 0); - buf.put((byte) 0); - - - - final FrameSource frameSource = _frameSource; - - AMQFrame frame; - FrameWriter writer = new FrameWriter(_registry); - - while(!frameSource.closed()) - { - - if(!writer.isComplete()) - { - writer.writeToBuffer(buf); - } - - while(buf.hasRemaining()) - { - - if((frame = frameSource.getNextFrame(buf.position()==0)) != null) - { - writer.setValue(frame); - - int size = writer.writeToBuffer(buf); - - } - else - { - break; - } - } - - if(buf.limit() != 0) - { - _outputStream.write(buffer,0, buf.position()); - buf.clear(); - } - } - - } - catch (IOException e) - { - e.printStackTrace(); - } - - } - } - - public static void main(String[] args) throws AmqpErrorException - { - byte[] buffer = new byte[76]; - ByteBuffer buf = ByteBuffer.wrap(buffer); - AMQPDescribedTypeRegistry registry = AMQPDescribedTypeRegistry.newInstance() - .registerTransportLayer() - .registerMessagingLayer() - .registerTransactionLayer(); - - Open open = new Open(); - // Open(container_id="venture", channel_max=10, hostname="foo", offered_capabilities=[Symbol("one"), Symbol("two"), Symbol("three")]) - open.setContainerId("venture"); - open.setChannelMax(UnsignedShort.valueOf((short) 10)); - open.setHostname("foo"); - open.setOfferedCapabilities(new Symbol[] {Symbol.valueOf("one"),Symbol.valueOf("two"),Symbol.valueOf("three")}); - - ValueWriter writer = registry.getValueWriter(open); - - System.out.println("------ Encode (time in ms for 1 million opens)"); - Long myLong = Long.valueOf(32); - ValueWriter writer2 = registry.getValueWriter(myLong); - Double myDouble = Double.valueOf(3.14159265359); - ValueWriter writer3 = registry.getValueWriter(myDouble); - for(int n = 0; n < 1/*00*/; n++) - { - long startTime = System.currentTimeMillis(); - for(int i = 1/*000000*/; i !=0; i--) - { - buf.position(0); - writer.setValue(open); - writer.writeToBuffer(buf); - writer2.setValue(myLong); - writer.writeToBuffer(buf); - writer3.setValue(myDouble); - writer3.writeToBuffer(buf); - - - } - long midTime = System.currentTimeMillis(); - System.out.println((midTime - startTime)); - - } - - - ValueHandler handler = new ValueHandler(registry); - System.out.println("------ Decode (time in ms for 1 million opens)"); - for(int n = 0; n < 100; n++) - { - long startTime = System.currentTimeMillis(); - for(int i = 1000000; i !=0; i--) - { - buf.flip(); - handler.parse(buf); - handler.parse(buf); - handler.parse(buf); - - } - long midTime = System.currentTimeMillis(); - System.out.println((midTime - startTime)); - } - - - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.codec.FrameWriter; +import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; +import org.apache.qpid.amqp_1_0.codec.ProtocolHeaderHandler; +import org.apache.qpid.amqp_1_0.codec.ValueHandler; +import org.apache.qpid.amqp_1_0.codec.ValueWriter; +import org.apache.qpid.amqp_1_0.transport.BytesProcessor; +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; + +import org.apache.qpid.amqp_1_0.transport.FrameOutputHandler; +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.Binary; +import org.apache.qpid.amqp_1_0.type.transport.Open; +import org.apache.qpid.amqp_1_0.type.Symbol; +import org.apache.qpid.amqp_1_0.type.UnsignedShort; +import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ConnectionHandler +{ + private final ConnectionEndpoint _connection; + private ProtocolHandler _delegate; + + private static final Logger FRAME_LOGGER = Logger.getLogger("FRM"); + private static final Logger RAW_LOGGER = Logger.getLogger("RAW"); + + public ConnectionHandler(final ConnectionEndpoint connection) + { + _connection = connection; + _delegate = new ProtocolHeaderHandler(connection); + } + + + public boolean parse(ByteBuffer in) + { + + while(in.hasRemaining() && !isDone()) + { + _delegate = _delegate.parse(in); + + } + return isDone(); + } + + public boolean isDone() + { + return _delegate.isDone(); + } + + + // ---------------------------------------------------------------- + + public static class FrameOutput implements FrameOutputHandler, FrameSource + { + + private static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.wrap(new byte[0]); + private final BlockingQueue> _queue = new ArrayBlockingQueue>(100); + private ConnectionEndpoint _conn; + + private final AMQFrame _endOfFrameMarker = new AMQFrame(null) + { + @Override public short getChannel() + { + throw new UnsupportedOperationException(); + } + + @Override public byte getFrameType() + { + throw new UnsupportedOperationException(); + } + }; + + private boolean _setForClose; + private boolean _closed; + private long _nextHeartbeat; + + public FrameOutput(final ConnectionEndpoint conn) + { + _conn = conn; + } + + public boolean canSend() + { + return _queue.remainingCapacity() != 0; + } + + public void send(AMQFrame frame) + { + send(frame, null); + } + + public void send(final AMQFrame frame, final ByteBuffer payload) + { + synchronized(_conn.getLock()) + { + try + { +// TODO HACK - check frame length + int size = _conn.getDescribedTypeRegistry() + .getValueWriter(frame.getFrameBody()).writeToBuffer(EMPTY_BYTEBUFFER) + 8; + + if(size > _conn.getMaxFrameSize()) + { + throw new OversizeFrameException(frame, size); + } + + while(!_queue.offer(frame)) + { + _conn.getLock().wait(1000L); + + } + _conn.getLock().notifyAll(); + } + catch (InterruptedException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + } + + + public void close() + { + synchronized (_conn.getLock()) + { + if(!_queue.offer(_endOfFrameMarker)) + { + _setForClose = true; + } + _conn.getLock().notifyAll(); + } + } + + public AMQFrame getNextFrame(final boolean wait) + { + synchronized(_conn.getLock()) + { + long time = System.currentTimeMillis(); + try + { + AMQFrame frame = null; + while(!closed() && (frame = _queue.poll()) == null && wait) + { + _conn.getLock().wait(_conn.getIdleTimeout()/2); + + if(_conn.getIdleTimeout()>0) + { + time = System.currentTimeMillis(); + + if(frame == null && time > _nextHeartbeat) + { + frame = new TransportFrame((short) 0,null); + break; + } + } + } + + + + + if(frame != null) + { + _nextHeartbeat = time + _conn.getIdleTimeout()/2; + + } + if(frame == _endOfFrameMarker) + { + _closed = true; + frame = null; + } + else if(_setForClose && frame != null) + { + _setForClose = !_queue.offer(_endOfFrameMarker); + } + + + if(frame != null && FRAME_LOGGER.isLoggable(Level.FINE)) + { + FRAME_LOGGER.fine("SEND[" + _conn.getRemoteAddress() + "|" + frame.getChannel() + "] : " + frame.getFrameBody()); + } + + _conn.getLock().notifyAll(); + + return frame; + } + catch (InterruptedException e) + { + _conn.setClosedForOutput(true); + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + return null; + } + } + } + + public boolean closed() + { + return _closed; + } + } + + public static interface FrameSource + { + AMQFrame getNextFrame(boolean wait); + boolean closed(); + } + + + public static interface BytesSource + { + void getBytes(BytesProcessor processor, boolean wait); + boolean closed(); + } + + public static class FrameToBytesSourceAdapter implements BytesSource + { + + private final FrameSource _frameSource; + private final FrameWriter _writer; + private static final int BUF_SIZE = 1<<16; + private final byte[] _bytes = new byte[BUF_SIZE]; + private final ByteBuffer _buffer = ByteBuffer.wrap(_bytes); + + public FrameToBytesSourceAdapter(final FrameSource frameSource, ValueWriter.Registry registry) + { + _frameSource = frameSource; + _writer = new FrameWriter(registry); + } + + public void getBytes(final BytesProcessor processor, final boolean wait) + { + + AMQFrame frame; + + if(_buffer.position() == 0 && !_frameSource.closed()) + { + if(!_writer.isComplete()) + { + _writer.writeToBuffer(_buffer); + } + + while(_buffer.hasRemaining()) + { + + if((frame = _frameSource.getNextFrame(wait && _buffer.position()==0)) != null) + { + _writer.setValue(frame); + + try + { + _writer.writeToBuffer(_buffer); + } + catch(RuntimeException e) + { + e.printStackTrace(); + throw e; + } + catch(Error e) + { + e.printStackTrace(); + throw e; + } + + + } + else + { + break; + } + } + _buffer.flip(); + } + if(_buffer.limit() != 0) + { + processor.processBytes(_buffer); + if(_buffer.remaining() == 0) + { + _buffer.clear(); + } + } + } + + public boolean closed() + { + return _buffer.position() == 0 && _frameSource.closed(); + } + } + + + public static class HeaderBytesSource implements BytesSource + { + + private final ByteBuffer _buffer; + private ConnectionEndpoint _conn; + + public HeaderBytesSource(ConnectionEndpoint conn, byte... headerBytes) + { + _conn = conn; + _buffer = ByteBuffer.wrap(headerBytes); + } + + public void getBytes(final BytesProcessor processor, final boolean wait) + { + if(!_conn.closedForOutput()) + { + processor.processBytes(_buffer); + } + } + + public boolean closed() + { + return !_conn.closedForOutput() && !_buffer.hasRemaining(); + } + } + + public static class SequentialBytesSource implements BytesSource + { + private Queue _sources = new LinkedList(); + + public SequentialBytesSource(BytesSource... sources) + { + _sources.addAll(Arrays.asList(sources)); + } + + public synchronized void addSource(BytesSource source) + { + _sources.add(source); + } + + public synchronized void getBytes(final BytesProcessor processor, final boolean wait) + { + BytesSource src = _sources.peek(); + while (src != null && src.closed()) + { + _sources.poll(); + src = _sources.peek(); + } + + if(src != null) + { + src.getBytes(processor, wait); + } + } + + public boolean closed() + { + return _sources.isEmpty(); + } + } + + + public static class BytesOutputHandler implements Runnable, BytesProcessor + { + + private final OutputStream _outputStream; + private BytesSource _bytesSource; + private boolean _closed; + private ConnectionEndpoint _conn; + + public BytesOutputHandler(OutputStream outputStream, BytesSource source, ConnectionEndpoint conn) + { + _outputStream = outputStream; + _bytesSource = source; + _conn = conn; + } + + public void run() + { + + final BytesSource bytesSource = _bytesSource; + + while(!(_closed || bytesSource.closed())) + { + _bytesSource.getBytes(this, true); + } + + } + + public void processBytes(final ByteBuffer buf) + { + try + { + if(RAW_LOGGER.isLoggable(Level.FINE)) + { + Binary bin = new Binary(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position()); + RAW_LOGGER.fine("SEND["+ _conn.getRemoteAddress() +"] : " + bin.toString()); + } + _outputStream.write(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position()); + buf.position(buf.limit()); + } + catch (IOException e) + { + _closed = true; + e.printStackTrace(); //TODO + } + } + } + + + public static class OutputHandler implements Runnable + { + + + + private final OutputStream _outputStream; + private FrameSource _frameSource; + + private static final int BUF_SIZE = 1<<16; + private ValueWriter.Registry _registry; + + + public OutputHandler(OutputStream outputStream, FrameSource source, ValueWriter.Registry registry) + { + _outputStream = outputStream; + _frameSource = source; + _registry = registry; + } + + public void run() + { + int i=0; + + + try + { + + byte[] buffer = new byte[BUF_SIZE]; + ByteBuffer buf = ByteBuffer.wrap(buffer); + + buf.put((byte)'A'); + buf.put((byte)'M'); + buf.put((byte)'Q'); + buf.put((byte)'P'); + buf.put((byte) 0); + buf.put((byte) 1); + buf.put((byte) 0); + buf.put((byte) 0); + + + + final FrameSource frameSource = _frameSource; + + AMQFrame frame; + FrameWriter writer = new FrameWriter(_registry); + + while(!frameSource.closed()) + { + + if(!writer.isComplete()) + { + writer.writeToBuffer(buf); + } + + while(buf.hasRemaining()) + { + + if((frame = frameSource.getNextFrame(buf.position()==0)) != null) + { + writer.setValue(frame); + + int size = writer.writeToBuffer(buf); + + } + else + { + break; + } + } + + if(buf.limit() != 0) + { + _outputStream.write(buffer,0, buf.position()); + buf.clear(); + } + } + + } + catch (IOException e) + { + e.printStackTrace(); + } + + } + } + + public static void main(String[] args) throws AmqpErrorException + { + byte[] buffer = new byte[76]; + ByteBuffer buf = ByteBuffer.wrap(buffer); + AMQPDescribedTypeRegistry registry = AMQPDescribedTypeRegistry.newInstance() + .registerTransportLayer() + .registerMessagingLayer() + .registerTransactionLayer(); + + Open open = new Open(); + // Open(container_id="venture", channel_max=10, hostname="foo", offered_capabilities=[Symbol("one"), Symbol("two"), Symbol("three")]) + open.setContainerId("venture"); + open.setChannelMax(UnsignedShort.valueOf((short) 10)); + open.setHostname("foo"); + open.setOfferedCapabilities(new Symbol[] {Symbol.valueOf("one"),Symbol.valueOf("two"),Symbol.valueOf("three")}); + + ValueWriter writer = registry.getValueWriter(open); + + System.out.println("------ Encode (time in ms for 1 million opens)"); + Long myLong = Long.valueOf(32); + ValueWriter writer2 = registry.getValueWriter(myLong); + Double myDouble = Double.valueOf(3.14159265359); + ValueWriter writer3 = registry.getValueWriter(myDouble); + for(int n = 0; n < 1/*00*/; n++) + { + long startTime = System.currentTimeMillis(); + for(int i = 1/*000000*/; i !=0; i--) + { + buf.position(0); + writer.setValue(open); + writer.writeToBuffer(buf); + writer2.setValue(myLong); + writer.writeToBuffer(buf); + writer3.setValue(myDouble); + writer3.writeToBuffer(buf); + + + } + long midTime = System.currentTimeMillis(); + System.out.println((midTime - startTime)); + + } + + + ValueHandler handler = new ValueHandler(registry); + System.out.println("------ Decode (time in ms for 1 million opens)"); + for(int n = 0; n < 100; n++) + { + long startTime = System.currentTimeMillis(); + for(int i = 1000000; i !=0; i--) + { + buf.flip(); + handler.parse(buf); + handler.parse(buf); + handler.parse(buf); + + } + long midTime = System.currentTimeMillis(); + System.out.println((midTime - startTime)); + } + + + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameHandler.java index 76a28d23e9..99b25d4193 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameHandler.java @@ -1,329 +1,329 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.codec.BinaryWriter; -import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; -import org.apache.qpid.amqp_1_0.codec.ValueHandler; -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.ErrorCondition; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; -import org.apache.qpid.amqp_1_0.type.transport.Transfer; - -import java.nio.ByteBuffer; -import java.util.Formatter; - -public class FrameHandler implements ProtocolHandler -{ - private ConnectionEndpoint _connection; - private ValueHandler _typeHandler; - - enum State { - SIZE_0, - SIZE_1, - SIZE_2, - SIZE_3, - PRE_PARSE, - BUFFERING, - PARSING, - ERROR - } - - private State _state = State.SIZE_0; - private int _size; - - private ByteBuffer _buffer; - - - - public FrameHandler(final ConnectionEndpoint connection) - { - _connection = connection; - _typeHandler = new ValueHandler(connection.getDescribedTypeRegistry()); - - } - - public ProtocolHandler parse(ByteBuffer in) - { - try - { - Error frameParsingError = null; - int size = _size; - State state = _state; - ByteBuffer oldIn = null; - - while(in.hasRemaining() && state != State.ERROR) - { - - final int remaining = in.remaining(); - if(remaining == 0) - { - return this; - } - - - switch(state) - { - case SIZE_0: - if(remaining >= 4) - { - size = in.getInt(); - state = State.PRE_PARSE; - break; - } - else - { - size = (in.get() << 24) & 0xFF000000; - if(!in.hasRemaining()) - { - state = State.SIZE_1; - break; - } - } - case SIZE_1: - size |= (in.get() << 16) & 0xFF0000; - if(!in.hasRemaining()) - { - state = State.SIZE_2; - break; - } - case SIZE_2: - size |= (in.get() << 8) & 0xFF00; - if(!in.hasRemaining()) - { - state = State.SIZE_3; - break; - } - case SIZE_3: - size |= in.get() & 0xFF; - state = State.PRE_PARSE; - - case PRE_PARSE: - - if(size < 8) - { - frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", _size, 8); - state = State.ERROR; - break; - } - - else if(size > _connection.getMaxFrameSize()) - { - frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connection.getMaxFrameSize()); - state = State.ERROR; - break; - } - - if(in.remaining() < size-4) - { - _buffer = ByteBuffer.allocate(size-4); - _buffer.put(in); - state = State.BUFFERING; - break; - } - case BUFFERING: - if(_buffer != null) - { - if(in.remaining() < _buffer.remaining()) - { - _buffer.put(in); - break; - } - else - { - ByteBuffer dup = in.duplicate(); - dup.limit(dup.position()+_buffer.remaining()); - int i = _buffer.remaining(); - int d = dup.remaining(); - in.position(in.position()+_buffer.remaining()); - _buffer.put(dup); - oldIn = in; - _buffer.flip(); - in = _buffer; - state = State.PARSING; - } - } - - case PARSING: - - int dataOffset = (in.get() << 2) & 0x3FF; - - if(dataOffset < 8) - { - frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8); - state = State.ERROR; - break; - } - else if(dataOffset > size) - { - frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, _size); - state = State.ERROR; - break; - } - - // type - - int type = in.get() & 0xFF; - int channel = in.getShort() & 0xFF; - - if(type != 0 && type != 1) - { - frameParsingError = createFramingError("unknown frame type: %d", type); - state = State.ERROR; - break; - } - - // channel - - /*if(channel > _connection.getChannelMax()) - { - frameParsingError = createError(AmqpError.DECODE_ERROR, - "frame received on invalid channel %d above channel-max %d", - channel, _connection.getChannelMax()); - - state = State.ERROR; - } -*/ - // ext header - if(dataOffset!=8) - { - in.position(in.position()+dataOffset-8); - } - - // oldIn null iff not working on duplicated buffer - if(oldIn == null) - { - oldIn = in; - in = in.duplicate(); - final int endPos = in.position() + size - dataOffset; - in.limit(endPos); - oldIn.position(endPos); - - } - - int inPos = in.position(); - int inLimit = in.limit(); - // PARSE HERE - try - { - Object val = _typeHandler.parse(in); - - if(in.hasRemaining()) - { - if(val instanceof Transfer) - { - ByteBuffer buf = ByteBuffer.allocate(in.remaining()); - buf.put(in); - buf.flip(); - ((Transfer)val).setPayload(buf); - } - } - - _connection.receive((short)channel,val); - reset(); - in = oldIn; - oldIn = null; - _buffer = null; - state = State.SIZE_0; - break; - - - } - catch (AmqpErrorException ex) - { - state = State.ERROR; - frameParsingError = ex.getError(); - } - catch (RuntimeException e) - { - in.position(inPos); - in.limit(inLimit); - System.err.println(toHex(in)); - throw e; - } - } - - } - - _state = state; - _size = size; - - if(_state == State.ERROR) - { - _connection.handleError(frameParsingError); - } - return this; - } - catch(RuntimeException e) - { - e.printStackTrace(); - throw e; - } - } - - private static String toHex(ByteBuffer in) - { - Formatter formatter = new Formatter(); - int count = 0; - while(in.hasRemaining()) - { - formatter.format("%02x ", in.get() & 0xFF); - if(count++ == 16) - { - formatter.format("\n"); - count = 0; - } - - } - return formatter.toString(); - } - - private Error createFramingError(String description, Object... args) - { - return createError(ConnectionError.FRAMING_ERROR, description, args); - } - - private Error createError(final ErrorCondition framingError, - final String description, - final Object... args) - { - Error error = new Error(); - error.setCondition(framingError); - Formatter formatter = new Formatter(); - error.setDescription(formatter.format(description, args).toString()); - return error; - } - - - private void reset() - { - _size = 0; - _state = State.SIZE_0; - } - - - public boolean isDone() - { - return _state == State.ERROR || _connection.closedForInput(); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.codec.BinaryWriter; +import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; +import org.apache.qpid.amqp_1_0.codec.ValueHandler; +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.ErrorCondition; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; +import org.apache.qpid.amqp_1_0.type.transport.Transfer; + +import java.nio.ByteBuffer; +import java.util.Formatter; + +public class FrameHandler implements ProtocolHandler +{ + private ConnectionEndpoint _connection; + private ValueHandler _typeHandler; + + enum State { + SIZE_0, + SIZE_1, + SIZE_2, + SIZE_3, + PRE_PARSE, + BUFFERING, + PARSING, + ERROR + } + + private State _state = State.SIZE_0; + private int _size; + + private ByteBuffer _buffer; + + + + public FrameHandler(final ConnectionEndpoint connection) + { + _connection = connection; + _typeHandler = new ValueHandler(connection.getDescribedTypeRegistry()); + + } + + public ProtocolHandler parse(ByteBuffer in) + { + try + { + Error frameParsingError = null; + int size = _size; + State state = _state; + ByteBuffer oldIn = null; + + while(in.hasRemaining() && state != State.ERROR) + { + + final int remaining = in.remaining(); + if(remaining == 0) + { + return this; + } + + + switch(state) + { + case SIZE_0: + if(remaining >= 4) + { + size = in.getInt(); + state = State.PRE_PARSE; + break; + } + else + { + size = (in.get() << 24) & 0xFF000000; + if(!in.hasRemaining()) + { + state = State.SIZE_1; + break; + } + } + case SIZE_1: + size |= (in.get() << 16) & 0xFF0000; + if(!in.hasRemaining()) + { + state = State.SIZE_2; + break; + } + case SIZE_2: + size |= (in.get() << 8) & 0xFF00; + if(!in.hasRemaining()) + { + state = State.SIZE_3; + break; + } + case SIZE_3: + size |= in.get() & 0xFF; + state = State.PRE_PARSE; + + case PRE_PARSE: + + if(size < 8) + { + frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", _size, 8); + state = State.ERROR; + break; + } + + else if(size > _connection.getMaxFrameSize()) + { + frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connection.getMaxFrameSize()); + state = State.ERROR; + break; + } + + if(in.remaining() < size-4) + { + _buffer = ByteBuffer.allocate(size-4); + _buffer.put(in); + state = State.BUFFERING; + break; + } + case BUFFERING: + if(_buffer != null) + { + if(in.remaining() < _buffer.remaining()) + { + _buffer.put(in); + break; + } + else + { + ByteBuffer dup = in.duplicate(); + dup.limit(dup.position()+_buffer.remaining()); + int i = _buffer.remaining(); + int d = dup.remaining(); + in.position(in.position()+_buffer.remaining()); + _buffer.put(dup); + oldIn = in; + _buffer.flip(); + in = _buffer; + state = State.PARSING; + } + } + + case PARSING: + + int dataOffset = (in.get() << 2) & 0x3FF; + + if(dataOffset < 8) + { + frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8); + state = State.ERROR; + break; + } + else if(dataOffset > size) + { + frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, _size); + state = State.ERROR; + break; + } + + // type + + int type = in.get() & 0xFF; + int channel = in.getShort() & 0xFF; + + if(type != 0 && type != 1) + { + frameParsingError = createFramingError("unknown frame type: %d", type); + state = State.ERROR; + break; + } + + // channel + + /*if(channel > _connection.getChannelMax()) + { + frameParsingError = createError(AmqpError.DECODE_ERROR, + "frame received on invalid channel %d above channel-max %d", + channel, _connection.getChannelMax()); + + state = State.ERROR; + } +*/ + // ext header + if(dataOffset!=8) + { + in.position(in.position()+dataOffset-8); + } + + // oldIn null iff not working on duplicated buffer + if(oldIn == null) + { + oldIn = in; + in = in.duplicate(); + final int endPos = in.position() + size - dataOffset; + in.limit(endPos); + oldIn.position(endPos); + + } + + int inPos = in.position(); + int inLimit = in.limit(); + // PARSE HERE + try + { + Object val = _typeHandler.parse(in); + + if(in.hasRemaining()) + { + if(val instanceof Transfer) + { + ByteBuffer buf = ByteBuffer.allocate(in.remaining()); + buf.put(in); + buf.flip(); + ((Transfer)val).setPayload(buf); + } + } + + _connection.receive((short)channel,val); + reset(); + in = oldIn; + oldIn = null; + _buffer = null; + state = State.SIZE_0; + break; + + + } + catch (AmqpErrorException ex) + { + state = State.ERROR; + frameParsingError = ex.getError(); + } + catch (RuntimeException e) + { + in.position(inPos); + in.limit(inLimit); + System.err.println(toHex(in)); + throw e; + } + } + + } + + _state = state; + _size = size; + + if(_state == State.ERROR) + { + _connection.handleError(frameParsingError); + } + return this; + } + catch(RuntimeException e) + { + e.printStackTrace(); + throw e; + } + } + + private static String toHex(ByteBuffer in) + { + Formatter formatter = new Formatter(); + int count = 0; + while(in.hasRemaining()) + { + formatter.format("%02x ", in.get() & 0xFF); + if(count++ == 16) + { + formatter.format("\n"); + count = 0; + } + + } + return formatter.toString(); + } + + private Error createFramingError(String description, Object... args) + { + return createError(ConnectionError.FRAMING_ERROR, description, args); + } + + private Error createError(final ErrorCondition framingError, + final String description, + final Object... args) + { + Error error = new Error(); + error.setCondition(framingError); + Formatter formatter = new Formatter(); + error.setDescription(formatter.format(description, args).toString()); + return error; + } + + + private void reset() + { + _size = 0; + _state = State.SIZE_0; + } + + + public boolean isDone() + { + return _state == State.ERROR || _connection.closedForInput(); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameParsingError.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameParsingError.java index e8bd462b87..44c2e1d533 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameParsingError.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameParsingError.java @@ -1,34 +1,34 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.framing; - -public enum FrameParsingError -{ - UNDERSIZED_FRAME_HEADER, - OVERSIZED_FRAME_HEADER, - DATA_OFFSET_IN_HEADER, - DATA_OFFSET_TOO_LARGE, - UNKNOWN_FRAME_TYPE, - CHANNEL_ID_BEYOND_MAX, - SPARE_OCTETS_IN_FRAME_BODY, - INSUFFICIENT_OCTETS_IN_FRAME_BODY, - UNKNOWN_TYPE_CODE, UNPARSABLE_TYPE; -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.framing; + +public enum FrameParsingError +{ + UNDERSIZED_FRAME_HEADER, + OVERSIZED_FRAME_HEADER, + DATA_OFFSET_IN_HEADER, + DATA_OFFSET_TOO_LARGE, + UNKNOWN_FRAME_TYPE, + CHANNEL_ID_BEYOND_MAX, + SPARE_OCTETS_IN_FRAME_BODY, + INSUFFICIENT_OCTETS_IN_FRAME_BODY, + UNKNOWN_TYPE_CODE, UNPARSABLE_TYPE; +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameTypeHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameTypeHandler.java index 6d680c8b02..902e5b0a51 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameTypeHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/FrameTypeHandler.java @@ -1,31 +1,31 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; - -public interface FrameTypeHandler extends ProtocolHandler -{ - void setExtHeaderRemaining(int size); - - void setBodySize(int size); - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; + +public interface FrameTypeHandler extends ProtocolHandler +{ + void setExtHeaderRemaining(int size); + + void setBodySize(int size); + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrame.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrame.java index 4155b605c4..eb45e4c2f5 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrame.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrame.java @@ -1,42 +1,42 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.type.SaslFrameBody; - -public final class SASLFrame extends AMQFrame -{ - - public SASLFrame(SaslFrameBody frameBody) - { - super(frameBody); - } - - @Override public short getChannel() - { - return (short)0; - } - - @Override public byte getFrameType() - { - return (byte)1; - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.type.SaslFrameBody; + +public final class SASLFrame extends AMQFrame +{ + + public SASLFrame(SaslFrameBody frameBody) + { + super(frameBody); + } + + @Override public short getChannel() + { + return (short)0; + } + + @Override public byte getFrameType() + { + return (byte)1; + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrameHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrameHandler.java index be7023bfdc..7a7a072f9d 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrameHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/SASLFrameHandler.java @@ -1,311 +1,311 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.amqp_1_0.framing; - -import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; -import org.apache.qpid.amqp_1_0.codec.ProtocolHeaderHandler; -import org.apache.qpid.amqp_1_0.codec.ValueHandler; -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.ErrorCondition; -import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.nio.ByteBuffer; -import java.util.Formatter; - -public class SASLFrameHandler implements ProtocolHandler -{ - private ConnectionEndpoint _connection; - private ValueHandler _typeHandler; - - enum State { - SIZE_0, - SIZE_1, - SIZE_2, - SIZE_3, - PRE_PARSE, - BUFFERING, - PARSING, - ERROR - } - - private State _state = State.SIZE_0; - private int _size; - - private ByteBuffer _buffer; - - - - public SASLFrameHandler(final ConnectionEndpoint connection) - { - _connection = connection; - _typeHandler = new ValueHandler(connection.getDescribedTypeRegistry()); - - } - - public ProtocolHandler parse(ByteBuffer in) - { - try - { - Error frameParsingError = null; - int size = _size; - State state = _state; - ByteBuffer oldIn = null; - - while(in.hasRemaining() && !_connection.isSASLComplete() && state != State.ERROR) - { - - final int remaining = in.remaining(); - if(remaining == 0) - { - return this; - } - - - switch(state) - { - case SIZE_0: - if(remaining >= 4) - { - size = in.getInt(); - state = State.PRE_PARSE; - break; - } - else - { - size = (in.get() << 24) & 0xFF000000; - if(!in.hasRemaining()) - { - state = State.SIZE_1; - break; - } - } - case SIZE_1: - size |= (in.get() << 16) & 0xFF0000; - if(!in.hasRemaining()) - { - state = State.SIZE_2; - break; - } - case SIZE_2: - size |= (in.get() << 8) & 0xFF00; - if(!in.hasRemaining()) - { - state = State.SIZE_3; - break; - } - case SIZE_3: - size |= in.get() & 0xFF; - state = State.PRE_PARSE; - - case PRE_PARSE: - - if(size < 8) - { - frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", _size, 8); - state = State.ERROR; - break; - } - - else if(size > _connection.getMaxFrameSize()) - { - frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connection.getMaxFrameSize()); - state = State.ERROR; - break; - } - - if(in.remaining() < size-4) - { - _buffer = ByteBuffer.allocate(size-4); - _buffer.put(in); - state = State.BUFFERING; - break; - } - case BUFFERING: - if(_buffer != null) - { - if(in.remaining() < _buffer.remaining()) - { - _buffer.put(in); - break; - } - else - { - ByteBuffer dup = in.duplicate(); - dup.limit(dup.position()+_buffer.remaining()); - int i = _buffer.remaining(); - int d = dup.remaining(); - in.position(in.position()+_buffer.remaining()); - _buffer.put(dup); - oldIn = in; - _buffer.flip(); - in = _buffer; - state = State.PARSING; - } - } - - case PARSING: - - int dataOffset = (in.get() << 2) & 0x3FF; - - if(dataOffset < 8) - { - frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8); - state = State.ERROR; - break; - } - else if(dataOffset > size) - { - frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, _size); - state = State.ERROR; - break; - } - - // type - - int type = in.get() & 0xFF; - int channel = in.getShort() & 0xFF; - - if(type != 0 && type != 1) - { - frameParsingError = createFramingError("unknown frame type: %d", type); - state = State.ERROR; - break; - } - - if(type != 1) - { - System.err.println("Wrong frame type for SASL Frame"); - } - - // channel - - /*if(channel > _connection.getChannelMax()) - { - frameParsingError = createError(AmqpError.DECODE_ERROR, - "frame received on invalid channel %d above channel-max %d", - channel, _connection.getChannelMax()); - - state = State.ERROR; - } -*/ - // ext header - if(dataOffset!=8) - { - in.position(in.position()+dataOffset-8); - } - - // oldIn null iff not working on duplicated buffer - if(oldIn == null) - { - oldIn = in; - in = in.duplicate(); - final int endPos = in.position() + size - dataOffset; - in.limit(endPos); - oldIn.position(endPos); - - } - - - // PARSE HERE - try - { - Object val = _typeHandler.parse(in); - - if(in.hasRemaining()) - { - state = State.ERROR; - frameParsingError = createFramingError("Frame length %d larger than contained frame body %s.", size, val); - - } - else - { - _connection.receive((short)channel,val); - reset(); - in = oldIn; - oldIn = null; - _buffer = null; - state = State.SIZE_0; - break; - } - - - } - catch (AmqpErrorException ex) - { - state = State.ERROR; - frameParsingError = ex.getError(); - } - } - - } - - _state = state; - _size = size; - - if(_state == State.ERROR) - { - _connection.handleError(frameParsingError); - } - if(_connection.isSASLComplete()) - { - return new ProtocolHeaderHandler(_connection); - } - else - { - return this; - } - } - catch(RuntimeException e) - { - e.printStackTrace(); - throw e; - } - } - - private Error createFramingError(String description, Object... args) - { - return createError(ConnectionError.FRAMING_ERROR, description, args); - } - - private Error createError(final ErrorCondition framingError, - final String description, - final Object... args) - { - Error error = new Error(); - error.setCondition(framingError); - Formatter formatter = new Formatter(); - error.setDescription(formatter.format(description, args).toString()); - return error; - } - - - private void reset() - { - _size = 0; - _state = State.SIZE_0; - } - - - public boolean isDone() - { - return _state == State.ERROR || _connection.closedForInput(); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.amqp_1_0.framing; + +import org.apache.qpid.amqp_1_0.codec.ProtocolHandler; +import org.apache.qpid.amqp_1_0.codec.ProtocolHeaderHandler; +import org.apache.qpid.amqp_1_0.codec.ValueHandler; +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.ErrorCondition; +import org.apache.qpid.amqp_1_0.type.transport.ConnectionError; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.nio.ByteBuffer; +import java.util.Formatter; + +public class SASLFrameHandler implements ProtocolHandler +{ + private ConnectionEndpoint _connection; + private ValueHandler _typeHandler; + + enum State { + SIZE_0, + SIZE_1, + SIZE_2, + SIZE_3, + PRE_PARSE, + BUFFERING, + PARSING, + ERROR + } + + private State _state = State.SIZE_0; + private int _size; + + private ByteBuffer _buffer; + + + + public SASLFrameHandler(final ConnectionEndpoint connection) + { + _connection = connection; + _typeHandler = new ValueHandler(connection.getDescribedTypeRegistry()); + + } + + public ProtocolHandler parse(ByteBuffer in) + { + try + { + Error frameParsingError = null; + int size = _size; + State state = _state; + ByteBuffer oldIn = null; + + while(in.hasRemaining() && !_connection.isSASLComplete() && state != State.ERROR) + { + + final int remaining = in.remaining(); + if(remaining == 0) + { + return this; + } + + + switch(state) + { + case SIZE_0: + if(remaining >= 4) + { + size = in.getInt(); + state = State.PRE_PARSE; + break; + } + else + { + size = (in.get() << 24) & 0xFF000000; + if(!in.hasRemaining()) + { + state = State.SIZE_1; + break; + } + } + case SIZE_1: + size |= (in.get() << 16) & 0xFF0000; + if(!in.hasRemaining()) + { + state = State.SIZE_2; + break; + } + case SIZE_2: + size |= (in.get() << 8) & 0xFF00; + if(!in.hasRemaining()) + { + state = State.SIZE_3; + break; + } + case SIZE_3: + size |= in.get() & 0xFF; + state = State.PRE_PARSE; + + case PRE_PARSE: + + if(size < 8) + { + frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", _size, 8); + state = State.ERROR; + break; + } + + else if(size > _connection.getMaxFrameSize()) + { + frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connection.getMaxFrameSize()); + state = State.ERROR; + break; + } + + if(in.remaining() < size-4) + { + _buffer = ByteBuffer.allocate(size-4); + _buffer.put(in); + state = State.BUFFERING; + break; + } + case BUFFERING: + if(_buffer != null) + { + if(in.remaining() < _buffer.remaining()) + { + _buffer.put(in); + break; + } + else + { + ByteBuffer dup = in.duplicate(); + dup.limit(dup.position()+_buffer.remaining()); + int i = _buffer.remaining(); + int d = dup.remaining(); + in.position(in.position()+_buffer.remaining()); + _buffer.put(dup); + oldIn = in; + _buffer.flip(); + in = _buffer; + state = State.PARSING; + } + } + + case PARSING: + + int dataOffset = (in.get() << 2) & 0x3FF; + + if(dataOffset < 8) + { + frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8); + state = State.ERROR; + break; + } + else if(dataOffset > size) + { + frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, _size); + state = State.ERROR; + break; + } + + // type + + int type = in.get() & 0xFF; + int channel = in.getShort() & 0xFF; + + if(type != 0 && type != 1) + { + frameParsingError = createFramingError("unknown frame type: %d", type); + state = State.ERROR; + break; + } + + if(type != 1) + { + System.err.println("Wrong frame type for SASL Frame"); + } + + // channel + + /*if(channel > _connection.getChannelMax()) + { + frameParsingError = createError(AmqpError.DECODE_ERROR, + "frame received on invalid channel %d above channel-max %d", + channel, _connection.getChannelMax()); + + state = State.ERROR; + } +*/ + // ext header + if(dataOffset!=8) + { + in.position(in.position()+dataOffset-8); + } + + // oldIn null iff not working on duplicated buffer + if(oldIn == null) + { + oldIn = in; + in = in.duplicate(); + final int endPos = in.position() + size - dataOffset; + in.limit(endPos); + oldIn.position(endPos); + + } + + + // PARSE HERE + try + { + Object val = _typeHandler.parse(in); + + if(in.hasRemaining()) + { + state = State.ERROR; + frameParsingError = createFramingError("Frame length %d larger than contained frame body %s.", size, val); + + } + else + { + _connection.receive((short)channel,val); + reset(); + in = oldIn; + oldIn = null; + _buffer = null; + state = State.SIZE_0; + break; + } + + + } + catch (AmqpErrorException ex) + { + state = State.ERROR; + frameParsingError = ex.getError(); + } + } + + } + + _state = state; + _size = size; + + if(_state == State.ERROR) + { + _connection.handleError(frameParsingError); + } + if(_connection.isSASLComplete()) + { + return new ProtocolHeaderHandler(_connection); + } + else + { + return this; + } + } + catch(RuntimeException e) + { + e.printStackTrace(); + throw e; + } + } + + private Error createFramingError(String description, Object... args) + { + return createError(ConnectionError.FRAMING_ERROR, description, args); + } + + private Error createError(final ErrorCondition framingError, + final String description, + final Object... args) + { + Error error = new Error(); + error.setCondition(framingError); + Formatter formatter = new Formatter(); + error.setDescription(formatter.format(description, args).toString()); + return error; + } + + + private void reset() + { + _size = 0; + _state = State.SIZE_0; + } + + + public boolean isDone() + { + return _state == State.ERROR || _connection.closedForInput(); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/MessageAttributes.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/MessageAttributes.java index 7c1c62388d..0adcb38a17 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/MessageAttributes.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/MessageAttributes.java @@ -1,27 +1,27 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.amqp_1_0.messaging; - -import org.apache.qpid.amqp_1_0.type.Symbol; - -import java.util.Map; - -public interface MessageAttributes extends Map -{ -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.amqp_1_0.messaging; + +import org.apache.qpid.amqp_1_0.type.Symbol; + +import java.util.Map; + +public interface MessageAttributes extends Map +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoder.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoder.java index f72d5848b6..63a9fd3cee 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoder.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoder.java @@ -1,35 +1,35 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.messaging; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.Section; - -import java.nio.ByteBuffer; -import java.util.List; - - -public interface SectionDecoder -{ - - public List
parseAll(ByteBuffer buf) throws AmqpErrorException; - public Section readSection(ByteBuffer buf) throws AmqpErrorException; - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.messaging; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.Section; + +import java.nio.ByteBuffer; +import java.util.List; + + +public interface SectionDecoder +{ + + public List
parseAll(ByteBuffer buf) throws AmqpErrorException; + public Section readSection(ByteBuffer buf) throws AmqpErrorException; + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoderImpl.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoderImpl.java index ed2b0094f9..6730b148af 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoderImpl.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionDecoderImpl.java @@ -1,62 +1,62 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.messaging; - -import org.apache.qpid.amqp_1_0.codec.ValueHandler; - -import org.apache.qpid.amqp_1_0.type.AmqpErrorException; -import org.apache.qpid.amqp_1_0.type.Section; -import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -public class SectionDecoderImpl implements SectionDecoder -{ - - private ValueHandler _valueHandler; - - - public SectionDecoderImpl(final AMQPDescribedTypeRegistry describedTypeRegistry) - { - _valueHandler = new ValueHandler(describedTypeRegistry); - } - - public List
parseAll(ByteBuffer buf) throws AmqpErrorException - { - - List
obj = new ArrayList
(); - while(buf.hasRemaining()) - { - Section section = (Section) _valueHandler.parse(buf); - obj.add(section); - } - - return obj; - } - - public Section readSection(ByteBuffer buf) throws AmqpErrorException - { - return (Section) _valueHandler.parse(buf); - } - - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.messaging; + +import org.apache.qpid.amqp_1_0.codec.ValueHandler; + +import org.apache.qpid.amqp_1_0.type.AmqpErrorException; +import org.apache.qpid.amqp_1_0.type.Section; +import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +public class SectionDecoderImpl implements SectionDecoder +{ + + private ValueHandler _valueHandler; + + + public SectionDecoderImpl(final AMQPDescribedTypeRegistry describedTypeRegistry) + { + _valueHandler = new ValueHandler(describedTypeRegistry); + } + + public List
parseAll(ByteBuffer buf) throws AmqpErrorException + { + + List
obj = new ArrayList
(); + while(buf.hasRemaining()) + { + Section section = (Section) _valueHandler.parse(buf); + obj.add(section); + } + + return obj; + } + + public Section readSection(ByteBuffer buf) throws AmqpErrorException + { + return (Section) _valueHandler.parse(buf); + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoder.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoder.java index bcb9112d14..dd8796293f 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoder.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoder.java @@ -1,34 +1,34 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.messaging; - -import org.apache.qpid.amqp_1_0.type.Binary; - - -public interface SectionEncoder -{ - void reset(); - - Binary getEncoding(); - - void encodeObject(Object obj); - - void encodeRaw(byte[] data); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.messaging; + +import org.apache.qpid.amqp_1_0.type.Binary; + + +public interface SectionEncoder +{ + void reset(); + + Binary getEncoding(); + + void encodeObject(Object obj); + + void encodeRaw(byte[] data); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoderImpl.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoderImpl.java index 9636dea4b7..fd5c571236 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoderImpl.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/messaging/SectionEncoderImpl.java @@ -1,111 +1,111 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.messaging; - -import org.apache.qpid.amqp_1_0.codec.ValueWriter; - -import org.apache.qpid.amqp_1_0.type.Binary; -import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; - -public class SectionEncoderImpl implements SectionEncoder -{ - private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); - private ValueWriter.Registry _registry; - - private int _totalSize = 0; - - private List _output = new ArrayList(); - private static final int DEFAULT_BUFFER_SIZE = 64 * 1024; - private ByteBuffer _current; - - public SectionEncoderImpl(final AMQPDescribedTypeRegistry describedTypeRegistry) - { - _registry = describedTypeRegistry; - reset(); - } - - public void reset() - { - _totalSize = 0; - _output.clear(); - _current = null; - - } - - public Binary getEncoding() - { - byte[] data = new byte[_totalSize]; - int offset = 0; - for(byte[] src : _output) - { - int length = src.length; - System.arraycopy(src, 0, data, offset, _totalSize - offset < length ? _totalSize - offset : length); - offset+= length; - } - return new Binary(data); - } - - public void encodeObject(Object obj) - { - final ValueWriter valueWriter = _registry.getValueWriter(obj); - valueWriter.setValue(obj); - int size = valueWriter.writeToBuffer(EMPTY_BYTE_BUFFER); - - byte[] data = new byte[size]; - _current = ByteBuffer.wrap(data); - valueWriter.writeToBuffer(_current); - _output.add(data); - - - _totalSize += size; - - - } - - public void encodeRaw(byte[] data) - { - if(_current == null) - { - byte[] buf = new byte[data.length]; - _current = ByteBuffer.wrap(buf); - _output.add(buf); - } - int remaining = _current.remaining(); - int length = data.length; - - if(remaining < length) - { - _current.put(data,0,remaining); - byte[] dst = new byte[length-remaining]; - _output.add(dst); - _current = ByteBuffer.wrap(dst).put(data,remaining,length-remaining); - } - else - { - _current.put(data); - } - _totalSize += data.length; - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.messaging; + +import org.apache.qpid.amqp_1_0.codec.ValueWriter; + +import org.apache.qpid.amqp_1_0.type.Binary; +import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +public class SectionEncoderImpl implements SectionEncoder +{ + private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); + private ValueWriter.Registry _registry; + + private int _totalSize = 0; + + private List _output = new ArrayList(); + private static final int DEFAULT_BUFFER_SIZE = 64 * 1024; + private ByteBuffer _current; + + public SectionEncoderImpl(final AMQPDescribedTypeRegistry describedTypeRegistry) + { + _registry = describedTypeRegistry; + reset(); + } + + public void reset() + { + _totalSize = 0; + _output.clear(); + _current = null; + + } + + public Binary getEncoding() + { + byte[] data = new byte[_totalSize]; + int offset = 0; + for(byte[] src : _output) + { + int length = src.length; + System.arraycopy(src, 0, data, offset, _totalSize - offset < length ? _totalSize - offset : length); + offset+= length; + } + return new Binary(data); + } + + public void encodeObject(Object obj) + { + final ValueWriter valueWriter = _registry.getValueWriter(obj); + valueWriter.setValue(obj); + int size = valueWriter.writeToBuffer(EMPTY_BYTE_BUFFER); + + byte[] data = new byte[size]; + _current = ByteBuffer.wrap(data); + valueWriter.writeToBuffer(_current); + _output.add(data); + + + _totalSize += size; + + + } + + public void encodeRaw(byte[] data) + { + if(_current == null) + { + byte[] buf = new byte[data.length]; + _current = ByteBuffer.wrap(buf); + _output.add(buf); + } + int remaining = _current.remaining(); + int length = data.length; + + if(remaining < length) + { + _current.put(data,0,remaining); + byte[] dst = new byte[length-remaining]; + _output.add(dst); + _current = ByteBuffer.wrap(dst).put(data,remaining,length-remaining); + } + else + { + _current.put(data); + } + _totalSize += data.length; + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionEndpoint.java index 17bc2caf5f..cdb2007b4a 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionEndpoint.java @@ -1,943 +1,969 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.codec.DescribedTypeConstructorRegistry; -import org.apache.qpid.amqp_1_0.codec.ValueWriter; -import org.apache.qpid.amqp_1_0.framing.AMQFrame; -import org.apache.qpid.amqp_1_0.framing.SASLFrame; -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.security.SaslChallenge; -import org.apache.qpid.amqp_1_0.type.security.SaslCode; -import org.apache.qpid.amqp_1_0.type.security.SaslInit; -import org.apache.qpid.amqp_1_0.type.security.SaslMechanisms; -import org.apache.qpid.amqp_1_0.type.security.SaslOutcome; -import org.apache.qpid.amqp_1_0.type.security.SaslResponse; -import org.apache.qpid.amqp_1_0.type.transport.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; -import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; - - -import javax.security.sasl.Sasl; -import javax.security.sasl.SaslException; -import javax.security.sasl.SaslServer; -import javax.security.sasl.SaslServerFactory; -import java.net.SocketAddress; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.security.Principal; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.logging.Level; -import java.util.logging.Logger; - - -public class ConnectionEndpoint implements DescribedTypeConstructorRegistry.Source, ValueWriter.Registry.Source, - ErrorHandler, SASLEndpoint - -{ - private static final short CONNECTION_CONTROL_CHANNEL = (short) 0; - private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); - - private final Container _container; - private Principal _user; - - private static final short DEFAULT_CHANNEL_MAX = 255; - private static final int DEFAULT_MAX_FRAME = Integer.getInteger("amqp.max_frame_size",1<<15); - - - private ConnectionState _state = ConnectionState.UNOPENED; - private short _channelMax; - private int _maxFrameSize = 4096; - private String _remoteContainerId; - - private SocketAddress _remoteAddress; - - // positioned by the *outgoing* channel - private SessionEndpoint[] _sendingSessions = new SessionEndpoint[DEFAULT_CHANNEL_MAX+1]; - - // positioned by the *incoming* channel - private SessionEndpoint[] _receivingSessions = new SessionEndpoint[DEFAULT_CHANNEL_MAX+1]; - private boolean _closedForInput; - private boolean _closedForOutput; - - private long _idleTimeout; - - private AMQPDescribedTypeRegistry _describedTypeRegistry = AMQPDescribedTypeRegistry.newInstance() - .registerTransportLayer() - .registerMessagingLayer() - .registerTransactionLayer() - .registerSecurityLayer(); - - private FrameOutputHandler _frameOutputHandler; - - private byte _majorVersion; - private byte _minorVersion; - private byte _revision; - private UnsignedInteger _handleMax = UnsignedInteger.MAX_VALUE; - private ConnectionEventListener _connectionEventListener = ConnectionEventListener.DEFAULT; - private String _password; - private final boolean _requiresSASLClient; - private final boolean _requiresSASLServer; - - - private FrameOutputHandler _saslFrameOutput; - - private boolean _saslComplete; - - private UnsignedInteger _desiredMaxFrameSize = UnsignedInteger.valueOf(DEFAULT_MAX_FRAME); - private Runnable _onSaslCompleteTask; - - private SaslServerProvider _saslServerProvider; - private SaslServer _saslServer; - private boolean _authenticated; - private String _remoteHostname; - - public ConnectionEndpoint(Container container, SaslServerProvider cbs) - { - _container = container; - _saslServerProvider = cbs; - _requiresSASLClient = false; - _requiresSASLServer = cbs != null; - } - - public ConnectionEndpoint(Container container, Principal user, String password) - { - _container = container; - _user = user; - _password = password; - _requiresSASLClient = user != null; - _requiresSASLServer = false; - } - - - public synchronized void open() - { - if(_requiresSASLClient) - { - synchronized (getLock()) - { - while(!_saslComplete) - { - try - { - getLock().wait(); - } - catch (InterruptedException e) - { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } - } - } - if(!_authenticated) - { - throw new RuntimeException("Could not connect - authentication error"); - } - } - if(_state == ConnectionState.UNOPENED) - { - sendOpen(DEFAULT_CHANNEL_MAX, DEFAULT_MAX_FRAME); - _state = ConnectionState.AWAITING_OPEN; - } - } - - public void setFrameOutputHandler(final FrameOutputHandler frameOutputHandler) - { - _frameOutputHandler = frameOutputHandler; - } - - public synchronized SessionEndpoint createSession(String name) - { - // todo assert connection state - SessionEndpoint endpoint = new SessionEndpoint(this); - short channel = getFirstFreeChannel(); - if(channel != -1) - { - _sendingSessions[channel] = endpoint; - endpoint.setSendingChannel(channel); - Begin begin = new Begin(); - begin.setNextOutgoingId(endpoint.getNextOutgoingId()); - begin.setOutgoingWindow(endpoint.getOutgoingWindowSize()); - begin.setIncomingWindow(endpoint.getIncomingWindowSize()); - - begin.setHandleMax(_handleMax); - send(channel, begin); - - } - else - { - // todo error - } - return endpoint; - } - - - public Container getContainer() - { - return _container; - } - - public Principal getUser() - { - return _user; - } - - public short getChannelMax() - { - return _channelMax; - } - - public int getMaxFrameSize() - { - return _maxFrameSize; - } - - public String getRemoteContainerId() - { - return _remoteContainerId; - } - - private void sendOpen(final short channelMax, final int maxFrameSize) - { - Open open = new Open(); - - open.setChannelMax(UnsignedShort.valueOf(DEFAULT_CHANNEL_MAX)); - open.setContainerId(_container.getId()); - open.setMaxFrameSize(getDesiredMaxFrameSize()); - open.setHostname(getRemoteHostname()); - - - send(CONNECTION_CONTROL_CHANNEL, open); - } - - public UnsignedInteger getDesiredMaxFrameSize() - { - return _desiredMaxFrameSize; - } - - - public void setDesiredMaxFrameSize(UnsignedInteger size) - { - _desiredMaxFrameSize = size; - } - - - - - private void closeSender() - { - setClosedForOutput(true); - _frameOutputHandler.close(); - } - - - short getFirstFreeChannel() - { - for(int i = 0; i<_sendingSessions.length;i++) - { - if(_sendingSessions[i]==null) - { - return (short) i; - } - } - return -1; - } - - private SessionEndpoint getSession(final short channel) - { - // TODO assert existence, check channel state - return _receivingSessions[channel]; - } - - - public synchronized void receiveOpen(short channel, Open open) - { - - _channelMax = open.getChannelMax() == null ? DEFAULT_CHANNEL_MAX - : open.getChannelMax().shortValue() < DEFAULT_CHANNEL_MAX - ? DEFAULT_CHANNEL_MAX - : open.getChannelMax().shortValue(); - - UnsignedInteger remoteDesiredMaxFrameSize = open.getMaxFrameSize() == null ? UnsignedInteger.valueOf(DEFAULT_MAX_FRAME) : open.getMaxFrameSize(); - - _maxFrameSize = (remoteDesiredMaxFrameSize.compareTo(_desiredMaxFrameSize) < 0 ? remoteDesiredMaxFrameSize : _desiredMaxFrameSize).intValue(); - - _remoteContainerId = open.getContainerId(); - - if(open.getIdleTimeOut() != null) - { - _idleTimeout = open.getIdleTimeOut().longValue(); - } - - switch(_state) - { - case UNOPENED: - sendOpen(_channelMax, _maxFrameSize); - case AWAITING_OPEN: - _state = ConnectionState.OPEN; - default: - // TODO bad stuff (connection already open) - - } - /*if(_state == ConnectionState.AWAITING_OPEN) - { - _state = ConnectionState.OPEN; - } -*/ - } - - public synchronized void receiveClose(short channel, Close close) - { - setClosedForInput(true); - _connectionEventListener.closeReceived(); - switch(_state) - { - case UNOPENED: - case AWAITING_OPEN: - Error error = new Error(); - error.setCondition(ConnectionError.CONNECTION_FORCED); - error.setDescription("Connection close sent before connection was opened"); - connectionError(error); - break; - case OPEN: - sendClose(new Close()); - break; - case CLOSE_SENT: - - default: - } - } - - protected synchronized void connectionError(Error error) - { - Close close = new Close(); - close.setError(error); - switch(_state) - { - case UNOPENED: - _state = ConnectionState.CLOSED; - break; - case AWAITING_OPEN: - case OPEN: - sendClose(close); - _state = ConnectionState.CLOSE_SENT; - case CLOSE_SENT: - case CLOSED: - // already sent our close - too late to do anything more - break; - default: - // TODO Unknown state - } - } - - public synchronized void inputClosed() - { - if(!_closedForInput) - { - _closedForInput = true; - for(int i = 0; i < _receivingSessions.length; i++) - { - if(_receivingSessions[i] != null) - { - _receivingSessions[i].end(); - _receivingSessions[i]=null; - - } - } - } - notifyAll(); - } - - private void sendClose(Close closeToSend) - { - send(CONNECTION_CONTROL_CHANNEL, closeToSend); - closeSender(); - } - - private synchronized void setClosedForInput(boolean closed) - { - _closedForInput = closed; - - notifyAll(); - } - - public synchronized void receiveBegin(short channel, Begin begin) - { - short myChannelId; - - - - if(begin.getRemoteChannel() != null) - { - myChannelId = begin.getRemoteChannel().shortValue(); - SessionEndpoint endpoint; - try - { - endpoint = _sendingSessions[myChannelId]; - } - catch(IndexOutOfBoundsException e) - { - final Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("BEGIN received on channel " + channel + " with given remote-channel " - + begin.getRemoteChannel() + " which is outside the valid range of 0 to " - + _channelMax + "."); - connectionError(error); - return; - } - if(endpoint != null) - { - if(_receivingSessions[channel] == null) - { - _receivingSessions[channel] = endpoint; - endpoint.setReceivingChannel(channel); - endpoint.setNextIncomingId(begin.getNextOutgoingId()); - endpoint.setOutgoingSessionCredit(begin.getIncomingWindow()); - } - else - { - final Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("BEGIN received on channel " + channel + " which is already in use."); - connectionError(error); - } - } - else - { - final Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("BEGIN received on channel " + channel + " with given remote-channel " - + begin.getRemoteChannel() + " which is not known as a begun session."); - connectionError(error); - } - - - } - else // Peer requesting session creation - { - - myChannelId = getFirstFreeChannel(); - if(myChannelId == -1) - { - // close any half open channel - myChannelId = getFirstFreeChannel(); - - } - - if(_receivingSessions[channel] == null) - { - SessionEndpoint endpoint = new SessionEndpoint(this,begin); - - _receivingSessions[channel] = endpoint; - _sendingSessions[myChannelId] = endpoint; - - Begin beginToSend = new Begin(); - - endpoint.setReceivingChannel(channel); - endpoint.setSendingChannel(myChannelId); - beginToSend.setRemoteChannel(UnsignedShort.valueOf(channel)); - beginToSend.setNextOutgoingId(endpoint.getNextOutgoingId()); - beginToSend.setOutgoingWindow(endpoint.getOutgoingWindowSize()); - beginToSend.setIncomingWindow(endpoint.getIncomingWindowSize()); - send(myChannelId, beginToSend); - - _connectionEventListener.remoteSessionCreation(endpoint); - } - else - { - final Error error = new Error(); - error.setCondition(ConnectionError.FRAMING_ERROR); - error.setDescription("BEGIN received on channel " + channel + " which is already in use."); - connectionError(error); - } - - } - - - - } - - - - public synchronized void receiveEnd(short channel, End end) - { - SessionEndpoint endpoint = _receivingSessions[channel]; - if(endpoint != null) - { - _receivingSessions[channel] = null; - - endpoint.end(end); - } - else - { - // TODO error - } - - } - - - public synchronized void sendEnd(short channel, End end) - { - send(channel, end); - _sendingSessions[channel] = null; - } - - public synchronized void receiveAttach(short channel, Attach attach) - { - SessionEndpoint endPoint = getSession(channel); - endPoint.receiveAttach(attach); - } - - - public synchronized void receiveDetach(short channel, Detach detach) - { - SessionEndpoint endPoint = getSession(channel); - endPoint.receiveDetach(detach); - } - - public synchronized void receiveTransfer(short channel, Transfer transfer) - { - SessionEndpoint endPoint = getSession(channel); - endPoint.receiveTransfer(transfer); - } - - public synchronized void receiveDisposition(short channel, Disposition disposition) - { - SessionEndpoint endPoint = getSession(channel); - endPoint.receiveDisposition(disposition); - } - - public synchronized void receiveFlow(short channel, Flow flow) - { - SessionEndpoint endPoint = getSession(channel); - endPoint.receiveFlow(flow); - } - - - public synchronized void send(short channel, FrameBody body) - { - send(channel, body, null); - } - - - public synchronized int send(short channel, FrameBody body, ByteBuffer payload) - { - if(!_closedForOutput) - { - ValueWriter writer = _describedTypeRegistry.getValueWriter(body); - int size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); - ByteBuffer payloadDup = payload == null ? null : payload.duplicate(); - int payloadSent = getMaxFrameSize() - (size + 9); - if(payloadSent < (payload == null ? 0 : payload.remaining())) - { - - if(body instanceof Transfer) - { - ((Transfer)body).setMore(Boolean.TRUE); - } - - writer = _describedTypeRegistry.getValueWriter(body); - size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); - payloadSent = getMaxFrameSize() - (size + 9); - - try - { - payloadDup.limit(payloadDup.position()+payloadSent); - } - catch(NullPointerException npe) - { - throw npe; - } - } - else - { - payloadSent = payload == null ? 0 : payload.remaining(); - } - _frameOutputHandler.send(AMQFrame.createAMQFrame(channel, body, payloadDup)); - return payloadSent; - } - else - { - return -1; - } - } - - - - public void invalidHeaderReceived() - { - // TODO - _closedForInput = true; - } - - public synchronized boolean closedForInput() - { - return _closedForInput; - } - - public synchronized void protocolHeaderReceived(final byte major, final byte minorVersion, final byte revision) - { - if(_requiresSASLServer && _state != ConnectionState.UNOPENED) - { - // TODO - bad stuff - } - - _majorVersion = major; - _minorVersion = minorVersion; - _revision = revision; - } - - public synchronized void handleError(final Error error) - { - if(!closedForOutput()) - { - Close close = new Close(); - close.setError(error); - send((short) 0, close); - } - _closedForInput = true; - } - - private final Logger _logger = Logger.getLogger("FRM"); - - public synchronized void receive(final short channel, final Object frame) - { - if(_logger.isLoggable(Level.FINE)) - { - _logger.fine("RECV["+ _remoteAddress + "|"+channel+"] : " + frame); - } - if(frame instanceof FrameBody) - { - ((FrameBody)frame).invoke(channel, this); - } - else if(frame instanceof SaslFrameBody) - { - ((SaslFrameBody)frame).invoke(this); - } - } - - public AMQPDescribedTypeRegistry getDescribedTypeRegistry() - { - return _describedTypeRegistry; - } - - public synchronized void setClosedForOutput(boolean b) - { - _closedForOutput = true; - notifyAll(); - } - - public synchronized boolean closedForOutput() - { - return _closedForOutput; - } - - - public Object getLock() - { - return this; - } - - public synchronized long getIdleTimeout() - { - return _idleTimeout; - } - - public synchronized void close() - { - switch(_state) - { - case AWAITING_OPEN: - case OPEN: - Close closeToSend = new Close(); - sendClose(closeToSend); - _state = ConnectionState.CLOSE_SENT; - break; - case CLOSE_SENT: - default: - } - - } - - public void setConnectionEventListener(final ConnectionEventListener connectionEventListener) - { - _connectionEventListener = connectionEventListener; - } - - public ConnectionEventListener getConnectionEventListener() - { - return _connectionEventListener; - } - - public byte getMinorVersion() - { - return _minorVersion; - } - - public byte getRevision() - { - return _revision; - } - - public byte getMajorVersion() - { - return _majorVersion; - } - - public void receiveSaslInit(final SaslInit saslInit) - { - Symbol mechanism = saslInit.getMechanism(); - final Binary initialResponse = saslInit.getInitialResponse(); - byte[] response = initialResponse == null ? new byte[0] : initialResponse.getArray(); - - - try - { - _saslServer = _saslServerProvider.getSaslServer(mechanism.toString(), "localhost"); - - // Process response from the client - byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); - - if (_saslServer.isComplete()) - { - SaslOutcome outcome = new SaslOutcome(); - - outcome.setCode(SaslCode.OK); - _saslFrameOutput.send(new SASLFrame(outcome), null); - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = true; - getLock().notifyAll(); - } - - if(_onSaslCompleteTask != null) - { - _onSaslCompleteTask.run(); - } - - } - else - { - SaslChallenge challengeBody = new SaslChallenge(); - challengeBody.setChallenge(new Binary(challenge)); - _saslFrameOutput.send(new SASLFrame(challengeBody), null); - - } - } - catch (SaslException e) - { - SaslOutcome outcome = new SaslOutcome(); - - outcome.setCode(SaslCode.AUTH); - _saslFrameOutput.send(new SASLFrame(outcome), null); - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = false; - getLock().notifyAll(); - } - if(_onSaslCompleteTask != null) - { - _onSaslCompleteTask.run(); - } - - } - } - - public void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) - { - if(Arrays.asList(saslMechanisms.getSaslServerMechanisms()).contains(Symbol.valueOf("PLAIN"))) - { - SaslInit init = new SaslInit(); - init.setMechanism(Symbol.valueOf("PLAIN")); - init.setHostname(_remoteHostname); - byte[] usernameBytes = _user.getName().getBytes(Charset.forName("UTF-8")); - byte[] passwordBytes = _password.getBytes(Charset.forName("UTF-8")); - byte[] initResponse = new byte[usernameBytes.length+passwordBytes.length+2]; - System.arraycopy(usernameBytes,0,initResponse,1,usernameBytes.length); - System.arraycopy(passwordBytes,0,initResponse,usernameBytes.length+2,passwordBytes.length); - init.setInitialResponse(new Binary(initResponse)); - _saslFrameOutput.send(new SASLFrame(init),null); - } - } - - public void receiveSaslChallenge(final SaslChallenge saslChallenge) - { - //To change body of implemented methods use File | Settings | File Templates. - } - - public void receiveSaslResponse(final SaslResponse saslResponse) - { - final Binary responseBinary = saslResponse.getResponse(); - byte[] response = responseBinary == null ? new byte[0] : responseBinary.getArray(); - - - try - { - - // Process response from the client - byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); - - if (_saslServer.isComplete()) - { - SaslOutcome outcome = new SaslOutcome(); - - outcome.setCode(SaslCode.OK); - _saslFrameOutput.send(new SASLFrame(outcome),null); - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = true; - getLock().notifyAll(); - } - if(_onSaslCompleteTask != null) - { - _onSaslCompleteTask.run(); - } - - } - else - { - SaslChallenge challengeBody = new SaslChallenge(); - challengeBody.setChallenge(new Binary(challenge)); - _saslFrameOutput.send(new SASLFrame(challengeBody), null); - - } - } - catch (SaslException e) - { - SaslOutcome outcome = new SaslOutcome(); - - outcome.setCode(SaslCode.AUTH); - _saslFrameOutput.send(new SASLFrame(outcome),null); - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = false; - getLock().notifyAll(); - } - if(_onSaslCompleteTask != null) - { - _onSaslCompleteTask.run(); - } - - } - } - - public void receiveSaslOutcome(final SaslOutcome saslOutcome) - { - if(saslOutcome.getCode() == SaslCode.OK) - { - _saslFrameOutput.close(); - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = true; - getLock().notifyAll(); - } - if(_onSaslCompleteTask != null) - { - _onSaslCompleteTask.run(); - } - } - else - { - synchronized (getLock()) - { - _saslComplete = true; - _authenticated = false; - getLock().notifyAll(); - } - setClosedForInput(true); - _saslFrameOutput.close(); - } - } - - public boolean requiresSASL() - { - return _requiresSASLClient || _requiresSASLServer; - } - - public void setSaslFrameOutput(final FrameOutputHandler saslFrameOutput) - { - _saslFrameOutput = saslFrameOutput; - } - - public void setOnSaslComplete(Runnable task) - { - _onSaslCompleteTask = task; - - } - - public boolean isAuthenticated() - { - return _authenticated; - } - - public void initiateSASL() - { - SaslMechanisms mechanisms = new SaslMechanisms(); - final Enumeration saslServerFactories = Sasl.getSaslServerFactories(); - - SaslServerFactory f; - ArrayList mechanismsList = new ArrayList(); - while(saslServerFactories.hasMoreElements()) - { - f = saslServerFactories.nextElement(); - final String[] mechanismNames = f.getMechanismNames(null); - for(String name : mechanismNames) - { - mechanismsList.add(Symbol.valueOf(name)); - } - - } - mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()])); - _saslFrameOutput.send(new SASLFrame(mechanisms), null); - } - - public boolean isSASLComplete() - { - return _saslComplete; - } - - public SocketAddress getRemoteAddress() - { - return _remoteAddress; - } - - public void setRemoteAddress(SocketAddress remoteAddress) - { - _remoteAddress = remoteAddress; - } - - public String getRemoteHostname() - { - return _remoteHostname; - } - - public void setRemoteHostname(final String remoteHostname) - { - _remoteHostname = remoteHostname; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import java.util.List; +import org.apache.qpid.amqp_1_0.codec.DescribedTypeConstructorRegistry; +import org.apache.qpid.amqp_1_0.codec.ValueWriter; +import org.apache.qpid.amqp_1_0.framing.AMQFrame; +import org.apache.qpid.amqp_1_0.framing.SASLFrame; +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.security.SaslChallenge; +import org.apache.qpid.amqp_1_0.type.security.SaslCode; +import org.apache.qpid.amqp_1_0.type.security.SaslInit; +import org.apache.qpid.amqp_1_0.type.security.SaslMechanisms; +import org.apache.qpid.amqp_1_0.type.security.SaslOutcome; +import org.apache.qpid.amqp_1_0.type.security.SaslResponse; +import org.apache.qpid.amqp_1_0.type.transport.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; +import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; + + +import javax.security.sasl.Sasl; +import javax.security.sasl.SaslException; +import javax.security.sasl.SaslServer; +import javax.security.sasl.SaslServerFactory; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.logging.Level; +import java.util.logging.Logger; + + +public class ConnectionEndpoint implements DescribedTypeConstructorRegistry.Source, ValueWriter.Registry.Source, + ErrorHandler, SASLEndpoint + +{ + private static final short CONNECTION_CONTROL_CHANNEL = (short) 0; + private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); + + private final Container _container; + private Principal _user; + + private static final short DEFAULT_CHANNEL_MAX = 255; + private static final int DEFAULT_MAX_FRAME = Integer.getInteger("amqp.max_frame_size",1<<15); + + + private ConnectionState _state = ConnectionState.UNOPENED; + private short _channelMax; + private int _maxFrameSize = 4096; + private String _remoteContainerId; + + private SocketAddress _remoteAddress; + + // positioned by the *outgoing* channel + private SessionEndpoint[] _sendingSessions = new SessionEndpoint[DEFAULT_CHANNEL_MAX+1]; + + // positioned by the *incoming* channel + private SessionEndpoint[] _receivingSessions = new SessionEndpoint[DEFAULT_CHANNEL_MAX+1]; + private boolean _closedForInput; + private boolean _closedForOutput; + + private long _idleTimeout; + + private AMQPDescribedTypeRegistry _describedTypeRegistry = AMQPDescribedTypeRegistry.newInstance() + .registerTransportLayer() + .registerMessagingLayer() + .registerTransactionLayer() + .registerSecurityLayer(); + + private FrameOutputHandler _frameOutputHandler; + + private byte _majorVersion; + private byte _minorVersion; + private byte _revision; + private UnsignedInteger _handleMax = UnsignedInteger.MAX_VALUE; + private ConnectionEventListener _connectionEventListener = ConnectionEventListener.DEFAULT; + private String _password; + private final boolean _requiresSASLClient; + private final boolean _requiresSASLServer; + + + private FrameOutputHandler _saslFrameOutput; + + private boolean _saslComplete; + + private UnsignedInteger _desiredMaxFrameSize = UnsignedInteger.valueOf(DEFAULT_MAX_FRAME); + private Runnable _onSaslCompleteTask; + + private SaslServerProvider _saslServerProvider; + private SaslServer _saslServer; + private boolean _authenticated; + private String _remoteHostname; + private Error _remoteError; + + public ConnectionEndpoint(Container container, SaslServerProvider cbs) + { + _container = container; + _saslServerProvider = cbs; + _requiresSASLClient = false; + _requiresSASLServer = cbs != null; + } + + public ConnectionEndpoint(Container container, Principal user, String password) + { + _container = container; + _user = user; + _password = password; + _requiresSASLClient = user != null; + _requiresSASLServer = false; + } + + + public synchronized void open() + { + if(_requiresSASLClient) + { + synchronized (getLock()) + { + while(!_saslComplete) + { + try + { + getLock().wait(); + } + catch (InterruptedException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + } + if(!_authenticated) + { + throw new RuntimeException("Could not connect - authentication error"); + } + } + if(_state == ConnectionState.UNOPENED) + { + sendOpen(DEFAULT_CHANNEL_MAX, DEFAULT_MAX_FRAME); + _state = ConnectionState.AWAITING_OPEN; + } + } + + public void setFrameOutputHandler(final FrameOutputHandler frameOutputHandler) + { + _frameOutputHandler = frameOutputHandler; + } + + public synchronized SessionEndpoint createSession(String name) + { + // todo assert connection state + SessionEndpoint endpoint = new SessionEndpoint(this); + short channel = getFirstFreeChannel(); + if(channel != -1) + { + _sendingSessions[channel] = endpoint; + endpoint.setSendingChannel(channel); + Begin begin = new Begin(); + begin.setNextOutgoingId(endpoint.getNextOutgoingId()); + begin.setOutgoingWindow(endpoint.getOutgoingWindowSize()); + begin.setIncomingWindow(endpoint.getIncomingWindowSize()); + + begin.setHandleMax(_handleMax); + send(channel, begin); + + } + else + { + // todo error + } + return endpoint; + } + + + public Container getContainer() + { + return _container; + } + + public Principal getUser() + { + return _user; + } + + public short getChannelMax() + { + return _channelMax; + } + + public int getMaxFrameSize() + { + return _maxFrameSize; + } + + public String getRemoteContainerId() + { + return _remoteContainerId; + } + + private void sendOpen(final short channelMax, final int maxFrameSize) + { + Open open = new Open(); + + open.setChannelMax(UnsignedShort.valueOf(DEFAULT_CHANNEL_MAX)); + open.setContainerId(_container.getId()); + open.setMaxFrameSize(getDesiredMaxFrameSize()); + open.setHostname(getRemoteHostname()); + + + send(CONNECTION_CONTROL_CHANNEL, open); + } + + public UnsignedInteger getDesiredMaxFrameSize() + { + return _desiredMaxFrameSize; + } + + + public void setDesiredMaxFrameSize(UnsignedInteger size) + { + _desiredMaxFrameSize = size; + } + + + + + private void closeSender() + { + setClosedForOutput(true); + _frameOutputHandler.close(); + } + + + short getFirstFreeChannel() + { + for(int i = 0; i<_sendingSessions.length;i++) + { + if(_sendingSessions[i]==null) + { + return (short) i; + } + } + return -1; + } + + private SessionEndpoint getSession(final short channel) + { + // TODO assert existence, check channel state + return _receivingSessions[channel]; + } + + + public synchronized void receiveOpen(short channel, Open open) + { + + _channelMax = open.getChannelMax() == null ? DEFAULT_CHANNEL_MAX + : open.getChannelMax().shortValue() < DEFAULT_CHANNEL_MAX + ? DEFAULT_CHANNEL_MAX + : open.getChannelMax().shortValue(); + + UnsignedInteger remoteDesiredMaxFrameSize = open.getMaxFrameSize() == null ? UnsignedInteger.valueOf(DEFAULT_MAX_FRAME) : open.getMaxFrameSize(); + + _maxFrameSize = (remoteDesiredMaxFrameSize.compareTo(_desiredMaxFrameSize) < 0 ? remoteDesiredMaxFrameSize : _desiredMaxFrameSize).intValue(); + + _remoteContainerId = open.getContainerId(); + + if(open.getIdleTimeOut() != null) + { + _idleTimeout = open.getIdleTimeOut().longValue(); + } + + switch(_state) + { + case UNOPENED: + sendOpen(_channelMax, _maxFrameSize); + case AWAITING_OPEN: + _state = ConnectionState.OPEN; + default: + // TODO bad stuff (connection already open) + + } + /*if(_state == ConnectionState.AWAITING_OPEN) + { + _state = ConnectionState.OPEN; + } +*/ + notifyAll(); + } + + public synchronized void receiveClose(short channel, Close close) + { + setClosedForInput(true); + _connectionEventListener.closeReceived(); + switch(_state) + { + case UNOPENED: + case AWAITING_OPEN: + Error error = new Error(); + error.setCondition(ConnectionError.CONNECTION_FORCED); + error.setDescription("Connection close sent before connection was opened"); + connectionError(error); + break; + case OPEN: + _state = ConnectionState.CLOSE_RECEIVED; + sendClose(new Close()); + _state = ConnectionState.CLOSED; + break; + case CLOSE_SENT: + _state = ConnectionState.CLOSED; + + default: + } + _remoteError = close.getError(); + + notifyAll(); + } + + protected synchronized void connectionError(Error error) + { + Close close = new Close(); + close.setError(error); + switch(_state) + { + case UNOPENED: + _state = ConnectionState.CLOSED; + break; + case AWAITING_OPEN: + case OPEN: + sendClose(close); + _state = ConnectionState.CLOSE_SENT; + case CLOSE_SENT: + case CLOSED: + // already sent our close - too late to do anything more + break; + default: + // TODO Unknown state + } + } + + public synchronized void inputClosed() + { + if(!_closedForInput) + { + _closedForInput = true; + for(int i = 0; i < _receivingSessions.length; i++) + { + if(_receivingSessions[i] != null) + { + _receivingSessions[i].end(); + _receivingSessions[i]=null; + + } + } + } + notifyAll(); + } + + private void sendClose(Close closeToSend) + { + send(CONNECTION_CONTROL_CHANNEL, closeToSend); + closeSender(); + } + + private synchronized void setClosedForInput(boolean closed) + { + _closedForInput = closed; + + notifyAll(); + } + + public synchronized void receiveBegin(short channel, Begin begin) + { + short myChannelId; + + + + if(begin.getRemoteChannel() != null) + { + myChannelId = begin.getRemoteChannel().shortValue(); + SessionEndpoint endpoint; + try + { + endpoint = _sendingSessions[myChannelId]; + } + catch(IndexOutOfBoundsException e) + { + final Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("BEGIN received on channel " + channel + " with given remote-channel " + + begin.getRemoteChannel() + " which is outside the valid range of 0 to " + + _channelMax + "."); + connectionError(error); + return; + } + if(endpoint != null) + { + if(_receivingSessions[channel] == null) + { + _receivingSessions[channel] = endpoint; + endpoint.setReceivingChannel(channel); + endpoint.setNextIncomingId(begin.getNextOutgoingId()); + endpoint.setOutgoingSessionCredit(begin.getIncomingWindow()); + } + else + { + final Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("BEGIN received on channel " + channel + " which is already in use."); + connectionError(error); + } + } + else + { + final Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("BEGIN received on channel " + channel + " with given remote-channel " + + begin.getRemoteChannel() + " which is not known as a begun session."); + connectionError(error); + } + + + } + else // Peer requesting session creation + { + + myChannelId = getFirstFreeChannel(); + if(myChannelId == -1) + { + // close any half open channel + myChannelId = getFirstFreeChannel(); + + } + + if(_receivingSessions[channel] == null) + { + SessionEndpoint endpoint = new SessionEndpoint(this,begin); + + _receivingSessions[channel] = endpoint; + _sendingSessions[myChannelId] = endpoint; + + Begin beginToSend = new Begin(); + + endpoint.setReceivingChannel(channel); + endpoint.setSendingChannel(myChannelId); + beginToSend.setRemoteChannel(UnsignedShort.valueOf(channel)); + beginToSend.setNextOutgoingId(endpoint.getNextOutgoingId()); + beginToSend.setOutgoingWindow(endpoint.getOutgoingWindowSize()); + beginToSend.setIncomingWindow(endpoint.getIncomingWindowSize()); + send(myChannelId, beginToSend); + + _connectionEventListener.remoteSessionCreation(endpoint); + } + else + { + final Error error = new Error(); + error.setCondition(ConnectionError.FRAMING_ERROR); + error.setDescription("BEGIN received on channel " + channel + " which is already in use."); + connectionError(error); + } + + } + + + + } + + + + public synchronized void receiveEnd(short channel, End end) + { + SessionEndpoint endpoint = _receivingSessions[channel]; + if(endpoint != null) + { + _receivingSessions[channel] = null; + + endpoint.end(end); + } + else + { + // TODO error + } + + } + + + public synchronized void sendEnd(short channel, End end) + { + send(channel, end); + _sendingSessions[channel] = null; + } + + public synchronized void receiveAttach(short channel, Attach attach) + { + SessionEndpoint endPoint = getSession(channel); + endPoint.receiveAttach(attach); + } + + + public synchronized void receiveDetach(short channel, Detach detach) + { + SessionEndpoint endPoint = getSession(channel); + endPoint.receiveDetach(detach); + } + + public synchronized void receiveTransfer(short channel, Transfer transfer) + { + SessionEndpoint endPoint = getSession(channel); + endPoint.receiveTransfer(transfer); + } + + public synchronized void receiveDisposition(short channel, Disposition disposition) + { + SessionEndpoint endPoint = getSession(channel); + endPoint.receiveDisposition(disposition); + } + + public synchronized void receiveFlow(short channel, Flow flow) + { + SessionEndpoint endPoint = getSession(channel); + endPoint.receiveFlow(flow); + } + + + public synchronized void send(short channel, FrameBody body) + { + send(channel, body, null); + } + + + public synchronized int send(short channel, FrameBody body, ByteBuffer payload) + { + if(!_closedForOutput) + { + ValueWriter writer = _describedTypeRegistry.getValueWriter(body); + int size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); + ByteBuffer payloadDup = payload == null ? null : payload.duplicate(); + int payloadSent = getMaxFrameSize() - (size + 9); + if(payloadSent < (payload == null ? 0 : payload.remaining())) + { + + if(body instanceof Transfer) + { + ((Transfer)body).setMore(Boolean.TRUE); + } + + writer = _describedTypeRegistry.getValueWriter(body); + size = writer.writeToBuffer(EMPTY_BYTE_BUFFER); + payloadSent = getMaxFrameSize() - (size + 9); + + try + { + payloadDup.limit(payloadDup.position()+payloadSent); + } + catch(NullPointerException npe) + { + throw npe; + } + } + else + { + payloadSent = payload == null ? 0 : payload.remaining(); + } + _frameOutputHandler.send(AMQFrame.createAMQFrame(channel, body, payloadDup)); + return payloadSent; + } + else + { + return -1; + } + } + + + + public void invalidHeaderReceived() + { + // TODO + _closedForInput = true; + } + + public synchronized boolean closedForInput() + { + return _closedForInput; + } + + public synchronized void protocolHeaderReceived(final byte major, final byte minorVersion, final byte revision) + { + if(_requiresSASLServer && _state != ConnectionState.UNOPENED) + { + // TODO - bad stuff + } + + _majorVersion = major; + _minorVersion = minorVersion; + _revision = revision; + } + + public synchronized void handleError(final Error error) + { + if(!closedForOutput()) + { + Close close = new Close(); + close.setError(error); + send((short) 0, close); + } + _closedForInput = true; + } + + private final Logger _logger = Logger.getLogger("FRM"); + + public synchronized void receive(final short channel, final Object frame) + { + if(_logger.isLoggable(Level.FINE)) + { + _logger.fine("RECV["+ _remoteAddress + "|"+channel+"] : " + frame); + } + if(frame instanceof FrameBody) + { + ((FrameBody)frame).invoke(channel, this); + } + else if(frame instanceof SaslFrameBody) + { + ((SaslFrameBody)frame).invoke(this); + } + } + + public AMQPDescribedTypeRegistry getDescribedTypeRegistry() + { + return _describedTypeRegistry; + } + + public synchronized void setClosedForOutput(boolean b) + { + _closedForOutput = true; + notifyAll(); + } + + public synchronized boolean closedForOutput() + { + return _closedForOutput; + } + + + public Object getLock() + { + return this; + } + + public synchronized long getIdleTimeout() + { + return _idleTimeout; + } + + public synchronized void close() + { + switch(_state) + { + case AWAITING_OPEN: + case OPEN: + Close closeToSend = new Close(); + sendClose(closeToSend); + _state = ConnectionState.CLOSE_SENT; + break; + case CLOSE_SENT: + default: + } + + } + + public void setConnectionEventListener(final ConnectionEventListener connectionEventListener) + { + _connectionEventListener = connectionEventListener; + } + + public ConnectionEventListener getConnectionEventListener() + { + return _connectionEventListener; + } + + public byte getMinorVersion() + { + return _minorVersion; + } + + public byte getRevision() + { + return _revision; + } + + public byte getMajorVersion() + { + return _majorVersion; + } + + public void receiveSaslInit(final SaslInit saslInit) + { + Symbol mechanism = saslInit.getMechanism(); + final Binary initialResponse = saslInit.getInitialResponse(); + byte[] response = initialResponse == null ? new byte[0] : initialResponse.getArray(); + + + try + { + _saslServer = _saslServerProvider.getSaslServer(mechanism.toString(), "localhost"); + + // Process response from the client + byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); + + if (_saslServer.isComplete()) + { + SaslOutcome outcome = new SaslOutcome(); + + outcome.setCode(SaslCode.OK); + _saslFrameOutput.send(new SASLFrame(outcome), null); + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = true; + getLock().notifyAll(); + } + + if(_onSaslCompleteTask != null) + { + _onSaslCompleteTask.run(); + } + + } + else + { + SaslChallenge challengeBody = new SaslChallenge(); + challengeBody.setChallenge(new Binary(challenge)); + _saslFrameOutput.send(new SASLFrame(challengeBody), null); + + } + } + catch (SaslException e) + { + SaslOutcome outcome = new SaslOutcome(); + + outcome.setCode(SaslCode.AUTH); + _saslFrameOutput.send(new SASLFrame(outcome), null); + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = false; + getLock().notifyAll(); + } + if(_onSaslCompleteTask != null) + { + _onSaslCompleteTask.run(); + } + + } + } + + public void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) + { + if(Arrays.asList(saslMechanisms.getSaslServerMechanisms()).contains(Symbol.valueOf("PLAIN"))) + { + SaslInit init = new SaslInit(); + init.setMechanism(Symbol.valueOf("PLAIN")); + init.setHostname(_remoteHostname); + byte[] usernameBytes = _user.getName().getBytes(Charset.forName("UTF-8")); + byte[] passwordBytes = _password.getBytes(Charset.forName("UTF-8")); + byte[] initResponse = new byte[usernameBytes.length+passwordBytes.length+2]; + System.arraycopy(usernameBytes,0,initResponse,1,usernameBytes.length); + System.arraycopy(passwordBytes,0,initResponse,usernameBytes.length+2,passwordBytes.length); + init.setInitialResponse(new Binary(initResponse)); + _saslFrameOutput.send(new SASLFrame(init),null); + } + } + + public void receiveSaslChallenge(final SaslChallenge saslChallenge) + { + //To change body of implemented methods use File | Settings | File Templates. + } + + public void receiveSaslResponse(final SaslResponse saslResponse) + { + final Binary responseBinary = saslResponse.getResponse(); + byte[] response = responseBinary == null ? new byte[0] : responseBinary.getArray(); + + + try + { + + // Process response from the client + byte[] challenge = _saslServer.evaluateResponse(response != null ? response : new byte[0]); + + if (_saslServer.isComplete()) + { + SaslOutcome outcome = new SaslOutcome(); + + outcome.setCode(SaslCode.OK); + _saslFrameOutput.send(new SASLFrame(outcome),null); + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = true; + getLock().notifyAll(); + } + if(_onSaslCompleteTask != null) + { + _onSaslCompleteTask.run(); + } + + } + else + { + SaslChallenge challengeBody = new SaslChallenge(); + challengeBody.setChallenge(new Binary(challenge)); + _saslFrameOutput.send(new SASLFrame(challengeBody), null); + + } + } + catch (SaslException e) + { + SaslOutcome outcome = new SaslOutcome(); + + outcome.setCode(SaslCode.AUTH); + _saslFrameOutput.send(new SASLFrame(outcome),null); + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = false; + getLock().notifyAll(); + } + if(_onSaslCompleteTask != null) + { + _onSaslCompleteTask.run(); + } + + } + } + + public void receiveSaslOutcome(final SaslOutcome saslOutcome) + { + if(saslOutcome.getCode() == SaslCode.OK) + { + _saslFrameOutput.close(); + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = true; + getLock().notifyAll(); + } + if(_onSaslCompleteTask != null) + { + _onSaslCompleteTask.run(); + } + } + else + { + synchronized (getLock()) + { + _saslComplete = true; + _authenticated = false; + getLock().notifyAll(); + } + setClosedForInput(true); + _saslFrameOutput.close(); + } + } + + public boolean requiresSASL() + { + return _requiresSASLClient || _requiresSASLServer; + } + + public void setSaslFrameOutput(final FrameOutputHandler saslFrameOutput) + { + _saslFrameOutput = saslFrameOutput; + } + + public void setOnSaslComplete(Runnable task) + { + _onSaslCompleteTask = task; + + } + + public boolean isAuthenticated() + { + return _authenticated; + } + + public void initiateSASL() + { + SaslMechanisms mechanisms = new SaslMechanisms(); + final Enumeration saslServerFactories = Sasl.getSaslServerFactories(); + + SaslServerFactory f; + ArrayList mechanismsList = new ArrayList(); + while(saslServerFactories.hasMoreElements()) + { + f = saslServerFactories.nextElement(); + final String[] mechanismNames = f.getMechanismNames(null); + for(String name : mechanismNames) + { + mechanismsList.add(Symbol.valueOf(name)); + } + + } + mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()])); + _saslFrameOutput.send(new SASLFrame(mechanisms), null); + } + + public boolean isSASLComplete() + { + return _saslComplete; + } + + public SocketAddress getRemoteAddress() + { + return _remoteAddress; + } + + public void setRemoteAddress(SocketAddress remoteAddress) + { + _remoteAddress = remoteAddress; + } + + public String getRemoteHostname() + { + return _remoteHostname; + } + + public void setRemoteHostname(final String remoteHostname) + { + _remoteHostname = remoteHostname; + } + + public boolean isOpen() + { + return _state == ConnectionState.OPEN; + } + + public boolean isClosed() + { + return _state == ConnectionState.CLOSED + || _state == ConnectionState.CLOSE_RECEIVED + || _state == ConnectionState.CLOSE_RECEIVED; + } + + public Error getRemoteError() + { + return _remoteError; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionState.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionState.java index a46526b58f..c4fd038a61 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionState.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ConnectionState.java @@ -1,31 +1,32 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public enum ConnectionState -{ - UNOPENED, - AWAITING_OPEN, - OPEN, - CLOSE_SENT, - CLOSED -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public enum ConnectionState +{ + UNOPENED, + AWAITING_OPEN, + OPEN, + CLOSE_RECEIVED, + CLOSE_SENT, + CLOSED +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Container.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Container.java index 2e7e2fe2ea..1edb73554a 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Container.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Container.java @@ -1,79 +1,79 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import java.lang.management.ManagementFactory; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.concurrent.atomic.AtomicInteger; - -public class Container -{ - - private static final AtomicInteger CONTAINER_ID = new AtomicInteger(0); - - private String _id; - - public Container() - { - String hostname; - try - { - InetAddress addr = InetAddress.getLocalHost(); - - - // Get hostname - hostname = addr.getHostName(); - } - catch (UnknownHostException e) - { - hostname="127.0.0.1"; - } - - String pid; - String hackForPid = ManagementFactory.getRuntimeMXBean().getName(); - if(hackForPid != null && hackForPid.contains("@")) - { - pid = hackForPid.split("@")[0]; - } - else - { - pid = "unknown"; - } - - _id = hostname + '(' + pid + ')' + ':' + CONTAINER_ID.incrementAndGet(); - - } - - - public Container(String id) - { - _id = id; - } - - public String getId() - { - return _id; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.concurrent.atomic.AtomicInteger; + +public class Container +{ + + private static final AtomicInteger CONTAINER_ID = new AtomicInteger(0); + + private String _id; + + public Container() + { + String hostname; + try + { + InetAddress addr = InetAddress.getLocalHost(); + + + // Get hostname + hostname = addr.getHostName(); + } + catch (UnknownHostException e) + { + hostname="127.0.0.1"; + } + + String pid; + String hackForPid = ManagementFactory.getRuntimeMXBean().getName(); + if(hackForPid != null && hackForPid.contains("@")) + { + pid = hackForPid.split("@")[0]; + } + else + { + pid = "unknown"; + } + + _id = hostname + '(' + pid + ')' + ':' + CONTAINER_ID.incrementAndGet(); + + } + + + public Container(String id) + { + _id = id; + } + + public String getId() + { + return _id; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/FrameOutputHandler.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/FrameOutputHandler.java index 77933702ba..2f3a49733a 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/FrameOutputHandler.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/FrameOutputHandler.java @@ -1,36 +1,36 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.framing.AMQFrame; - -import java.nio.ByteBuffer; - -public interface FrameOutputHandler -{ - boolean canSend(); - - void send(AMQFrame frame); - void send(AMQFrame frame, ByteBuffer payload); - - void close(); - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.framing.AMQFrame; + +import java.nio.ByteBuffer; + +public interface FrameOutputHandler +{ + boolean canSend(); + + void send(AMQFrame frame); + void send(AMQFrame frame, ByteBuffer payload); + + void close(); + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/LinkEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/LinkEndpoint.java index 60c1427e10..3a7af39d8a 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/LinkEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/LinkEndpoint.java @@ -1,542 +1,542 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public abstract class LinkEndpoint -{ - - private T _linkEventListener; - private DeliveryStateHandler _deliveryStateHandler; - private Object _flowTransactionId; - private SenderSettleMode _sendingSettlementMode; - private ReceiverSettleMode _receivingSettlementMode; - private Map _initialUnsettledMap; - private Map _localUnsettled; - private UnsignedInteger _lastSentCreditLimit; - - - private enum State - { - DETACHED, - ATTACH_SENT, - ATTACH_RECVD, - ATTACHED, - DETACH_SENT, - DETACH_RECVD - }; - - private final String _name; - - private SessionEndpoint _session; - - - private volatile State _state = State.DETACHED; - - private Source _source; - private Target _target; - private UnsignedInteger _deliveryCount; - private UnsignedInteger _linkCredit; - private UnsignedInteger _available; - private Boolean _drain; - private UnsignedInteger _localHandle; - private UnsignedLong _maxMessageSize; - - private Map _unsettledTransfers = new HashMap(); - - LinkEndpoint(final SessionEndpoint sessionEndpoint, String name, Map unsettled) - { - _name = name; - _session = sessionEndpoint; - _linkCredit = UnsignedInteger.valueOf(0); - _drain = Boolean.FALSE; - _localUnsettled = unsettled; - - } - - LinkEndpoint(final SessionEndpoint sessionEndpoint,final Attach attach) - { - _session = sessionEndpoint; - - _name = attach.getName(); - _initialUnsettledMap = attach.getUnsettled(); - - _state = State.ATTACH_RECVD; - } - - public String getName() - { - return _name; - } - - public abstract Role getRole(); - - public Source getSource() - { - return _source; - } - - public void setSource(final Source source) - { - _source = source; - } - - public Target getTarget() - { - return _target; - } - - public void setTarget(final Target target) - { - _target = target; - } - - public void setDeliveryCount(final UnsignedInteger deliveryCount) - { - _deliveryCount = deliveryCount; - } - - public void setLinkCredit(final UnsignedInteger linkCredit) - { - _linkCredit = linkCredit; - } - - public void setAvailable(final UnsignedInteger available) - { - _available = available; - } - - public void setDrain(final Boolean drain) - { - _drain = drain; - } - - public UnsignedInteger getDeliveryCount() - { - return _deliveryCount; - } - - public UnsignedInteger getAvailable() - { - return _available; - } - - public Boolean getDrain() - { - return _drain; - } - - public UnsignedInteger getLinkCredit() - { - return _linkCredit; - } - - public void remoteDetached(Detach detach) - { - synchronized (getLock()) - { - switch(_state) - { - case DETACH_SENT: - _state = State.DETACHED; - break; - case ATTACHED: - _state = State.DETACH_RECVD; - _linkEventListener.remoteDetached(this, detach); - break; - } - getLock().notifyAll(); - } - } - - public void receiveTransfer(final Transfer transfer, final Delivery delivery) - { - // TODO - } - - public void settledByPeer(final Binary deliveryTag) - { - // TODO - } - - public void receiveFlow(final Flow flow) - { - } - - public void addUnsettled(final Delivery unsettled) - { - synchronized(getLock()) - { - _unsettledTransfers.put(unsettled.getDeliveryTag(), unsettled); - getLock().notifyAll(); - } - } - - public void receiveDeliveryState(final Delivery unsettled, - final DeliveryState state, - final Boolean settled) - { - // TODO - synchronized(getLock()) - { - if(_deliveryStateHandler != null) - { - _deliveryStateHandler.handle(unsettled.getDeliveryTag(), state, settled); - } - - if(settled) - { - settle(unsettled.getDeliveryTag()); - } - - getLock().notifyAll(); - } - - } - - public void settle(final Binary deliveryTag) - { - Delivery delivery = _unsettledTransfers.remove(deliveryTag); - if(delivery != null) - { - getSession().settle(getRole(),delivery.getDeliveryId()); - } - - } - - public int getUnsettledCount() - { - synchronized(getLock()) - { - return _unsettledTransfers.size(); - } - } - - public void setLocalHandle(final UnsignedInteger localHandle) - { - _localHandle = localHandle; - } - - public void receiveAttach(final Attach attach) - { - synchronized(getLock()) - { - switch(_state) - { - case ATTACH_SENT: - { - - _state = State.ATTACHED; - getLock().notifyAll(); - - _initialUnsettledMap = attach.getUnsettled(); - /* TODO - don't yet handle: - - attach.getUnsettled(); - attach.getProperties(); - attach.getDurable(); - attach.getExpiryPolicy(); - attach.getTimeout(); - */ - - break; - } - - case DETACHED: - { - _state = State.ATTACHED; - getLock().notifyAll(); - } - - - } - - if(attach.getRole() == Role.SENDER) - { - _source = attach.getSource(); - } - else - { - _target = attach.getTarget(); - } - - if(getRole() == Role.SENDER) - { - _maxMessageSize = attach.getMaxMessageSize(); - } - - } - } - - public synchronized boolean isAttached() - { - return _state == State.ATTACHED; - } - - public synchronized boolean isDetached() - { - return _state == State.DETACHED; - } - - public SessionEndpoint getSession() - { - return _session; - } - - public UnsignedInteger getLocalHandle() - { - return _localHandle; - } - - public Object getLock() - { - return _session.getLock(); - } - - public void attach() - { - synchronized(getLock()) - { - Attach attachToSend = new Attach(); - attachToSend.setName(getName()); - attachToSend.setRole(getRole()); - attachToSend.setHandle(getLocalHandle()); - attachToSend.setSource(getSource()); - attachToSend.setTarget(getTarget()); - attachToSend.setSndSettleMode(getSendingSettlementMode()); - attachToSend.setRcvSettleMode(getReceivingSettlementMode()); - attachToSend.setUnsettled(_localUnsettled); - - if(getRole() == Role.SENDER) - { - attachToSend.setInitialDeliveryCount(_deliveryCount); - } - - switch(_state) - { - case DETACHED: - _state = State.ATTACH_SENT; - break; - case ATTACH_RECVD: - _state = State.ATTACHED; - break; - default: - // TODO ERROR - } - - getSession().sendAttach(attachToSend); - - getLock().notifyAll(); - - } - - } - - - public void detach() - { - detach(null, false); - } - - public void close() - { - detach(null, true); - } - - public void close(Error error) - { - detach(error, true); - } - - public void detach(Error error) - { - detach(error, false); - } - - private void detach(Error error, boolean close) - { - synchronized(getLock()) - { - //TODO - switch(_state) - { - case ATTACHED: - _state = State.DETACH_SENT; - break; - case DETACH_RECVD: - _state = State.DETACHED; - break; - default: - return; - } - - Detach detach = new Detach(); - detach.setHandle(getLocalHandle()); - if(close) - detach.setClosed(close); - detach.setError(error); - - getSession().sendDetach(detach); - - getLock().notifyAll(); - } - - } - - - - - public void setTransactionId(final Object txnId) - { - _flowTransactionId = txnId; - } - - public void sendFlowConditional() - { - if(_lastSentCreditLimit != null) - { - UnsignedInteger clientsCredit = _lastSentCreditLimit.subtract(_deliveryCount); - int i = _linkCredit.subtract(clientsCredit).compareTo(clientsCredit); - if(i >=0) - { - sendFlow(_flowTransactionId != null); - } - else - { - getSession().sendFlowConditional(); - } - } - else - { - sendFlow(_flowTransactionId != null); - } - } - - - public void sendFlow() - { - sendFlow(_flowTransactionId != null); - } - - public void sendFlow(boolean setTransactionId) - { - if(_state == State.ATTACHED || _state == State.ATTACH_SENT) - { - Flow flow = new Flow(); - flow.setLinkCredit(_linkCredit); - flow.setDeliveryCount(_deliveryCount); - _lastSentCreditLimit = _linkCredit.add(_deliveryCount); - flow.setAvailable(_available); - flow.setDrain(_drain); - if(setTransactionId) - { - flow.setProperties(Collections.singletonMap(Symbol.valueOf("txn-id"), _flowTransactionId)); - } - flow.setHandle(getLocalHandle()); - getSession().sendFlow(flow); - } - } - - public T getLinkEventListener() - { - return _linkEventListener; - } - - public void setLinkEventListener(final T linkEventListener) - { - synchronized(getLock()) - { - _linkEventListener = linkEventListener; - } - } - - public DeliveryStateHandler getDeliveryStateHandler() - { - return _deliveryStateHandler; - } - - public void setDeliveryStateHandler(final DeliveryStateHandler deliveryStateHandler) - { - synchronized(getLock()) - { - _deliveryStateHandler = deliveryStateHandler; - } - } - - public void setSendingSettlementMode(SenderSettleMode sendingSettlementMode) - { - _sendingSettlementMode = sendingSettlementMode; - } - - public SenderSettleMode getSendingSettlementMode() - { - return _sendingSettlementMode; - } - - public ReceiverSettleMode getReceivingSettlementMode() - { - return _receivingSettlementMode; - } - - public void setReceivingSettlementMode(ReceiverSettleMode receivingSettlementMode) - { - _receivingSettlementMode = receivingSettlementMode; - } - - public Map getInitialUnsettledMap() - { - return _initialUnsettledMap; - } - - - public abstract void flowStateChanged(); - - public void setLocalUnsettled(Map unsettled) - { - _localUnsettled = unsettled; - } - - @Override public String toString() - { - return "LinkEndpoint{" + - "_name='" + _name + '\'' + - ", _session=" + _session + - ", _state=" + _state + - ", _role=" + getRole() + - ", _source=" + _source + - ", _target=" + _target + - ", _transferCount=" + _deliveryCount + - ", _linkCredit=" + _linkCredit + - ", _available=" + _available + - ", _drain=" + _drain + - ", _localHandle=" + _localHandle + - ", _maxMessageSize=" + _maxMessageSize + - '}'; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public abstract class LinkEndpoint +{ + + private T _linkEventListener; + private DeliveryStateHandler _deliveryStateHandler; + private Object _flowTransactionId; + private SenderSettleMode _sendingSettlementMode; + private ReceiverSettleMode _receivingSettlementMode; + private Map _initialUnsettledMap; + private Map _localUnsettled; + private UnsignedInteger _lastSentCreditLimit; + + + private enum State + { + DETACHED, + ATTACH_SENT, + ATTACH_RECVD, + ATTACHED, + DETACH_SENT, + DETACH_RECVD + }; + + private final String _name; + + private SessionEndpoint _session; + + + private volatile State _state = State.DETACHED; + + private Source _source; + private Target _target; + private UnsignedInteger _deliveryCount; + private UnsignedInteger _linkCredit; + private UnsignedInteger _available; + private Boolean _drain; + private UnsignedInteger _localHandle; + private UnsignedLong _maxMessageSize; + + private Map _unsettledTransfers = new HashMap(); + + LinkEndpoint(final SessionEndpoint sessionEndpoint, String name, Map unsettled) + { + _name = name; + _session = sessionEndpoint; + _linkCredit = UnsignedInteger.valueOf(0); + _drain = Boolean.FALSE; + _localUnsettled = unsettled; + + } + + LinkEndpoint(final SessionEndpoint sessionEndpoint,final Attach attach) + { + _session = sessionEndpoint; + + _name = attach.getName(); + _initialUnsettledMap = attach.getUnsettled(); + + _state = State.ATTACH_RECVD; + } + + public String getName() + { + return _name; + } + + public abstract Role getRole(); + + public Source getSource() + { + return _source; + } + + public void setSource(final Source source) + { + _source = source; + } + + public Target getTarget() + { + return _target; + } + + public void setTarget(final Target target) + { + _target = target; + } + + public void setDeliveryCount(final UnsignedInteger deliveryCount) + { + _deliveryCount = deliveryCount; + } + + public void setLinkCredit(final UnsignedInteger linkCredit) + { + _linkCredit = linkCredit; + } + + public void setAvailable(final UnsignedInteger available) + { + _available = available; + } + + public void setDrain(final Boolean drain) + { + _drain = drain; + } + + public UnsignedInteger getDeliveryCount() + { + return _deliveryCount; + } + + public UnsignedInteger getAvailable() + { + return _available; + } + + public Boolean getDrain() + { + return _drain; + } + + public UnsignedInteger getLinkCredit() + { + return _linkCredit; + } + + public void remoteDetached(Detach detach) + { + synchronized (getLock()) + { + switch(_state) + { + case DETACH_SENT: + _state = State.DETACHED; + break; + case ATTACHED: + _state = State.DETACH_RECVD; + _linkEventListener.remoteDetached(this, detach); + break; + } + getLock().notifyAll(); + } + } + + public void receiveTransfer(final Transfer transfer, final Delivery delivery) + { + // TODO + } + + public void settledByPeer(final Binary deliveryTag) + { + // TODO + } + + public void receiveFlow(final Flow flow) + { + } + + public void addUnsettled(final Delivery unsettled) + { + synchronized(getLock()) + { + _unsettledTransfers.put(unsettled.getDeliveryTag(), unsettled); + getLock().notifyAll(); + } + } + + public void receiveDeliveryState(final Delivery unsettled, + final DeliveryState state, + final Boolean settled) + { + // TODO + synchronized(getLock()) + { + if(_deliveryStateHandler != null) + { + _deliveryStateHandler.handle(unsettled.getDeliveryTag(), state, settled); + } + + if(settled) + { + settle(unsettled.getDeliveryTag()); + } + + getLock().notifyAll(); + } + + } + + public void settle(final Binary deliveryTag) + { + Delivery delivery = _unsettledTransfers.remove(deliveryTag); + if(delivery != null) + { + getSession().settle(getRole(),delivery.getDeliveryId()); + } + + } + + public int getUnsettledCount() + { + synchronized(getLock()) + { + return _unsettledTransfers.size(); + } + } + + public void setLocalHandle(final UnsignedInteger localHandle) + { + _localHandle = localHandle; + } + + public void receiveAttach(final Attach attach) + { + synchronized(getLock()) + { + switch(_state) + { + case ATTACH_SENT: + { + + _state = State.ATTACHED; + getLock().notifyAll(); + + _initialUnsettledMap = attach.getUnsettled(); + /* TODO - don't yet handle: + + attach.getUnsettled(); + attach.getProperties(); + attach.getDurable(); + attach.getExpiryPolicy(); + attach.getTimeout(); + */ + + break; + } + + case DETACHED: + { + _state = State.ATTACHED; + getLock().notifyAll(); + } + + + } + + if(attach.getRole() == Role.SENDER) + { + _source = attach.getSource(); + } + else + { + _target = attach.getTarget(); + } + + if(getRole() == Role.SENDER) + { + _maxMessageSize = attach.getMaxMessageSize(); + } + + } + } + + public synchronized boolean isAttached() + { + return _state == State.ATTACHED; + } + + public synchronized boolean isDetached() + { + return _state == State.DETACHED || _session.isEnded(); + } + + public SessionEndpoint getSession() + { + return _session; + } + + public UnsignedInteger getLocalHandle() + { + return _localHandle; + } + + public Object getLock() + { + return _session.getLock(); + } + + public void attach() + { + synchronized(getLock()) + { + Attach attachToSend = new Attach(); + attachToSend.setName(getName()); + attachToSend.setRole(getRole()); + attachToSend.setHandle(getLocalHandle()); + attachToSend.setSource(getSource()); + attachToSend.setTarget(getTarget()); + attachToSend.setSndSettleMode(getSendingSettlementMode()); + attachToSend.setRcvSettleMode(getReceivingSettlementMode()); + attachToSend.setUnsettled(_localUnsettled); + + if(getRole() == Role.SENDER) + { + attachToSend.setInitialDeliveryCount(_deliveryCount); + } + + switch(_state) + { + case DETACHED: + _state = State.ATTACH_SENT; + break; + case ATTACH_RECVD: + _state = State.ATTACHED; + break; + default: + // TODO ERROR + } + + getSession().sendAttach(attachToSend); + + getLock().notifyAll(); + + } + + } + + + public void detach() + { + detach(null, false); + } + + public void close() + { + detach(null, true); + } + + public void close(Error error) + { + detach(error, true); + } + + public void detach(Error error) + { + detach(error, false); + } + + private void detach(Error error, boolean close) + { + synchronized(getLock()) + { + //TODO + switch(_state) + { + case ATTACHED: + _state = State.DETACH_SENT; + break; + case DETACH_RECVD: + _state = State.DETACHED; + break; + default: + return; + } + + Detach detach = new Detach(); + detach.setHandle(getLocalHandle()); + if(close) + detach.setClosed(close); + detach.setError(error); + + getSession().sendDetach(detach); + + getLock().notifyAll(); + } + + } + + + + + public void setTransactionId(final Object txnId) + { + _flowTransactionId = txnId; + } + + public void sendFlowConditional() + { + if(_lastSentCreditLimit != null) + { + UnsignedInteger clientsCredit = _lastSentCreditLimit.subtract(_deliveryCount); + int i = _linkCredit.subtract(clientsCredit).compareTo(clientsCredit); + if(i >=0) + { + sendFlow(_flowTransactionId != null); + } + else + { + getSession().sendFlowConditional(); + } + } + else + { + sendFlow(_flowTransactionId != null); + } + } + + + public void sendFlow() + { + sendFlow(_flowTransactionId != null); + } + + public void sendFlow(boolean setTransactionId) + { + if(_state == State.ATTACHED || _state == State.ATTACH_SENT) + { + Flow flow = new Flow(); + flow.setLinkCredit(_linkCredit); + flow.setDeliveryCount(_deliveryCount); + _lastSentCreditLimit = _linkCredit.add(_deliveryCount); + flow.setAvailable(_available); + flow.setDrain(_drain); + if(setTransactionId) + { + flow.setProperties(Collections.singletonMap(Symbol.valueOf("txn-id"), _flowTransactionId)); + } + flow.setHandle(getLocalHandle()); + getSession().sendFlow(flow); + } + } + + public T getLinkEventListener() + { + return _linkEventListener; + } + + public void setLinkEventListener(final T linkEventListener) + { + synchronized(getLock()) + { + _linkEventListener = linkEventListener; + } + } + + public DeliveryStateHandler getDeliveryStateHandler() + { + return _deliveryStateHandler; + } + + public void setDeliveryStateHandler(final DeliveryStateHandler deliveryStateHandler) + { + synchronized(getLock()) + { + _deliveryStateHandler = deliveryStateHandler; + } + } + + public void setSendingSettlementMode(SenderSettleMode sendingSettlementMode) + { + _sendingSettlementMode = sendingSettlementMode; + } + + public SenderSettleMode getSendingSettlementMode() + { + return _sendingSettlementMode; + } + + public ReceiverSettleMode getReceivingSettlementMode() + { + return _receivingSettlementMode; + } + + public void setReceivingSettlementMode(ReceiverSettleMode receivingSettlementMode) + { + _receivingSettlementMode = receivingSettlementMode; + } + + public Map getInitialUnsettledMap() + { + return _initialUnsettledMap; + } + + + public abstract void flowStateChanged(); + + public void setLocalUnsettled(Map unsettled) + { + _localUnsettled = unsettled; + } + + @Override public String toString() + { + return "LinkEndpoint{" + + "_name='" + _name + '\'' + + ", _session=" + _session + + ", _state=" + _state + + ", _role=" + getRole() + + ", _source=" + _source + + ", _target=" + _target + + ", _transferCount=" + _deliveryCount + + ", _linkCredit=" + _linkCredit + + ", _available=" + _available + + ", _drain=" + _drain + + ", _localHandle=" + _localHandle + + ", _maxMessageSize=" + _maxMessageSize + + '}'; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Node.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Node.java index fb2270b51b..fe1b535d1c 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Node.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/Node.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.transport; - -public class Node -{ - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.transport; + +public class Node +{ + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingLinkEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingLinkEndpoint.java index 5fbca0b695..e5019f9479 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingLinkEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingLinkEndpoint.java @@ -1,456 +1,456 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transaction.TransactionalState; -import org.apache.qpid.amqp_1_0.type.transport.*; - - -import java.util.*; - -public class ReceivingLinkEndpoint extends LinkEndpoint -{ - - - private UnsignedInteger _lastDeliveryId; - - private static class TransientState - { - - UnsignedInteger _deliveryId; - int _credit = 1; - boolean _settled; - - private TransientState(final UnsignedInteger transferId) - { - _deliveryId = transferId; - } - - void incrementCredit() - { - _credit++; - } - - public int getCredit() - { - return _credit; - } - - public UnsignedInteger getDeliveryId() - { - return _deliveryId; - } - - public boolean isSettled() - { - return _settled; - } - - public void setSettled(boolean settled) - { - _settled = settled; - } - } - - private Map _unsettledMap = new LinkedHashMap(); - private Map _unsettledIds = new LinkedHashMap(); - private boolean _creditWindow; - private boolean _remoteDrain; - private UnsignedInteger _remoteTransferCount; - private UnsignedInteger _drainLimit; - - - public ReceivingLinkEndpoint(final SessionEndpoint session, String name) - { - this(session,name,null); - } - - public ReceivingLinkEndpoint(final SessionEndpoint session, String name, Map unsettledMap) - { - super(session, name, unsettledMap); - setDeliveryCount(UnsignedInteger.valueOf(0)); - setLinkEventListener(ReceivingLinkListener.DEFAULT); - } - - public ReceivingLinkEndpoint(final SessionEndpoint session, final Attach attach) - { - super(session, attach); - setDeliveryCount(attach.getInitialDeliveryCount()); - setLinkEventListener(ReceivingLinkListener.DEFAULT); - setSendingSettlementMode(attach.getSndSettleMode()); - setReceivingSettlementMode(attach.getRcvSettleMode()); - } - - - @Override public Role getRole() - { - return Role.RECEIVER; - } - - @Override - public void receiveTransfer(final Transfer transfer, final Delivery delivery) - { - synchronized (getLock()) - { - TransientState transientState; - final Binary deliveryTag = delivery.getDeliveryTag(); - boolean existingState = _unsettledMap.containsKey(deliveryTag); - if(!existingState || transfer.getState() != null) - { - _unsettledMap.put(deliveryTag, transfer.getState()); - } - if(!existingState) - { - transientState = new TransientState(transfer.getDeliveryId()); - if(delivery.isSettled()) - { - transientState.setSettled(true); - } - _unsettledIds.put(deliveryTag, transientState); - setLinkCredit(getLinkCredit().subtract(UnsignedInteger.ONE)); - setDeliveryCount(getDeliveryCount().add(UnsignedInteger.ONE)); - - } - else - { - transientState = _unsettledIds.get(deliveryTag); - transientState.incrementCredit(); - if(delivery.isSettled()) - { - transientState.setSettled(true); - } - } - - if(transientState.isSettled() && delivery.isComplete()) - { - _unsettledMap.remove(deliveryTag); - } - getLinkEventListener().messageTransfer(transfer); - - - getLock().notifyAll(); - } - } - - @Override public void receiveFlow(final Flow flow) - { - synchronized (getLock()) - { - super.receiveFlow(flow); - _remoteDrain = Boolean.TRUE.equals((Boolean)flow.getDrain()); - setAvailable(flow.getAvailable()); - setDeliveryCount(flow.getDeliveryCount()); - getLock().notifyAll(); - } - } - - - public boolean isDrained() - { - return getDrain() && getDeliveryCount().equals(getDrainLimit()); - } - - @Override - public void settledByPeer(final Binary deliveryTag) - { - synchronized (getLock()) - { - // TODO XXX : need to do anything about the window here? - if(settled(deliveryTag) && _creditWindow) - { - sendFlowConditional(); - } - } - } - - public boolean settled(final Binary deliveryTag) - { - synchronized(getLock()) - { - boolean deleted; - if(deleted = (_unsettledIds.remove(deliveryTag) != null)) - { - _unsettledMap.remove(deliveryTag); - - getLock().notifyAll(); - } - - return deleted; - } - } - - public void updateDisposition(final Binary deliveryTag, DeliveryState state, boolean settled) - { - synchronized(getLock()) - { - if(_unsettledMap.containsKey(deliveryTag)) - { - boolean outcomeUpdate = false; - Outcome outcome=null; - if(state instanceof Outcome) - { - outcome = (Outcome)state; - } - else if(state instanceof TransactionalState) - { - // TODO? Is this correct - outcome = ((TransactionalState)state).getOutcome(); - } - - if(outcome != null) - { - Object oldOutcome = _unsettledMap.put(deliveryTag, outcome); - outcomeUpdate = !outcome.equals(oldOutcome); - } - - - - - TransientState transientState = _unsettledIds.get(deliveryTag); - if(outcomeUpdate || settled) - { - - final UnsignedInteger transferId = transientState.getDeliveryId(); - - getSession().updateDisposition(getRole(), transferId, transferId, state, settled); - } - - - if(settled) - { - - if(settled(deliveryTag)) - { - if(!isDetached() && _creditWindow) - { - setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); - sendFlowConditional(); - } - else - { - getSession().sendFlowConditional(); - } - } - } - getLock().notifyAll(); - } - else - { - TransientState transientState = _unsettledIds.get(deliveryTag); - if(_creditWindow) - { - setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); - sendFlowConditional(); - } - - } - } - - } - - - public void setCreditWindow() - { - setCreditWindow(true); - } - public void setCreditWindow(boolean window) - { - - _creditWindow = window; - sendFlowConditional(); - - } - - public void drain() - { - synchronized (getLock()) - { - setDrain(true); - _creditWindow = false; - _drainLimit = getDeliveryCount().add(getLinkCredit()); - sendFlow(); - getLock().notifyAll(); - } - } - - @Override - public void receiveDeliveryState(final Delivery unsettled, final DeliveryState state, final Boolean settled) - { - super.receiveDeliveryState(unsettled, state, settled); - if(_creditWindow) - { - if(Boolean.TRUE.equals(settled)) - { - setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); - sendFlowConditional(); - } - } - } - - public void requestTransactionalSend(Object txnId) - { - synchronized (getLock()) - { - setDrain(true); - _creditWindow = false; - setTransactionId(txnId); - sendFlow(); - getLock().notifyAll(); - } - } - - private void sendFlow(final Object transactionId) - { - sendFlow(); - } - - - public void clearDrain() - { - synchronized (getLock()) - { - setDrain(false); - sendFlow(); - getLock().notifyAll(); - } - } - - public void updateAllDisposition(Binary deliveryTag, DeliveryState deliveryState, boolean settled) - { - synchronized(getLock()) - { - if(!_unsettledIds.isEmpty()) - { - Binary firstTag = _unsettledIds.keySet().iterator().next(); - Binary lastTag = deliveryTag; - updateDispositions(firstTag, lastTag, deliveryState, settled); - } - } - } - - private void updateDispositions(Binary firstTag, Binary lastTag, DeliveryState state, boolean settled) - { - SortedMap ranges = new TreeMap(); - - synchronized(getLock()) - { - - Iterator iter = _unsettledIds.keySet().iterator(); - List tagsToUpdate = new ArrayList(); - Binary tag = null; - - while(iter.hasNext() && !(tag = iter.next()).equals(firstTag)); - - if(firstTag.equals(tag)) - { - tagsToUpdate.add(tag); - - UnsignedInteger deliveryId = _unsettledIds.get(firstTag).getDeliveryId(); - - UnsignedInteger first = deliveryId; - UnsignedInteger last = first; - - while(iter.hasNext()) - { - tag = iter.next(); - tagsToUpdate.add(tag); - - deliveryId = _unsettledIds.get(tag).getDeliveryId(); - - if(deliveryId.equals(last.add(UnsignedInteger.ONE))) - { - last = deliveryId; - } - else - { - ranges.put(first,last); - first = last = deliveryId; - } - - if(tag.equals(lastTag)) - { - break; - } - } - - ranges.put(first,last); - } - - if(settled) - { - - for(Binary deliveryTag : tagsToUpdate) - { - if(settled(deliveryTag) && _creditWindow) - { - setLinkCredit(getLinkCredit().add(UnsignedInteger.valueOf(1))); - } - } - sendFlowConditional(); - } - - - - for(Map.Entry range : ranges.entrySet()) - { - getSession().updateDisposition(getRole(), range.getKey(), range.getValue(), state, settled); - } - - - getLock().notifyAll(); - } - - } - - @Override - public void settle(Binary deliveryTag) - { - super.settle(deliveryTag); - if(_creditWindow) - { - sendFlowConditional(); - } - - } - - public void flowStateChanged() - { - } - - public UnsignedInteger getDrainLimit() - { - return _drainLimit; - } - - UnsignedInteger getLastDeliveryId() - { - return _lastDeliveryId; - } - - void setLastDeliveryId(UnsignedInteger lastDeliveryId) - { - _lastDeliveryId = lastDeliveryId; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transaction.TransactionalState; +import org.apache.qpid.amqp_1_0.type.transport.*; + + +import java.util.*; + +public class ReceivingLinkEndpoint extends LinkEndpoint +{ + + + private UnsignedInteger _lastDeliveryId; + + private static class TransientState + { + + UnsignedInteger _deliveryId; + int _credit = 1; + boolean _settled; + + private TransientState(final UnsignedInteger transferId) + { + _deliveryId = transferId; + } + + void incrementCredit() + { + _credit++; + } + + public int getCredit() + { + return _credit; + } + + public UnsignedInteger getDeliveryId() + { + return _deliveryId; + } + + public boolean isSettled() + { + return _settled; + } + + public void setSettled(boolean settled) + { + _settled = settled; + } + } + + private Map _unsettledMap = new LinkedHashMap(); + private Map _unsettledIds = new LinkedHashMap(); + private boolean _creditWindow; + private boolean _remoteDrain; + private UnsignedInteger _remoteTransferCount; + private UnsignedInteger _drainLimit; + + + public ReceivingLinkEndpoint(final SessionEndpoint session, String name) + { + this(session,name,null); + } + + public ReceivingLinkEndpoint(final SessionEndpoint session, String name, Map unsettledMap) + { + super(session, name, unsettledMap); + setDeliveryCount(UnsignedInteger.valueOf(0)); + setLinkEventListener(ReceivingLinkListener.DEFAULT); + } + + public ReceivingLinkEndpoint(final SessionEndpoint session, final Attach attach) + { + super(session, attach); + setDeliveryCount(attach.getInitialDeliveryCount()); + setLinkEventListener(ReceivingLinkListener.DEFAULT); + setSendingSettlementMode(attach.getSndSettleMode()); + setReceivingSettlementMode(attach.getRcvSettleMode()); + } + + + @Override public Role getRole() + { + return Role.RECEIVER; + } + + @Override + public void receiveTransfer(final Transfer transfer, final Delivery delivery) + { + synchronized (getLock()) + { + TransientState transientState; + final Binary deliveryTag = delivery.getDeliveryTag(); + boolean existingState = _unsettledMap.containsKey(deliveryTag); + if(!existingState || transfer.getState() != null) + { + _unsettledMap.put(deliveryTag, transfer.getState()); + } + if(!existingState) + { + transientState = new TransientState(transfer.getDeliveryId()); + if(delivery.isSettled()) + { + transientState.setSettled(true); + } + _unsettledIds.put(deliveryTag, transientState); + setLinkCredit(getLinkCredit().subtract(UnsignedInteger.ONE)); + setDeliveryCount(getDeliveryCount().add(UnsignedInteger.ONE)); + + } + else + { + transientState = _unsettledIds.get(deliveryTag); + transientState.incrementCredit(); + if(delivery.isSettled()) + { + transientState.setSettled(true); + } + } + + if(transientState.isSettled() && delivery.isComplete()) + { + _unsettledMap.remove(deliveryTag); + } + getLinkEventListener().messageTransfer(transfer); + + + getLock().notifyAll(); + } + } + + @Override public void receiveFlow(final Flow flow) + { + synchronized (getLock()) + { + super.receiveFlow(flow); + _remoteDrain = Boolean.TRUE.equals((Boolean)flow.getDrain()); + setAvailable(flow.getAvailable()); + setDeliveryCount(flow.getDeliveryCount()); + getLock().notifyAll(); + } + } + + + public boolean isDrained() + { + return getDrain() && getDeliveryCount().equals(getDrainLimit()); + } + + @Override + public void settledByPeer(final Binary deliveryTag) + { + synchronized (getLock()) + { + // TODO XXX : need to do anything about the window here? + if(settled(deliveryTag) && _creditWindow) + { + sendFlowConditional(); + } + } + } + + public boolean settled(final Binary deliveryTag) + { + synchronized(getLock()) + { + boolean deleted; + if(deleted = (_unsettledIds.remove(deliveryTag) != null)) + { + _unsettledMap.remove(deliveryTag); + + getLock().notifyAll(); + } + + return deleted; + } + } + + public void updateDisposition(final Binary deliveryTag, DeliveryState state, boolean settled) + { + synchronized(getLock()) + { + if(_unsettledMap.containsKey(deliveryTag)) + { + boolean outcomeUpdate = false; + Outcome outcome=null; + if(state instanceof Outcome) + { + outcome = (Outcome)state; + } + else if(state instanceof TransactionalState) + { + // TODO? Is this correct + outcome = ((TransactionalState)state).getOutcome(); + } + + if(outcome != null) + { + Object oldOutcome = _unsettledMap.put(deliveryTag, outcome); + outcomeUpdate = !outcome.equals(oldOutcome); + } + + + + + TransientState transientState = _unsettledIds.get(deliveryTag); + if(outcomeUpdate || settled) + { + + final UnsignedInteger transferId = transientState.getDeliveryId(); + + getSession().updateDisposition(getRole(), transferId, transferId, state, settled); + } + + + if(settled) + { + + if(settled(deliveryTag)) + { + if(!isDetached() && _creditWindow) + { + setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); + sendFlowConditional(); + } + else + { + getSession().sendFlowConditional(); + } + } + } + getLock().notifyAll(); + } + else + { + TransientState transientState = _unsettledIds.get(deliveryTag); + if(_creditWindow) + { + setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); + sendFlowConditional(); + } + + } + } + + } + + + public void setCreditWindow() + { + setCreditWindow(true); + } + public void setCreditWindow(boolean window) + { + + _creditWindow = window; + sendFlowConditional(); + + } + + public void drain() + { + synchronized (getLock()) + { + setDrain(true); + _creditWindow = false; + _drainLimit = getDeliveryCount().add(getLinkCredit()); + sendFlow(); + getLock().notifyAll(); + } + } + + @Override + public void receiveDeliveryState(final Delivery unsettled, final DeliveryState state, final Boolean settled) + { + super.receiveDeliveryState(unsettled, state, settled); + if(_creditWindow) + { + if(Boolean.TRUE.equals(settled)) + { + setLinkCredit(getLinkCredit().add(UnsignedInteger.ONE)); + sendFlowConditional(); + } + } + } + + public void requestTransactionalSend(Object txnId) + { + synchronized (getLock()) + { + setDrain(true); + _creditWindow = false; + setTransactionId(txnId); + sendFlow(); + getLock().notifyAll(); + } + } + + private void sendFlow(final Object transactionId) + { + sendFlow(); + } + + + public void clearDrain() + { + synchronized (getLock()) + { + setDrain(false); + sendFlow(); + getLock().notifyAll(); + } + } + + public void updateAllDisposition(Binary deliveryTag, DeliveryState deliveryState, boolean settled) + { + synchronized(getLock()) + { + if(!_unsettledIds.isEmpty()) + { + Binary firstTag = _unsettledIds.keySet().iterator().next(); + Binary lastTag = deliveryTag; + updateDispositions(firstTag, lastTag, deliveryState, settled); + } + } + } + + private void updateDispositions(Binary firstTag, Binary lastTag, DeliveryState state, boolean settled) + { + SortedMap ranges = new TreeMap(); + + synchronized(getLock()) + { + + Iterator iter = _unsettledIds.keySet().iterator(); + List tagsToUpdate = new ArrayList(); + Binary tag = null; + + while(iter.hasNext() && !(tag = iter.next()).equals(firstTag)); + + if(firstTag.equals(tag)) + { + tagsToUpdate.add(tag); + + UnsignedInteger deliveryId = _unsettledIds.get(firstTag).getDeliveryId(); + + UnsignedInteger first = deliveryId; + UnsignedInteger last = first; + + while(iter.hasNext()) + { + tag = iter.next(); + tagsToUpdate.add(tag); + + deliveryId = _unsettledIds.get(tag).getDeliveryId(); + + if(deliveryId.equals(last.add(UnsignedInteger.ONE))) + { + last = deliveryId; + } + else + { + ranges.put(first,last); + first = last = deliveryId; + } + + if(tag.equals(lastTag)) + { + break; + } + } + + ranges.put(first,last); + } + + if(settled) + { + + for(Binary deliveryTag : tagsToUpdate) + { + if(settled(deliveryTag) && _creditWindow) + { + setLinkCredit(getLinkCredit().add(UnsignedInteger.valueOf(1))); + } + } + sendFlowConditional(); + } + + + + for(Map.Entry range : ranges.entrySet()) + { + getSession().updateDisposition(getRole(), range.getKey(), range.getValue(), state, settled); + } + + + getLock().notifyAll(); + } + + } + + @Override + public void settle(Binary deliveryTag) + { + super.settle(deliveryTag); + if(_creditWindow) + { + sendFlowConditional(); + } + + } + + public void flowStateChanged() + { + } + + public UnsignedInteger getDrainLimit() + { + return _drainLimit; + } + + UnsignedInteger getLastDeliveryId() + { + return _lastDeliveryId; + } + + void setLastDeliveryId(UnsignedInteger lastDeliveryId) + { + _lastDeliveryId = lastDeliveryId; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingSessionHalfEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingSessionHalfEndpoint.java index d4b8e500d9..0fa15d4e51 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingSessionHalfEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/ReceivingSessionHalfEndpoint.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public class ReceivingSessionHalfEndpoint extends SessionHalfEndpoint -{ -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public class ReceivingSessionHalfEndpoint extends SessionHalfEndpoint +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SASLEndpointImpl.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SASLEndpointImpl.java index b371b1217c..68884e21cf 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SASLEndpointImpl.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SASLEndpointImpl.java @@ -1,291 +1,291 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.codec.DescribedTypeConstructorRegistry; -import org.apache.qpid.amqp_1_0.codec.ValueWriter; -import org.apache.qpid.amqp_1_0.type.Binary; -import org.apache.qpid.amqp_1_0.type.SaslFrameBody; -import org.apache.qpid.amqp_1_0.type.Symbol; -import org.apache.qpid.amqp_1_0.type.UnsignedInteger; -import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; -import org.apache.qpid.amqp_1_0.type.security.SaslChallenge; -import org.apache.qpid.amqp_1_0.type.security.SaslInit; -import org.apache.qpid.amqp_1_0.type.security.SaslMechanisms; -import org.apache.qpid.amqp_1_0.type.security.SaslOutcome; -import org.apache.qpid.amqp_1_0.type.security.SaslResponse; - - -import javax.security.auth.callback.CallbackHandler; -import javax.security.sasl.Sasl; -import javax.security.sasl.SaslClient; -import javax.security.sasl.SaslException; -import javax.security.sasl.SaslServer; -import java.io.PrintWriter; -import java.util.Arrays; - - -public class SASLEndpointImpl - implements DescribedTypeConstructorRegistry.Source, ValueWriter.Registry.Source, SASLEndpoint -{ - private static final short SASL_CONTROL_CHANNEL = (short) 0; - - private static final byte[] EMPTY_CHALLENGE = new byte[0]; - - private FrameTransport _transport; - - - private static enum State - { - BEGIN_SERVER, - BEGIN_CLIENT, - SENT_MECHANISMS, - SENT_INIT, - SENT_REPSONSE, - SENT_CHALLENGE, - SENT_OUTCOME - }; - - - public PrintWriter _out; - - - private State _state; - - private SaslClient _saslClient; - private SaslServer _saslServer; - - private boolean _isReadable; - private boolean _isWritable; - private boolean _closedForInput; - private boolean _closedForOutput; - - private AMQPDescribedTypeRegistry _describedTypeRegistry = AMQPDescribedTypeRegistry.newInstance().registerSecurityLayer(); - - private FrameOutputHandler _frameOutputHandler; - - private byte _majorVersion; - private byte _minorVersion; - private byte _revision; - private UnsignedInteger _handleMax = UnsignedInteger.MAX_VALUE; - private ConnectionEventListener _connectionEventListener = ConnectionEventListener.DEFAULT; - private Symbol[] _mechanisms; - private Symbol _mechanism; - - - private SASLEndpointImpl(FrameTransport transport, State initialState, Symbol... mechanisms) - { - _transport = transport; - _state = initialState; - _mechanisms = mechanisms; - } - - - public void setFrameOutputHandler(final FrameOutputHandler frameOutputHandler) - { - _frameOutputHandler = frameOutputHandler; - if(_state == State.BEGIN_SERVER) - { - sendMechanisms(); - } - } - - private synchronized void sendMechanisms() - { - SaslMechanisms saslMechanisms = new SaslMechanisms(); - - saslMechanisms.setSaslServerMechanisms(_mechanisms); - - _state = State.SENT_MECHANISMS; - - send(saslMechanisms); - } - - public boolean isReadable() - { - return _isReadable; - } - - public boolean isWritable() - { - return _isWritable; - } - - - public synchronized void send(SaslFrameBody body) - { - if(!_closedForOutput) - { - if(_out != null) - { - _out.println("SEND : " + body); - _out.flush(); - } - //_frameOutputHandler.send(new SASLFrame(body)); - } - } - - - - public void invalidHeaderReceived() - { - // TODO - _closedForInput = true; - } - - public synchronized boolean closedForInput() - { - return _closedForInput; - } - - public synchronized void protocolHeaderReceived(final byte major, final byte minorVersion, final byte revision) - { - _majorVersion = major; - _minorVersion = minorVersion; - _revision = revision; - } - - - public synchronized void receive(final short channel, final Object frame) - { - if(_out != null) - { - _out.println( "RECV["+channel+"] : " + frame); - _out.flush(); - } - if(frame instanceof SaslFrameBody) - { - ((SaslFrameBody)frame).invoke(this); - } - else - { - // TODO - } - } - - public AMQPDescribedTypeRegistry getDescribedTypeRegistry() - { - return _describedTypeRegistry; - } - - public synchronized void setClosedForOutput(boolean b) - { - _closedForOutput = true; - notifyAll(); - } - - public synchronized boolean closedForOutput() - { - return _closedForOutput; - } - - - public Object getLock() - { - return this; - } - - - public byte getMajorVersion() - { - return _majorVersion; - } - - - public byte getMinorVersion() - { - return _minorVersion; - } - - public byte getRevision() - { - return _revision; - } - - - public void receiveSaslInit(final SaslInit saslInit) - { - _mechanism = saslInit.getMechanism(); - try - { - _saslServer = Sasl.createSaslServer(_mechanism.toString(), "AMQP", "localhost", null, createServerCallbackHandler(_mechanism)); - } - catch (SaslException e) - { - e.printStackTrace(); //TODO - } - } - - private CallbackHandler createServerCallbackHandler(final Symbol mechanism) - { - return null; //TODO - } - - public synchronized void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) - { - Symbol[] serverMechanisms = saslMechanisms.getSaslServerMechanisms(); - for(Symbol mechanism : _mechanisms) - { - if(Arrays.asList(serverMechanisms).contains(mechanism)) - { - _mechanism = mechanism; - break; - } - } - // TODO - no matching mechanism - try - { - _saslClient = Sasl.createSaslClient(new String[] { _mechanism.toString() }, null, "AMQP", "localhost", null, - createClientCallbackHandler(_mechanism)); - SaslInit init = new SaslInit(); - init.setMechanism(_mechanism); - init.setInitialResponse(_saslClient.hasInitialResponse() ? new Binary(_saslClient.evaluateChallenge(EMPTY_CHALLENGE)) : null); - send(init); - } - catch (SaslException e) - { - e.printStackTrace(); //TODO - } - } - - private CallbackHandler createClientCallbackHandler(final Symbol mechanism) - { - return null; //TODO - } - - public void receiveSaslChallenge(final SaslChallenge saslChallenge) - { - //TODO - } - - public void receiveSaslResponse(final SaslResponse saslResponse) - { - //TODO - } - - - public void receiveSaslOutcome(final SaslOutcome saslOutcome) - { - //TODO - } - - - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.codec.DescribedTypeConstructorRegistry; +import org.apache.qpid.amqp_1_0.codec.ValueWriter; +import org.apache.qpid.amqp_1_0.type.Binary; +import org.apache.qpid.amqp_1_0.type.SaslFrameBody; +import org.apache.qpid.amqp_1_0.type.Symbol; +import org.apache.qpid.amqp_1_0.type.UnsignedInteger; +import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry; +import org.apache.qpid.amqp_1_0.type.security.SaslChallenge; +import org.apache.qpid.amqp_1_0.type.security.SaslInit; +import org.apache.qpid.amqp_1_0.type.security.SaslMechanisms; +import org.apache.qpid.amqp_1_0.type.security.SaslOutcome; +import org.apache.qpid.amqp_1_0.type.security.SaslResponse; + + +import javax.security.auth.callback.CallbackHandler; +import javax.security.sasl.Sasl; +import javax.security.sasl.SaslClient; +import javax.security.sasl.SaslException; +import javax.security.sasl.SaslServer; +import java.io.PrintWriter; +import java.util.Arrays; + + +public class SASLEndpointImpl + implements DescribedTypeConstructorRegistry.Source, ValueWriter.Registry.Source, SASLEndpoint +{ + private static final short SASL_CONTROL_CHANNEL = (short) 0; + + private static final byte[] EMPTY_CHALLENGE = new byte[0]; + + private FrameTransport _transport; + + + private static enum State + { + BEGIN_SERVER, + BEGIN_CLIENT, + SENT_MECHANISMS, + SENT_INIT, + SENT_REPSONSE, + SENT_CHALLENGE, + SENT_OUTCOME + }; + + + public PrintWriter _out; + + + private State _state; + + private SaslClient _saslClient; + private SaslServer _saslServer; + + private boolean _isReadable; + private boolean _isWritable; + private boolean _closedForInput; + private boolean _closedForOutput; + + private AMQPDescribedTypeRegistry _describedTypeRegistry = AMQPDescribedTypeRegistry.newInstance().registerSecurityLayer(); + + private FrameOutputHandler _frameOutputHandler; + + private byte _majorVersion; + private byte _minorVersion; + private byte _revision; + private UnsignedInteger _handleMax = UnsignedInteger.MAX_VALUE; + private ConnectionEventListener _connectionEventListener = ConnectionEventListener.DEFAULT; + private Symbol[] _mechanisms; + private Symbol _mechanism; + + + private SASLEndpointImpl(FrameTransport transport, State initialState, Symbol... mechanisms) + { + _transport = transport; + _state = initialState; + _mechanisms = mechanisms; + } + + + public void setFrameOutputHandler(final FrameOutputHandler frameOutputHandler) + { + _frameOutputHandler = frameOutputHandler; + if(_state == State.BEGIN_SERVER) + { + sendMechanisms(); + } + } + + private synchronized void sendMechanisms() + { + SaslMechanisms saslMechanisms = new SaslMechanisms(); + + saslMechanisms.setSaslServerMechanisms(_mechanisms); + + _state = State.SENT_MECHANISMS; + + send(saslMechanisms); + } + + public boolean isReadable() + { + return _isReadable; + } + + public boolean isWritable() + { + return _isWritable; + } + + + public synchronized void send(SaslFrameBody body) + { + if(!_closedForOutput) + { + if(_out != null) + { + _out.println("SEND : " + body); + _out.flush(); + } + //_frameOutputHandler.send(new SASLFrame(body)); + } + } + + + + public void invalidHeaderReceived() + { + // TODO + _closedForInput = true; + } + + public synchronized boolean closedForInput() + { + return _closedForInput; + } + + public synchronized void protocolHeaderReceived(final byte major, final byte minorVersion, final byte revision) + { + _majorVersion = major; + _minorVersion = minorVersion; + _revision = revision; + } + + + public synchronized void receive(final short channel, final Object frame) + { + if(_out != null) + { + _out.println( "RECV["+channel+"] : " + frame); + _out.flush(); + } + if(frame instanceof SaslFrameBody) + { + ((SaslFrameBody)frame).invoke(this); + } + else + { + // TODO + } + } + + public AMQPDescribedTypeRegistry getDescribedTypeRegistry() + { + return _describedTypeRegistry; + } + + public synchronized void setClosedForOutput(boolean b) + { + _closedForOutput = true; + notifyAll(); + } + + public synchronized boolean closedForOutput() + { + return _closedForOutput; + } + + + public Object getLock() + { + return this; + } + + + public byte getMajorVersion() + { + return _majorVersion; + } + + + public byte getMinorVersion() + { + return _minorVersion; + } + + public byte getRevision() + { + return _revision; + } + + + public void receiveSaslInit(final SaslInit saslInit) + { + _mechanism = saslInit.getMechanism(); + try + { + _saslServer = Sasl.createSaslServer(_mechanism.toString(), "AMQP", "localhost", null, createServerCallbackHandler(_mechanism)); + } + catch (SaslException e) + { + e.printStackTrace(); //TODO + } + } + + private CallbackHandler createServerCallbackHandler(final Symbol mechanism) + { + return null; //TODO + } + + public synchronized void receiveSaslMechanisms(final SaslMechanisms saslMechanisms) + { + Symbol[] serverMechanisms = saslMechanisms.getSaslServerMechanisms(); + for(Symbol mechanism : _mechanisms) + { + if(Arrays.asList(serverMechanisms).contains(mechanism)) + { + _mechanism = mechanism; + break; + } + } + // TODO - no matching mechanism + try + { + _saslClient = Sasl.createSaslClient(new String[] { _mechanism.toString() }, null, "AMQP", "localhost", null, + createClientCallbackHandler(_mechanism)); + SaslInit init = new SaslInit(); + init.setMechanism(_mechanism); + init.setInitialResponse(_saslClient.hasInitialResponse() ? new Binary(_saslClient.evaluateChallenge(EMPTY_CHALLENGE)) : null); + send(init); + } + catch (SaslException e) + { + e.printStackTrace(); //TODO + } + } + + private CallbackHandler createClientCallbackHandler(final Symbol mechanism) + { + return null; //TODO + } + + public void receiveSaslChallenge(final SaslChallenge saslChallenge) + { + //TODO + } + + public void receiveSaslResponse(final SaslResponse saslResponse) + { + //TODO + } + + + public void receiveSaslOutcome(final SaslOutcome saslOutcome) + { + //TODO + } + + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingLinkEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingLinkEndpoint.java index a53b3661be..16e198e957 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingLinkEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingLinkEndpoint.java @@ -1,214 +1,214 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.transport.Attach; -import org.apache.qpid.amqp_1_0.type.transport.Flow; -import org.apache.qpid.amqp_1_0.type.transport.Role; -import org.apache.qpid.amqp_1_0.type.transport.Transfer; - -import java.util.HashMap; -import java.util.Map; - -public class SendingLinkEndpoint extends LinkEndpoint -{ - - private UnsignedInteger _lastDeliveryId; - private Binary _lastDeliveryTag; - private Map _unsettledMap = new HashMap(); - private Binary _transactionId; - - public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, String name) - { - this(sessionEndpoint, name, null); - } - - public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, String name, Map unsettled) - { - super(sessionEndpoint, name, unsettled); - init(); - } - - public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, final Attach attach) - { - super(sessionEndpoint, attach); - setSendingSettlementMode(attach.getSndSettleMode()); - setReceivingSettlementMode(attach.getRcvSettleMode()); - init(); - } - - private void init() - { - setDeliveryCount(UnsignedInteger.valueOf(0)); - setAvailable(UnsignedInteger.valueOf(0)); - setLinkEventListener(SendingLinkListener.DEFAULT); - } - - @Override public Role getRole() - { - return Role.SENDER; - } - - public boolean transfer(final Transfer xfr) - { - SessionEndpoint s = getSession(); - int transferCount; - transferCount = _lastDeliveryTag == null ? 1 : 1; - xfr.setMessageFormat(UnsignedInteger.ZERO); - synchronized(getLock()) - { - - final int currentCredit = getLinkCredit().intValue() - transferCount; - - if(currentCredit < 0) - { - return false; - } - else - { - setLinkCredit(UnsignedInteger.valueOf((int)currentCredit)); - } - - setDeliveryCount(UnsignedInteger.valueOf((getDeliveryCount().intValue() + transferCount))); - - xfr.setHandle(getLocalHandle()); - - s.sendTransfer(xfr, this, !xfr.getDeliveryTag().equals(_lastDeliveryTag)); - - if(!Boolean.TRUE.equals(xfr.getSettled())) - { - _unsettledMap.put(xfr.getDeliveryTag(), xfr.getDeliveryId()); - } - } - - if(Boolean.TRUE.equals(xfr.getMore())) - { - _lastDeliveryTag = xfr.getDeliveryTag(); - } - else - { - _lastDeliveryTag = null; - } - - return true; - } - - - public void drained() - { - synchronized (getLock()) - { - setDeliveryCount(getDeliveryCount().add(getLinkCredit())); - setLinkCredit(UnsignedInteger.ZERO); - sendFlow(); - } - } - - @Override - public void receiveFlow(final Flow flow) - { - super.receiveFlow(flow); - UnsignedInteger t = flow.getDeliveryCount(); - UnsignedInteger c = flow.getLinkCredit(); - setDrain(flow.getDrain()); - - Map options; - if((options = flow.getProperties()) != null) - { - _transactionId = (Binary) options.get(Symbol.valueOf("txn-id")); - } - - if(t == null) - { - setLinkCredit(c); - } - else - { - UnsignedInteger limit = t.add(c); - if(limit.compareTo(getDeliveryCount())<=0) - { - setLinkCredit(UnsignedInteger.valueOf(0)); - } - else - { - setLinkCredit(limit.subtract(getDeliveryCount())); - } - } - getLinkEventListener().flowStateChanged(); - - } - - @Override - public void flowStateChanged() - { - getLinkEventListener().flowStateChanged(); - } - - public boolean hasCreditToSend() - { - UnsignedInteger linkCredit = getLinkCredit(); - return linkCredit != null && (linkCredit.compareTo(UnsignedInteger.valueOf(0)) > 0) - && getSession().hasCreditToSend(); - } - - public void receiveDeliveryState(final Delivery unsettled, - final DeliveryState state, - final Boolean settled) - { - super.receiveDeliveryState(unsettled, state, settled); - if(settled) - { - _unsettledMap.remove(unsettled.getDeliveryTag()); - } - } - - public UnsignedInteger getLastDeliveryId() - { - return _lastDeliveryId; - } - - public void setLastDeliveryId(final UnsignedInteger deliveryId) - { - _lastDeliveryId = deliveryId; - } - - public void updateDisposition(final Binary deliveryTag, DeliveryState state, boolean settled) - { - synchronized(getLock()) - { - UnsignedInteger deliveryId; - if(settled && (deliveryId = _unsettledMap.remove(deliveryTag))!=null) - { - settle(deliveryTag); - getSession().updateDisposition(getRole(), deliveryId, deliveryId, state, settled); - } - - } - } - - public Binary getTransactionId() - { - return _transactionId; - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.transport.Attach; +import org.apache.qpid.amqp_1_0.type.transport.Flow; +import org.apache.qpid.amqp_1_0.type.transport.Role; +import org.apache.qpid.amqp_1_0.type.transport.Transfer; + +import java.util.HashMap; +import java.util.Map; + +public class SendingLinkEndpoint extends LinkEndpoint +{ + + private UnsignedInteger _lastDeliveryId; + private Binary _lastDeliveryTag; + private Map _unsettledMap = new HashMap(); + private Binary _transactionId; + + public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, String name) + { + this(sessionEndpoint, name, null); + } + + public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, String name, Map unsettled) + { + super(sessionEndpoint, name, unsettled); + init(); + } + + public SendingLinkEndpoint(final SessionEndpoint sessionEndpoint, final Attach attach) + { + super(sessionEndpoint, attach); + setSendingSettlementMode(attach.getSndSettleMode()); + setReceivingSettlementMode(attach.getRcvSettleMode()); + init(); + } + + private void init() + { + setDeliveryCount(UnsignedInteger.valueOf(0)); + setAvailable(UnsignedInteger.valueOf(0)); + setLinkEventListener(SendingLinkListener.DEFAULT); + } + + @Override public Role getRole() + { + return Role.SENDER; + } + + public boolean transfer(final Transfer xfr) + { + SessionEndpoint s = getSession(); + int transferCount; + transferCount = _lastDeliveryTag == null ? 1 : 1; + xfr.setMessageFormat(UnsignedInteger.ZERO); + synchronized(getLock()) + { + + final int currentCredit = getLinkCredit().intValue() - transferCount; + + if(currentCredit < 0) + { + return false; + } + else + { + setLinkCredit(UnsignedInteger.valueOf((int)currentCredit)); + } + + setDeliveryCount(UnsignedInteger.valueOf((getDeliveryCount().intValue() + transferCount))); + + xfr.setHandle(getLocalHandle()); + + s.sendTransfer(xfr, this, !xfr.getDeliveryTag().equals(_lastDeliveryTag)); + + if(!Boolean.TRUE.equals(xfr.getSettled())) + { + _unsettledMap.put(xfr.getDeliveryTag(), xfr.getDeliveryId()); + } + } + + if(Boolean.TRUE.equals(xfr.getMore())) + { + _lastDeliveryTag = xfr.getDeliveryTag(); + } + else + { + _lastDeliveryTag = null; + } + + return true; + } + + + public void drained() + { + synchronized (getLock()) + { + setDeliveryCount(getDeliveryCount().add(getLinkCredit())); + setLinkCredit(UnsignedInteger.ZERO); + sendFlow(); + } + } + + @Override + public void receiveFlow(final Flow flow) + { + super.receiveFlow(flow); + UnsignedInteger t = flow.getDeliveryCount(); + UnsignedInteger c = flow.getLinkCredit(); + setDrain(flow.getDrain()); + + Map options; + if((options = flow.getProperties()) != null) + { + _transactionId = (Binary) options.get(Symbol.valueOf("txn-id")); + } + + if(t == null) + { + setLinkCredit(c); + } + else + { + UnsignedInteger limit = t.add(c); + if(limit.compareTo(getDeliveryCount())<=0) + { + setLinkCredit(UnsignedInteger.valueOf(0)); + } + else + { + setLinkCredit(limit.subtract(getDeliveryCount())); + } + } + getLinkEventListener().flowStateChanged(); + + } + + @Override + public void flowStateChanged() + { + getLinkEventListener().flowStateChanged(); + } + + public boolean hasCreditToSend() + { + UnsignedInteger linkCredit = getLinkCredit(); + return linkCredit != null && (linkCredit.compareTo(UnsignedInteger.valueOf(0)) > 0) + && getSession().hasCreditToSend(); + } + + public void receiveDeliveryState(final Delivery unsettled, + final DeliveryState state, + final Boolean settled) + { + super.receiveDeliveryState(unsettled, state, settled); + if(settled) + { + _unsettledMap.remove(unsettled.getDeliveryTag()); + } + } + + public UnsignedInteger getLastDeliveryId() + { + return _lastDeliveryId; + } + + public void setLastDeliveryId(final UnsignedInteger deliveryId) + { + _lastDeliveryId = deliveryId; + } + + public void updateDisposition(final Binary deliveryTag, DeliveryState state, boolean settled) + { + synchronized(getLock()) + { + UnsignedInteger deliveryId; + if(settled && (deliveryId = _unsettledMap.remove(deliveryTag))!=null) + { + settle(deliveryTag); + getSession().updateDisposition(getRole(), deliveryId, deliveryId, state, settled); + } + + } + } + + public Binary getTransactionId() + { + return _transactionId; + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingSessionHalfEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingSessionHalfEndpoint.java index 2bc9a42dc5..995df132d7 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingSessionHalfEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SendingSessionHalfEndpoint.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public class SendingSessionHalfEndpoint extends SessionHalfEndpoint -{ -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public class SendingSessionHalfEndpoint extends SessionHalfEndpoint +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SequenceNumber.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SequenceNumber.java index 3f6490c2aa..b67ff32f0e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SequenceNumber.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SequenceNumber.java @@ -1,110 +1,110 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public class SequenceNumber implements Comparable, Cloneable -{ - private int _seqNo; - - public SequenceNumber(int seqNo) - { - _seqNo = seqNo; - } - - public SequenceNumber incr() - { - _seqNo++; - return this; - } - - public SequenceNumber decr() - { - _seqNo--; - return this; - } - - public static SequenceNumber add(SequenceNumber a, int i) - { - return a.clone().add(i); - } - - public static SequenceNumber subtract(SequenceNumber a, int i) - { - return a.clone().add(-i); - } - - private SequenceNumber add(int i) - { - _seqNo+=i; - return this; - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - SequenceNumber that = (SequenceNumber) o; - - if (_seqNo != that._seqNo) - { - return false; - } - - return true; - } - - @Override - public int hashCode() - { - return _seqNo; - } - - public int compareTo(SequenceNumber o) - { - return _seqNo - o._seqNo; - } - - @Override - public SequenceNumber clone() - { - return new SequenceNumber(_seqNo); - } - - @Override - public String toString() - { - return "SN{" + _seqNo + '}'; - } - - public int intValue() - { - return _seqNo; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public class SequenceNumber implements Comparable, Cloneable +{ + private int _seqNo; + + public SequenceNumber(int seqNo) + { + _seqNo = seqNo; + } + + public SequenceNumber incr() + { + _seqNo++; + return this; + } + + public SequenceNumber decr() + { + _seqNo--; + return this; + } + + public static SequenceNumber add(SequenceNumber a, int i) + { + return a.clone().add(i); + } + + public static SequenceNumber subtract(SequenceNumber a, int i) + { + return a.clone().add(-i); + } + + private SequenceNumber add(int i) + { + _seqNo+=i; + return this; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + SequenceNumber that = (SequenceNumber) o; + + if (_seqNo != that._seqNo) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + return _seqNo; + } + + public int compareTo(SequenceNumber o) + { + return _seqNo - o._seqNo; + } + + @Override + public SequenceNumber clone() + { + return new SequenceNumber(_seqNo); + } + + @Override + public String toString() + { + return "SN{" + _seqNo + '}'; + } + + public int intValue() + { + return _seqNo; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionAttachment.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionAttachment.java index 6bb1e922e5..63fd59c41a 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionAttachment.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionAttachment.java @@ -1,92 +1,92 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public class SessionAttachment -{ - private final SessionEndpoint _sessionEndpoint; - private final ConnectionEndpoint _connectionEndpoint; - private final short _channel; - - public SessionAttachment(SessionEndpoint sessionEndpoint, ConnectionEndpoint connectionEndpoint, short channel) - { - _sessionEndpoint = sessionEndpoint; - _connectionEndpoint = connectionEndpoint; - _channel = channel; - } - - public SessionEndpoint getSessionEndpoint() - { - return _sessionEndpoint; - } - - public ConnectionEndpoint getConnectionEndpoint() - { - return _connectionEndpoint; - } - - public short getChannel() - { - return _channel; - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - SessionAttachment that = (SessionAttachment) o; - - if (_channel != that._channel) - { - return false; - } - if (_connectionEndpoint != null - ? !_connectionEndpoint.equals(that._connectionEndpoint) - : that._connectionEndpoint != null) - { - return false; - } - if (_sessionEndpoint != null ? !_sessionEndpoint.equals(that._sessionEndpoint) : that._sessionEndpoint != null) - { - return false; - } - - return true; - } - - @Override - public int hashCode() - { - int result = _sessionEndpoint != null ? _sessionEndpoint.hashCode() : 0; - result = 31 * result + (_connectionEndpoint != null ? _connectionEndpoint.hashCode() : 0); - result = 31 * result + (int) _channel; - return result; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public class SessionAttachment +{ + private final SessionEndpoint _sessionEndpoint; + private final ConnectionEndpoint _connectionEndpoint; + private final short _channel; + + public SessionAttachment(SessionEndpoint sessionEndpoint, ConnectionEndpoint connectionEndpoint, short channel) + { + _sessionEndpoint = sessionEndpoint; + _connectionEndpoint = connectionEndpoint; + _channel = channel; + } + + public SessionEndpoint getSessionEndpoint() + { + return _sessionEndpoint; + } + + public ConnectionEndpoint getConnectionEndpoint() + { + return _connectionEndpoint; + } + + public short getChannel() + { + return _channel; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + SessionAttachment that = (SessionAttachment) o; + + if (_channel != that._channel) + { + return false; + } + if (_connectionEndpoint != null + ? !_connectionEndpoint.equals(that._connectionEndpoint) + : that._connectionEndpoint != null) + { + return false; + } + if (_sessionEndpoint != null ? !_sessionEndpoint.equals(that._sessionEndpoint) : that._sessionEndpoint != null) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = _sessionEndpoint != null ? _sessionEndpoint.hashCode() : 0; + result = 31 * result + (_connectionEndpoint != null ? _connectionEndpoint.hashCode() : 0); + result = 31 * result + (int) _channel; + return result; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionEndpoint.java index 17f90fef59..0ab2e1a9b7 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionEndpoint.java @@ -1,797 +1,806 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.framing.OversizeFrameException; -import org.apache.qpid.amqp_1_0.type.*; -import org.apache.qpid.amqp_1_0.type.messaging.Source; -import org.apache.qpid.amqp_1_0.type.messaging.Target; -import org.apache.qpid.amqp_1_0.type.messaging.TerminusDurability; -import org.apache.qpid.amqp_1_0.type.messaging.TerminusExpiryPolicy; -import org.apache.qpid.amqp_1_0.type.transaction.*; -import org.apache.qpid.amqp_1_0.type.transaction.TxnCapability; -import org.apache.qpid.amqp_1_0.type.transport.*; -import org.apache.qpid.amqp_1_0.type.transport.Error; - - -import java.nio.ByteBuffer; -import java.util.*; - -public class SessionEndpoint -{ - private SessionState _state = SessionState.INACTIVE; - - private final Map _linkMap = new HashMap(); - private final Map _localLinkEndpoints = new HashMap(); - private final Map _remoteLinkEndpoints = new HashMap(); - - private long _timeout; - - - private ConnectionEndpoint _connection; - private long _lastAttachedTime; - - private short _receivingChannel; - private short _sendingChannel; - - private LinkedHashMap _outgoingUnsettled; - private LinkedHashMap _incomingUnsettled; - - // has to be a power of two - private static final int DEFAULT_SESSION_BUFFER_SIZE = 1 << 11; - private static final int BUFFER_SIZE_MASK = DEFAULT_SESSION_BUFFER_SIZE - 1; - - private SequenceNumber _nextIncomingTransferId; - private SequenceNumber _nextOutgoingTransferId; - - private int _nextOutgoingDeliveryId; - - //private SequenceNumber _incomingLWM; - //private SequenceNumber _outgoingLWM; - - private UnsignedInteger _outgoingSessionCredit; - - - - private UnsignedInteger _initialOutgoingId; - - private SessionEventListener _sessionEventListener = SessionEventListener.DEFAULT; - - private int _availableIncomingCredit; - private int _availableOutgoingCredit; - private UnsignedInteger _lastSentIncomingLimit; - - public SessionEndpoint(final ConnectionEndpoint connectionEndpoint) - { - this(connectionEndpoint, UnsignedInteger.valueOf(0)); - } - - public SessionEndpoint(final ConnectionEndpoint connectionEndpoint, Begin begin) - { - this(connectionEndpoint, UnsignedInteger.valueOf(0)); - _state = SessionState.BEGIN_RECVD; - _nextIncomingTransferId = new SequenceNumber(begin.getNextOutgoingId().intValue()); - } - - - public SessionEndpoint(final ConnectionEndpoint connectionEndpoint, UnsignedInteger nextOutgoingId) - { - _connection = connectionEndpoint; - - _initialOutgoingId = nextOutgoingId; - _nextOutgoingTransferId = new SequenceNumber(nextOutgoingId.intValue()); - - _outgoingUnsettled = new LinkedHashMap(DEFAULT_SESSION_BUFFER_SIZE); - _incomingUnsettled = new LinkedHashMap(DEFAULT_SESSION_BUFFER_SIZE); - _availableIncomingCredit = DEFAULT_SESSION_BUFFER_SIZE; - _availableOutgoingCredit = DEFAULT_SESSION_BUFFER_SIZE; - } - - - public void setReceivingChannel(final short receivingChannel) - { - _receivingChannel = receivingChannel; - switch(_state) - { - case INACTIVE: - _state = SessionState.BEGIN_RECVD; - break; - case BEGIN_SENT: - _state = SessionState.ACTIVE; - break; - default: - // TODO error - - } - } - - - public void setSendingChannel(final short sendingChannel) - { - _sendingChannel = sendingChannel; - switch(_state) - { - case INACTIVE: - _state = SessionState.BEGIN_SENT; - break; - case BEGIN_RECVD: - _state = SessionState.ACTIVE; - break; - default: - // TODO error - - } - } - - public SessionState getState() - { - return _state; - } - - public void end() - { - end(null); - } - - public void end(final End end) - { - synchronized(getLock()) - { - switch(_state) - { - case END_SENT: - _state = SessionState.ENDED; - break; - case ACTIVE: - detachLinks(); - _sessionEventListener.remoteEnd(end); - short sendChannel = getSendingChannel(); - _connection.sendEnd(sendChannel, new End()); - _state = end == null ? SessionState.END_SENT : SessionState.ENDED; - break; - default: - sendChannel = getSendingChannel(); - End reply = new End(); - Error error = new Error(); - error.setCondition(AmqpError.ILLEGAL_STATE); - error.setDescription("END called on Session which has not been opened"); - reply.setError(error); - _connection.sendEnd(sendChannel, reply); - break; - - - } - getLock().notifyAll(); - } - } - - private void detachLinks() - { - Collection handles = new ArrayList(_remoteLinkEndpoints.keySet()); - for(UnsignedInteger handle : handles) - { - detach(handle, null); - } - } - - public short getSendingChannel() - { - return _sendingChannel; - } - - - public void receiveAttach(final Attach attach) - { - if(_state == SessionState.ACTIVE) - { - UnsignedInteger handle = attach.getHandle(); - if(_remoteLinkEndpoints.containsKey(handle)) - { - // TODO - Error - handle busy? - } - else - { - LinkEndpoint endpoint = getLinkMap().get(attach.getName()); - if(endpoint == null) - { - endpoint = attach.getRole() == Role.RECEIVER - ? new SendingLinkEndpoint(this, attach) - : new ReceivingLinkEndpoint(this, attach); - - // TODO : fix below - distinguish between local and remote owned - endpoint.setSource(attach.getSource()); - endpoint.setTarget(attach.getTarget()); - - - } - - if(attach.getRole() == Role.SENDER) - { - endpoint.setDeliveryCount(attach.getInitialDeliveryCount()); - } - - _remoteLinkEndpoints.put(handle, endpoint); - - if(!_localLinkEndpoints.containsKey(endpoint)) - { - UnsignedInteger localHandle = findNextAvailableHandle(); - endpoint.setLocalHandle(localHandle); - _localLinkEndpoints.put(endpoint, localHandle); - - _sessionEventListener.remoteLinkCreation(endpoint); - - - } - else - { - endpoint.receiveAttach(attach); - } - } - } - } - - private void send(final FrameBody frameBody) - { - _connection.send(this.getSendingChannel(), frameBody); - } - - - private int send(final FrameBody frameBody, ByteBuffer payload) - { - return _connection.send(this.getSendingChannel(), frameBody, payload); - } - - private UnsignedInteger findNextAvailableHandle() - { - int i = 0; - do - { - if(!_localLinkEndpoints.containsValue(UnsignedInteger.valueOf(i))) - { - return UnsignedInteger.valueOf(i); - } - } while(++i != 0); - - // TODO - throw new RuntimeException(); - } - - public void receiveDetach(final Detach detach) - { - UnsignedInteger handle = detach.getHandle(); - detach(handle, detach); - } - - private void detach(UnsignedInteger handle, Detach detach) - { - if(_remoteLinkEndpoints.containsKey(handle)) - { - LinkEndpoint endpoint = _remoteLinkEndpoints.remove(handle); - - endpoint.remoteDetached(detach); - - _localLinkEndpoints.remove(endpoint); - - - } - else - { - // TODO - } - } - - public void receiveTransfer(final Transfer transfer) - { - synchronized(getLock()) - { - _nextIncomingTransferId.incr(); -/* - _availableIncomingCredit--; -*/ - - UnsignedInteger handle = transfer.getHandle(); - - - - LinkEndpoint endpoint = _remoteLinkEndpoints.get(handle); - - if(endpoint == null) - { - //TODO - error unknown link - System.err.println("Unknown endpoint " + transfer); - - } - - UnsignedInteger deliveryId = transfer.getDeliveryId(); - if(deliveryId == null) - { - deliveryId = ((ReceivingLinkEndpoint)endpoint).getLastDeliveryId(); - } - - Delivery delivery = _incomingUnsettled.get(deliveryId); - if(delivery == null) - { - delivery = new Delivery(transfer, endpoint); - _incomingUnsettled.put(deliveryId,delivery); - if(delivery.isSettled() || Boolean.TRUE.equals(transfer.getAborted())) - { -/* - _availableIncomingCredit++; -*/ - } - - if(Boolean.TRUE.equals(transfer.getMore())) - { - ((ReceivingLinkEndpoint)endpoint).setLastDeliveryId(transfer.getDeliveryId()); - } - } - else - { - if(delivery.getDeliveryId().equals(deliveryId)) - { - delivery.addTransfer(transfer); - if(delivery.isSettled()) - { -/* - _availableIncomingCredit++; -*/ - } - else if(Boolean.TRUE.equals(transfer.getAborted())) - { -/* - _availableIncomingCredit += delivery.getTransfers().size(); -*/ - } - - if(!Boolean.TRUE.equals(transfer.getMore())) - { - ((ReceivingLinkEndpoint)endpoint).setLastDeliveryId(null); - } - } - else - { - // TODO - error - System.err.println("Incorrect transfer id " + transfer); - } - } - - if(endpoint != null) - { - endpoint.receiveTransfer(transfer, delivery); - } - - if((delivery.isComplete() && delivery.isSettled() || Boolean.TRUE.equals(transfer.getAborted()))) - { - _incomingUnsettled.remove(deliveryId); - } - } - } - - public void receiveFlow(final Flow flow) - { - - synchronized(getLock()) - { - UnsignedInteger handle = flow.getHandle(); - LinkEndpoint endpoint = handle == null ? null : _remoteLinkEndpoints.get(handle); - - final UnsignedInteger nextOutgoingId = flow.getNextIncomingId() == null ? _initialOutgoingId : flow.getNextIncomingId(); - int limit = (nextOutgoingId.intValue() + flow.getIncomingWindow().intValue()); - _outgoingSessionCredit = UnsignedInteger.valueOf(limit - _nextOutgoingTransferId.intValue()); - - if(endpoint != null) - { - endpoint.receiveFlow( flow ); - } - else - { - for(LinkEndpoint le : _remoteLinkEndpoints.values()) - { - le.flowStateChanged(); - } - } - - getLock().notifyAll(); - } - - - } - - public void receiveDisposition(final Disposition disposition) - { - Role dispositionRole = disposition.getRole(); - - LinkedHashMap unsettledTransfers; - - if(dispositionRole == Role.RECEIVER) - { - unsettledTransfers = _outgoingUnsettled; - } - else - { - unsettledTransfers = _incomingUnsettled; - - } - - UnsignedInteger deliveryId = disposition.getFirst(); - UnsignedInteger last = disposition.getLast(); - if(last == null) - { - last = deliveryId; - } - - - while(deliveryId.compareTo(last)<=0) - { - - Delivery delivery = unsettledTransfers.get(deliveryId); - if(delivery != null) - { - delivery.getLinkEndpoint().receiveDeliveryState(delivery, - disposition.getState(), - disposition.getSettled()); - } - deliveryId = deliveryId.add(UnsignedInteger.ONE); - } - if(disposition.getSettled()) - { - checkSendFlow(); - } - - } - - private void checkSendFlow() - { - //TODO - } - - public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, final String sourceAddr) - { - return createSendingLinkEndpoint(name, targetAddr, sourceAddr, null); - } - - public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, final String sourceAddr, Map unsettled) - { - return createSendingLinkEndpoint(name, targetAddr, sourceAddr, false, unsettled); - } - - public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, - final String sourceAddr, boolean durable, - Map unsettled) - { - - Source source = new Source(); - source.setAddress(sourceAddr); - Target target = new Target(); - target.setAddress(targetAddr); - if(durable) - { - target.setDurable(TerminusDurability.UNSETTLED_STATE); - target.setExpiryPolicy(TerminusExpiryPolicy.NEVER); - } - - return createSendingLinkEndpoint(name, source, target, unsettled); - - } - - public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final Source source, final org.apache.qpid.amqp_1_0.type.Target target) - { - return createSendingLinkEndpoint(name, source, target, null); - } - - public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final Source source, final org.apache.qpid.amqp_1_0.type.Target target, Map unsettled) - { - SendingLinkEndpoint endpoint = new SendingLinkEndpoint(this, name, unsettled); - endpoint.setSource(source); - endpoint.setTarget(target); - UnsignedInteger handle = findNextAvailableHandle(); - _localLinkEndpoints.put(endpoint, handle); - endpoint.setLocalHandle(handle); - getLinkMap().put(name, endpoint); - - return endpoint; - } - - public void sendAttach(Attach attach) - { - send(attach); - } - - public void sendTransfer(final Transfer xfr, SendingLinkEndpoint endpoint, boolean newDelivery) - { - _nextOutgoingTransferId.incr(); - UnsignedInteger deliveryId; - if(newDelivery) - { - deliveryId = UnsignedInteger.valueOf(_nextOutgoingDeliveryId++); - endpoint.setLastDeliveryId(deliveryId); - } - else - { - deliveryId = endpoint.getLastDeliveryId(); - } - xfr.setDeliveryId(deliveryId); - - if(!Boolean.TRUE.equals(xfr.getSettled())) - { - Delivery delivery; - if((delivery = _outgoingUnsettled.get(deliveryId))== null) - { - delivery = new Delivery(xfr, endpoint); - _outgoingUnsettled.put(deliveryId, delivery); - - } - else - { - delivery.addTransfer(xfr); - } - _outgoingSessionCredit = _outgoingSessionCredit.subtract(UnsignedInteger.ONE); - endpoint.addUnsettled(delivery); - - } - - try - { - ByteBuffer payload = xfr.getPayload(); - int payloadSent = send(xfr, payload); - - if(payload != null && payloadSent < payload.remaining()) - { - payload = payload.duplicate(); -try -{ - payload.position(payload.position()+payloadSent); -} -catch(IllegalArgumentException e) -{ - System.err.println("UNEXPECTED"); - System.err.println("Payload Position: " + payload.position()); - System.err.println("Payload Sent: " + payloadSent); - System.err.println("Payload Remaining: " + payload.remaining()); - throw e; - -} - - Transfer secondTransfer = new Transfer(); - - secondTransfer.setDeliveryTag(xfr.getDeliveryTag()); - secondTransfer.setHandle(xfr.getHandle()); - secondTransfer.setSettled(xfr.getSettled()); - secondTransfer.setState(xfr.getState()); - secondTransfer.setMessageFormat(xfr.getMessageFormat()); - secondTransfer.setPayload(payload); - - sendTransfer(secondTransfer, endpoint, false); - - } - } - catch(OversizeFrameException e) - { - e.printStackTrace(); - } - - } - - public Object getLock() - { - return _connection.getLock(); - } - - public ReceivingLinkEndpoint createReceivingLinkEndpoint(final String name, - String targetAddr, - String sourceAddr, - UnsignedInteger initialCredit, - final DistributionMode distributionMode) - { - Source source = new Source(); - source.setAddress(sourceAddr); - source.setDistributionMode(distributionMode); - Target target = new Target(); - target.setAddress(targetAddr); - - return createReceivingLinkEndpoint(name, target, source, initialCredit); - } - - public ReceivingLinkEndpoint createReceivingLinkEndpoint(final String name, - Target target, - Source source, - UnsignedInteger initialCredit) - { - ReceivingLinkEndpoint endpoint = new ReceivingLinkEndpoint(this, name); - endpoint.setLinkCredit(initialCredit); - endpoint.setSource(source); - endpoint.setTarget(target); - UnsignedInteger handle = findNextAvailableHandle(); - _localLinkEndpoints.put(endpoint, handle); - endpoint.setLocalHandle(handle); - getLinkMap().put(name, endpoint); - - return endpoint; - - } - - public void updateDisposition(final Role role, - final UnsignedInteger first, - final UnsignedInteger last, - final DeliveryState state, - final boolean settled) - { - - - Disposition disposition = new Disposition(); - disposition.setRole(role); - disposition.setFirst(first); - disposition.setLast(last); - disposition.setSettled(settled); - - disposition.setState(state); - - - if(settled) - { - if(role == Role.RECEIVER) - { - SequenceNumber pos = new SequenceNumber(first.intValue()); - SequenceNumber end = new SequenceNumber(last.intValue()); - while(pos.compareTo(end)<=0) - { - Delivery d = _incomingUnsettled.remove(new UnsignedInteger(pos.intValue())); - -/* - _availableIncomingCredit += d.getTransfers().size(); -*/ - - pos.incr(); - } - } - } - - send(disposition); - checkSendFlow(); - } - - public void settle(Role role, final UnsignedInteger deliveryId) - { - if(role == Role.RECEIVER) - { - Delivery d = _incomingUnsettled.remove(deliveryId); - if(d != null) - { -/* - _availableIncomingCredit += d.getTransfers().size(); -*/ - } - } - else - { - Delivery d = _outgoingUnsettled.remove(deliveryId); -/* if(d != null) - { - _availableOutgoingCredit += d.getTransfers().size(); - - }*/ - } - - } - - public void sendFlow() - { - sendFlow(new Flow()); - } - public void sendFlow(final Flow flow) - { - final int nextIncomingId = _nextIncomingTransferId.intValue(); - flow.setNextIncomingId(UnsignedInteger.valueOf(nextIncomingId)); - flow.setIncomingWindow(UnsignedInteger.valueOf(_availableIncomingCredit)); - _lastSentIncomingLimit = UnsignedInteger.valueOf(nextIncomingId + _availableIncomingCredit); - - flow.setNextOutgoingId(UnsignedInteger.valueOf(_nextOutgoingTransferId.intValue())); - flow.setOutgoingWindow(UnsignedInteger.valueOf(_availableOutgoingCredit)); - send(flow); - } - - public void sendFlowConditional() - { - UnsignedInteger clientsCredit = _lastSentIncomingLimit.subtract(UnsignedInteger.valueOf(_nextIncomingTransferId.intValue())); - int i = UnsignedInteger.valueOf(_availableIncomingCredit).subtract(clientsCredit).compareTo(clientsCredit); - if(i >=0) - { - sendFlow(); - } - - } - - public void sendDetach(Detach detach) - { - send(detach); - - } - - void doEnd(End end) - { - } - - public void setNextIncomingId(final UnsignedInteger nextIncomingId) - { - _nextIncomingTransferId = new SequenceNumber(nextIncomingId.intValue()); - - } - - public void setOutgoingSessionCredit(final UnsignedInteger outgoingSessionCredit) - { - _outgoingSessionCredit = outgoingSessionCredit; - } - - public UnsignedInteger getNextOutgoingId() - { - return UnsignedInteger.valueOf(_nextOutgoingTransferId.intValue()); - } - - public UnsignedInteger getOutgoingWindowSize() - { - return UnsignedInteger.valueOf(_availableOutgoingCredit); - } - - public boolean hasCreditToSend() - { - boolean b = _outgoingSessionCredit != null && _outgoingSessionCredit.intValue() > 0; - boolean b1 = getOutgoingWindowSize() != null && getOutgoingWindowSize().compareTo(UnsignedInteger.ZERO) > 0; - return b && b1; - } - - public UnsignedInteger getIncomingWindowSize() - { - return UnsignedInteger.valueOf(_availableIncomingCredit); - } - - public SessionEventListener getSessionEventListener() - { - return _sessionEventListener; - } - - public void setSessionEventListener(final SessionEventListener sessionEventListener) - { - _sessionEventListener = sessionEventListener; - } - - public ConnectionEndpoint getConnection() - { - return _connection; - } - - public SendingLinkEndpoint createTransactionController(String name, TxnCapability... capabilities) - { - Coordinator coordinator = new Coordinator(); - coordinator.setCapabilities(capabilities); - - Source src = new Source(); - - return createSendingLinkEndpoint(name, src, coordinator); - } - - Map getLinkMap() - { - return _linkMap; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.framing.OversizeFrameException; +import org.apache.qpid.amqp_1_0.type.*; +import org.apache.qpid.amqp_1_0.type.messaging.Source; +import org.apache.qpid.amqp_1_0.type.messaging.Target; +import org.apache.qpid.amqp_1_0.type.messaging.TerminusDurability; +import org.apache.qpid.amqp_1_0.type.messaging.TerminusExpiryPolicy; +import org.apache.qpid.amqp_1_0.type.transaction.*; +import org.apache.qpid.amqp_1_0.type.transaction.TxnCapability; +import org.apache.qpid.amqp_1_0.type.transport.*; +import org.apache.qpid.amqp_1_0.type.transport.Error; + + +import java.nio.ByteBuffer; +import java.util.*; + +public class SessionEndpoint +{ + private SessionState _state = SessionState.INACTIVE; + + private final Map _linkMap = new HashMap(); + private final Map _localLinkEndpoints = new HashMap(); + private final Map _remoteLinkEndpoints = new HashMap(); + + private long _timeout; + + + private ConnectionEndpoint _connection; + private long _lastAttachedTime; + + private short _receivingChannel; + private short _sendingChannel; + + private LinkedHashMap _outgoingUnsettled; + private LinkedHashMap _incomingUnsettled; + + // has to be a power of two + private static final int DEFAULT_SESSION_BUFFER_SIZE = 1 << 11; + private static final int BUFFER_SIZE_MASK = DEFAULT_SESSION_BUFFER_SIZE - 1; + + private SequenceNumber _nextIncomingTransferId; + private SequenceNumber _nextOutgoingTransferId; + + private int _nextOutgoingDeliveryId; + + //private SequenceNumber _incomingLWM; + //private SequenceNumber _outgoingLWM; + + private UnsignedInteger _outgoingSessionCredit; + + + + private UnsignedInteger _initialOutgoingId; + + private SessionEventListener _sessionEventListener = SessionEventListener.DEFAULT; + + private int _availableIncomingCredit; + private int _availableOutgoingCredit; + private UnsignedInteger _lastSentIncomingLimit; + + public SessionEndpoint(final ConnectionEndpoint connectionEndpoint) + { + this(connectionEndpoint, UnsignedInteger.valueOf(0)); + } + + public SessionEndpoint(final ConnectionEndpoint connectionEndpoint, Begin begin) + { + this(connectionEndpoint, UnsignedInteger.valueOf(0)); + _state = SessionState.BEGIN_RECVD; + _nextIncomingTransferId = new SequenceNumber(begin.getNextOutgoingId().intValue()); + } + + + public SessionEndpoint(final ConnectionEndpoint connectionEndpoint, UnsignedInteger nextOutgoingId) + { + _connection = connectionEndpoint; + + _initialOutgoingId = nextOutgoingId; + _nextOutgoingTransferId = new SequenceNumber(nextOutgoingId.intValue()); + + _outgoingUnsettled = new LinkedHashMap(DEFAULT_SESSION_BUFFER_SIZE); + _incomingUnsettled = new LinkedHashMap(DEFAULT_SESSION_BUFFER_SIZE); + _availableIncomingCredit = DEFAULT_SESSION_BUFFER_SIZE; + _availableOutgoingCredit = DEFAULT_SESSION_BUFFER_SIZE; + } + + + public void setReceivingChannel(final short receivingChannel) + { + _receivingChannel = receivingChannel; + switch(_state) + { + case INACTIVE: + _state = SessionState.BEGIN_RECVD; + break; + case BEGIN_SENT: + _state = SessionState.ACTIVE; + break; + default: + // TODO error + + } + } + + + public void setSendingChannel(final short sendingChannel) + { + _sendingChannel = sendingChannel; + switch(_state) + { + case INACTIVE: + _state = SessionState.BEGIN_SENT; + break; + case BEGIN_RECVD: + _state = SessionState.ACTIVE; + break; + default: + // TODO error + + } + } + + public SessionState getState() + { + return _state; + } + + public void end() + { + end(null); + } + + public void end(final End end) + { + synchronized(getLock()) + { + switch(_state) + { + case END_SENT: + _state = SessionState.ENDED; + break; + case ACTIVE: + detachLinks(); + _sessionEventListener.remoteEnd(end); + short sendChannel = getSendingChannel(); + _connection.sendEnd(sendChannel, new End()); + _state = end == null ? SessionState.END_SENT : SessionState.ENDED; + break; + default: + sendChannel = getSendingChannel(); + End reply = new End(); + Error error = new Error(); + error.setCondition(AmqpError.ILLEGAL_STATE); + error.setDescription("END called on Session which has not been opened"); + reply.setError(error); + _connection.sendEnd(sendChannel, reply); + break; + + + } + getLock().notifyAll(); + } + } + + private void detachLinks() + { + Collection handles = new ArrayList(_remoteLinkEndpoints.keySet()); + for(UnsignedInteger handle : handles) + { + detach(handle, null); + } + } + + public short getSendingChannel() + { + return _sendingChannel; + } + + + public void receiveAttach(final Attach attach) + { + if(_state == SessionState.ACTIVE) + { + UnsignedInteger handle = attach.getHandle(); + if(_remoteLinkEndpoints.containsKey(handle)) + { + // TODO - Error - handle busy? + } + else + { + LinkEndpoint endpoint = getLinkMap().get(attach.getName()); + if(endpoint == null) + { + endpoint = attach.getRole() == Role.RECEIVER + ? new SendingLinkEndpoint(this, attach) + : new ReceivingLinkEndpoint(this, attach); + + // TODO : fix below - distinguish between local and remote owned + endpoint.setSource(attach.getSource()); + endpoint.setTarget(attach.getTarget()); + + + } + + if(attach.getRole() == Role.SENDER) + { + endpoint.setDeliveryCount(attach.getInitialDeliveryCount()); + } + + _remoteLinkEndpoints.put(handle, endpoint); + + if(!_localLinkEndpoints.containsKey(endpoint)) + { + UnsignedInteger localHandle = findNextAvailableHandle(); + endpoint.setLocalHandle(localHandle); + _localLinkEndpoints.put(endpoint, localHandle); + + _sessionEventListener.remoteLinkCreation(endpoint); + + + } + else + { + endpoint.receiveAttach(attach); + } + } + } + } + + private void send(final FrameBody frameBody) + { + _connection.send(this.getSendingChannel(), frameBody); + } + + + private int send(final FrameBody frameBody, ByteBuffer payload) + { + return _connection.send(this.getSendingChannel(), frameBody, payload); + } + + private UnsignedInteger findNextAvailableHandle() + { + int i = 0; + do + { + if(!_localLinkEndpoints.containsValue(UnsignedInteger.valueOf(i))) + { + return UnsignedInteger.valueOf(i); + } + } while(++i != 0); + + // TODO + throw new RuntimeException(); + } + + public void receiveDetach(final Detach detach) + { + UnsignedInteger handle = detach.getHandle(); + detach(handle, detach); + } + + private void detach(UnsignedInteger handle, Detach detach) + { + if(_remoteLinkEndpoints.containsKey(handle)) + { + LinkEndpoint endpoint = _remoteLinkEndpoints.remove(handle); + + endpoint.remoteDetached(detach); + + _localLinkEndpoints.remove(endpoint); + + + } + else + { + // TODO + } + } + + public void receiveTransfer(final Transfer transfer) + { + synchronized(getLock()) + { + _nextIncomingTransferId.incr(); +/* + _availableIncomingCredit--; +*/ + + UnsignedInteger handle = transfer.getHandle(); + + + + LinkEndpoint endpoint = _remoteLinkEndpoints.get(handle); + + if(endpoint == null) + { + //TODO - error unknown link + System.err.println("Unknown endpoint " + transfer); + + } + + UnsignedInteger deliveryId = transfer.getDeliveryId(); + if(deliveryId == null) + { + deliveryId = ((ReceivingLinkEndpoint)endpoint).getLastDeliveryId(); + } + + Delivery delivery = _incomingUnsettled.get(deliveryId); + if(delivery == null) + { + delivery = new Delivery(transfer, endpoint); + _incomingUnsettled.put(deliveryId,delivery); + if(delivery.isSettled() || Boolean.TRUE.equals(transfer.getAborted())) + { +/* + _availableIncomingCredit++; +*/ + } + + if(Boolean.TRUE.equals(transfer.getMore())) + { + ((ReceivingLinkEndpoint)endpoint).setLastDeliveryId(transfer.getDeliveryId()); + } + } + else + { + if(delivery.getDeliveryId().equals(deliveryId)) + { + delivery.addTransfer(transfer); + if(delivery.isSettled()) + { +/* + _availableIncomingCredit++; +*/ + } + else if(Boolean.TRUE.equals(transfer.getAborted())) + { +/* + _availableIncomingCredit += delivery.getTransfers().size(); +*/ + } + + if(!Boolean.TRUE.equals(transfer.getMore())) + { + ((ReceivingLinkEndpoint)endpoint).setLastDeliveryId(null); + } + } + else + { + // TODO - error + System.err.println("Incorrect transfer id " + transfer); + } + } + + if(endpoint != null) + { + endpoint.receiveTransfer(transfer, delivery); + } + + if((delivery.isComplete() && delivery.isSettled() || Boolean.TRUE.equals(transfer.getAborted()))) + { + _incomingUnsettled.remove(deliveryId); + } + } + } + + public void receiveFlow(final Flow flow) + { + + synchronized(getLock()) + { + UnsignedInteger handle = flow.getHandle(); + LinkEndpoint endpoint = handle == null ? null : _remoteLinkEndpoints.get(handle); + + final UnsignedInteger nextOutgoingId = flow.getNextIncomingId() == null ? _initialOutgoingId : flow.getNextIncomingId(); + int limit = (nextOutgoingId.intValue() + flow.getIncomingWindow().intValue()); + _outgoingSessionCredit = UnsignedInteger.valueOf(limit - _nextOutgoingTransferId.intValue()); + + if(endpoint != null) + { + endpoint.receiveFlow( flow ); + } + else + { + for(LinkEndpoint le : _remoteLinkEndpoints.values()) + { + le.flowStateChanged(); + } + } + + getLock().notifyAll(); + } + + + } + + public void receiveDisposition(final Disposition disposition) + { + Role dispositionRole = disposition.getRole(); + + LinkedHashMap unsettledTransfers; + + if(dispositionRole == Role.RECEIVER) + { + unsettledTransfers = _outgoingUnsettled; + } + else + { + unsettledTransfers = _incomingUnsettled; + + } + + UnsignedInteger deliveryId = disposition.getFirst(); + UnsignedInteger last = disposition.getLast(); + if(last == null) + { + last = deliveryId; + } + + + while(deliveryId.compareTo(last)<=0) + { + + Delivery delivery = unsettledTransfers.get(deliveryId); + if(delivery != null) + { + delivery.getLinkEndpoint().receiveDeliveryState(delivery, + disposition.getState(), + disposition.getSettled()); + } + deliveryId = deliveryId.add(UnsignedInteger.ONE); + } + if(disposition.getSettled()) + { + checkSendFlow(); + } + + } + + private void checkSendFlow() + { + //TODO + } + + public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, final String sourceAddr) + { + return createSendingLinkEndpoint(name, targetAddr, sourceAddr, null); + } + + public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, final String sourceAddr, Map unsettled) + { + return createSendingLinkEndpoint(name, targetAddr, sourceAddr, false, unsettled); + } + + public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final String targetAddr, + final String sourceAddr, boolean durable, + Map unsettled) + { + + Source source = new Source(); + source.setAddress(sourceAddr); + Target target = new Target(); + target.setAddress(targetAddr); + if(durable) + { + target.setDurable(TerminusDurability.UNSETTLED_STATE); + target.setExpiryPolicy(TerminusExpiryPolicy.NEVER); + } + + return createSendingLinkEndpoint(name, source, target, unsettled); + + } + + public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final Source source, final org.apache.qpid.amqp_1_0.type.Target target) + { + return createSendingLinkEndpoint(name, source, target, null); + } + + public SendingLinkEndpoint createSendingLinkEndpoint(final String name, final Source source, final org.apache.qpid.amqp_1_0.type.Target target, Map unsettled) + { + SendingLinkEndpoint endpoint = new SendingLinkEndpoint(this, name, unsettled); + endpoint.setSource(source); + endpoint.setTarget(target); + UnsignedInteger handle = findNextAvailableHandle(); + _localLinkEndpoints.put(endpoint, handle); + endpoint.setLocalHandle(handle); + getLinkMap().put(name, endpoint); + + return endpoint; + } + + public void sendAttach(Attach attach) + { + send(attach); + } + + public void sendTransfer(final Transfer xfr, SendingLinkEndpoint endpoint, boolean newDelivery) + { + _nextOutgoingTransferId.incr(); + UnsignedInteger deliveryId; + if(newDelivery) + { + deliveryId = UnsignedInteger.valueOf(_nextOutgoingDeliveryId++); + endpoint.setLastDeliveryId(deliveryId); + } + else + { + deliveryId = endpoint.getLastDeliveryId(); + } + xfr.setDeliveryId(deliveryId); + + if(!Boolean.TRUE.equals(xfr.getSettled())) + { + Delivery delivery; + if((delivery = _outgoingUnsettled.get(deliveryId))== null) + { + delivery = new Delivery(xfr, endpoint); + _outgoingUnsettled.put(deliveryId, delivery); + + } + else + { + delivery.addTransfer(xfr); + } + _outgoingSessionCredit = _outgoingSessionCredit.subtract(UnsignedInteger.ONE); + endpoint.addUnsettled(delivery); + + } + + try + { + ByteBuffer payload = xfr.getPayload(); + int payloadSent = send(xfr, payload); + + if(payload != null && payloadSent < payload.remaining()) + { + payload = payload.duplicate(); +try +{ + payload.position(payload.position()+payloadSent); +} +catch(IllegalArgumentException e) +{ + System.err.println("UNEXPECTED"); + System.err.println("Payload Position: " + payload.position()); + System.err.println("Payload Sent: " + payloadSent); + System.err.println("Payload Remaining: " + payload.remaining()); + throw e; + +} + + Transfer secondTransfer = new Transfer(); + + secondTransfer.setDeliveryTag(xfr.getDeliveryTag()); + secondTransfer.setHandle(xfr.getHandle()); + secondTransfer.setSettled(xfr.getSettled()); + secondTransfer.setState(xfr.getState()); + secondTransfer.setMessageFormat(xfr.getMessageFormat()); + secondTransfer.setPayload(payload); + + sendTransfer(secondTransfer, endpoint, false); + + } + } + catch(OversizeFrameException e) + { + e.printStackTrace(); + } + + } + + public Object getLock() + { + return _connection.getLock(); + } + + public ReceivingLinkEndpoint createReceivingLinkEndpoint(final String name, + String targetAddr, + String sourceAddr, + UnsignedInteger initialCredit, + final DistributionMode distributionMode) + { + Source source = new Source(); + source.setAddress(sourceAddr); + source.setDistributionMode(distributionMode); + Target target = new Target(); + target.setAddress(targetAddr); + + return createReceivingLinkEndpoint(name, target, source, initialCredit); + } + + public ReceivingLinkEndpoint createReceivingLinkEndpoint(final String name, + Target target, + Source source, + UnsignedInteger initialCredit) + { + ReceivingLinkEndpoint endpoint = new ReceivingLinkEndpoint(this, name); + endpoint.setLinkCredit(initialCredit); + endpoint.setSource(source); + endpoint.setTarget(target); + UnsignedInteger handle = findNextAvailableHandle(); + _localLinkEndpoints.put(endpoint, handle); + endpoint.setLocalHandle(handle); + getLinkMap().put(name, endpoint); + + return endpoint; + + } + + public void updateDisposition(final Role role, + final UnsignedInteger first, + final UnsignedInteger last, + final DeliveryState state, + final boolean settled) + { + + + Disposition disposition = new Disposition(); + disposition.setRole(role); + disposition.setFirst(first); + disposition.setLast(last); + disposition.setSettled(settled); + + disposition.setState(state); + + + if(settled) + { + if(role == Role.RECEIVER) + { + SequenceNumber pos = new SequenceNumber(first.intValue()); + SequenceNumber end = new SequenceNumber(last.intValue()); + while(pos.compareTo(end)<=0) + { + Delivery d = _incomingUnsettled.remove(new UnsignedInteger(pos.intValue())); + +/* + _availableIncomingCredit += d.getTransfers().size(); +*/ + + pos.incr(); + } + } + } + + send(disposition); + checkSendFlow(); + } + + public void settle(Role role, final UnsignedInteger deliveryId) + { + if(role == Role.RECEIVER) + { + Delivery d = _incomingUnsettled.remove(deliveryId); + if(d != null) + { +/* + _availableIncomingCredit += d.getTransfers().size(); +*/ + } + } + else + { + Delivery d = _outgoingUnsettled.remove(deliveryId); +/* if(d != null) + { + _availableOutgoingCredit += d.getTransfers().size(); + + }*/ + } + + } + + public void sendFlow() + { + sendFlow(new Flow()); + } + public void sendFlow(final Flow flow) + { + final int nextIncomingId = _nextIncomingTransferId.intValue(); + flow.setNextIncomingId(UnsignedInteger.valueOf(nextIncomingId)); + flow.setIncomingWindow(UnsignedInteger.valueOf(_availableIncomingCredit)); + _lastSentIncomingLimit = UnsignedInteger.valueOf(nextIncomingId + _availableIncomingCredit); + + flow.setNextOutgoingId(UnsignedInteger.valueOf(_nextOutgoingTransferId.intValue())); + flow.setOutgoingWindow(UnsignedInteger.valueOf(_availableOutgoingCredit)); + send(flow); + } + + public void sendFlowConditional() + { + UnsignedInteger clientsCredit = _lastSentIncomingLimit.subtract(UnsignedInteger.valueOf(_nextIncomingTransferId.intValue())); + int i = UnsignedInteger.valueOf(_availableIncomingCredit).subtract(clientsCredit).compareTo(clientsCredit); + if(i >=0) + { + sendFlow(); + } + + } + + public void sendDetach(Detach detach) + { + send(detach); + + } + + void doEnd(End end) + { + } + + public void setNextIncomingId(final UnsignedInteger nextIncomingId) + { + _nextIncomingTransferId = new SequenceNumber(nextIncomingId.intValue()); + + } + + public void setOutgoingSessionCredit(final UnsignedInteger outgoingSessionCredit) + { + _outgoingSessionCredit = outgoingSessionCredit; + } + + public UnsignedInteger getNextOutgoingId() + { + return UnsignedInteger.valueOf(_nextOutgoingTransferId.intValue()); + } + + public UnsignedInteger getOutgoingWindowSize() + { + return UnsignedInteger.valueOf(_availableOutgoingCredit); + } + + public boolean hasCreditToSend() + { + boolean b = _outgoingSessionCredit != null && _outgoingSessionCredit.intValue() > 0; + boolean b1 = getOutgoingWindowSize() != null && getOutgoingWindowSize().compareTo(UnsignedInteger.ZERO) > 0; + return b && b1; + } + + public UnsignedInteger getIncomingWindowSize() + { + return UnsignedInteger.valueOf(_availableIncomingCredit); + } + + public SessionEventListener getSessionEventListener() + { + return _sessionEventListener; + } + + public void setSessionEventListener(final SessionEventListener sessionEventListener) + { + _sessionEventListener = sessionEventListener; + } + + public ConnectionEndpoint getConnection() + { + return _connection; + } + + public SendingLinkEndpoint createTransactionController(String name, TxnCapability... capabilities) + { + Coordinator coordinator = new Coordinator(); + coordinator.setCapabilities(capabilities); + + Source src = new Source(); + + return createSendingLinkEndpoint(name, src, coordinator); + } + + Map getLinkMap() + { + return _linkMap; + } + + + public boolean isEnded() + { + return _state == SessionState.ENDED || _connection.isClosed(); + } + + public boolean isActive() + { + return _state == SessionState.ACTIVE; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionHalfEndpoint.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionHalfEndpoint.java index fce1ce021f..d09f1e617d 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionHalfEndpoint.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionHalfEndpoint.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public class SessionHalfEndpoint -{ -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public class SessionHalfEndpoint +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionState.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionState.java index b09e27d980..9c4c3532bd 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionState.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/SessionState.java @@ -1,34 +1,34 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.transport; - -public enum SessionState -{ - ACTIVE, - INACTIVE, - BEGIN_SENT, - BEGIN_RECVD, - END_SENT, - END_RECVD, - ENDED; - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.transport; + +public enum SessionState +{ + ACTIVE, + INACTIVE, + BEGIN_SENT, + BEGIN_RECVD, + END_SENT, + END_RECVD, + ENDED; + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/UnsettledTransfer.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/UnsettledTransfer.java index 7a39fd9a54..25b65ea50e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/UnsettledTransfer.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/transport/UnsettledTransfer.java @@ -1,53 +1,53 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.transport; - -import org.apache.qpid.amqp_1_0.type.transport.Transfer; - -public class UnsettledTransfer -{ - private final Transfer _transfer; - private final LinkEndpoint _linkEndpoint; - - public UnsettledTransfer(final Transfer transfer, final LinkEndpoint linkEndpoint) - { - _transfer = transfer; - _linkEndpoint = linkEndpoint; - } - - public Transfer getTransfer() - { - return _transfer; - } - - public LinkEndpoint getLinkEndpoint() - { - return _linkEndpoint; - } - - @Override public String toString() - { - return "UnsettledTransfer{" + - "_transfer=" + _transfer + - ", _linkEndpoint=" + _linkEndpoint + - '}'; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.transport; + +import org.apache.qpid.amqp_1_0.type.transport.Transfer; + +public class UnsettledTransfer +{ + private final Transfer _transfer; + private final LinkEndpoint _linkEndpoint; + + public UnsettledTransfer(final Transfer transfer, final LinkEndpoint linkEndpoint) + { + _transfer = transfer; + _linkEndpoint = linkEndpoint; + } + + public Transfer getTransfer() + { + return _transfer; + } + + public LinkEndpoint getLinkEndpoint() + { + return _linkEndpoint; + } + + @Override public String toString() + { + return "UnsettledTransfer{" + + "_transfer=" + _transfer + + ", _linkEndpoint=" + _linkEndpoint + + '}'; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/AmqpErrorException.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/AmqpErrorException.java index 74ae7de470..0787cc2d83 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/AmqpErrorException.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/AmqpErrorException.java @@ -1,54 +1,54 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.type; - -import org.apache.qpid.amqp_1_0.type.transport.Error; - -import java.util.Formatter; - -public class AmqpErrorException extends Exception -{ - private final Error _error; - - - public AmqpErrorException(final Error error) - { - _error = error; - } - - public AmqpErrorException(ErrorCondition condition) - { - _error = new Error(); - _error.setCondition(condition); - } - - public AmqpErrorException(ErrorCondition condition, String format, Object... args) - { - _error = new Error(); - _error.setCondition(condition); - _error.setDescription((new Formatter()).format(format, args).toString()); - } - - public Error getError() - { - return _error; - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.type; + +import org.apache.qpid.amqp_1_0.type.transport.Error; + +import java.util.Formatter; + +public class AmqpErrorException extends Exception +{ + private final Error _error; + + + public AmqpErrorException(final Error error) + { + _error = error; + } + + public AmqpErrorException(ErrorCondition condition) + { + _error = new Error(); + _error.setCondition(condition); + } + + public AmqpErrorException(ErrorCondition condition, String format, Object... args) + { + _error = new Error(); + _error.setCondition(condition); + _error.setDescription((new Formatter()).format(format, args).toString()); + } + + public Error getError() + { + return _error; + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Binary.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Binary.java index 0c8dc3444a..8074354051 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Binary.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Binary.java @@ -1,161 +1,161 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.type; - -import java.nio.ByteBuffer; -import java.util.Collection; - -import static java.lang.Math.min; - -public class Binary -{ - - private final byte[] _data; - private final int _offset; - private final int _length; - private final int _hashCode; - - public Binary(final byte[] data) - { - this(data, 0, data.length); - } - - public Binary(final byte[] data, final int offset, final int length) - { - - _data = data; - _offset = offset; - _length = length; - int hc = 0; - for (int i = 0; i < length; i++) - { - hc = 31*hc + (0xFF & data[offset + i]); - } - _hashCode = hc; - } - - public ByteBuffer asByteBuffer() - { - return ByteBuffer.wrap(_data, _offset, _length); - } - - public final int hashCode() - { - return _hashCode; - } - - public final boolean equals(Object o) - { - Binary buf = (Binary) o; - if(o == null) - { - return false; - } - final int size = _length; - if (size != buf._length) - { - return false; - } - - final byte[] myData = _data; - final byte[] theirData = buf._data; - int myOffset = _offset; - int theirOffset = buf._offset; - final int myLimit = myOffset + size; - - while(myOffset < myLimit) - { - if (myData[myOffset++] != theirData[theirOffset++]) - { - return false; - } - } - - return true; - } - - - public int getArrayOffset() - { - return _offset; - } - - public byte[] getArray() - { - return _data; - } - - public int getLength() - { - return _length; - } - - public String toString() - { - StringBuilder str = new StringBuilder(); - - - for (int i = _offset; i < _length; i++) - { - byte c = _data[i]; - - if (c > 31 && c < 127 && c != '\\') - { - str.append((char)c); - } - else - { - str.append(String.format("\\x%02x", c)); - } - } - - return str.toString(); - - } - - public static Binary combine(final Collection binaries) - { - - if(binaries.size() == 1) - { - return binaries.iterator().next(); - } - - int size = 0; - for(Binary binary : binaries) - { - size += binary.getLength(); - } - byte[] data = new byte[size]; - int offset = 0; - for(Binary binary : binaries) - { - System.arraycopy(binary._data, binary._offset, data, offset, binary._length); - offset += binary._length; - } - return new Binary(data); - } - - public Binary subBinary(final int offset, final int length) - { - return new Binary(_data, _offset+offset, length); - } -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.type; + +import java.nio.ByteBuffer; +import java.util.Collection; + +import static java.lang.Math.min; + +public class Binary +{ + + private final byte[] _data; + private final int _offset; + private final int _length; + private final int _hashCode; + + public Binary(final byte[] data) + { + this(data, 0, data.length); + } + + public Binary(final byte[] data, final int offset, final int length) + { + + _data = data; + _offset = offset; + _length = length; + int hc = 0; + for (int i = 0; i < length; i++) + { + hc = 31*hc + (0xFF & data[offset + i]); + } + _hashCode = hc; + } + + public ByteBuffer asByteBuffer() + { + return ByteBuffer.wrap(_data, _offset, _length); + } + + public final int hashCode() + { + return _hashCode; + } + + public final boolean equals(Object o) + { + Binary buf = (Binary) o; + if(o == null) + { + return false; + } + final int size = _length; + if (size != buf._length) + { + return false; + } + + final byte[] myData = _data; + final byte[] theirData = buf._data; + int myOffset = _offset; + int theirOffset = buf._offset; + final int myLimit = myOffset + size; + + while(myOffset < myLimit) + { + if (myData[myOffset++] != theirData[theirOffset++]) + { + return false; + } + } + + return true; + } + + + public int getArrayOffset() + { + return _offset; + } + + public byte[] getArray() + { + return _data; + } + + public int getLength() + { + return _length; + } + + public String toString() + { + StringBuilder str = new StringBuilder(); + + + for (int i = _offset; i < _length; i++) + { + byte c = _data[i]; + + if (c > 31 && c < 127 && c != '\\') + { + str.append((char)c); + } + else + { + str.append(String.format("\\x%02x", c)); + } + } + + return str.toString(); + + } + + public static Binary combine(final Collection binaries) + { + + if(binaries.size() == 1) + { + return binaries.iterator().next(); + } + + int size = 0; + for(Binary binary : binaries) + { + size += binary.getLength(); + } + byte[] data = new byte[size]; + int offset = 0; + for(Binary binary : binaries) + { + System.arraycopy(binary._data, binary._offset, data, offset, binary._length); + offset += binary._length; + } + return new Binary(data); + } + + public Binary subBinary(final int offset, final int length) + { + return new Binary(_data, _offset+offset, length); + } +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DeliveryState.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DeliveryState.java index a16cc46729..b82a3631ee 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DeliveryState.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DeliveryState.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface DeliveryState -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface DeliveryState +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DistributionMode.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DistributionMode.java index 751bdd1406..0be77b7841 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DistributionMode.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/DistributionMode.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface DistributionMode -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface DistributionMode +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/ErrorCondition.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/ErrorCondition.java index a9f8115784..7c3e0ae285 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/ErrorCondition.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/ErrorCondition.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.type; - -public interface ErrorCondition -{ - public Symbol getValue(); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.type; + +public interface ErrorCondition +{ + public Symbol getValue(); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/FrameBody.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/FrameBody.java index 0312378019..f0036778c8 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/FrameBody.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/FrameBody.java @@ -1,29 +1,29 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; - -public interface FrameBody -{ - public void invoke(short channel, ConnectionEndpoint conn); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint; + +public interface FrameBody +{ + public void invoke(short channel, ConnectionEndpoint conn); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/GlobalTxId.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/GlobalTxId.java index f17a8648c6..e3f2372c02 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/GlobalTxId.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/GlobalTxId.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public class GlobalTxId -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public class GlobalTxId +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/LifetimePolicy.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/LifetimePolicy.java index af13cdaffe..8ca7b7161e 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/LifetimePolicy.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/LifetimePolicy.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface LifetimePolicy -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface LifetimePolicy +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Outcome.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Outcome.java index 0741ebdf70..949258a7f7 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Outcome.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Outcome.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface Outcome -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface Outcome +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/RestrictedType.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/RestrictedType.java index 9bcfc752a1..7e324b6184 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/RestrictedType.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/RestrictedType.java @@ -1,26 +1,26 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.amqp_1_0.type; - -public interface RestrictedType -{ - V getValue(); -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.amqp_1_0.type; + +public interface RestrictedType +{ + V getValue(); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/SaslFrameBody.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/SaslFrameBody.java index cb4332f020..8470a6f29b 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/SaslFrameBody.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/SaslFrameBody.java @@ -1,27 +1,27 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.qpid.amqp_1_0.type; - -import org.apache.qpid.amqp_1_0.transport.SASLEndpoint; - -public interface SaslFrameBody -{ - public void invoke(SASLEndpoint conn); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.qpid.amqp_1_0.type; + +import org.apache.qpid.amqp_1_0.transport.SASLEndpoint; + +public interface SaslFrameBody +{ + public void invoke(SASLEndpoint conn); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Section.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Section.java index 1e2872b66a..34fb92b19c 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Section.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Section.java @@ -1,27 +1,27 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.qpid.amqp_1_0.type; - -import org.apache.qpid.amqp_1_0.messaging.SectionEncoder; - -public interface Section -{ - - Binary encode(SectionEncoder encoder); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.qpid.amqp_1_0.type; + +import org.apache.qpid.amqp_1_0.messaging.SectionEncoder; + +public interface Section +{ + + Binary encode(SectionEncoder encoder); +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Source.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Source.java index c033ed4d55..eadec0456f 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Source.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Source.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface Source -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface Source +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Symbol.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Symbol.java index 6efcdcf952..de4e86f201 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Symbol.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Symbol.java @@ -1,90 +1,94 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -import java.util.concurrent.ConcurrentHashMap; - -public final class Symbol implements Comparable, CharSequence -{ - private final String _underlying; - private static final ConcurrentHashMap _symbols = new ConcurrentHashMap(2048); - - private Symbol(String underlying) - { - _underlying = underlying; - } - - public int length() - { - return _underlying.length(); - } - - public int compareTo(Symbol o) - { - return _underlying.compareTo(o._underlying); - } - - public char charAt(int index) - { - return _underlying.charAt(index); - } - - public CharSequence subSequence(int beginIndex, int endIndex) - { - return _underlying.subSequence(beginIndex, endIndex); - } - - @Override - public String toString() - { - return _underlying; - } - - @Override - public int hashCode() - { - return _underlying.hashCode(); - } - - public static Symbol valueOf(String symbolVal) - { - return getSymbol(symbolVal); - } - - public static Symbol getSymbol(String symbolVal) - { - Symbol symbol = _symbols.get(symbolVal); - if(symbol == null) - { - symbolVal = symbolVal.intern(); - symbol = new Symbol(symbolVal); - Symbol existing; - if((existing = _symbols.putIfAbsent(symbolVal, symbol)) != null) - { - symbol = existing; - } - } - return symbol; - } - - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +import java.util.concurrent.ConcurrentHashMap; + +public final class Symbol implements Comparable, CharSequence +{ + private final String _underlying; + private static final ConcurrentHashMap _symbols = new ConcurrentHashMap(2048); + + private Symbol(String underlying) + { + _underlying = underlying; + } + + public int length() + { + return _underlying.length(); + } + + public int compareTo(Symbol o) + { + return _underlying.compareTo(o._underlying); + } + + public char charAt(int index) + { + return _underlying.charAt(index); + } + + public CharSequence subSequence(int beginIndex, int endIndex) + { + return _underlying.subSequence(beginIndex, endIndex); + } + + @Override + public String toString() + { + return _underlying; + } + + @Override + public int hashCode() + { + return _underlying.hashCode(); + } + + public static Symbol valueOf(String symbolVal) + { + return getSymbol(symbolVal); + } + + public static Symbol getSymbol(String symbolVal) + { + if(symbolVal == null) + { + return null; + } + Symbol symbol = _symbols.get(symbolVal); + if(symbol == null) + { + symbolVal = symbolVal.intern(); + symbol = new Symbol(symbolVal); + Symbol existing; + if((existing = _symbols.putIfAbsent(symbolVal, symbol)) != null) + { + symbol = existing; + } + } + return symbol; + } + + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Target.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Target.java index 345f6c418f..d01128a64c 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Target.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/Target.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface Target -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface Target +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/TxnCapability.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/TxnCapability.java index 3d7d2137a6..6249e8ab80 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/TxnCapability.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/TxnCapability.java @@ -1,26 +1,26 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface TxnCapability -{ -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface TxnCapability +{ +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedByte.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedByte.java index 07739e068e..7d608e2359 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedByte.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedByte.java @@ -1,134 +1,134 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -public final class UnsignedByte extends Number implements Comparable -{ - private final byte _underlying; - private static final UnsignedByte[] cachedValues = new UnsignedByte[256]; - - static - { - for(int i = 0; i<256; i++) - { - cachedValues[i] = new UnsignedByte((byte)i); - } - } - - public UnsignedByte(byte underlying) - { - _underlying = underlying; - } - - @Override - public byte byteValue() - { - return _underlying; - } - - @Override - public short shortValue() - { - return (short) intValue(); - } - - @Override - public int intValue() - { - return ((int)_underlying) & 0xFF; - } - - @Override - public long longValue() - { - return ((long) _underlying) & 0xFFl; - } - - @Override - public float floatValue() - { - return (float) longValue(); - } - - @Override - public double doubleValue() - { - return (double) longValue(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - UnsignedByte that = (UnsignedByte) o; - - if (_underlying != that._underlying) - { - return false; - } - - return true; - } - - public int compareTo(UnsignedByte o) - { - return Integer.signum(intValue() - o.intValue()); - } - - @Override - public int hashCode() - { - return _underlying; - } - - @Override - public String toString() - { - return String.valueOf(intValue()); - } - - public static UnsignedByte valueOf(byte underlying) - { - final int index = ((int) underlying) & 0xFF; - return cachedValues[index]; - } - - public static UnsignedByte valueOf(final String value) - throws NumberFormatException - { - int intVal = Integer.parseInt(value); - if(intVal < 0 || intVal >= (1<<8)) - { - throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<8) +")."); - } - return valueOf((byte)intVal); - } - +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +public final class UnsignedByte extends Number implements Comparable +{ + private final byte _underlying; + private static final UnsignedByte[] cachedValues = new UnsignedByte[256]; + + static + { + for(int i = 0; i<256; i++) + { + cachedValues[i] = new UnsignedByte((byte)i); + } + } + + public UnsignedByte(byte underlying) + { + _underlying = underlying; + } + + @Override + public byte byteValue() + { + return _underlying; + } + + @Override + public short shortValue() + { + return (short) intValue(); + } + + @Override + public int intValue() + { + return ((int)_underlying) & 0xFF; + } + + @Override + public long longValue() + { + return ((long) _underlying) & 0xFFl; + } + + @Override + public float floatValue() + { + return (float) longValue(); + } + + @Override + public double doubleValue() + { + return (double) longValue(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + UnsignedByte that = (UnsignedByte) o; + + if (_underlying != that._underlying) + { + return false; + } + + return true; + } + + public int compareTo(UnsignedByte o) + { + return Integer.signum(intValue() - o.intValue()); + } + + @Override + public int hashCode() + { + return _underlying; + } + + @Override + public String toString() + { + return String.valueOf(intValue()); + } + + public static UnsignedByte valueOf(byte underlying) + { + final int index = ((int) underlying) & 0xFF; + return cachedValues[index]; + } + + public static UnsignedByte valueOf(final String value) + throws NumberFormatException + { + int intVal = Integer.parseInt(value); + if(intVal < 0 || intVal >= (1<<8)) + { + throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<8) +")."); + } + return valueOf((byte)intVal); + } + } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedInteger.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedInteger.java index f9a8f96868..515e1d08a1 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedInteger.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedInteger.java @@ -1,149 +1,149 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -public final class UnsignedInteger extends Number implements Comparable -{ - private final int _underlying; - private static final UnsignedInteger[] cachedValues = new UnsignedInteger[256]; - - static - { - for(int i = 0; i < 256; i++) - { - cachedValues[i] = new UnsignedInteger(i); - } - } - - public static final UnsignedInteger ZERO = cachedValues[0]; - public static final UnsignedInteger ONE = cachedValues[1]; - public static final UnsignedInteger MAX_VALUE = new UnsignedInteger(0xffffffff); - - - public UnsignedInteger(int underlying) - { - _underlying = underlying; - } - - @Override - public int intValue() - { - return _underlying; - } - - @Override - public long longValue() - { - return ((long) _underlying) & 0xFFFFFFFFl; - } - - @Override - public float floatValue() - { - return (float) longValue(); - } - - @Override - public double doubleValue() - { - return (double) longValue(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - UnsignedInteger that = (UnsignedInteger) o; - - if (_underlying != that._underlying) - { - return false; - } - - return true; - } - - public int compareTo(UnsignedInteger o) - { - return Long.signum(longValue() - o.longValue()); - } - - @Override - public int hashCode() - { - return _underlying; - } - - @Override - public String toString() - { - return String.valueOf(longValue()); - } - - public static UnsignedInteger valueOf(int underlying) - { - if((underlying & 0xFFFFFF00) == 0) - { - return cachedValues[underlying]; - } - else - { - return new UnsignedInteger(underlying); - } - } - - public UnsignedInteger add(final UnsignedInteger i) - { - int val = _underlying + i._underlying; - return UnsignedInteger.valueOf(val); - } - - public UnsignedInteger subtract(final UnsignedInteger i) - { - int val = _underlying - i._underlying; - return UnsignedInteger.valueOf(val); - } - - public static UnsignedInteger valueOf(final String value) - { - long longVal = Long.parseLong(value); - return valueOf(longVal); - } - - public static UnsignedInteger valueOf(final long longVal) - { - if(longVal < 0L || longVal >= (1L<<32)) - { - throw new NumberFormatException("Value \""+longVal+"\" lies outside the range [" + 0L + "-" + (1L<<32) +")."); - } - return valueOf((int)longVal); - } - -} +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +public final class UnsignedInteger extends Number implements Comparable +{ + private final int _underlying; + private static final UnsignedInteger[] cachedValues = new UnsignedInteger[256]; + + static + { + for(int i = 0; i < 256; i++) + { + cachedValues[i] = new UnsignedInteger(i); + } + } + + public static final UnsignedInteger ZERO = cachedValues[0]; + public static final UnsignedInteger ONE = cachedValues[1]; + public static final UnsignedInteger MAX_VALUE = new UnsignedInteger(0xffffffff); + + + public UnsignedInteger(int underlying) + { + _underlying = underlying; + } + + @Override + public int intValue() + { + return _underlying; + } + + @Override + public long longValue() + { + return ((long) _underlying) & 0xFFFFFFFFl; + } + + @Override + public float floatValue() + { + return (float) longValue(); + } + + @Override + public double doubleValue() + { + return (double) longValue(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + UnsignedInteger that = (UnsignedInteger) o; + + if (_underlying != that._underlying) + { + return false; + } + + return true; + } + + public int compareTo(UnsignedInteger o) + { + return Long.signum(longValue() - o.longValue()); + } + + @Override + public int hashCode() + { + return _underlying; + } + + @Override + public String toString() + { + return String.valueOf(longValue()); + } + + public static UnsignedInteger valueOf(int underlying) + { + if((underlying & 0xFFFFFF00) == 0) + { + return cachedValues[underlying]; + } + else + { + return new UnsignedInteger(underlying); + } + } + + public UnsignedInteger add(final UnsignedInteger i) + { + int val = _underlying + i._underlying; + return UnsignedInteger.valueOf(val); + } + + public UnsignedInteger subtract(final UnsignedInteger i) + { + int val = _underlying - i._underlying; + return UnsignedInteger.valueOf(val); + } + + public static UnsignedInteger valueOf(final String value) + { + long longVal = Long.parseLong(value); + return valueOf(longVal); + } + + public static UnsignedInteger valueOf(final long longVal) + { + if(longVal < 0L || longVal >= (1L<<32)) + { + throw new NumberFormatException("Value \""+longVal+"\" lies outside the range [" + 0L + "-" + (1L<<32) +")."); + } + return valueOf((int)longVal); + } + +} diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedLong.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedLong.java index 0586423fe0..ebf8bc9c28 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedLong.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedLong.java @@ -1,162 +1,162 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -import java.math.BigInteger; - -public final class UnsignedLong extends Number implements Comparable -{ - private static final BigInteger TWO_TO_THE_SIXTY_FOUR = new BigInteger( new byte[] { (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }); - private static final BigInteger LONG_MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE); - - private static final UnsignedLong[] cachedValues = new UnsignedLong[256]; - - static - { - for(int i = 0; i<256; i++) - { - cachedValues[i] = new UnsignedLong(i); - } - } - - public static final UnsignedLong ZERO = cachedValues[0]; - public static final UnsignedLong ONE = cachedValues[1]; - - private final long _underlying; - - - - public UnsignedLong(long underlying) - { - _underlying = underlying; - } - - @Override - public int intValue() - { - return (int) _underlying; - } - - @Override - public long longValue() - { - return _underlying; - } - - public BigInteger bigIntegerValue() - { - if(_underlying >= 0L) - { - return BigInteger.valueOf(_underlying); - } - else - { - return TWO_TO_THE_SIXTY_FOUR.add(BigInteger.valueOf(_underlying)); - } - } - - @Override - public float floatValue() - { - return (float) longValue(); - } - - @Override - public double doubleValue() - { - return (double) longValue(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - UnsignedLong that = (UnsignedLong) o; - - if (_underlying != that._underlying) - { - return false; - } - - return true; - } - - public int compareTo(UnsignedLong o) - { - return bigIntegerValue().compareTo(o.bigIntegerValue()); - } - - @Override - public int hashCode() - { - return (int)(_underlying ^ (_underlying >>> 32)); - } - - @Override - public String toString() - { - return String.valueOf(bigIntegerValue()); - } - - public static UnsignedLong valueOf(long underlying) - { - if((underlying & 0xFFL) == underlying) - { - return cachedValues[(int)underlying]; - } - else - { - return new UnsignedLong(underlying); - } - } - - public static UnsignedLong valueOf(final String value) - { - BigInteger bigInt = new BigInteger(value); - if(bigInt.signum() == -1 || bigInt.bitCount()>64) - { - throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0L + "- 2^64)."); - } - else if(bigInt.compareTo(LONG_MAX_VALUE)>=0) - { - return UnsignedLong.valueOf(bigInt.longValue()); - } - else - { - return UnsignedLong.valueOf(TWO_TO_THE_SIXTY_FOUR.subtract(bigInt).negate().longValue()); - } - - } - - public UnsignedLong add(UnsignedLong unsignedLong) - { - return UnsignedLong.valueOf(_underlying + unsignedLong._underlying); - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +import java.math.BigInteger; + +public final class UnsignedLong extends Number implements Comparable +{ + private static final BigInteger TWO_TO_THE_SIXTY_FOUR = new BigInteger( new byte[] { (byte) 1, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }); + private static final BigInteger LONG_MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE); + + private static final UnsignedLong[] cachedValues = new UnsignedLong[256]; + + static + { + for(int i = 0; i<256; i++) + { + cachedValues[i] = new UnsignedLong(i); + } + } + + public static final UnsignedLong ZERO = cachedValues[0]; + public static final UnsignedLong ONE = cachedValues[1]; + + private final long _underlying; + + + + public UnsignedLong(long underlying) + { + _underlying = underlying; + } + + @Override + public int intValue() + { + return (int) _underlying; + } + + @Override + public long longValue() + { + return _underlying; + } + + public BigInteger bigIntegerValue() + { + if(_underlying >= 0L) + { + return BigInteger.valueOf(_underlying); + } + else + { + return TWO_TO_THE_SIXTY_FOUR.add(BigInteger.valueOf(_underlying)); + } + } + + @Override + public float floatValue() + { + return (float) longValue(); + } + + @Override + public double doubleValue() + { + return (double) longValue(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + UnsignedLong that = (UnsignedLong) o; + + if (_underlying != that._underlying) + { + return false; + } + + return true; + } + + public int compareTo(UnsignedLong o) + { + return bigIntegerValue().compareTo(o.bigIntegerValue()); + } + + @Override + public int hashCode() + { + return (int)(_underlying ^ (_underlying >>> 32)); + } + + @Override + public String toString() + { + return String.valueOf(bigIntegerValue()); + } + + public static UnsignedLong valueOf(long underlying) + { + if((underlying & 0xFFL) == underlying) + { + return cachedValues[(int)underlying]; + } + else + { + return new UnsignedLong(underlying); + } + } + + public static UnsignedLong valueOf(final String value) + { + BigInteger bigInt = new BigInteger(value); + if(bigInt.signum() == -1 || bigInt.bitCount()>64) + { + throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0L + "- 2^64)."); + } + else if(bigInt.compareTo(LONG_MAX_VALUE)>=0) + { + return UnsignedLong.valueOf(bigInt.longValue()); + } + else + { + return UnsignedLong.valueOf(TWO_TO_THE_SIXTY_FOUR.subtract(bigInt).negate().longValue()); + } + + } + + public UnsignedLong add(UnsignedLong unsignedLong) + { + return UnsignedLong.valueOf(_underlying + unsignedLong._underlying); + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedShort.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedShort.java index 01c72ac159..a576db2a55 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedShort.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/UnsignedShort.java @@ -1,132 +1,132 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.qpid.amqp_1_0.type; - -public final class UnsignedShort extends Number implements Comparable -{ - private final short _underlying; - private static final UnsignedShort[] cachedValues = new UnsignedShort[256]; - - static - { - for(short i = 0; i < 256; i++) - { - cachedValues[i] = new UnsignedShort(i); - } - } - - public UnsignedShort(short underlying) - { - _underlying = underlying; - } - - public short shortValue() - { - return _underlying; - } - - @Override - public int intValue() - { - return _underlying & 0xFFFF; - } - - @Override - public long longValue() - { - return ((long) _underlying) & 0xFFFFl; - } - - @Override - public float floatValue() - { - return (float) intValue(); - } - - @Override - public double doubleValue() - { - return (double) intValue(); - } - - @Override - public boolean equals(Object o) - { - if (this == o) - { - return true; - } - if (o == null || getClass() != o.getClass()) - { - return false; - } - - UnsignedShort that = (UnsignedShort) o; - - if (_underlying != that._underlying) - { - return false; - } - - return true; - } - - public int compareTo(UnsignedShort o) - { - return Integer.signum(intValue() - o.intValue()); - } - - @Override - public int hashCode() - { - return _underlying; - } - - @Override - public String toString() - { - return String.valueOf(longValue()); - } - - public static UnsignedShort valueOf(short underlying) - { - if((underlying & 0xFF00) == 0) - { - return cachedValues[underlying]; - } - else - { - return new UnsignedShort(underlying); - } - } - - public static UnsignedShort valueOf(final String value) - { - int intVal = Integer.parseInt(value); - if(intVal < 0 || intVal >= (1<<16)) - { - throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<16) +")."); - } - return valueOf((short)intVal); - - } +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.qpid.amqp_1_0.type; + +public final class UnsignedShort extends Number implements Comparable +{ + private final short _underlying; + private static final UnsignedShort[] cachedValues = new UnsignedShort[256]; + + static + { + for(short i = 0; i < 256; i++) + { + cachedValues[i] = new UnsignedShort(i); + } + } + + public UnsignedShort(short underlying) + { + _underlying = underlying; + } + + public short shortValue() + { + return _underlying; + } + + @Override + public int intValue() + { + return _underlying & 0xFFFF; + } + + @Override + public long longValue() + { + return ((long) _underlying) & 0xFFFFl; + } + + @Override + public float floatValue() + { + return (float) intValue(); + } + + @Override + public double doubleValue() + { + return (double) intValue(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + + UnsignedShort that = (UnsignedShort) o; + + if (_underlying != that._underlying) + { + return false; + } + + return true; + } + + public int compareTo(UnsignedShort o) + { + return Integer.signum(intValue() - o.intValue()); + } + + @Override + public int hashCode() + { + return _underlying; + } + + @Override + public String toString() + { + return String.valueOf(longValue()); + } + + public static UnsignedShort valueOf(short underlying) + { + if((underlying & 0xFF00) == 0) + { + return cachedValues[underlying]; + } + else + { + return new UnsignedShort(underlying); + } + } + + public static UnsignedShort valueOf(final String value) + { + int intVal = Integer.parseInt(value); + if(intVal < 0 || intVal >= (1<<16)) + { + throw new NumberFormatException("Value \""+value+"\" lies outside the range [" + 0 + "-" + (1<<16) +")."); + } + return valueOf((short)intVal); + + } } \ No newline at end of file diff --git a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/WrapperType.java b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/WrapperType.java index 359926d9d4..2304559da9 100644 --- a/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/WrapperType.java +++ b/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/type/WrapperType.java @@ -1,27 +1,27 @@ -/* -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ - -package org.apache.qpid.amqp_1_0.type; - -public interface WrapperType -{ - Object getValue(); -} +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +package org.apache.qpid.amqp_1_0.type; + +public interface WrapperType +{ + Object getValue(); +} -- cgit v1.2.1