This implementation is intended to optimise the performance of lookup(String)
+ * to about the level of a HashMap get. It has been observed that the scheme
+ * resolution phase performed by the JVM takes considerably longer, so for
+ * optimum performance lookups should be coded like:
+ *
+ * Context componentContext = (Context)new InitialContext().lookup("java:comp");
+ * String envEntry = (String) componentContext.lookup("env/myEntry");
+ * String envEntry2 = (String) componentContext.lookup("env/myEntry2");
+ *
+ */
+public class ReadOnlyContext implements Context, Serializable
+{
+ private static final long serialVersionUID = -5754338187296859149L;
+ protected static final NameParser nameParser = new NameParserImpl();
+
+ protected final Hashtable environment; // environment for this context
+ protected final Map bindings; // bindings at my level
+ protected final Map treeBindings; // all bindings under me
+
+ private boolean frozen = false;
+ private String nameInNamespace = "";
+ public static final String SEPARATOR = "/";
+
+ public ReadOnlyContext()
+ {
+ environment = new Hashtable();
+ bindings = new HashMap();
+ treeBindings = new HashMap();
+ }
+
+ public ReadOnlyContext(Hashtable env)
+ {
+ if (env == null)
+ {
+ this.environment = new Hashtable();
+ }
+ else
+ {
+ this.environment = new Hashtable(env);
+ }
+
+ this.bindings = Collections.EMPTY_MAP;
+ this.treeBindings = Collections.EMPTY_MAP;
+ }
+
+ public ReadOnlyContext(Hashtable environment, Map bindings)
+ {
+ if (environment == null)
+ {
+ this.environment = new Hashtable();
+ }
+ else
+ {
+ this.environment = new Hashtable(environment);
+ }
+
+ this.bindings = bindings;
+ treeBindings = new HashMap();
+ frozen = true;
+ }
+
+ public ReadOnlyContext(Hashtable environment, Map bindings, String nameInNamespace)
+ {
+ this(environment, bindings);
+ this.nameInNamespace = nameInNamespace;
+ }
+
+ protected ReadOnlyContext(ReadOnlyContext clone, Hashtable env)
+ {
+ this.bindings = clone.bindings;
+ this.treeBindings = clone.treeBindings;
+ this.environment = new Hashtable(env);
+ }
+
+ protected ReadOnlyContext(ReadOnlyContext clone, Hashtable env, String nameInNamespace)
+ {
+ this(clone, env);
+ this.nameInNamespace = nameInNamespace;
+ }
+
+ public void freeze()
+ {
+ frozen = true;
+ }
+
+ boolean isFrozen()
+ {
+ return frozen;
+ }
+
+ /**
+ * internalBind is intended for use only during setup or possibly by suitably synchronized superclasses.
+ * It binds every possible lookup into a map in each context. To do this, each context
+ * strips off one name segment and if necessary creates a new context for it. Then it asks that context
+ * to bind the remaining name. It returns a map containing all the bindings from the next context, plus
+ * the context it just created (if it in fact created it). (the names are suitably extended by the segment
+ * originally lopped off).
+ *
+ * @param name
+ * @param value
+ * @return
+ * @throws javax.naming.NamingException
+ */
+ protected Map internalBind(String name, Object value) throws NamingException
+ {
+ assert (name != null) && (name.length() > 0);
+ assert !frozen;
+
+ Map newBindings = new HashMap();
+ int pos = name.indexOf('/');
+ if (pos == -1)
+ {
+ if (treeBindings.put(name, value) != null)
+ {
+ throw new NamingException("Something already bound at " + name);
+ }
+
+ bindings.put(name, value);
+ newBindings.put(name, value);
+ }
+ else
+ {
+ String segment = name.substring(0, pos);
+ assert segment != null;
+ assert !segment.equals("");
+ Object o = treeBindings.get(segment);
+ if (o == null)
+ {
+ o = newContext();
+ treeBindings.put(segment, o);
+ bindings.put(segment, o);
+ newBindings.put(segment, o);
+ }
+ else if (!(o instanceof ReadOnlyContext))
+ {
+ throw new NamingException("Something already bound where a subcontext should go");
+ }
+
+ ReadOnlyContext readOnlyContext = (ReadOnlyContext) o;
+ String remainder = name.substring(pos + 1);
+ Map subBindings = readOnlyContext.internalBind(remainder, value);
+ for (Iterator iterator = subBindings.entrySet().iterator(); iterator.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) iterator.next();
+ String subName = segment + "/" + (String) entry.getKey();
+ Object bound = entry.getValue();
+ treeBindings.put(subName, bound);
+ newBindings.put(subName, bound);
+ }
+ }
+
+ return newBindings;
+ }
+
+ protected ReadOnlyContext newContext()
+ {
+ return new ReadOnlyContext();
+ }
+
+ public Object addToEnvironment(String propName, Object propVal) throws NamingException
+ {
+ return environment.put(propName, propVal);
+ }
+
+ public Hashtable getEnvironment() throws NamingException
+ {
+ return (Hashtable) environment.clone();
+ }
+
+ public Object removeFromEnvironment(String propName) throws NamingException
+ {
+ return environment.remove(propName);
+ }
+
+ public Object lookup(String name) throws NamingException
+ {
+ if (name.length() == 0)
+ {
+ return this;
+ }
+
+ Object result = treeBindings.get(name);
+ if (result == null)
+ {
+ result = bindings.get(name);
+ }
+
+ if (result == null)
+ {
+ int pos = name.indexOf(':');
+ if (pos > 0)
+ {
+ String scheme = name.substring(0, pos);
+ Context ctx = NamingManager.getURLContext(scheme, environment);
+ if (ctx == null)
+ {
+ throw new NamingException("scheme " + scheme + " not recognized");
+ }
+
+ return ctx.lookup(name);
+ }
+ else
+ {
+ // Split out the first name of the path
+ // and look for it in the bindings map.
+ CompositeName path = new CompositeName(name);
+
+ if (path.size() == 0)
+ {
+ return this;
+ }
+ else
+ {
+ String first = path.get(0);
+ Object obj = bindings.get(first);
+ if (obj == null)
+ {
+ throw new NameNotFoundException(name);
+ }
+ else if ((obj instanceof Context) && (path.size() > 1))
+ {
+ Context subContext = (Context) obj;
+ obj = subContext.lookup(path.getSuffix(1));
+ }
+
+ return obj;
+ }
+ }
+ }
+
+ if (result instanceof LinkRef)
+ {
+ LinkRef ref = (LinkRef) result;
+ result = lookup(ref.getLinkName());
+ }
+
+ if (result instanceof Reference)
+ {
+ try
+ {
+ result = NamingManager.getObjectInstance(result, null, null, this.environment);
+ }
+ catch (NamingException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
+ }
+ }
+
+ if (result instanceof ReadOnlyContext)
+ {
+ String prefix = getNameInNamespace();
+ if (prefix.length() > 0)
+ {
+ prefix = prefix + SEPARATOR;
+ }
+
+ result = new ReadOnlyContext((ReadOnlyContext) result, environment, prefix + name);
+ }
+
+ return result;
+ }
+
+ public Object lookup(Name name) throws NamingException
+ {
+ return lookup(name.toString());
+ }
+
+ public Object lookupLink(String name) throws NamingException
+ {
+ return lookup(name);
+ }
+
+ public Name composeName(Name name, Name prefix) throws NamingException
+ {
+ Name result = (Name) prefix.clone();
+ result.addAll(name);
+
+ return result;
+ }
+
+ public String composeName(String name, String prefix) throws NamingException
+ {
+ CompositeName result = new CompositeName(prefix);
+ result.addAll(new CompositeName(name));
+
+ return result.toString();
+ }
+
+ public NamingEnumeration list(String name) throws NamingException
+ {
+ Object o = lookup(name);
+ if (o == this)
+ {
+ return new ListEnumeration();
+ }
+ else if (o instanceof Context)
+ {
+ return ((Context) o).list("");
+ }
+ else
+ {
+ throw new NotContextException();
+ }
+ }
+
+ public NamingEnumeration listBindings(String name) throws NamingException
+ {
+ Object o = lookup(name);
+ if (o == this)
+ {
+ return new ListBindingEnumeration();
+ }
+ else if (o instanceof Context)
+ {
+ return ((Context) o).listBindings("");
+ }
+ else
+ {
+ throw new NotContextException();
+ }
+ }
+
+ public Object lookupLink(Name name) throws NamingException
+ {
+ return lookupLink(name.toString());
+ }
+
+ public NamingEnumeration list(Name name) throws NamingException
+ {
+ return list(name.toString());
+ }
+
+ public NamingEnumeration listBindings(Name name) throws NamingException
+ {
+ return listBindings(name.toString());
+ }
+
+ public void bind(Name name, Object obj) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void bind(String name, Object obj) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void close() throws NamingException
+ {
+ // ignore
+ }
+
+ public Context createSubcontext(Name name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public Context createSubcontext(String name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void destroySubcontext(Name name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void destroySubcontext(String name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public String getNameInNamespace() throws NamingException
+ {
+ return nameInNamespace;
+ }
+
+ public NameParser getNameParser(Name name) throws NamingException
+ {
+ return nameParser;
+ }
+
+ public NameParser getNameParser(String name) throws NamingException
+ {
+ return nameParser;
+ }
+
+ public void rebind(Name name, Object obj) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void rebind(String name, Object obj) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void rename(Name oldName, Name newName) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void rename(String oldName, String newName) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void unbind(Name name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ public void unbind(String name) throws NamingException
+ {
+ throw new OperationNotSupportedException();
+ }
+
+ private abstract class LocalNamingEnumeration implements NamingEnumeration
+ {
+ private Iterator i = bindings.entrySet().iterator();
+
+ public boolean hasMore() throws NamingException
+ {
+ return i.hasNext();
+ }
+
+ public boolean hasMoreElements()
+ {
+ return i.hasNext();
+ }
+
+ protected Map.Entry getNext()
+ {
+ return (Map.Entry) i.next();
+ }
+
+ public void close() throws NamingException
+ { }
+ }
+
+ private class ListEnumeration extends LocalNamingEnumeration
+ {
+ public Object next() throws NamingException
+ {
+ return nextElement();
+ }
+
+ public Object nextElement()
+ {
+ Map.Entry entry = getNext();
+
+ return new NameClassPair((String) entry.getKey(), entry.getValue().getClass().getName());
+ }
+ }
+
+ private class ListBindingEnumeration extends LocalNamingEnumeration
+ {
+ public Object next() throws NamingException
+ {
+ return nextElement();
+ }
+
+ public Object nextElement()
+ {
+ Map.Entry entry = getNext();
+
+ return new Binding((String) entry.getKey(), entry.getValue());
+ }
+ }
+}
diff --git a/java/amqp-1-0-client/build.xml b/java/amqp-1-0-client/build.xml
new file mode 100644
index 0000000000..173d7540d4
--- /dev/null
+++ b/java/amqp-1-0-client/build.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/AcknowledgeMode.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/AcknowledgeMode.java
new file mode 100644
index 0000000000..05d176bc35
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/AcknowledgeMode.java
@@ -0,0 +1,28 @@
+/*
+ *
+ * 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.client;
+
+public enum AcknowledgeMode
+{
+ AMO,
+ ALO,
+ EO
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Command.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Command.java
new file mode 100644
index 0000000000..3bb26744c4
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Command.java
@@ -0,0 +1,43 @@
+/*
+ *
+ * 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.client;
+
+import java.lang.reflect.InvocationTargetException;
+
+public class Command
+{
+ public static void main(String[] args) throws
+ ClassNotFoundException,
+ NoSuchMethodException,
+ InvocationTargetException,
+ IllegalAccessException,
+ InstantiationException
+ {
+ String name = args[0];
+ String[] cmdArgs = new String[args.length-1];
+ System.arraycopy(args,1,cmdArgs,0,args.length-1);
+ name = "org.apache.qpid.amqp_1_0.client." + String.valueOf(name.charAt(0)).toUpperCase() + name.substring(1).toLowerCase();
+ Class clazz = (Class) Class.forName(name);
+ Util util = clazz.getDeclaredConstructor(String[].class).newInstance((Object)cmdArgs);
+ util.run();
+
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java
new file mode 100644
index 0000000000..e3d56fae09
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Connection.java
@@ -0,0 +1,481 @@
+/*
+ *
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.framing.ConnectionHandler;
+import org.apache.qpid.amqp_1_0.transport.AMQPTransport;
+import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint;
+import org.apache.qpid.amqp_1_0.transport.Container;
+import org.apache.qpid.amqp_1_0.transport.StateChangeListener;
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.qpid.amqp_1_0.type.FrameBody;
+import org.apache.qpid.amqp_1_0.type.SaslFrameBody;
+import org.apache.qpid.amqp_1_0.type.UnsignedInteger;
+
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.nio.ByteBuffer;
+import java.security.Principal;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class Connection
+{
+ private static final Logger RAW_LOGGER = Logger.getLogger("RAW");
+ private static final int MAX_FRAME_SIZE = 65536;
+
+ private String _address;
+ private ConnectionEndpoint _conn;
+ private int _sessionCount;
+
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password) throws ConnectionException
+ {
+ this(address, port, username, password, MAX_FRAME_SIZE);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password, String remoteHostname) throws ConnectionException
+ {
+ this(address, port, username, password, MAX_FRAME_SIZE,new Container(),remoteHostname);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final int maxFrameSize) throws ConnectionException
+ {
+ this(address,port,username,password,maxFrameSize,new Container());
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final Container container) throws ConnectionException
+ {
+ this(address,port,username,password,MAX_FRAME_SIZE,container);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final int maxFrameSize,
+ final Container container) throws ConnectionException
+ {
+ this(address,port,username,password,maxFrameSize,container, null);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final int maxFrameSize,
+ final Container container,
+ final String remoteHostname) throws ConnectionException
+ {
+ this(address,port,username,password,maxFrameSize,container,remoteHostname,false);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final Container container,
+ final boolean ssl) throws ConnectionException
+ {
+ this(address, port, username, password, MAX_FRAME_SIZE,container,null,ssl);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final String remoteHost,
+ final boolean ssl) throws ConnectionException
+ {
+ this(address, port, username, password, MAX_FRAME_SIZE,new Container(),remoteHost,ssl);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final Container container,
+ final String remoteHost,
+ final boolean ssl) throws ConnectionException
+ {
+ this(address, port, username, password, MAX_FRAME_SIZE,container,remoteHost,ssl);
+ }
+
+ public Connection(final String address,
+ final int port,
+ final String username,
+ final String password,
+ final int maxFrameSize,
+ final Container container,
+ final String remoteHostname, boolean ssl) throws ConnectionException
+ {
+
+ _address = address;
+
+ try
+ {
+ final Socket s;
+ if(ssl)
+ {
+ s = SSLSocketFactory.getDefault().createSocket(address, port);
+ }
+ else
+ {
+ s = new Socket(address, port);
+ }
+
+
+ Principal principal = username == null ? null : new Principal()
+ {
+
+ public String getName()
+ {
+ return username;
+ }
+ };
+ _conn = new ConnectionEndpoint(container, principal, password);
+ _conn.setDesiredMaxFrameSize(UnsignedInteger.valueOf(maxFrameSize));
+ _conn.setRemoteAddress(s.getRemoteSocketAddress());
+ _conn.setRemoteHostname(remoteHostname);
+
+
+
+ ConnectionHandler.FrameOutput out = new ConnectionHandler.FrameOutput(_conn);
+
+
+ final OutputStream outputStream = s.getOutputStream();
+
+ ConnectionHandler.BytesSource src;
+
+ if(_conn.requiresSASL())
+ {
+ ConnectionHandler.FrameOutput saslOut = new ConnectionHandler.FrameOutput(_conn);
+
+ src = new ConnectionHandler.SequentialBytesSource(new ConnectionHandler.HeaderBytesSource(_conn, (byte)'A',
+ (byte)'M',
+ (byte)'Q',
+ (byte)'P',
+ (byte)3,
+ (byte)1,
+ (byte)0,
+ (byte)0),
+ new ConnectionHandler.FrameToBytesSourceAdapter(saslOut,_conn.getDescribedTypeRegistry()),
+ new ConnectionHandler.HeaderBytesSource(_conn, (byte)'A',
+ (byte)'M',
+ (byte)'Q',
+ (byte)'P',
+ (byte)0,
+ (byte)1,
+ (byte)0,
+ (byte)0),
+ new ConnectionHandler.FrameToBytesSourceAdapter(out,_conn.getDescribedTypeRegistry())
+ );
+
+ _conn.setSaslFrameOutput(saslOut);
+ }
+ else
+ {
+ src = new ConnectionHandler.SequentialBytesSource(new ConnectionHandler.HeaderBytesSource(_conn,(byte)'A',
+ (byte)'M',
+ (byte)'Q',
+ (byte)'P',
+ (byte)0,
+ (byte)1,
+ (byte)0,
+ (byte)0),
+ new ConnectionHandler.FrameToBytesSourceAdapter(out,_conn.getDescribedTypeRegistry())
+ );
+ }
+
+
+ //ConnectionHandler.OutputHandler outputHandler = new ConnectionHandler.OutputHandler(outputStream, out, _conn.getDescribedTypeRegistry());
+ ConnectionHandler.BytesOutputHandler outputHandler = new ConnectionHandler.BytesOutputHandler(outputStream, src, _conn);
+ Thread outputThread = new Thread(outputHandler);
+ outputThread.setDaemon(true);
+ outputThread.start();
+ _conn.setFrameOutputHandler(out);
+
+
+
+ final ConnectionHandler handler = new ConnectionHandler(_conn);
+ final InputStream inputStream = s.getInputStream();
+
+ //final AMQPTransport transport = new AMQPTransport(new AMQPFrameTransport(_conn));
+
+ Thread inputThread = new Thread(new Runnable()
+ {
+
+ public void run()
+ {
+ try
+ {
+ doRead(handler, inputStream);
+// doRead(transport, inputStream);
+ }
+ finally
+ {
+ if(_conn.closedForInput() && _conn.closedForOutput())
+ {
+ try
+ {
+ s.close();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+ }
+ });
+
+ inputThread.setDaemon(true);
+ inputThread.start();
+
+/*
+ Thread outputThread = new Thread(new Runnable()
+ {
+
+ private int _lastWrite;
+
+ public void run()
+ {
+ try
+ {
+// doRead(handler, inputStream);
+ final Object lock = new Object();
+ transport.setOutputStateChangeListener(new StateChangeListener()
+ {
+
+ public void onStateChange(final boolean active)
+ {
+ synchronized (lock)
+ {
+ lock.notifyAll();
+ }
+ }
+ });
+
+ synchronized(lock)
+ {
+ while(transport.isOpenForOutput())
+ {
+ _lastWrite = 0;
+ transport.getNextBytes(new BytesProcessor()
+ {
+
+ public void processBytes(final ByteBuffer buf)
+ {
+ _lastWrite = buf.remaining();
+ try
+ {
+ outputStream.write(buf.array(),
+ buf.arrayOffset() + buf.position(),
+ buf.limit() - buf.position());
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ });
+ if(_lastWrite == 0 && transport.isOpenForOutput())
+ {
+ try
+ {
+ lock.wait(1000);
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+ }
+ }
+ finally
+ {
+ if(_conn.closedForInput() && _conn.closedForOutput())
+ {
+ try
+ {
+ s.close();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+ }
+ });
+*/
+
+ _conn.open();
+
+ }
+ catch (IOException e)
+ {
+ throw new ConnectionException(e);
+ }
+
+
+ }
+
+
+
+ private void doRead(final AMQPTransport transport, final InputStream inputStream)
+ {
+ byte[] buf = new byte[2<<15];
+ ByteBuffer bbuf = ByteBuffer.wrap(buf);
+ final Object lock = new Object();
+ transport.setInputStateChangeListener(new StateChangeListener(){
+
+ public void onStateChange(final boolean active)
+ {
+ synchronized(lock)
+ {
+ lock.notifyAll();
+ }
+ }
+ });
+
+ try
+ {
+ int read;
+ while((read = inputStream.read(buf)) != -1)
+ {
+ bbuf.position(0);
+ bbuf.limit(read);
+
+ while(bbuf.hasRemaining() && transport.isOpenForInput())
+ {
+ transport.processBytes(bbuf);
+ }
+
+
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+
+ }
+
+ public Session createSession()
+ {
+ Session session = new Session(this,String.valueOf(_sessionCount++));
+ return session;
+ }
+
+ public ConnectionEndpoint getEndpoint()
+ {
+ return _conn;
+ }
+
+ private void doRead(final ConnectionHandler handler, final InputStream inputStream)
+ {
+ byte[] buf = new byte[2<<15];
+
+
+ try
+ {
+ int read;
+ boolean done = false;
+ while(!done && (read = inputStream.read(buf)) != -1)
+ {
+ ByteBuffer bbuf = ByteBuffer.wrap(buf, 0, read);
+ Binary b = new Binary(buf,0,read);
+
+ if(RAW_LOGGER.isLoggable(Level.FINE))
+ {
+ RAW_LOGGER.fine("RECV [" + _conn.getRemoteAddress() + "] : " + b.toString());
+ }
+ /*System.err.println(b);
+ System.err.println("XXX: " + bbuf.hasRemaining() + "; " + handler.isDone());
+ if(handler.isDone())
+ {
+ System.err.println(handler.getClass().getName() + "IS DONE!");
+ } */
+ while(bbuf.hasRemaining() && !handler.isDone())
+ {
+ handler.parse(bbuf);
+ }
+
+
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+
+ public void close()
+ {
+ _conn.close();
+
+ synchronized (_conn.getLock())
+ {
+ while(!_conn.closedForInput())
+ {
+ try
+ {
+ _conn.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+
+ }
+ }
+ }
+ }
+
+
+ public static class ConnectionException extends Exception
+ {
+ public ConnectionException(Throwable cause)
+ {
+ super(cause);
+ }
+ }
+
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java
new file mode 100644
index 0000000000..b58ce6bfe5
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Demo.java
@@ -0,0 +1,407 @@
+/*
+ *
+ * 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.client;
+
+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:";
+ private static final String OPCODE = "opcode";
+ private static final String ACTION = "action";
+ private static final String MESSAGE_ID = "message-id";
+ private static final String VENDOR = "vendor";
+ private static final String LOG = "log";
+ private static final String RECEIVED = "received";
+ private static final String TEST = "test";
+ private static final String APACHE = "apache";
+ private static final String SENT = "sent";
+ private static final String LINK_REF = "link-ref";
+ private static final String HOST = "host";
+ private static final String PORT = "port";
+ private static final String SASL_USER = "sasl-user";
+ private static final String SASL_PASSWORD = "sasl-password";
+ private static final String ROLE = "role";
+ private static final String ADDRESS = "address";
+ private static final String SENDER = "sender";
+ private static final String SEND_MESSAGE = "send-message";
+ private static final String ANNOUNCE = "announce";
+ private static final String MESSAGE_VENDOR = "message-vendor";
+ private static final String CREATE_LINK = "create-link";
+
+ public static void main(String[] args)
+ {
+ new Demo(args).run();
+ }
+
+ public Demo(String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasWindowSizeOption()
+ {
+ return false;
+ }
+
+ public void run()
+ {
+
+ try
+ {
+
+ final String vendor = getArgs()[0];
+ final String queue = "control";
+
+ String message = "";
+
+ Connection conn = newConnection();
+ Session session = conn.createSession();
+
+
+ Receiver responseReceiver;
+
+ responseReceiver = session.createTemporaryQueueReceiver();
+
+
+
+
+ responseReceiver.setCredit(UnsignedInteger.valueOf(getWindowSize()), true);
+
+
+
+ Sender s = session.createSender(queue, getWindowSize(), getMode());
+
+
+ Properties properties = new Properties();
+ properties.setMessageId(java.util.UUID.randomUUID());
+ properties.setReplyTo(responseReceiver.getAddress());
+
+ HashMap appPropMap = new HashMap();
+ ApplicationProperties appProperties = new ApplicationProperties(appPropMap);
+
+ appPropMap.put(OPCODE, ANNOUNCE);
+ appPropMap.put(VENDOR, vendor);
+ appPropMap.put(ADDRESS,responseReceiver.getAddress());
+
+ AmqpValue amqpValue = new AmqpValue(message);
+ Section[] sections = { properties, appProperties, amqpValue};
+ final Message message1 = new Message(Arrays.asList(sections));
+
+ s.send(message1);
+
+ Map sendingLinks = new HashMap();
+ Map receivingLinks = new HashMap();
+
+
+ boolean done = false;
+
+ while(!done)
+ {
+ boolean wait = true;
+ Message m = responseReceiver.receive(false);
+ if(m != null)
+ {
+ List payload = m.getPayload();
+ wait = false;
+ ApplicationProperties props = m.getApplicationProperties();
+ Map map = props.getValue();
+ String op = (String) map.get(OPCODE);
+ if("reset".equals(op))
+ {
+ for(Sender sender : sendingLinks.values())
+ {
+ try
+ {
+ sender.close();
+ Session session1 = sender.getSession();
+ session1.close();
+ session1.getConnection().close();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ for(Receiver receiver : receivingLinks.values())
+ {
+ try
+ {
+ receiver.close();
+ receiver.getSession().close();
+ receiver.getSession().getConnection().close();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ sendingLinks.clear();
+ receivingLinks.clear();
+ }
+ else if(CREATE_LINK.equals(op))
+ {
+ Object linkRef = map.get(LINK_REF);
+ String host = (String) map.get(HOST);
+ Object o = map.get(PORT);
+ int port = Integer.parseInt(String.valueOf(o));
+ String user = (String) map.get(SASL_USER);
+ String password = (String) map.get(SASL_PASSWORD);
+ String role = (String) map.get(ROLE);
+ String address = (String) map.get(ADDRESS);
+ System.err.println("Host: " + host + "\tPort: " + port + "\t user: " + user +"\t password: " + password);
+ try{
+
+
+ Connection conn2 = new Connection(host, port, user, password, host);
+ Session session2 = conn2.createSession();
+ if(sendingLinks.containsKey(linkRef))
+ {
+ try
+ {
+ sendingLinks.remove(linkRef).close();
+ }
+ catch (Exception e)
+ {
+
+ }
+ }
+ if(receivingLinks.containsKey(linkRef))
+ {
+ try
+ {
+ receivingLinks.remove(linkRef).close();
+ }
+ catch (Exception e)
+ {
+
+ }
+ }
+ if(SENDER.equals(role))
+ {
+
+ System.err.println("%%% Creating sender (" + linkRef + ")");
+ Sender sender = session2.createSender(address);
+ sendingLinks.put(linkRef, sender);
+ }
+ else
+ {
+
+ System.err.println("%%% Creating receiver (" + linkRef + ")");
+ Receiver receiver2 = session2.createReceiver(address);
+ receiver2.setCredit(UnsignedInteger.valueOf(getWindowSize()), true);
+
+ receivingLinks.put(linkRef, receiver2);
+ }
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ else if(SEND_MESSAGE.equals(op))
+ {
+ Sender sender = sendingLinks.get(map.get(LINK_REF));
+ Properties m2props = new Properties();
+ Object messageId = map.get(MESSAGE_ID);
+ m2props.setMessageId(messageId);
+ Map m2propmap = new HashMap();
+ m2propmap.put(OPCODE, TEST);
+ m2propmap.put(VENDOR, vendor);
+ ApplicationProperties m2appProps = new ApplicationProperties(m2propmap);
+ Message m2 = new Message(Arrays.asList(m2props, m2appProps, new AmqpValue("AMQP-"+messageId)));
+ sender.send(m2);
+
+ Map m3propmap = new HashMap();
+ m3propmap.put(OPCODE, LOG);
+ m3propmap.put(ACTION, SENT);
+ m3propmap.put(MESSAGE_ID, messageId);
+ m3propmap.put(VENDOR, vendor);
+ m3propmap.put(MESSAGE_VENDOR, vendor);
+
+
+ Message m3 = new Message(Arrays.asList(new ApplicationProperties(m3propmap),
+ new AmqpValue("AMQP-"+messageId)));
+ s.send(m3);
+
+ }
+
+ responseReceiver.acknowledge(m);
+ }
+ else
+ {
+ for(Map.Entry entry : receivingLinks.entrySet())
+ {
+ m = entry.getValue().receive(false);
+ if(m != null)
+ {
+ wait = false;
+
+ System.err.println("%%% Received message from " + entry.getKey());
+
+ Properties mp = m.getProperties();
+ ApplicationProperties ap = m.getApplicationProperties();
+
+ Map m3propmap = new HashMap();
+ m3propmap.put(OPCODE, LOG);
+ m3propmap.put(ACTION, RECEIVED);
+ m3propmap.put(MESSAGE_ID, mp.getMessageId());
+ m3propmap.put(VENDOR, vendor);
+ m3propmap.put(MESSAGE_VENDOR, ap.getValue().get(VENDOR));
+
+ Message m3 = new Message(Arrays.asList(new ApplicationProperties(m3propmap),
+ new AmqpValue("AMQP-"+mp.getMessageId())));
+ s.send(m3);
+
+ entry.getValue().acknowledge(m);
+ }
+
+ }
+ }
+
+ if(wait)
+ {
+ try
+ {
+ Thread.sleep(500l);
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ }
+
+ }
+
+
+
+
+
+
+
+
+
+ s.close();
+ session.close();
+ conn.close();
+
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Sender.SenderClosingException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Sender.SenderCreationException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (AmqpErrorException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+
+ }
+
+ protected boolean hasSingleLinkPerConnectionMode()
+ {
+ return false;
+ }
+
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+ }
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java
new file mode 100644
index 0000000000..f61fd64a61
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Dump.java
@@ -0,0 +1,116 @@
+package org.apache.qpid.amqp_1_0.client;
+
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.commons.cli.Options;
+
+public class Dump extends Util
+{
+ private static final String USAGE_STRING = "dump [options] \n\nOptions:";
+
+
+ protected Dump(String[] args)
+ {
+ super(args);
+ }
+
+ public static void main(String[] args)
+ {
+ new Dump(args).run();
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected void printUsage(Options options)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ protected void run()
+ {
+ final String queue = getArgs()[0];
+
+ try
+ {
+ Connection conn = newConnection();
+
+ Session session = conn.createSession();
+
+
+ Sender s = session.createSender(queue, 10);
+
+ Message message = new Message("dump me");
+ message.setDeliveryTag(new Binary("dump".getBytes()));
+
+ s.send(message);
+
+ s.close();
+ session.close();
+ conn.close();
+
+ } catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ catch (Sender.SenderCreationException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ } catch (Sender.SenderClosingException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java
new file mode 100644
index 0000000000..43ddd6ca25
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filereceiver.java
@@ -0,0 +1,327 @@
+package org.apache.qpid.amqp_1_0.client;
+
+import org.apache.qpid.amqp_1_0.type.*;
+import org.apache.qpid.amqp_1_0.type.messaging.*;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.util.*;
+
+public class Filereceiver extends Util
+{
+ private static final String USAGE_STRING = "filereceiver [options] \n\nOptions:";
+
+ protected Filereceiver(String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+
+ }
+
+ @Override
+ protected void run()
+ {
+ final String queue = getArgs()[0];
+ final String directoryName = getArgs()[1];
+
+ try
+ {
+ Connection conn = newConnection();
+
+ Session session = conn.createSession();
+
+ final File directory = new File(directoryName);
+ if(directory.isDirectory() && directory.canWrite())
+ {
+ File tmpDirectory = new File(directoryName, ".tmp");
+ if(!tmpDirectory.exists())
+ {
+ tmpDirectory.mkdir();
+ }
+
+ String[] unsettledFiles = tmpDirectory.list();
+
+ Map unsettled = new HashMap();
+ final Map unsettledFileNames = new HashMap();
+
+ Accepted accepted = new Accepted();
+
+ for(String fileName : unsettledFiles)
+ {
+ File theFile = new File(tmpDirectory, fileName);
+ if(theFile.isFile())
+ {
+ if(fileName.startsWith("~") && fileName.endsWith("~"))
+ {
+ theFile.delete();
+ }
+ else
+ {
+ int splitPoint = fileName.indexOf(".");
+ String deliveryTagStr = fileName.substring(0,splitPoint);
+ String actualFileName = fileName.substring(splitPoint+1);
+
+ byte[] bytes = new byte[deliveryTagStr.length()/2];
+
+
+ for(int i = 0; i < bytes.length; i++)
+ {
+ char c = deliveryTagStr.charAt(2*i);
+ char d = deliveryTagStr.charAt(1+(2*i));
+
+ bytes[i] = (byte) (((c <= '9' ? c - '0' : c - 'W') << 4)
+ | (d <= '9' ? d - '0' : d - 'W'));
+
+ }
+ Binary deliveryTag = new Binary(bytes);
+ unsettled.put(deliveryTag, accepted);
+ unsettledFileNames.put(deliveryTag, fileName);
+ }
+ }
+
+ }
+
+ Receiver r = session.createReceiver(queue, AcknowledgeMode.EO, getLinkName(), isDurableLink(),
+ unsettled);
+
+ Map remoteUnsettled = r.getRemoteUnsettled();
+
+ for(Map.Entry entry : unsettledFileNames.entrySet())
+ {
+ if(remoteUnsettled == null || !remoteUnsettled.containsKey(entry.getKey()))
+ {
+
+ File tmpFile = new File(tmpDirectory, entry.getValue());
+ final File dest = new File(directory,
+ entry.getValue().substring(entry.getValue().indexOf(".") + 1));
+ if(dest.exists())
+ {
+ System.err.println("Duplicate detected - filename " + dest.getName());
+ }
+
+ tmpFile.renameTo(dest);
+ }
+ }
+
+
+ int credit = 10;
+
+ r.setCredit(UnsignedInteger.valueOf(credit), true);
+
+
+ int received = 0;
+ Message m = null;
+ do
+ {
+ m = isBlock() && received == 0 ? r.receive() : r.receive(10000);
+ if(m != null)
+ {
+ if(m.isResume() && unsettled.containsKey(m.getDeliveryTag()))
+ {
+ final String tmpFileName = unsettledFileNames.get(m.getDeliveryTag());
+ final File unsettledFile = new File(tmpDirectory,
+ tmpFileName);
+ r.acknowledge(m, new Receiver.SettledAction()
+ {
+ public void onSettled(final Binary deliveryTag)
+ {
+ int splitPoint = tmpFileName.indexOf(".");
+
+ String fileName = tmpFileName.substring(splitPoint+1);
+
+ final File dest = new File(directory, fileName);
+ if(dest.exists())
+ {
+ System.err.println("Duplicate detected - filename " + dest.getName());
+ }
+ unsettledFile.renameTo(dest);
+ unsettledFileNames.remove(deliveryTag);
+ }
+ });
+ }
+ else
+ {
+ received++;
+ List sections = m.getPayload();
+ Binary deliveryTag = m.getDeliveryTag();
+ StringBuilder tagNameBuilder = new StringBuilder();
+
+ ByteBuffer dtbuf = deliveryTag.asByteBuffer();
+ while(dtbuf.hasRemaining())
+ {
+ tagNameBuilder.append(String.format("%02x", dtbuf.get()));
+ }
+
+
+ ApplicationProperties properties = null;
+ List data = new ArrayList();
+ int totalSize = 0;
+ for(Section section : sections)
+ {
+ if(section instanceof ApplicationProperties)
+ {
+ properties = (ApplicationProperties) section;
+ }
+ else if(section instanceof AmqpValue)
+ {
+ AmqpValue value = (AmqpValue) section;
+ if(value.getValue() instanceof Binary)
+ {
+ Binary binary = (Binary) value.getValue();
+ data.add(binary);
+ totalSize += binary.getLength();
+
+ }
+ else
+ {
+ // TODO exception
+ }
+ }
+ else if(section instanceof Data)
+ {
+ Data value = (Data) section;
+ Binary binary = value.getValue();
+ data.add(binary);
+ totalSize += binary.getLength();
+
+ }
+ }
+ if(properties != null)
+ {
+ final String fileName = (String) properties.getValue().get("filename");
+ byte[] fileData = new byte[totalSize];
+ ByteBuffer buf = ByteBuffer.wrap(fileData);
+ int offset = 0;
+ for(Binary bin : data)
+ {
+ buf.put(bin.asByteBuffer());
+ }
+ File outputFile = new File(tmpDirectory, "~"+fileName+"~");
+ if(outputFile.exists())
+ {
+ outputFile.delete();
+ }
+ FileOutputStream fos = new FileOutputStream(outputFile);
+ fos.write(fileData);
+ fos.flush();
+ fos.close();
+
+ final File unsettledFile = new File(tmpDirectory, tagNameBuilder.toString() + "." +
+ fileName);
+ outputFile.renameTo(unsettledFile);
+ r.acknowledge(m, new Receiver.SettledAction()
+ {
+ public void onSettled(final Binary deliveryTag)
+ {
+ final File dest = new File(directory, fileName);
+ if(dest.exists())
+ {
+ System.err.println("Duplicate detected - filename " + dest.getName());
+ }
+ unsettledFile.renameTo(dest);
+
+ }
+ });
+
+ }
+ }
+ }
+ }
+ while(m != null);
+
+
+ r.close();
+ }
+ else
+ {
+ System.err.println("No such directory: " + directoryName);
+ }
+ session.close();
+ conn.close();
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace();
+ }
+ catch (FileNotFoundException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (AmqpErrorException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+
+ }
+
+ public static void main(String[] args)
+ {
+ new Filereceiver(args).run();
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java
new file mode 100644
index 0000000000..83b305ac03
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Filesender.java
@@ -0,0 +1,276 @@
+package org.apache.qpid.amqp_1_0.client;
+
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.qpid.amqp_1_0.type.DeliveryState;
+import org.apache.qpid.amqp_1_0.type.Outcome;
+import org.apache.qpid.amqp_1_0.type.Section;
+import org.apache.qpid.amqp_1_0.type.messaging.*;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
+
+public class Filesender extends Util
+{
+ private static final String USAGE_STRING = "filesender [options] \n\nOptions:";
+
+ protected Filesender(String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+
+ }
+
+ @Override
+ protected void run()
+ {
+ final String queue = getArgs()[0];
+ final String directoryName = getArgs()[1];
+
+ try
+ {
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
+ Connection conn = newConnection();
+
+ Session session = conn.createSession();
+
+ File directory = new File(directoryName);
+ if(directory.isDirectory() && directory.canWrite())
+ {
+
+ File tmpDirectory = new File(directoryName, ".tmp");
+ if(!tmpDirectory.exists())
+ {
+ tmpDirectory.mkdir();
+ }
+
+ String[] unsettledFiles = tmpDirectory.list();
+
+
+
+ Map unsettled = new HashMap();
+ Map unsettledFileNames = new HashMap();
+ for(String fileName : unsettledFiles)
+ {
+ File aFile = new File(tmpDirectory, fileName);
+ if(aFile.canRead() && aFile.canWrite())
+ {
+ Binary deliveryTag = new Binary(md5.digest(fileName.getBytes()));
+ unsettled.put(deliveryTag, null);
+ unsettledFileNames.put(deliveryTag, fileName);
+ }
+ }
+
+
+ Sender s = session.createSender(queue, 10, AcknowledgeMode.EO, getLinkName(), isDurableLink(),
+ unsettled);
+
+ Map remoteUnsettled = s.getRemoteUnsettled();
+
+ for(Map.Entry entry: unsettledFileNames.entrySet())
+ {
+ if(remoteUnsettled == null || !remoteUnsettled.containsKey(entry.getKey()))
+ {
+ (new File(tmpDirectory, entry.getValue())).renameTo(new File(directory, entry.getValue()));
+ }
+ }
+
+ if(remoteUnsettled != null)
+ {
+ for(Map.Entry entry : remoteUnsettled.entrySet())
+ {
+ if(entry.getValue() instanceof Accepted)
+ {
+ final String fileName = unsettledFileNames.get(entry.getKey());
+ if(fileName != null)
+ {
+
+ Message resumed = new Message();
+ resumed.setDeliveryTag(entry.getKey());
+ resumed.setDeliveryState(entry.getValue());
+ resumed.setResume(Boolean.TRUE);
+ resumed.setSettled(Boolean.TRUE);
+
+
+
+ final File unsettledFile = new File(tmpDirectory, fileName);
+ unsettledFile.delete();
+
+ s.send(resumed);
+
+ }
+
+ }
+ else if(entry.getValue() instanceof Received || entry.getValue() == null)
+ {
+ final File unsettledFile = new File(tmpDirectory, unsettledFileNames.get(entry.getKey()));
+ Message resumed = createMessageFromFile(md5, unsettledFileNames.get(entry.getKey()), unsettledFile);
+ resumed.setResume(Boolean.TRUE);
+ Sender.OutcomeAction action = new Sender.OutcomeAction()
+ {
+ public void onOutcome(Binary deliveryTag, Outcome outcome)
+ {
+ if(outcome instanceof Accepted)
+ {
+ unsettledFile.delete();
+ }
+ }
+ };
+ s.send(resumed, action);
+
+ }
+ }
+ }
+
+
+
+ String[] files = directory.list();
+
+ for(String fileName : files)
+ {
+ final File file = new File(directory, fileName);
+
+ if(file.canRead() && file.canWrite() && !file.isDirectory())
+ {
+ Message message = createMessageFromFile(md5, fileName, file);
+
+ final File unsettledFile = new File(tmpDirectory, fileName);
+
+ Sender.OutcomeAction action = new Sender.OutcomeAction()
+ {
+ public void onOutcome(Binary deliveryTag, Outcome outcome)
+ {
+ if(outcome instanceof Accepted)
+ {
+ unsettledFile.delete();
+ }
+ }
+ };
+
+ file.renameTo(unsettledFile);
+
+ s.send(message, action);
+ }
+ }
+
+ s.close();
+ }
+ else
+ {
+ System.err.println("No such directory: " + directory);
+ }
+ session.close();
+ conn.close();
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace();
+ }
+ catch (Sender.SenderCreationException e)
+ {
+ e.printStackTrace();
+ } catch (FileNotFoundException e)
+ {
+ e.printStackTrace();
+ } catch (IOException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ } catch (NoSuchAlgorithmException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ } catch (Sender.SenderClosingException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+
+ }
+
+ private Message createMessageFromFile(MessageDigest md5, String fileName, File file) throws IOException
+ {
+ FileInputStream fis = new FileInputStream(file);
+ byte[] data = new byte[(int) file.length()];
+
+ int read = fis.read(data);
+
+ fis.close();
+
+ Section applicationProperties = new ApplicationProperties(Collections.singletonMap("filename", fileName));
+ Section amqpValue = new Data(new Binary(data));
+ Message message = new Message(Arrays.asList(applicationProperties, amqpValue));
+ Binary deliveryTag = new Binary(md5.digest(fileName.getBytes()));
+ message.setDeliveryTag(deliveryTag);
+ md5.reset();
+ return message;
+ }
+
+ public static void main(String[] args)
+ {
+ new Filesender(args).run();
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Message.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Message.java
new file mode 100644
index 0000000000..7c1172898b
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Message.java
@@ -0,0 +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.client;
+
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.qpid.amqp_1_0.type.DeliveryState;
+import org.apache.qpid.amqp_1_0.type.Section;
+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.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public class Message
+{
+ private Binary _deliveryTag;
+ private List _payload = new ArrayList();
+ private Boolean _resume;
+ private boolean _settled;
+ private DeliveryState _deliveryState;
+ private Receiver _receiver;
+
+
+ public Message()
+ {
+ }
+
+ public Message(Collection sections)
+ {
+ _payload.addAll(sections);
+ }
+
+ public Message(Section section)
+ {
+ this(Collections.singletonList(section));
+ }
+
+ public Message(String message)
+ {
+ this(new AmqpValue(message));
+ }
+
+
+ public Binary getDeliveryTag()
+ {
+ return _deliveryTag;
+ }
+
+ public void setDeliveryTag(Binary deliveryTag)
+ {
+ _deliveryTag = deliveryTag;
+ }
+
+ public List getPayload()
+ {
+ return Collections.unmodifiableList(_payload);
+ }
+
+ private T getSection(Class clazz)
+ {
+ for(Section s : _payload)
+ {
+ if(clazz.isAssignableFrom(s.getClass()))
+ {
+ return (T) s;
+ }
+ }
+ return null;
+ }
+
+ public ApplicationProperties getApplicationProperties()
+ {
+ return getSection(ApplicationProperties.class);
+ }
+
+ public Properties getProperties()
+ {
+ return getSection(Properties.class);
+ }
+
+ public Header getHeader()
+ {
+ return getSection(Header.class);
+ }
+
+
+ public void setResume(final Boolean resume)
+ {
+ _resume = resume;
+ }
+
+ public boolean isResume()
+ {
+ return Boolean.TRUE.equals(_resume);
+ }
+
+ public void setDeliveryState(DeliveryState state)
+ {
+ _deliveryState = state;
+ }
+
+ public DeliveryState getDeliveryState()
+ {
+ return _deliveryState;
+ }
+
+ public void setSettled(boolean settled)
+ {
+ _settled = settled;
+ }
+
+ public boolean getSettled()
+ {
+ return _settled;
+ }
+
+ public void setReceiver(final Receiver receiver)
+ {
+ _receiver = receiver;
+ }
+
+ public Receiver getReceiver()
+ {
+ return _receiver;
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ReadBytes.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ReadBytes.java
new file mode 100644
index 0000000000..07ae54b54f
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/ReadBytes.java
@@ -0,0 +1,77 @@
+/*
+ * 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.client;
+
+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.codec.AMQPDescribedTypeRegistry;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+public class ReadBytes
+{
+
+ public static void main(String[] args) throws IOException, AmqpErrorException
+ {
+
+ if(args.length == 0)
+ {
+ readBytes(System.in);
+ }
+ else
+ {
+ for(String fileName : args)
+ {
+ System.out.println("=========================== " + fileName + " ===========================");
+ final FileInputStream fis = new FileInputStream(fileName);
+ readBytes(fis);
+ fis.close();
+ }
+ }
+
+ }
+
+ private static void readBytes(final InputStream inputStream) throws IOException, AmqpErrorException
+ {
+ byte[] bytes = new byte[4096];
+
+ ValueHandler valueHandler = new ValueHandler(AMQPDescribedTypeRegistry.newInstance());
+
+ int count;
+
+ while((count = inputStream.read(bytes))!=-1)
+ {
+ ByteBuffer buf = ByteBuffer.wrap(bytes);
+ buf.limit(count);
+ while(buf.hasRemaining())
+ {
+
+ final Object value = valueHandler.parse(buf);
+ System.out.print((value == null ? "" : value.getClass().getName() + ":") +value +"\n");
+
+ }
+ }
+
+ }
+
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java
new file mode 100644
index 0000000000..0da9dc3fb7
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receive.java
@@ -0,0 +1,246 @@
+/*
+ *
+ * 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.client;
+
+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.UnsignedLong;
+import org.apache.commons.cli.*;
+import org.apache.qpid.amqp_1_0.type.messaging.ExactSubjectFilter;
+import org.apache.qpid.amqp_1_0.type.messaging.Filter;
+import org.apache.qpid.amqp_1_0.type.messaging.MatchingSubjectFilter;
+
+import java.util.Collections;
+
+public class Receive extends Util
+{
+ private static final String USAGE_STRING = "receive [options] \n\nOptions:";
+ private static final UnsignedLong UNSIGNED_LONG_ONE = UnsignedLong.valueOf(1L);
+ private UnsignedLong _lastCorrelationId;
+
+ public static void main(String[] args)
+ {
+ new Receive(args).run();
+ }
+
+
+ public Receive(final String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasWindowSizeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasFilterOption()
+ {
+ return true;
+ }
+
+ protected void run()
+ {
+
+ try
+ {
+ final String queue = getArgs()[0];
+
+ String message = "";
+
+ Connection conn = newConnection();
+
+
+ Session session = conn.createSession();
+
+ Filter filter = null;
+ if(getFilter() != null)
+ {
+ String[] filterParts = getFilter().split("=",2);
+ if("exact-subject".equals(filterParts[0]))
+ {
+ filter = new ExactSubjectFilter(filterParts[1]);
+ }
+ else if("matching-subject".equals(filterParts[0]))
+ {
+ filter = new MatchingSubjectFilter(filterParts[1]);
+ }
+ else
+ {
+ System.err.println("Unknown filter type: " + filterParts[0]);
+ }
+ }
+
+ Receiver r =
+ filter == null
+ ? session.createReceiver(queue, getMode(), getLinkName(), isDurableLink())
+ : session.createReceiver(queue, getMode(), getLinkName(), isDurableLink(), Collections.singletonMap(Symbol.valueOf("filter"), filter), null);
+ Transaction txn = null;
+
+ int credit = 0;
+ int receivedCount = 0;
+
+ if(!useStdIn())
+ {
+ if(getArgs().length <= 2)
+ {
+
+ Transaction txn2 = null;
+ if(useTran())
+ {
+ txn = session.createSessionLocalTransaction();
+ txn2 = session.createSessionLocalTransaction();
+ }
+
+ for(int i = 0; i < getCount(); i++)
+ {
+
+ if(credit == 0)
+ {
+ if(getCount() - i <= getWindowSize())
+ {
+ credit = getCount() - i;
+
+ }
+ else
+ {
+ credit = getWindowSize();
+
+ }
+
+ {
+ r.setCredit(UnsignedInteger.valueOf(credit), false);
+ }
+ if(!isBlock())
+ r.drain();
+ }
+
+ Message m = isBlock() ? r.receive() : r.receive(1000L);
+ credit--;
+ if(m==null)
+ {
+ break;
+ }
+
+
+
+ r.acknowledge(m.getDeliveryTag(),txn);
+
+ receivedCount++;
+
+ System.out.println("Received Message : " + m.getPayload());
+ }
+
+ if(useTran())
+ {
+ txn.commit();
+ }
+ }
+ else
+ {
+ // TODO
+ }
+ }
+ else
+ {
+ // TODO
+ }
+ r.close();
+ session.close();
+ conn.close();
+ System.out.println("Total Messages Received: " + receivedCount);
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (AmqpErrorException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+
+ }
+
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+ }
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java
new file mode 100644
index 0000000000..ad390fd498
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Receiver.java
@@ -0,0 +1,561 @@
+/*
+ *
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.messaging.SectionDecoder;
+import org.apache.qpid.amqp_1_0.transport.DeliveryStateHandler;
+import org.apache.qpid.amqp_1_0.transport.LinkEndpoint;
+import org.apache.qpid.amqp_1_0.transport.ReceivingLinkEndpoint;
+import org.apache.qpid.amqp_1_0.transport.ReceivingLinkListener;
+
+import org.apache.qpid.amqp_1_0.type.*;
+import org.apache.qpid.amqp_1_0.type.DeliveryState;
+import org.apache.qpid.amqp_1_0.type.messaging.*;
+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.transaction.TransactionalState;
+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.*;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+public class Receiver implements DeliveryStateHandler
+{
+ private ReceivingLinkEndpoint _endpoint;
+ private int _id;
+ private static final UnsignedInteger DEFAULT_INITIAL_CREDIT = UnsignedInteger.valueOf(100);
+ private Session _session;
+
+ private Queue _prefetchQueue = new ConcurrentLinkedQueue();
+ private Map _unsettledMap = new HashMap();
+ private MessageArrivalListener _messageArrivalListener;
+ private org.apache.qpid.amqp_1_0.type.transport.Error _error;
+
+ public Receiver(final Session session,
+ final String linkName,
+ final Target target,
+ final Source source,
+ final AcknowledgeMode ackMode) throws AmqpErrorException
+ {
+ this(session, linkName, target, source, ackMode, false);
+ }
+
+ public Receiver(final Session session,
+ final String linkName,
+ final Target target,
+ final Source source,
+ final AcknowledgeMode ackMode,
+ boolean isDurable) throws AmqpErrorException
+ {
+ this(session,linkName,target,source,ackMode,isDurable,null);
+ }
+
+ public Receiver(final Session session,
+ final String linkName,
+ final Target target,
+ final Source source,
+ final AcknowledgeMode ackMode,
+ final boolean isDurable,
+ final Map unsettled) throws AmqpErrorException
+ {
+
+ _session = session;
+ if(isDurable)
+ {
+ source.setDurable(TerminusDurability.UNSETTLED_STATE);
+ source.setExpiryPolicy(TerminusExpiryPolicy.NEVER);
+ }
+ else if(source != null)
+ {
+ source.setDurable(TerminusDurability.NONE);
+ source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
+ }
+ _endpoint = session.getEndpoint().createReceivingLinkEndpoint(linkName, target, source,
+ UnsignedInteger.ZERO);
+
+ _endpoint.setDeliveryStateHandler(this);
+
+ switch(ackMode)
+ {
+ case ALO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.UNSETTLED);
+ _endpoint.setReceivingSettlementMode(ReceiverSettleMode.FIRST);
+ break;
+ case AMO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.SETTLED);
+ _endpoint.setReceivingSettlementMode(ReceiverSettleMode.FIRST);
+ break;
+ case EO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.UNSETTLED);
+ _endpoint.setReceivingSettlementMode(ReceiverSettleMode.SECOND);
+ break;
+
+ }
+
+ _endpoint.setLinkEventListener(new ReceivingLinkListener.DefaultLinkEventListener()
+ {
+ @Override public void messageTransfer(final Transfer xfr)
+ {
+ _prefetchQueue.add(xfr);
+ postPrefetchAction();
+ }
+
+ @Override
+ public void remoteDetached(final LinkEndpoint endpoint, final Detach detach)
+ {
+ _error = detach.getError();
+ super.remoteDetached(endpoint, detach);
+ }
+ });
+
+ _endpoint.setLocalUnsettled(unsettled);
+ _endpoint.attach();
+
+ synchronized(_endpoint.getLock())
+ {
+ while(!_endpoint.isAttached() && !_endpoint.isDetached())
+ {
+ try
+ {
+ _endpoint.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+
+ if(_endpoint.getSource() == null)
+ {
+ synchronized(_endpoint.getLock())
+ {
+ while(!_endpoint.isDetached())
+ {
+ try
+ {
+ _endpoint.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+ throw new AmqpErrorException(getError());
+ }
+ else
+ {
+
+ }
+ }
+
+ private void postPrefetchAction()
+ {
+ if(_messageArrivalListener != null)
+ {
+ _messageArrivalListener.messageArrived(this);
+ }
+ }
+
+ public void setCredit(UnsignedInteger credit, boolean window)
+ {
+ _endpoint.setLinkCredit(credit);
+ _endpoint.setCreditWindow(window);
+
+ }
+
+
+ public String getAddress()
+ {
+ return ((Source)_endpoint.getSource()).getAddress();
+ }
+
+ public Map getFilter()
+ {
+ return ((Source)_endpoint.getSource()).getFilter();
+ }
+
+ public Message receive()
+ {
+ return receive(-1L);
+ }
+
+ public Message receive(boolean wait)
+ {
+ return receive(wait ? -1L : 0L);
+ }
+
+ // 0 means no wait, -1 wait forever
+ public Message receive(long wait)
+ {
+ Message m = null;
+ Transfer xfr;
+ long endTime = wait > 0L ? System.currentTimeMillis() + wait : 0L;
+
+ while((xfr = receiveFromPrefetch(wait)) != null )
+ {
+
+ if(!Boolean.TRUE.equals(xfr.getAborted()))
+ {
+ Binary deliveryTag = xfr.getDeliveryTag();
+ Boolean resume = xfr.getResume();
+
+ List sections = new ArrayList();
+ List payloads = new ArrayList();
+ int totalSize = 0;
+
+ boolean hasMore;
+ do
+ {
+ hasMore = Boolean.TRUE.equals(xfr.getMore());
+
+ ByteBuffer buf = xfr.getPayload();
+
+ if(buf != null)
+ {
+
+ totalSize += buf.remaining();
+
+ payloads.add(buf);
+ }
+ if(hasMore)
+ {
+ xfr = receiveFromPrefetch(0L);
+ if(xfr== null)
+ {
+ // TODO - this is wrong!!!!
+ System.out.println("eeek");
+ }
+ }
+ }
+ while(hasMore && !Boolean.TRUE.equals(xfr.getAborted()));
+
+ if(!Boolean.TRUE.equals(xfr.getAborted()))
+ {
+ ByteBuffer allPayload = ByteBuffer.allocate(totalSize);
+ for(ByteBuffer payload : payloads)
+ {
+ allPayload.put(payload);
+ }
+ allPayload.flip();
+ SectionDecoder decoder = _session.getSectionDecoder();
+
+ try
+ {
+ sections = decoder.parseAll(allPayload);
+ }
+ catch (AmqpErrorException e)
+ {
+ // todo - throw a sensible error
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ m = new Message(sections);
+ m.setDeliveryTag(deliveryTag);
+ m.setResume(resume);
+ m.setReceiver(this);
+ break;
+ }
+ }
+
+ if(wait > 0L)
+ {
+ wait = endTime - System.currentTimeMillis();
+ if(wait <=0L)
+ {
+ break;
+ }
+ }
+ }
+
+
+ return m;
+
+ }
+
+ private Transfer receiveFromPrefetch(long wait)
+ {
+ long endTime = ((wait >0L) ? (System.currentTimeMillis() + wait) : 0L);
+ final Object lock = _endpoint.getLock();
+ synchronized(lock)
+ {
+ Transfer xfr;
+ while(((xfr = _prefetchQueue.peek()) == null) && !_endpoint.isDrained() && !_endpoint.isDetached()
+ && wait != 0)
+ {
+ try
+ {
+ if(wait>0L)
+ {
+ lock.wait(wait);
+ }
+ else if(wait<0L)
+ {
+ lock.wait();
+ }
+ }
+ catch (InterruptedException e)
+ {
+ return null;
+ }
+ if(wait > 0L)
+ {
+ wait = endTime - System.currentTimeMillis();
+ if(wait <= 0L)
+ {
+ break;
+ }
+ }
+
+ }
+ if(xfr != null)
+ {
+ _prefetchQueue.poll();
+
+ }
+
+ return xfr;
+ }
+
+ }
+
+
+ public void release(final Message m)
+ {
+ release(m.getDeliveryTag());
+ }
+
+ public void release(Binary deliveryTag)
+ {
+ update(new Released(), deliveryTag, null, null);
+ }
+
+
+ public void modified(Binary tag)
+ {
+ final Modified outcome = new Modified();
+ outcome.setDeliveryFailed(true);
+
+ update(outcome, tag, null, null);
+ }
+
+ public void acknowledge(final Message m)
+ {
+ acknowledge(m.getDeliveryTag());
+ }
+
+ public void acknowledge(final Message m, SettledAction a)
+ {
+ acknowledge(m.getDeliveryTag(), a);
+ }
+
+
+ public void acknowledge(final Message m, Transaction txn)
+ {
+ acknowledge(m.getDeliveryTag(), txn);
+ }
+
+
+ public void acknowledge(final Binary deliveryTag)
+ {
+ acknowledge(deliveryTag, null, null);
+ }
+
+
+ public void acknowledge(final Binary deliveryTag, SettledAction a)
+ {
+ acknowledge(deliveryTag, null, a);
+ }
+
+ public void acknowledge(final Binary deliveryTag, final Transaction txn)
+ {
+ acknowledge(deliveryTag, txn, null);
+ }
+
+ public void acknowledge(final Binary deliveryTag, final Transaction txn, SettledAction action)
+ {
+ update(new Accepted(), deliveryTag, txn, action);
+ }
+
+ public void update(Outcome outcome, final Binary deliveryTag, final Transaction txn, SettledAction action)
+ {
+
+ DeliveryState state;
+ if(txn != null)
+ {
+ TransactionalState txnState = new TransactionalState();
+ txnState.setOutcome(outcome);
+ txnState.setTxnId(txn.getTxnId());
+ state = txnState;
+ }
+ else
+ {
+ state = (DeliveryState) outcome;
+ }
+ boolean settled = txn == null && !ReceiverSettleMode.SECOND.equals(_endpoint.getReceivingSettlementMode());
+
+ if(!(settled || action == null))
+ {
+ _unsettledMap.put(deliveryTag, action);
+ }
+
+ _endpoint.updateDisposition(deliveryTag,state, settled);
+ }
+
+ public Error getError()
+ {
+ return _error;
+ }
+
+ public void acknowledgeAll(Message m)
+ {
+ acknowledgeAll(m.getDeliveryTag());
+ }
+
+ public void acknowledgeAll(Binary deliveryTag)
+ {
+ acknowledgeAll(deliveryTag, null, null);
+ }
+
+ public void acknowledgeAll(Binary deliveryTag, final Transaction txn, SettledAction action)
+ {
+ updateAll(new Accepted(), deliveryTag, txn, action);
+ }
+
+ public void updateAll(Outcome outcome, Binary deliveryTag)
+ {
+ updateAll(outcome, deliveryTag, null, null);
+ }
+
+ public void updateAll(Outcome outcome, Binary deliveryTag, final Transaction txn, SettledAction action)
+ {
+ DeliveryState state;
+
+ if(txn != null)
+ {
+ TransactionalState txnState = new TransactionalState();
+ txnState.setOutcome(outcome);
+ txnState.setTxnId(txn.getTxnId());
+ state = txnState;
+ }
+ else
+ {
+ state = (DeliveryState) outcome;
+ }
+ boolean settled = txn == null && !ReceiverSettleMode.SECOND.equals(_endpoint.getReceivingSettlementMode());
+
+ if(!(settled || action == null))
+ {
+ _unsettledMap.put(deliveryTag, action);
+ }
+ _endpoint.updateAllDisposition(deliveryTag, state, settled);
+ }
+
+
+
+ public void close()
+ {
+ _endpoint.setTarget(null);
+ _endpoint.close();
+ Message msg;
+ while((msg = receive(-1l)) != null)
+ {
+ release(msg);
+ }
+
+ }
+
+
+ public void detach()
+ {
+ _endpoint.setTarget(null);
+ _endpoint.detach();
+ Message msg;
+ while((msg = receive(-1l)) != null)
+ {
+ release(msg);
+ }
+
+ }
+
+ public void drain()
+ {
+ _endpoint.drain();
+ }
+
+ public void setCreditWithTransaction(final UnsignedInteger credit, final Transaction txn)
+ {
+ _endpoint.setLinkCredit(credit);
+ _endpoint.setTransactionId(txn == null ? null : txn.getTxnId());
+ _endpoint.setCreditWindow(false);
+
+ }
+
+ public void handle(final Binary deliveryTag, final DeliveryState state, final Boolean settled)
+ {
+ if(Boolean.TRUE.equals(settled))
+ {
+ SettledAction action = _unsettledMap.remove(deliveryTag);
+ if(action != null)
+ {
+ action.onSettled(deliveryTag);
+ }
+ }
+ }
+
+ public Map getRemoteUnsettled()
+ {
+ return _endpoint.getInitialUnsettledMap();
+ }
+
+
+ public void setMessageArrivalListener(final MessageArrivalListener messageArrivalListener)
+ {
+ synchronized(_endpoint.getLock())
+ {
+ _messageArrivalListener = messageArrivalListener;
+ }
+ }
+
+ public Session getSession()
+ {
+ return _session;
+ }
+
+ public org.apache.qpid.amqp_1_0.type.Source getSource()
+ {
+ return _endpoint.getSource();
+ }
+
+ public static interface SettledAction
+ {
+ public void onSettled(Binary deliveryTag);
+ }
+
+
+ public interface MessageArrivalListener
+ {
+ void messageArrived(Receiver receiver);
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java
new file mode 100644
index 0000000000..6e1d15376c
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Request.java
@@ -0,0 +1,249 @@
+/*
+ *
+ * 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.client;
+
+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.Header;
+import org.apache.qpid.amqp_1_0.type.messaging.Properties;
+import org.apache.commons.cli.*;
+
+import java.util.Arrays;
+
+public class Request extends Util
+{
+ private static final String USAGE_STRING = "request [options] [ ...]\n\nOptions:";
+
+ public static void main(String[] args)
+ {
+ new Request(args).run();
+ }
+
+ public Request(String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasWindowSizeOption()
+ {
+ return true;
+ }
+
+ public void run()
+ {
+
+ try
+ {
+
+
+ final String queue = getArgs()[0];
+
+ String message = "";
+
+ Connection conn = newConnection();
+ Session session = conn.createSession();
+
+ Connection conn2;
+ Session session2;
+ Receiver responseReceiver;
+
+ if(isUseMultipleConnections())
+ {
+ conn2 = newConnection();
+ session2 = conn2.createSession();
+ responseReceiver = session2.createTemporaryQueueReceiver();
+ }
+ else
+ {
+ conn2 = null;
+ session2 = null;
+ responseReceiver = session.createTemporaryQueueReceiver();
+ }
+
+
+
+
+ responseReceiver.setCredit(UnsignedInteger.valueOf(getWindowSize()), true);
+
+
+
+ Sender s = session.createSender(queue, getWindowSize(), getMode());
+
+ Transaction txn = null;
+
+ if(useTran())
+ {
+ txn = session.createSessionLocalTransaction();
+ }
+
+ int received = 0;
+
+ if(getArgs().length >= 2)
+ {
+ message = getArgs()[1];
+ if(message.length() < getMessageSize())
+ {
+ StringBuilder builder = new StringBuilder(getMessageSize());
+ builder.append(message);
+ for(int x = message.length(); x < getMessageSize(); x++)
+ {
+ builder.append('.');
+ }
+ message = builder.toString();
+ }
+
+ for(int i = 0; i < getCount(); i++)
+ {
+ Properties properties = new Properties();
+ properties.setMessageId(UnsignedLong.valueOf(i));
+ properties.setReplyTo(responseReceiver.getAddress());
+
+ AmqpValue amqpValue = new AmqpValue(message);
+ Section[] sections = { new Header() , properties, amqpValue};
+ final Message message1 = new Message(Arrays.asList(sections));
+
+ s.send(message1, txn);
+
+ Message responseMessage = responseReceiver.receive(false);
+ if(responseMessage != null)
+ {
+ responseReceiver.acknowledge(responseMessage.getDeliveryTag(),txn);
+ received++;
+ }
+ }
+ }
+
+ if(txn != null)
+ {
+ txn.commit();
+ }
+
+
+ while(received < getCount())
+ {
+ Message responseMessage = responseReceiver.receive();
+ responseReceiver.acknowledge(responseMessage.getDeliveryTag());
+ received++;
+ }
+
+
+
+
+ s.close();
+ session.close();
+ conn.close();
+
+ if(session2 != null)
+ {
+ session2.close();
+ conn2.close();
+ }
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Sender.SenderClosingException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Sender.SenderCreationException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (AmqpErrorException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+
+ }
+
+ protected boolean hasSingleLinkPerConnectionMode()
+ {
+ return true;
+ }
+
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+ }
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java
new file mode 100644
index 0000000000..8d9de4893f
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Respond.java
@@ -0,0 +1,347 @@
+/*
+ *
+ * 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.client;
+
+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.Properties;
+import org.apache.commons.cli.*;
+
+import java.util.*;
+
+public class Respond extends Util
+{
+ private static final String USAGE_STRING = "respond [options] \n\nOptions:";
+ private Connection _conn;
+ private Session _session;
+ private Receiver _receiver;
+ private Transaction _txn;
+ private Map _senders;
+ private UnsignedLong _responseMsgId = UnsignedLong.ZERO;
+ private Connection _conn2;
+ private Session _session2;
+
+ public Respond(final String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasSingleLinkPerConnectionMode()
+ {
+ return true;
+ }
+
+
+ @Override
+ protected boolean hasWindowSizeOption()
+ {
+ return true;
+ }
+
+ public static void main(String[] args)
+ {
+ new Respond(args).run();
+ }
+
+ public void run()
+ {
+ try
+ {
+
+ _senders = new HashMap();
+
+ final String queue = getArgs()[0];
+
+ String message = "";
+
+ _conn = newConnection();
+
+
+
+ if(isUseMultipleConnections())
+ {
+ _conn2 = newConnection();
+ _session2 = _conn2.createSession();
+ }
+
+
+ _session = _conn.createSession();
+
+
+ _receiver = _session.createReceiver(queue, getMode());
+ _txn = null;
+
+ int credit = 0;
+ int receivedCount = 0;
+ _responseMsgId = UnsignedLong.ZERO;
+
+ Random random = null;
+ int batch = 0;
+ List txnMessages = null;
+ if(useTran())
+ {
+ if(getRollbackRatio() != 0)
+ {
+ random = new Random();
+ }
+ batch = getBatchSize();
+ _txn = _session.createSessionLocalTransaction();
+ txnMessages = new ArrayList(batch);
+ }
+
+
+ for(int i = 0; receivedCount < getCount(); i++)
+ {
+
+ if(credit == 0)
+ {
+ if(getCount() - i <= getWindowSize())
+ {
+ credit = getCount() - i;
+
+ }
+ else
+ {
+ credit = getWindowSize();
+
+ }
+
+ _receiver.setCredit(UnsignedInteger.valueOf(credit), false);
+
+ if(!isBlock())
+ _receiver.drain();
+ }
+
+ Message m = isBlock() ? (receivedCount == 0 ? _receiver.receive() : _receiver.receive(10000L)) : _receiver.receive(1000L);
+ credit--;
+ if(m==null)
+ {
+ if(useTran() && batch != getBatchSize())
+ {
+ _txn.commit();
+ }
+ break;
+ }
+
+ System.out.println("Received Message: " + m.getPayload());
+
+ respond(m);
+
+
+
+ if(useTran())
+ {
+
+ txnMessages.add(m);
+
+ if(--batch == 0)
+ {
+
+ if(getRollbackRatio() == 0 || random.nextDouble() >= getRollbackRatio())
+ {
+ _txn.commit();
+ txnMessages.clear();
+ receivedCount += getBatchSize();
+ }
+ else
+ {
+ System.out.println("Random Rollback");
+ _txn.rollback();
+ double result;
+ do
+ {
+ _txn = _session.createSessionLocalTransaction();
+
+ for(Message msg : txnMessages)
+ {
+ respond(msg);
+ }
+
+ result = random.nextDouble();
+ if(result sections = m.getPayload();
+ String replyTo = null;
+ Object correlationId = null;
+ for(Section section : sections)
+ {
+ if(section instanceof Properties)
+ {
+ replyTo = getResponseQueue() == null ? ((Properties)section).getReplyTo() : getResponseQueue();
+ correlationId = ((Properties) section).getMessageId();
+ break;
+ }
+ }
+
+ if(replyTo != null)
+ {
+ Sender s = _senders.get(replyTo);
+ if(s == null)
+ {
+ s = (isUseMultipleConnections() ? _session2 : _session).createSender(replyTo,getWindowSize());
+ _senders.put(replyTo, s);
+ }
+
+ List replySections = new ArrayList(sections);
+
+ ListIterator sectionIterator = replySections.listIterator();
+
+ while(sectionIterator.hasNext())
+ {
+ Section section = sectionIterator.next();
+ if(section instanceof Properties)
+ {
+ Properties newProps = new Properties();
+ newProps.setTo(replyTo);
+ newProps.setCorrelationId(correlationId);
+ newProps.setMessageId(_responseMsgId);
+ _responseMsgId = _responseMsgId.add(UnsignedLong.ONE);
+ sectionIterator.set(newProps);
+ }
+ }
+
+ Message replyMessage = new Message(replySections);
+ System.out.println("Sent Message: " + replySections);
+ s.send(replyMessage, _txn);
+
+ }
+ _receiver.acknowledge(m.getDeliveryTag(), _txn);
+ }
+
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+ }
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java
new file mode 100644
index 0000000000..6f6575e083
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Send.java
@@ -0,0 +1,244 @@
+/*
+ *
+ * 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.client;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.util.Arrays;
+
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.qpid.amqp_1_0.type.Section;
+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.Data;
+import org.apache.qpid.amqp_1_0.type.messaging.Properties;
+import org.apache.commons.cli.*;
+
+public class Send extends Util
+{
+ private static final String USAGE_STRING = "send [options] [ ...]\n\nOptions:";
+ 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
+ {
+ new Send(args).run();
+ }
+
+
+ public Send(final String[] args)
+ {
+ super(args);
+ }
+
+ @Override
+ protected boolean hasLinkDurableOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasLinkNameOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasResponseQueueOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasSizeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasBlockOption()
+ {
+ return false;
+ }
+
+ @Override
+ protected boolean hasStdInOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasTxnOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasModeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasCountOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasWindowSizeOption()
+ {
+ return true;
+ }
+
+ @Override
+ protected boolean hasSubjectOption()
+ {
+ return true;
+ }
+
+ public void run()
+ {
+
+ final String queue = getArgs()[0];
+
+ String message = "";
+
+ try
+ {
+ Connection conn = newConnection();
+
+ Session session = conn.createSession();
+
+
+ Sender s = session.createSender(queue, getWindowSize(), getMode(), getLinkName());
+
+ Transaction txn = null;
+
+ if(useTran())
+ {
+ txn = session.createSessionLocalTransaction();
+ }
+
+ if(!useStdIn())
+ {
+ if(getArgs().length <= 2)
+ {
+ if(getArgs().length == 2)
+ {
+ message = getArgs()[1];
+ }
+ for(int i = 0; i < getCount(); i++)
+ {
+
+ Properties properties = new Properties();
+ properties.setMessageId(UnsignedLong.valueOf(i));
+ if(getSubject() != null)
+ {
+ properties.setSubject(getSubject());
+ }
+ Section bodySection;
+ byte[] bytes = (message + " " + i).getBytes();
+ if(bytes.length < getMessageSize())
+ {
+ byte[] origBytes = bytes;
+ bytes = new byte[getMessageSize()];
+ System.arraycopy(origBytes,0,bytes,0,origBytes.length);
+ for(int x = origBytes.length; x < bytes.length; x++)
+ {
+ bytes[x] = (byte) '.';
+ }
+ bodySection = new Data(new Binary(bytes));
+ }
+ else
+ {
+ bodySection = new AmqpValue(message + " " + i);
+ }
+
+ Section[] sections = {properties, bodySection};
+ final Message message1 = new Message(Arrays.asList(sections));
+
+ s.send(message1, txn);
+ }
+ }
+ else
+ {
+ for(int i = 1; i < getArgs().length; i++)
+ {
+ s.send(new Message(getArgs()[i]), txn);
+ }
+
+ }
+ }
+ else
+ {
+ LineNumberReader buf = new LineNumberReader(new InputStreamReader(System.in));
+
+
+ try
+ {
+ while((message = buf.readLine()) != null)
+ {
+ s.send(new Message(message), txn);
+ }
+ }
+ catch (IOException e)
+ {
+ // TODO
+ e.printStackTrace();
+ }
+ }
+
+ if(txn != null)
+ {
+ txn.commit();
+ }
+
+ s.close();
+
+ session.close();
+ conn.close();
+ }
+ catch (Sender.SenderClosingException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Connection.ConnectionException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+ catch (Sender.SenderCreationException e)
+ {
+ e.printStackTrace(); //TODO.
+ }
+
+
+ }
+
+ protected void printUsage(Options options)
+ {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(USAGE_STRING, options );
+ }
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/SendBytes.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/SendBytes.java
new file mode 100644
index 0000000000..6f97ecd810
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/SendBytes.java
@@ -0,0 +1,331 @@
+/*
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.codec.FrameWriter;
+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.type.Binary;
+import org.apache.qpid.amqp_1_0.type.FrameBody;
+import org.apache.qpid.amqp_1_0.type.Section;
+import org.apache.qpid.amqp_1_0.type.Symbol;
+import org.apache.qpid.amqp_1_0.type.UnsignedByte;
+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.UnsignedShort;
+import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry;
+import org.apache.qpid.amqp_1_0.type.messaging.Footer;
+import org.apache.qpid.amqp_1_0.type.messaging.Header;
+import org.apache.qpid.amqp_1_0.type.messaging.Properties;
+import org.apache.qpid.amqp_1_0.type.transport.Flow;
+
+import org.apache.qpid.amqp_1_0.type.transport.Transfer;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+public class SendBytes
+{
+
+ public static void main(String[] args) throws
+ Sender.SenderCreationException,
+ Sender.SenderClosingException,
+ Connection.ConnectionException,
+ IOException, ParseException
+ {
+ Transfer xfr = new Transfer();
+ Flow fs = new Flow();
+ fs.setIncomingWindow(UnsignedInteger.valueOf(1024));
+ fs.setDeliveryCount(UnsignedInteger.valueOf(2));
+ fs.setLinkCredit(UnsignedInteger.valueOf(18));
+ fs.setAvailable(UnsignedInteger.valueOf(0));
+ fs.setDrain(false);
+
+ xfr.setHandle(UnsignedInteger.valueOf(0));
+ xfr.setDeliveryTag(new Binary("\"queue\"<-6ec024a7-d98e-4196-9348-15f6026c32ca:0".getBytes()));
+ //xfr.setDeliveryTag(new Binary(new byte[] {0}));
+ xfr.setDeliveryId(UnsignedInteger.valueOf(0));
+ xfr.setSettled(true);
+
+
+ Header h = new Header();
+ Properties p = new Properties();
+ p.setTo("queue");
+ //p.setMessageId(new Binary(UUID.randomUUID().toString().getBytes()));
+
+ Footer f = new Footer(Collections.EMPTY_MAP);
+
+ Section[] sections = new Section[] { h,p,f};
+ //Section[] sections = new Section[] { b };
+ //Section[] sections = { h,p, b};
+/*
+ Fragment[] fragments = new Fragment[5];
+
+ final AMQPDescribedTypeRegistry typeRegistry = AMQPDescribedTypeRegistry.newInstance().registerTransportLayer().registerMessagingLayer();
+
+ SectionEncoderImpl encoder = new SectionEncoderImpl(typeRegistry);
+
+ int num = 0;
+ int i = 0;
+ for(Section s : sections)
+ {
+ Fragment frag = new Fragment();
+
+ frag.setPayload(s.encode(encoder));
+ frag.setFirst(true);
+ frag.setLast(true);
+ frag.setSectionCode(s.getSectionCode());
+ frag.setSectionNumber(UnsignedInteger.valueOf(num++));
+ frag.setSectionOffset(UnsignedLong.valueOf(0L));
+ fragments[i++] =frag;
+ }
+
+ xfr.setFragments(fragments);
+*/
+
+ encodeTypes("xfr",xfr);
+
+ final byte[] result;
+ final Object input = xfr;
+/*
+ result = encode(1024, input);
+
+ boolean ok = true;
+
+ for(int j = 10; ok && j < 400; j++)
+ {
+
+ byte[] result2 = encode(j,input);
+
+ for(int i = 0; i <400; i++)
+ {
+ if(result[i] != result2[i])
+ {
+ System.out.println("result differs at " + i + " Splitting at " + j+ " [" + result[i] + " - " + result2[i] + "]");
+ //break;
+ //ok = false;
+
+ }
+ }
+ }*/
+ //System.out.println(Arrays.equals(result, result2));
+
+ //doEncodes();
+ /*OutputStream out = System.out;
+ if(args.length > 0)
+ {
+ out = new FileOutputStream(args[0]);
+ }
+
+ Transfer xfr = new Transfer();
+ fs.setSessionCredit(UnsignedInteger.valueOf(1024));
+ fs.setTransferCount(UnsignedInteger.valueOf(2));
+ fs.setLinkCredit(UnsignedInteger.valueOf(18));
+ fs.setAvailable(UnsignedInteger.valueOf(0));
+ fs.setDrain(false);
+
+ xfr.setHandle(UnsignedInteger.valueOf(0));
+ //xfr.setDeliveryTag(new Binary("\"queue\"<-6ec024a7-d98e-4196-9348-15f6026c32ca:0".getBytes()));
+ xfr.setDeliveryTag(new Binary(new byte[] {0}));
+ xfr.setTransferId(UnsignedInteger.valueOf(0));
+ xfr.setSettled(true);
+ xfr.setFlowState(fs);
+
+ Header h = new Header();
+ h.setTransmitTime(new Date(System.currentTimeMillis()));
+ Properties p = new Properties();
+ p.setTo(new Address("queue"));
+ //p.setMessageId(new Binary(UUID.randomUUID().toString().getBytes()));
+ AmqpMapSection m = new AmqpMapSection();
+ DataSection b = new DataSection("Hello World!".getBytes());
+
+ Footer f = new Footer();
+
+ Section[] sections = new Section[] { h,p,m,b,f};
+ //Section[] sections = new Section[] { b };
+ //Section[] sections = { h,p, b};
+ List fragments = new ArrayList(5);
+
+ final AMQPDescribedTypeRegistry typeRegistry = AMQPDescribedTypeRegistry.newInstance();
+
+ SectionEncoderImpl encoder = new SectionEncoderImpl(typeRegistry);
+
+ for(Section s : sections)
+ {
+ Fragment frag = new Fragment();
+
+ frag.setPayload(s.encode(encoder));
+ frag.setFirst(true);
+ frag.setLast(true);
+ frag.setFormatCode(s.getSectionCode());
+ frag.setFragmentOffset(null);
+ fragments.add(frag);
+ }
+
+ xfr.setFragments(fragments);
+
+
+ Object[] objectsToWrite = new Object[] { xfr };
+ ByteBuffer buf = ByteBuffer.allocate(4096);
+
+
+ for(Object obj : objectsToWrite)
+ {
+ ValueWriter writer = typeRegistry.getValueWriter(obj);
+
+ int count;
+
+
+ do
+ {
+ count = writer.writeToBuffer(buf);
+ out.write(buf.array(), buf.arrayOffset(), count);
+ buf.clear();
+ } while (!writer.isComplete());
+
+ }
+
+ out.flush();
+ out.close();*/
+
+ }
+
+ public static void doEncodes() throws IOException, ParseException
+ {
+ encodeTypes("boolean", Boolean.TRUE, Boolean.FALSE);
+ encodeTypes("ubyte", UnsignedByte.valueOf((byte)0), UnsignedByte.valueOf((byte)1 ),UnsignedByte.valueOf((byte)3), UnsignedByte.valueOf((byte)42), UnsignedByte.valueOf("255"));
+ encodeTypes("byte", Byte.valueOf((byte)0), Byte.valueOf( (byte)1), Byte.valueOf((byte) 3), Byte.valueOf((byte) 42), Byte.valueOf((byte) 127), Byte.valueOf((byte) -1), Byte.valueOf((byte) -3), Byte.valueOf((byte) -42), Byte.valueOf( (byte)-128));
+ encodeTypes("ushort", UnsignedShort.valueOf((short)0), UnsignedShort.valueOf((short)1), UnsignedShort.valueOf((short)3), UnsignedShort.valueOf((short)42), UnsignedShort.valueOf("65535"));
+ encodeTypes("short", Short.valueOf((short)0), Short.valueOf((short)1), Short.valueOf((short)3), Short.valueOf((short)42), Short.valueOf((short)32767), Short.valueOf((short)-1), Short.valueOf((short)-3), Short.valueOf((short)-42), Short.valueOf((short)-32768));
+ encodeTypes("uint",UnsignedInteger.valueOf(0), UnsignedInteger.valueOf(1), UnsignedInteger.valueOf(3), UnsignedInteger.valueOf(42), UnsignedInteger.valueOf("4294967295"));
+ encodeTypes("int", 0, 1, 3, 42, 2147483647, -1, -3, -42, -2147483648);
+ encodeTypes("ulong", UnsignedLong.valueOf(0), UnsignedLong.valueOf(1), UnsignedLong.valueOf(3), UnsignedLong.valueOf(42), UnsignedLong.valueOf("18446744073709551615"));
+ encodeTypes("long", 0l, 1l, 3l, 42l, 9223372036854775807l, -1l, -3l, -42l, -9223372036854775808l);
+ encodeTypes("float", 3.14159);
+ encodeTypes("double", Double.valueOf(3.14159265359));
+ encodeTypes("char", '?');
+
+ SimpleDateFormat df = new SimpleDateFormat("HHa z MMM d yyyy");
+
+ encodeTypes("timestamp", df.parse("9AM PST Dec 6 2010"), df.parse("9AM PST Dec 6 1910"));
+ encodeTypes("uuid", UUID.fromString("f275ea5e-0c57-4ad7-b11a-b20c563d3b71"));
+ encodeTypes("binary", new Binary( new byte[] {(byte)0xDE, (byte)0xAD, (byte)0xBE, (byte)0xEF}), new Binary(new byte[] { (byte)0xCA,(byte)0xFE, (byte)0xBA, (byte)0xBE}));
+ encodeTypes("string", "The quick brown fox jumped over the lazy cow.");
+ encodeTypes("symbol", Symbol.valueOf("connectathon"));
+ encodeTypes("list", Arrays.asList(new Object[] {Long.valueOf(1), "two", Double.valueOf(3.14159265359), null, Boolean.FALSE}));
+ Map map = new HashMap();
+ map.put("one", Long.valueOf(1));
+ map.put("two", Long.valueOf(2));
+ map.put("pi", Double.valueOf(3.14159265359));
+ map.put("list:", Arrays.asList(new Object[] {Long.valueOf(1), "two", Double.valueOf(3.14159265359), null, Boolean.FALSE}));
+ map.put(null, Boolean.TRUE);
+ encodeTypes("map", map);
+ encodeTypes("null", null);
+
+ }
+
+ static void encodeTypes(String name, Object... vals ) throws IOException
+ {
+ FileOutputStream out = new FileOutputStream("/home/rob/"+name+".out");
+ ByteBuffer buf = ByteBuffer.allocate(4096);
+ final AMQPDescribedTypeRegistry typeRegistry = AMQPDescribedTypeRegistry.newInstance();
+
+ if(vals != null)
+ {
+ for(Object obj : vals)
+ {
+ ValueWriter writer = typeRegistry.getValueWriter(obj);
+
+ int count;
+
+
+ do
+ {
+ count = writer.writeToBuffer(buf);
+ out.write(buf.array(), buf.arrayOffset(), count);
+ buf.clear();
+ } while (!writer.isComplete());
+
+ }
+ }
+ else
+ {
+ ValueWriter writer = typeRegistry.getValueWriter(null);
+
+ int count;
+
+
+ do
+ {
+ count = writer.writeToBuffer(buf);
+ out.write(buf.array(), buf.arrayOffset(), count);
+ buf.clear();
+ } while (!writer.isComplete());
+
+ }
+ out.flush();
+ out.close();
+
+ }
+
+ static byte[] encode(int size, Object... vals)
+ {
+ byte[] result = new byte[10000];
+ int pos = 0;
+
+ final AMQPDescribedTypeRegistry typeRegistry = AMQPDescribedTypeRegistry.newInstance();
+ AMQFrame frame = AMQFrame.createAMQFrame((short) 0, (FrameBody) vals[0]);
+ FrameWriter writer = new FrameWriter(typeRegistry);
+ /*for(Object obj : vals)
+ {
+ final AMQPDescribedTypeRegistry typeRegistry = AMQPDescribedTypeRegistry.newInstance();
+ ValueWriter writer = typeRegistry.getValueWriter(obj);
+*/
+ int count;
+
+ ByteBuffer buf = ByteBuffer.wrap(result, pos, size);
+
+ do
+ {
+
+ writer.writeToBuffer(buf);
+ pos = buf.position();
+ buf = ByteBuffer.wrap(result, pos, size);
+ if(!writer.isComplete())
+ {
+ count = 3;
+ }
+
+ } while (!writer.isComplete());
+/*
+
+ }
+*/
+
+ return result;
+
+ }
+
+
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java
new file mode 100644
index 0000000000..c20eec6c8e
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Sender.java
@@ -0,0 +1,392 @@
+/*
+ *
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.messaging.SectionEncoder;
+import org.apache.qpid.amqp_1_0.transport.DeliveryStateHandler;
+import org.apache.qpid.amqp_1_0.transport.SendingLinkEndpoint;
+import org.apache.qpid.amqp_1_0.type.*;
+import org.apache.qpid.amqp_1_0.type.Source;
+import org.apache.qpid.amqp_1_0.type.Target;
+import org.apache.qpid.amqp_1_0.type.messaging.*;
+import org.apache.qpid.amqp_1_0.type.transaction.TransactionalState;
+import org.apache.qpid.amqp_1_0.type.transport.*;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Sender implements DeliveryStateHandler
+{
+ private SendingLinkEndpoint _endpoint;
+ private int _id;
+ private Session _session;
+ private int _windowSize;
+ private Map _outcomeActions = Collections.synchronizedMap(new HashMap());
+ private boolean _closed;
+
+ public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr)
+ throws SenderCreationException
+ {
+ this(session, linkName, targetAddr, sourceAddr, false);
+ }
+
+ public Sender(final Session session, final String linkName, final String targetAddr, final String sourceAddr,
+ boolean synchronous)
+ throws SenderCreationException
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ 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
+ {
+ this(session, linkName, createTarget(targetAddr, isDurable), createSource(sourceAddr), window, mode, unsettled);
+ }
+
+ private static org.apache.qpid.amqp_1_0.type.messaging.Source createSource(final String sourceAddr)
+ {
+ org.apache.qpid.amqp_1_0.type.messaging.Source source = new org.apache.qpid.amqp_1_0.type.messaging.Source();
+ source.setAddress(sourceAddr);
+ return source;
+ }
+
+ private static org.apache.qpid.amqp_1_0.type.messaging.Target createTarget(final String targetAddr, final boolean isDurable)
+ {
+ org.apache.qpid.amqp_1_0.type.messaging.Target target = new org.apache.qpid.amqp_1_0.type.messaging.Target();
+ target.setAddress(targetAddr);
+ if(isDurable)
+ {
+ target.setDurable(TerminusDurability.UNSETTLED_STATE);
+ target.setExpiryPolicy(TerminusExpiryPolicy.NEVER);
+ }
+ return target;
+ }
+
+ 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
+ {
+
+ _session = session;
+ _endpoint = session.getEndpoint().createSendingLinkEndpoint(linkName,
+ source, target, unsettled);
+
+
+ switch(mode)
+ {
+ case ALO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.UNSETTLED);
+ _endpoint.setReceivingSettlementMode(ReceiverSettleMode.FIRST);
+ break;
+ case AMO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.SETTLED);
+ break;
+ case EO:
+ _endpoint.setSendingSettlementMode(SenderSettleMode.UNSETTLED);
+ _endpoint.setReceivingSettlementMode(ReceiverSettleMode.SECOND);
+ break;
+
+ }
+ _endpoint.setDeliveryStateHandler(this);
+ _endpoint.attach();
+ _windowSize = window;
+
+ synchronized(_endpoint.getLock())
+ {
+ while(!(_endpoint.isAttached() || _endpoint.isDetached()))
+ {
+ try
+ {
+ _endpoint.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+ throw new SenderCreationException(e);
+ }
+ }
+ if(_endpoint.getTarget()== null)
+ {
+ throw new SenderCreationException("Peer did not create remote endpoint for link, target: " + target.getAddress());
+ };
+ }
+ }
+
+ public Source getSource()
+ {
+ return _endpoint.getSource();
+ }
+
+ public Target getTarget()
+ {
+ return _endpoint.getTarget();
+ }
+
+ public void send(Message message)
+ {
+ send(message, null, null);
+ }
+
+ public void send(Message message, final OutcomeAction action)
+ {
+ send(message, null, action);
+ }
+
+ public void send(Message message, final Transaction txn)
+ {
+ send(message, txn, null);
+ }
+
+ public void send(Message message, final Transaction txn, OutcomeAction action)
+ {
+
+ List sections = message.getPayload();
+
+ Transfer xfr = new Transfer();
+
+ if(sections != null && !sections.isEmpty())
+ {
+ SectionEncoder encoder = _session.getSectionEncoder();
+ encoder.reset();
+
+ int sectionNumber = 0;
+ for(Section section : sections)
+ {
+ encoder.encodeObject(section);
+ }
+
+
+ Binary encoding = encoder.getEncoding();
+ ByteBuffer payload = encoding.asByteBuffer();
+ xfr.setPayload(payload);
+ }
+ if(message.getDeliveryTag() == null)
+ {
+ message.setDeliveryTag(new Binary(String.valueOf(_id++).getBytes()));
+ }
+ if(message.isResume())
+ {
+ xfr.setResume(Boolean.TRUE);
+ }
+ if(message.getDeliveryState() != null)
+ {
+ xfr.setState(message.getDeliveryState());
+ }
+
+ xfr.setDeliveryTag(message.getDeliveryTag());
+ //xfr.setSettled(_windowSize ==0);
+ if(txn != null)
+ {
+ xfr.setSettled(false);
+ TransactionalState deliveryState = new TransactionalState();
+ deliveryState.setTxnId(txn.getTxnId());
+ xfr.setState(deliveryState);
+ }
+ else
+ {
+ xfr.setSettled(message.getSettled() || _endpoint.getSendingSettlementMode() == SenderSettleMode.SETTLED);
+ }
+ final Object lock = _endpoint.getLock();
+ synchronized(lock)
+ {
+ while(!_endpoint.hasCreditToSend())
+ {
+ try
+ {
+ lock.wait();
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ if(action != null)
+ {
+ _outcomeActions.put(message.getDeliveryTag(), action);
+ }
+ _endpoint.transfer(xfr);
+ //TODO - rationalise sending of flows
+ // _endpoint.sendFlow();
+ }
+
+ if(_windowSize != 0)
+ {
+ synchronized(lock)
+ {
+
+
+ while(_endpoint.getUnsettledCount() >= _windowSize)
+ {
+ try
+ {
+ lock.wait();
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+
+ }
+
+
+ }
+
+ public void close() throws SenderClosingException
+ {
+
+ if(_windowSize != 0)
+ {
+ synchronized(_endpoint.getLock())
+ {
+
+
+ while(_endpoint.getUnsettledCount() > 0)
+ {
+ try
+ {
+ _endpoint.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+ }
+
+ }
+ _session.removeSender(this);
+ _endpoint.setSource(null);
+ _endpoint.detach();
+ _closed = true;
+
+ synchronized(_endpoint.getLock())
+ {
+ while(!_endpoint.isDetached())
+ {
+ try
+ {
+ _endpoint.getLock().wait();
+ }
+ catch (InterruptedException e)
+ {
+ throw new SenderClosingException(e);
+ }
+ }
+ }
+ }
+
+ public boolean isClosed()
+ {
+ return _closed;
+ }
+
+ public void handle(Binary deliveryTag, DeliveryState state, Boolean settled)
+ {
+ if(state instanceof Outcome)
+ {
+ OutcomeAction action;
+ if((action = _outcomeActions.remove(deliveryTag)) != null)
+ {
+ action.onOutcome(deliveryTag, (Outcome) state);
+ }
+ if(!Boolean.TRUE.equals(settled))
+ {
+ _endpoint.updateDisposition(deliveryTag, state, true);
+ }
+ }
+ }
+
+ public Map getRemoteUnsettled()
+ {
+ return _endpoint.getInitialUnsettledMap();
+ }
+
+ public Session getSession()
+ {
+ return _session;
+ }
+
+ public class SenderCreationException extends Exception
+ {
+ public SenderCreationException(Throwable e)
+ {
+ super(e);
+ }
+
+ public SenderCreationException(String e)
+ {
+ super(e);
+
+ }
+ }
+
+ public class SenderClosingException extends Exception
+ {
+ public SenderClosingException(Throwable e)
+ {
+ super(e);
+ }
+ }
+
+ public static interface OutcomeAction
+ {
+ public void onOutcome(Binary deliveryTag, Outcome outcome);
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java
new file mode 100644
index 0000000000..5e1e1b1d7c
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Session.java
@@ -0,0 +1,354 @@
+/*
+ *
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.messaging.SectionDecoder;
+import org.apache.qpid.amqp_1_0.messaging.SectionDecoderImpl;
+import org.apache.qpid.amqp_1_0.messaging.SectionEncoder;
+import org.apache.qpid.amqp_1_0.messaging.SectionEncoderImpl;
+import org.apache.qpid.amqp_1_0.transport.SendingLinkEndpoint;
+import org.apache.qpid.amqp_1_0.transport.SessionEndpoint;
+import org.apache.qpid.amqp_1_0.transport.SessionState;
+import org.apache.qpid.amqp_1_0.type.*;
+import org.apache.qpid.amqp_1_0.type.messaging.Filter;
+import org.apache.qpid.amqp_1_0.type.messaging.Source;
+import org.apache.qpid.amqp_1_0.type.messaging.StdDistMode;
+import org.apache.qpid.amqp_1_0.type.messaging.Target;
+import org.apache.qpid.amqp_1_0.type.transaction.TxnCapability;
+import org.apache.qpid.amqp_1_0.type.transport.ReceiverSettleMode;
+import org.apache.qpid.amqp_1_0.type.transport.SenderSettleMode;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+public class Session
+{
+ private SessionEndpoint _endpoint;
+ private List _receivers = new ArrayList();
+ private List _senders = new ArrayList();
+ private SectionEncoder _sectionEncoder;
+ private SectionDecoder _sectionDecoder;
+ private TransactionController _sessionLocalTC;
+ private Connection _connection;
+
+ public Session(final Connection connection, String name)
+ {
+ _connection = connection;
+ _endpoint = connection.getEndpoint().createSession(name);
+ _sectionEncoder = new SectionEncoderImpl(connection.getEndpoint().getDescribedTypeRegistry());
+ _sectionDecoder = new SectionDecoderImpl(connection.getEndpoint().getDescribedTypeRegistry());
+ }
+
+
+ public synchronized Sender createSender(final String targetName) throws Sender.SenderCreationException
+ {
+ return createSender(targetName, false);
+ }
+
+ public synchronized Sender createSender(final String targetName, boolean synchronous) throws Sender.SenderCreationException
+ {
+
+ final String sourceName = UUID.randomUUID().toString();
+ return new Sender(this, targetName+"<-"+sourceName, targetName, sourceName, synchronous);
+
+ }
+
+ public synchronized Sender createSender(final String targetName, int window) throws Sender.SenderCreationException
+ {
+ 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
+ {
+
+ return createSender(targetName, window, mode, null);
+ }
+
+ public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName) throws Sender.SenderCreationException
+ {
+ return createSender(targetName, window, mode, linkName, null);
+ }
+ public Sender createSender(String targetName, int window, AcknowledgeMode mode, String linkName, Map unsettled) throws Sender.SenderCreationException
+ {
+ 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
+ {
+ return new Sender(this, linkName == null ? "->" + targetName + '(' + UUID.randomUUID().toString()+')': linkName,
+ targetName, null, window, mode, isDurable, unsettled);
+
+ }
+
+
+ public Receiver createReceiver(final String sourceAddr) throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, null, AcknowledgeMode.ALO);
+ }
+
+
+ public Receiver createReceiver(final String queue, final AcknowledgeMode mode) throws AmqpErrorException
+ {
+ return createReceiver(queue, null, mode);
+ }
+
+ public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName)
+ throws AmqpErrorException
+ {
+ return createReceiver(queue, null, mode, linkName);
+ }
+
+ public Receiver createReceiver(final String queue, final AcknowledgeMode mode, String linkName, boolean isDurable)
+ throws AmqpErrorException
+ {
+ 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
+ {
+ 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
+ {
+ return createReceiver(queue, null, mode, linkName, isDurable, unsettled);
+ }
+
+
+ private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode)
+ throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, mode, AcknowledgeMode.ALO);
+ }
+
+ private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode, String linkName)
+ throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, mode, AcknowledgeMode.ALO, linkName);
+ }
+
+
+ private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode,
+ final AcknowledgeMode ackMode) throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, mode, ackMode, null);
+ }
+
+ private synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode,
+ final AcknowledgeMode ackMode, String linkName) throws AmqpErrorException
+ {
+ 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
+ {
+ 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
+ {
+ return createReceiver(sourceAddr,mode,ackMode, linkName, isDurable, null, unsettled);
+ }
+
+ public synchronized Receiver createReceiver(final String sourceAddr, DistributionMode mode,
+ final AcknowledgeMode ackMode, String linkName, boolean isDurable,
+ Map filters, Map unsettled)
+ throws AmqpErrorException
+ {
+
+ final Target target = new Target();
+ final Source source = new Source();
+ source.setAddress(sourceAddr);
+ source.setDistributionMode(mode);
+ source.setFilter(filters);
+
+ if(linkName == null)
+ {
+ linkName = sourceAddr + "-> (" + UUID.randomUUID().toString() + ")";
+ }
+
+ final Receiver receiver =
+ new Receiver(this, linkName,
+ target, source, ackMode, isDurable, unsettled);
+ _receivers.add(receiver);
+
+ return receiver;
+
+ }
+
+ public synchronized Receiver createCopyingReceiver(final String sourceAddr) throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, StdDistMode.COPY);
+ }
+
+ public synchronized Receiver createMovingReceiver(final String sourceAddr) throws AmqpErrorException
+ {
+ return createReceiver(sourceAddr, StdDistMode.MOVE);
+ }
+
+ public Receiver createTemporaryQueueReceiver() throws AmqpErrorException
+ {
+ Source source = new Source();
+ source.setDynamic(true);
+
+ final Receiver receiver = new Receiver(this, "tempSender"+UUID.randomUUID().toString(), new Target(),
+ source, AcknowledgeMode.ALO);
+ _receivers.add(receiver);
+ return receiver;
+ }
+
+ public Sender createTemporaryQueueSender() throws Sender.SenderCreationException
+ {
+ Target target = new Target();
+ target.setDynamic(true);
+
+ final Sender sender;
+ sender = new Sender(this, "tempSender"+ UUID.randomUUID().toString(), target,
+ new Source(), 0, AcknowledgeMode.ALO);
+ _senders.add(sender);
+ return sender;
+ }
+
+
+
+ public SessionEndpoint getEndpoint()
+ {
+ return _endpoint;
+ }
+
+ public synchronized void close()
+ {
+ try
+ {
+ for(Sender sender : new ArrayList(_senders))
+ {
+ sender.close();
+ }
+ for(Receiver receiver : new ArrayList(_receivers))
+ {
+ receiver.detach();
+ }
+ if(_sessionLocalTC != null)
+ {
+ _sessionLocalTC.close();
+ }
+ _endpoint.end();
+ }
+ catch (Sender.SenderClosingException e)
+ {
+// TODO
+ e.printStackTrace();
+ }
+
+ //TODO
+
+ }
+
+ void removeSender(Sender sender)
+ {
+ _senders.remove(sender);
+ }
+
+ void removeReceiver(Receiver receiver)
+ {
+ _receivers.remove(receiver);
+ }
+
+ public SectionEncoder getSectionEncoder()
+ {
+ return _sectionEncoder;
+ }
+
+ public SectionDecoder getSectionDecoder()
+ {
+ return _sectionDecoder;
+ }
+
+
+ public Transaction createSessionLocalTransaction()
+ {
+ TransactionController localController = getSessionLocalTransactionController();
+ return localController.beginTransaction();
+ }
+
+ private TransactionController getSessionLocalTransactionController()
+ {
+ if(_sessionLocalTC == null)
+ {
+ _sessionLocalTC = createSessionLocalTransactionController();
+ }
+ return _sessionLocalTC;
+ }
+
+ private TransactionController createSessionLocalTransactionController()
+ {
+ String name = "txnControllerLink";
+ SendingLinkEndpoint tcLinkEndpoint = _endpoint.createTransactionController(name, TxnCapability.LOCAL_TXN,
+ TxnCapability.MULTI_TXNS_PER_SSN);
+ tcLinkEndpoint.setReceivingSettlementMode(ReceiverSettleMode.FIRST);
+ tcLinkEndpoint.setSendingSettlementMode(SenderSettleMode.UNSETTLED);
+ tcLinkEndpoint.attach();
+ return new TransactionController(this, tcLinkEndpoint);
+ }
+
+
+ public Message receive()
+ {
+ while(getEndpoint().getState() == SessionState.ACTIVE)
+ {
+ synchronized (getEndpoint().getLock())
+ {
+ try
+ {
+ for(Receiver r : _receivers)
+ {
+ Message m = r.receive(false);
+ if(m != null)
+ return m;
+ }
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+ }
+ return null;
+ }
+
+ public Connection getConnection()
+ {
+ return _connection;
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Transaction.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Transaction.java
new file mode 100644
index 0000000000..a379463710
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Transaction.java
@@ -0,0 +1,49 @@
+/*
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.type.Binary;
+
+public class Transaction
+{
+ private TransactionController _transactionController;
+ private Binary _txnId;
+
+ Transaction(final TransactionController transactionController, Binary txnId)
+ {
+ _transactionController = transactionController;
+ _txnId = txnId;
+ }
+
+ public void commit()
+ {
+ _transactionController.commit(this);
+ }
+
+ public void rollback()
+ {
+ _transactionController.rollback(this);
+ }
+
+ public Binary getTxnId()
+ {
+ return _txnId;
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TransactionController.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TransactionController.java
new file mode 100644
index 0000000000..9f2c76bc72
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/TransactionController.java
@@ -0,0 +1,194 @@
+/*
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.messaging.SectionEncoder;
+import org.apache.qpid.amqp_1_0.transport.DeliveryStateHandler;
+import org.apache.qpid.amqp_1_0.transport.SendingLinkEndpoint;
+import org.apache.qpid.amqp_1_0.type.Binary;
+import org.apache.qpid.amqp_1_0.type.DeliveryState;
+import org.apache.qpid.amqp_1_0.type.messaging.AmqpValue;
+import org.apache.qpid.amqp_1_0.type.transaction.Declare;
+import org.apache.qpid.amqp_1_0.type.transaction.Declared;
+import org.apache.qpid.amqp_1_0.type.transaction.Discharge;
+import org.apache.qpid.amqp_1_0.type.transport.Transfer;
+
+
+public class TransactionController implements DeliveryStateHandler
+{
+ private static final Binary DELIVERY_TAG = new Binary(new byte[]{(byte) 0});
+ private SendingLinkEndpoint _endpoint;
+ private Session _session;
+ private volatile DeliveryState _state;
+ private boolean _received;
+
+ public TransactionController(Session session, SendingLinkEndpoint tcLinkEndpoint)
+ {
+ _session = session;
+ _endpoint = tcLinkEndpoint;
+ _endpoint.setDeliveryStateHandler(this);
+ }
+
+ public Transaction beginTransaction()
+ {
+
+
+ Binary txnId = declare();
+ return new Transaction(this, txnId);
+ }
+
+ private Binary declare()
+ {
+ SectionEncoder encoder = _session.getSectionEncoder();
+
+
+ AmqpValue section = new AmqpValue(new Declare());
+
+
+ Transfer transfer = new Transfer();
+ transfer.setPayload(section.encode(encoder).asByteBuffer());
+ transfer.setDeliveryTag(DELIVERY_TAG);
+ transfer.setSettled(Boolean.FALSE);
+ final Object lock = _endpoint.getLock();
+ synchronized(lock)
+ {
+ while(!_endpoint.hasCreditToSend())
+ {
+ try
+ {
+ lock.wait();
+ }
+ catch (InterruptedException e)
+ {
+
+ }
+ }
+ _state = null;
+ _received = false;
+ _endpoint.transfer(transfer);
+
+ //TODO - rationalise sending of flows
+ // _endpoint.sendFlow();
+ }
+ synchronized (this)
+ {
+ while(!_received)
+ {
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+
+ }
+ }
+ }
+
+
+ return ((Declared) _state).getTxnId();
+ }
+
+
+ public void commit(final Transaction transaction)
+ {
+ discharge(transaction.getTxnId(), false);
+ }
+
+ public void rollback(final Transaction transaction)
+ {
+ discharge(transaction.getTxnId(), true);
+ }
+
+ private void discharge(final Binary txnId, final boolean fail)
+ {
+ Discharge discharge = new Discharge();
+ discharge.setTxnId(txnId);
+ discharge.setFail(fail);
+ SectionEncoder encoder = _session.getSectionEncoder();
+
+
+ AmqpValue section = new AmqpValue(discharge);
+
+ Transfer transfer = new Transfer();
+ transfer.setPayload(section.encode(encoder).asByteBuffer());
+ transfer.setDeliveryTag(DELIVERY_TAG);
+ transfer.setSettled(Boolean.FALSE);
+
+ final Object lock = _endpoint.getLock();
+ synchronized(lock)
+ {
+ while(!_endpoint.hasCreditToSend())
+ {
+ try
+ {
+ lock.wait();
+ }
+ catch (InterruptedException e)
+ {
+
+ }
+ }
+ _state = null;
+ _received = false;
+ _endpoint.transfer(transfer);
+
+ //TODO - rationalise sending of flows
+ // _endpoint.sendFlow();
+ }
+ synchronized (this)
+ {
+ while(!_received)
+ {
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+
+ }
+ }
+ }
+
+
+ }
+
+ public void handle(final Binary deliveryTag, final DeliveryState state, final Boolean settled)
+ {
+ synchronized(this)
+ {
+ _state = state;
+ _received = true;
+
+ if(!Boolean.TRUE.equals(settled))
+ {
+ _endpoint.updateDisposition(deliveryTag, state, true);
+ }
+
+ notifyAll();
+ }
+ }
+
+ public void close()
+ {
+ _endpoint.close();
+ }
+}
diff --git a/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java
new file mode 100644
index 0000000000..6fe2a6d510
--- /dev/null
+++ b/java/amqp-1-0-client/src/main/java/org/apache/qpid/amqp_1_0/client/Util.java
@@ -0,0 +1,529 @@
+/*
+ *
+ * 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.client;
+
+import org.apache.qpid.amqp_1_0.transport.Container;
+import org.apache.commons.cli.*;
+
+import java.util.logging.*;
+
+public abstract class Util
+{
+
+ private static final Logger FRAME_LOGGER = Logger.getLogger("FRM");
+ private static final Logger RAW_LOGGER = Logger.getLogger("RAW");
+ private String _host;
+ private String _username;
+ private String _password;
+ private int _port;
+ private int _count;
+ private boolean _useStdIn;
+ private boolean _useTran;
+ private String[] _args;
+ private AcknowledgeMode _mode;
+ private boolean _block;
+ private int _frameSize;
+ private int _messageSize;
+ private String _responseQueue;
+ private int _batchSize;
+ private double _rollbackRatio;
+ private String _linkName;
+ private String _containerName;
+ private boolean _durableLink;
+ private boolean _useMultipleConnections;
+ private int _windowSize = 100;
+ private String _subject;
+ private String _filter;
+ private String _remoteHost;
+ private boolean _useSSL;
+
+ protected Util(String[] args)
+ {
+ CommandLineParser cmdLineParse = new PosixParser();
+
+ Options options = new Options();
+ options.addOption("h","help",false,"show this help message and exit");
+ options.addOption(OptionBuilder.withLongOpt("host")
+ .withDescription( "host to connect to (default 0.0.0.0)" )
+ .hasArg(true)
+ .withArgName("HOST")
+ .create('H'));
+ options.addOption(OptionBuilder.withLongOpt("username")
+ .withDescription( "username to use for authentication" )
+ .hasArg(true)
+ .withArgName("USERNAME")
+ .create('u'));
+ options.addOption(OptionBuilder.withLongOpt("password")
+ .withDescription( "password to use for authentication" )
+ .hasArg(true)
+ .withArgName("PASSWORD")
+ .create('w'));
+ options.addOption(OptionBuilder.withLongOpt("port")
+ .withDescription( "port to connect to (default 5672)" )
+ .hasArg(true)
+ .withArgName("PORT")
+ .create('p'));
+ options.addOption(OptionBuilder.withLongOpt("frame-size")
+ .withDescription( "specify the maximum frame size" )
+ .hasArg(true)
+ .withArgName("FRAME_SIZE")
+ .create('f'));
+ options.addOption(OptionBuilder.withLongOpt("container-name")
+ .withDescription( "Container name" )
+ .hasArg(true)
+ .withArgName("CONTAINER_NAME")
+ .create('C'));
+
+ options.addOption(OptionBuilder.withLongOpt("ssl")
+ .withDescription("Use SSL")
+ .create('S'));
+
+ options.addOption(OptionBuilder.withLongOpt("remote-hostname")
+ .withDescription( "hostname to supply in the open frame" )
+ .hasArg(true)
+ .withArgName("HOST")
+ .create('O'));
+
+ if(hasBlockOption())
+ options.addOption(OptionBuilder.withLongOpt("block")
+ .withDescription("block until messages arrive")
+ .create('b'));
+
+ if(hasCountOption())
+ options.addOption(OptionBuilder.withLongOpt("count")
+ .withDescription( "number of messages to send (default 1)" )
+ .hasArg(true)
+ .withArgName("COUNT")
+ .create('c'));
+ if(hasModeOption())
+ options.addOption(OptionBuilder.withLongOpt("acknowledge-mode")
+ .withDescription( "acknowledgement mode: AMO|ALO|EO (At Least Once, At Most Once, Exactly Once" )
+ .hasArg(true)
+ .withArgName("MODE")
+ .create('k'));
+
+ if(hasSubjectOption())
+ options.addOption(OptionBuilder.withLongOpt("subject")
+ .withDescription( "subject message property" )
+ .hasArg(true)
+ .withArgName("SUBJECT")
+ .create('s'));
+
+
+ if(hasSingleLinkPerConnectionMode())
+ options.addOption(OptionBuilder.withLongOpt("single-link-per-connection")
+ .withDescription("acknowledgement mode: AMO|ALO|EO (At Least Once, At Most Once, Exactly Once")
+ .hasArg(false)
+ .create('Z'));
+
+ if(hasFilterOption())
+ options.addOption(OptionBuilder.withLongOpt("filter")
+ .withDescription("filter, e.g. exact-subject=hello; matching-subject=%.a.#")
+ .hasArg(true)
+ .withArgName("=")
+ .create('F'));
+
+
+ if(hasTxnOption())
+ {
+ options.addOption("x","txn",false,"use transactions");
+ options.addOption(OptionBuilder.withLongOpt("batch-size")
+ .withDescription( "transaction batch size (default: 1)" )
+ .hasArg(true)
+ .withArgName("BATCH-SIZE")
+ .create('B'));
+ options.addOption(OptionBuilder.withLongOpt("rollback-ratio")
+ .withDescription( "rollback ratio - must be between 0 and 1 (default: 0)" )
+ .hasArg(true)
+ .withArgName("RATIO")
+ .create('R'));
+ }
+
+ if(hasLinkDurableOption())
+ {
+ options.addOption("d","durable-link",false,"use a durable link");
+ }
+
+ if(hasStdInOption())
+ options.addOption("i","stdin",false,"read messages from stdin (one message per line)");
+
+ options.addOption(OptionBuilder.withLongOpt("trace")
+ .withDescription("trace logging specified categories: RAW, FRM")
+ .hasArg(true)
+ .withArgName("TRACE")
+ .create('t'));
+ if(hasSizeOption())
+ options.addOption(OptionBuilder.withLongOpt("message-size")
+ .withDescription( "size to pad outgoing messages to" )
+ .hasArg(true)
+ .withArgName("SIZE")
+ .create('z'));
+
+ if(hasResponseQueueOption())
+ options.addOption(OptionBuilder.withLongOpt("response-queue")
+ .withDescription( "response queue to reply to" )
+ .hasArg(true)
+ .withArgName("RESPONSE_QUEUE")
+ .create('r'));
+
+ if(hasLinkNameOption())
+ {
+ options.addOption(OptionBuilder.withLongOpt("link")
+ .withDescription( "link name" )
+ .hasArg(true)
+ .withArgName("LINK")
+ .create('l'));
+ }
+
+ if(hasWindowSizeOption())
+ {
+ options.addOption(OptionBuilder.withLongOpt("window-size")
+ .withDescription("credit window size")
+ .hasArg(true)
+ .withArgName("WINDOW-SIZE")
+ .create('W'));
+ }
+
+ CommandLine cmdLine = null;
+ try
+ {
+ cmdLine = cmdLineParse.parse(options, args);
+
+ }
+ catch (ParseException e)
+ {
+ printUsage(options);
+ System.exit(-1);
+ }
+
+ if(cmdLine.hasOption('h') || cmdLine.getArgList().isEmpty())
+ {
+ printUsage(options);
+ System.exit(0);
+ }
+ _host = cmdLine.getOptionValue('H',"0.0.0.0");
+ _remoteHost = cmdLine.getOptionValue('O',null);
+ String portStr = cmdLine.getOptionValue('p',"5672");
+ String countStr = cmdLine.getOptionValue('c',"1");
+
+ _useSSL = cmdLine.hasOption('S');
+
+ if(hasWindowSizeOption())
+ {
+ String windowSizeStr = cmdLine.getOptionValue('W',"100");
+ _windowSize = Integer.parseInt(windowSizeStr);
+ }
+
+ if(hasSubjectOption())
+ {
+ _subject = cmdLine.getOptionValue('s');
+ }
+
+ if(cmdLine.hasOption('u'))
+ {
+ _username = cmdLine.getOptionValue('u');
+ }
+
+ if(cmdLine.hasOption('w'))
+ {
+ _password = cmdLine.getOptionValue('w');
+ }
+
+ if(cmdLine.hasOption('F'))
+ {
+ _filter = cmdLine.getOptionValue('F');
+ }
+
+ _port = Integer.parseInt(portStr);
+
+ _containerName = cmdLine.getOptionValue('C');
+
+ if(hasBlockOption())
+ _block = cmdLine.hasOption('b');
+
+ if(hasLinkNameOption())
+ _linkName = cmdLine.getOptionValue('l');
+
+
+ if(hasLinkDurableOption())
+ _durableLink = cmdLine.hasOption('d');
+
+ if(hasCountOption())
+ _count = Integer.parseInt(countStr);
+
+ if(hasStdInOption())
+ _useStdIn = cmdLine.hasOption('i');
+
+ if(hasSingleLinkPerConnectionMode())
+ _useMultipleConnections = cmdLine.hasOption('Z');
+
+ if(hasTxnOption())
+ {
+ _useTran = cmdLine.hasOption('x');
+ _batchSize = Integer.parseInt(cmdLine.getOptionValue('B',"1"));
+ _rollbackRatio = Double.parseDouble(cmdLine.getOptionValue('R',"0"));
+ }
+
+ if(hasModeOption())
+ {
+ _mode = AcknowledgeMode.ALO;
+
+ if(cmdLine.hasOption('k'))
+ {
+ _mode = AcknowledgeMode.valueOf(cmdLine.getOptionValue('k'));
+ }
+ }
+
+ if(hasResponseQueueOption())
+ {
+ _responseQueue = cmdLine.getOptionValue('r');
+ }
+
+ _frameSize = Integer.parseInt(cmdLine.getOptionValue('f',"65536"));
+
+ if(hasSizeOption())
+ {
+ _messageSize = Integer.parseInt(cmdLine.getOptionValue('z',"-1"));
+ }
+
+ String categoriesList = cmdLine.getOptionValue('t');
+ String[]categories = categoriesList == null ? new String[0] : categoriesList.split("[, ]");
+ for(String cat : categories)
+ {
+ if(cat.equalsIgnoreCase("FRM"))
+ {
+ FRAME_LOGGER.setLevel(Level.FINE);
+ Formatter formatter = new Formatter()
+ {
+ @Override
+ public String format(final LogRecord record)
+ {
+ return "[" + record.getMillis() + " FRM]\t" + record.getMessage() + "\n";
+ }
+ };
+ for(Handler handler : FRAME_LOGGER.getHandlers())
+ {
+ FRAME_LOGGER.removeHandler(handler);
+ }
+ Handler handler = new ConsoleHandler();
+ handler.setLevel(Level.FINE);
+ handler.setFormatter(formatter);
+ FRAME_LOGGER.addHandler(handler);
+ }
+ else if (cat.equalsIgnoreCase("RAW"))
+ {
+ RAW_LOGGER.setLevel(Level.FINE);
+ Formatter formatter = new Formatter()
+ {
+ @Override
+ public String format(final LogRecord record)
+ {
+ return "[" + record.getMillis() + " RAW]\t" + record.getMessage() + "\n";
+ }
+ };
+ for(Handler handler : RAW_LOGGER.getHandlers())
+ {
+ RAW_LOGGER.removeHandler(handler);
+ }
+ Handler handler = new ConsoleHandler();
+ handler.setLevel(Level.FINE);
+ handler.setFormatter(formatter);
+ RAW_LOGGER.addHandler(handler);
+ }
+ }
+
+
+ _args = cmdLine.getArgs();
+
+ }
+
+ protected boolean hasFilterOption()
+ {
+ return false;
+ }
+
+ protected boolean hasSubjectOption()
+ {
+ return false;
+ }
+
+ protected boolean hasWindowSizeOption()
+ {
+ return false;
+ }
+
+ protected boolean hasSingleLinkPerConnectionMode()
+ {
+ return false;
+ }
+
+ protected abstract boolean hasLinkDurableOption();
+
+ protected abstract boolean hasLinkNameOption();
+
+ protected abstract boolean hasResponseQueueOption();
+
+ protected abstract boolean hasSizeOption();
+
+ protected abstract boolean hasBlockOption();
+
+ protected abstract boolean hasStdInOption();
+
+ protected abstract boolean hasTxnOption();
+
+ protected abstract boolean hasModeOption();
+
+ protected abstract boolean hasCountOption();
+
+ public String getHost()
+ {
+ return _host;
+ }
+
+ public String getUsername()
+ {
+ return _username;
+ }
+
+ public String getPassword()
+ {
+ return _password;
+ }
+
+ public int getPort()
+ {
+ return _port;
+ }
+
+ public int getCount()
+ {
+ return _count;
+ }
+
+ public boolean useStdIn()
+ {
+ return _useStdIn;
+ }
+
+ public boolean useTran()
+ {
+ return _useTran;
+ }
+
+ public AcknowledgeMode getMode()
+ {
+ return _mode;
+ }
+
+ public boolean isBlock()
+ {
+ return _block;
+ }
+
+ public String[] getArgs()
+ {
+ return _args;
+ }
+
+ public int getMessageSize()
+ {
+ return _messageSize;
+ }
+
+ public String getResponseQueue()
+ {
+ return _responseQueue;
+ }
+
+ public int getBatchSize()
+ {
+ return _batchSize;
+ }
+
+ public double getRollbackRatio()
+ {
+ return _rollbackRatio;
+ }
+
+ public String getLinkName()
+ {
+ return _linkName;
+ }
+
+ public boolean isDurableLink()
+ {
+ return _durableLink;
+ }
+
+ public boolean isUseMultipleConnections()
+ {
+ return _useMultipleConnections;
+ }
+
+ public void setUseMultipleConnections(boolean useMultipleConnections)
+ {
+ _useMultipleConnections = useMultipleConnections;
+ }
+
+ public String getSubject()
+ {
+ return _subject;
+ }
+
+ public void setSubject(String subject)
+ {
+ _subject = subject;
+ }
+
+ protected abstract void printUsage(final Options options);
+
+ protected abstract void run();
+
+
+ public Connection newConnection() throws Connection.ConnectionException
+ {
+ Container container = getContainerName() == null ? new Container() : new Container(getContainerName());
+ return getUsername() == null ? new Connection(getHost(), getPort(), null, null, _frameSize, container,
+ _remoteHost == null ? getHost() : _remoteHost, _useSSL)
+ : new Connection(getHost(), getPort(), getUsername(), getPassword(), _frameSize,
+ container, _remoteHost == null ? getHost() : _remoteHost, _useSSL);
+ }
+
+ public String getContainerName()
+ {
+ return _containerName;
+ }
+
+ public int getWindowSize()
+ {
+ return _windowSize;
+ }
+
+ public void setWindowSize(int windowSize)
+ {
+ _windowSize = windowSize;
+ }
+
+ public String getFilter()
+ {
+ return _filter;
+ }
+}
diff --git a/java/amqp-1-0-common/build.xml b/java/amqp-1-0-common/build.xml
new file mode 100644
index 0000000000..20b8b731b0
--- /dev/null
+++ b/java/amqp-1-0-common/build.xml
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java
new file mode 100644
index 0000000000..6977a40902
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractDescribedTypeWriter.java
@@ -0,0 +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;
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java
new file mode 100644
index 0000000000..655b1f2164
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractListWriter.java
@@ -0,0 +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;
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java
new file mode 100644
index 0000000000..0fa29b5210
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/AbstractMapWriter.java
@@ -0,0 +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();
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java
new file mode 100644
index 0000000000..68239ad143
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayTypeConstructor.java
@@ -0,0 +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;
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java
new file mode 100644
index 0000000000..7766a486f0
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ArrayWriter.java
@@ -0,0 +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.
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java
new file mode 100644
index 0000000000..76bdac8ed7
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryString.java
@@ -0,0 +1,68 @@
+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/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java
new file mode 100644
index 0000000000..e83718d88d
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryTypeConstructor.java
@@ -0,0 +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;
+
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java
new file mode 100644
index 0000000000..8ab4569646
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BinaryWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanConstructor.java
new file mode 100644
index 0000000000..5cad87cbd8
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanConstructor.java
@@ -0,0 +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.transport.*;
+import org.apache.qpid.amqp_1_0.type.transport.Error;
+
+import java.nio.ByteBuffer;
+
+public class BooleanConstructor
+{
+ private static final TypeConstructor TRUE_INSTANCE = new TypeConstructor()
+ {
+ public Boolean construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+ return Boolean.TRUE;
+ }
+ };
+
+ private static final TypeConstructor FALSE_INSTANCE = new TypeConstructor()
+ {
+ public Boolean construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+ return Boolean.FALSE;
+ }
+ };
+ private static final TypeConstructor BYTE_INSTANCE = new TypeConstructor()
+ {
+ public Boolean construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+ if(in.hasRemaining())
+ {
+ byte b = in.get();
+ return b != (byte) 0;
+ }
+ else
+ {
+ org.apache.qpid.amqp_1_0.type.transport.Error error = new Error();
+ error.setCondition(ConnectionError.FRAMING_ERROR);
+ error.setDescription("Cannot construct boolean: insufficient input data");
+ throw new AmqpErrorException(error);
+ }
+ }
+
+ };
+
+
+ public static TypeConstructor getTrueInstance()
+ {
+ return TRUE_INSTANCE;
+ }
+
+ public static TypeConstructor getFalseInstance()
+ {
+ return FALSE_INSTANCE;
+ }
+
+ public static TypeConstructor getByteInstance()
+ {
+ return BYTE_INSTANCE;
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java
new file mode 100644
index 0000000000..fb4449fb2c
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/BooleanWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java
new file mode 100644
index 0000000000..662b4085db
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteArrayWriter.java
@@ -0,0 +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);
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java
new file mode 100644
index 0000000000..41bd20c0a2
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteBufferWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java
new file mode 100644
index 0000000000..03db2c568c
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteTypeConstructor.java
@@ -0,0 +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);
+
+ }
+
+ }
+
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java
new file mode 100644
index 0000000000..6155de4d2a
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ByteWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java
new file mode 100644
index 0000000000..6a2ce2d725
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharTypeConstructor.java
@@ -0,0 +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);
+
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java
new file mode 100644
index 0000000000..05f6e28d2f
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CharWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java
new file mode 100644
index 0000000000..5625797f74
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeAssembler.java
@@ -0,0 +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;
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java
new file mode 100644
index 0000000000..fc4fcdf9ee
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundTypeConstructor.java
@@ -0,0 +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();
+
+ }
+
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java
new file mode 100644
index 0000000000..39dce2b448
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/CompoundWriter.java
@@ -0,0 +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;
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DecimalConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DecimalConstructor.java
new file mode 100644
index 0000000000..6c3b64aa25
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DecimalConstructor.java
@@ -0,0 +1,230 @@
+/*
+ * 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 java.math.BigDecimal;
+import java.nio.ByteBuffer;
+
+public abstract class DecimalConstructor implements TypeConstructor
+{
+
+ private static final DecimalConstructor DECIMAL_32 = new DecimalConstructor()
+ {
+
+ public BigDecimal construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+
+
+ int val;
+
+ if(in.remaining()>=4)
+ {
+ val = in.getInt();
+ }
+ else
+ {
+ throw new AmqpErrorException(ConnectionError.FRAMING_ERROR, "Cannot construct decimal32: insufficient input data");
+ }
+
+ return constructFrom32(val);}
+
+ };
+
+
+ private static final DecimalConstructor DECIMAL_64 = new DecimalConstructor()
+ {
+
+ public BigDecimal construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+ long val;
+
+ if(in.remaining()>=8)
+ {
+ val = in.getLong();
+ }
+ else
+ {
+ throw new AmqpErrorException(ConnectionError.FRAMING_ERROR, "Cannot construct decimal64: insufficient input data");
+ }
+
+ return constructFrom64(val);
+
+ }
+
+ };
+
+
+ private static final DecimalConstructor DECIMAL_128 = new DecimalConstructor()
+ {
+
+ public BigDecimal construct(final ByteBuffer in, final ValueHandler handler) throws AmqpErrorException
+ {
+ long high;
+ long low;
+
+ if(in.remaining()>=16)
+ {
+ high = in.getLong();
+ low = in.getLong();
+ }
+ else
+ {
+ throw new AmqpErrorException(ConnectionError.FRAMING_ERROR, "Cannot construct decimal128: insufficient input data");
+ }
+
+ return constructFrom128(high, low);
+
+ }
+
+ };
+
+ private static final BigDecimal TWO_TO_THE_SIXTY_FOUR = new BigDecimal(2).pow(64);
+
+ private static BigDecimal constructFrom128(long high, long low)
+ {
+ int sign = ((high & 0x8000000000000000l) == 0) ? 1 : -1;
+
+ int exponent = 0;
+ long significand = high;
+
+ if((high & 0x6000000000000000l) != 0x6000000000000000l)
+ {
+ exponent = ((int) ((high & 0x7FFE000000000000l) >> 49)) - 6176;
+ significand = high & 0x0001ffffffffffffl;
+ }
+ else if((high & 0x7800000000000000l) != 0x7800000000000000l)
+ {
+ exponent = ((int)((high & 0x1fff800000000000l)>>47)) - 6176;
+ significand = (0x00007fffffffffffl & high) | 0x0004000000000000l;
+ }
+ else
+ {
+ // NaN or infinite
+ return null;
+ }
+
+
+ BigDecimal bigDecimal = new BigDecimal(significand).multiply(TWO_TO_THE_SIXTY_FOUR);
+ if(low >=0)
+ {
+ bigDecimal = bigDecimal.add(new BigDecimal(low));
+ }
+ else
+ {
+ bigDecimal = bigDecimal.add(TWO_TO_THE_SIXTY_FOUR.add(new BigDecimal(low)));
+ }
+ if(((high & 0x8000000000000000l) != 0))
+ {
+ bigDecimal = bigDecimal.negate();
+ }
+ bigDecimal = bigDecimal.scaleByPowerOfTen(exponent);
+ return bigDecimal;
+ }
+
+
+ private static BigDecimal constructFrom64(final long val)
+ {
+ int sign = ((val & 0x8000000000000000l) == 0) ? 1 : -1;
+
+ int exponent = 0;
+ long significand = val;
+
+ if((val & 0x6000000000000000l) != 0x6000000000000000l)
+ {
+ exponent = ((int) ((val & 0x7FE0000000000000l) >> 53)) - 398;
+ significand = val & 0x001fffffffffffffl;
+ }
+ else if((val & 0x7800000000000000l) != 0x7800000000000000l)
+ {
+ exponent = ((int)((val & 0x1ff8000000000000l)>>51)) - 398;
+ significand = (0x0007ffffffffffffl & val) | 0x0020000000000000l;
+ }
+ else
+ {
+ // NaN or infinite
+ return null;
+ }
+
+ BigDecimal bigDecimal = new BigDecimal(sign * significand);
+ bigDecimal = bigDecimal.scaleByPowerOfTen(exponent);
+ return bigDecimal;
+ }
+
+ private static BigDecimal constructFrom32(final int val)
+ {
+ int sign = ((val & 0x80000000) == 0) ? 1 : -1;
+
+ int exponent = 0;
+ int significand = val;
+
+ if((val & 0x60000000) != 0x60000000)
+ {
+ exponent = ((int) ((val & 0x7F800000) >> 23)) - 101;
+ significand = val & 0x007fffffff;
+ }
+ else if((val & 0x78000000) != 0x78000000)
+ {
+ exponent = ((int)((val & 0x1fe00000)>>21)) - 101;
+ significand = (0x001fffff & val) | 0x00800000;
+ }
+ else
+ {
+ // NaN or infinite
+ return null;
+ }
+
+ BigDecimal bigDecimal = new BigDecimal(sign * significand);
+ bigDecimal = bigDecimal.scaleByPowerOfTen(exponent);
+ return bigDecimal;
+ }
+
+/*
+
+ public static void main(String[] args)
+ {
+ System.out.println(constructFrom128(0l,0l));
+ System.out.println(constructFrom128(0x3041ED09BEAD87C0l,0x378D8E63FFFFFFFFl));
+ System.out.println(constructFrom64(0l));
+ System.out.println(constructFrom64(0x5fe0000000000001l));
+ System.out.println(constructFrom64(0xec7386F26FC0FFFFl));
+ System.out.println(constructFrom32(0));
+ System.out.println(constructFrom32(0x6cb8967f));
+
+ }
+*/
+
+ public static TypeConstructor getDecimal32Instance()
+ {
+ return DECIMAL_32;
+ }
+
+ public static TypeConstructor getDecimal64Instance()
+ {
+ return DECIMAL_64;
+ }
+
+ public static TypeConstructor getDecimal128Instance()
+ {
+ return DECIMAL_128;
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java
new file mode 100644
index 0000000000..48b2045298
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DefaultDescribedTypeConstructor.java
@@ -0,0 +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;
+ }
+ }
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java
new file mode 100644
index 0000000000..b11530d94f
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DelegatingValueWriter.java
@@ -0,0 +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();
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java
new file mode 100644
index 0000000000..2f171c49b2
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedType.java
@@ -0,0 +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 +
+ '}';
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java
new file mode 100644
index 0000000000..4093583441
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructor.java
@@ -0,0 +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);
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java
new file mode 100644
index 0000000000..38cfa0f5a7
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DescribedTypeConstructorRegistry.java
@@ -0,0 +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);
+
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java
new file mode 100644
index 0000000000..439ad73875
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleTypeConstructor.java
@@ -0,0 +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);
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java
new file mode 100644
index 0000000000..372e739a51
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/DoubleWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/Encoder.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/Encoder.java
new file mode 100644
index 0000000000..9b2a654f10
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/Encoder.java
@@ -0,0 +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;
+
+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.UnsignedByte;
+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.UnsignedShort;
+
+import java.nio.ByteBuffer;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+public interface Encoder
+{
+
+ public boolean writeNull();
+
+ public boolean writeBoolean(boolean b);
+
+ public boolean writeByte(byte b);
+
+ public boolean writeUnsignedByte(ByteBuffer buf, UnsignedByte ub);
+
+ public boolean writeShort(ByteBuffer buf, short s);
+
+ public boolean writeUnsignedShort(UnsignedShort s);
+
+ public boolean writeInt(int i);
+
+ public boolean writeUnsignedInt(UnsignedInteger i);
+
+ public boolean writeLong(long l);
+
+ public boolean writeUnsignedLong(UnsignedLong l);
+
+ public boolean writeFloat(float f);
+
+ public boolean writeDouble(double d);
+
+ public boolean writeChar(char c);
+
+ public boolean writeTimestamp(Date d);
+
+ public boolean writeSymbol(Symbol s);
+
+ public boolean writeString(String s);
+
+ public boolean writeBytes(byte[] bytes);
+
+ public boolean writeBytes(Binary bin);
+
+ public boolean writeList(List l);
+
+ public boolean writeMap(Map m);
+
+ public boolean writeEncodable(EncodableValue o);
+
+ public boolean writeDescribedType(EncodableValue descriptor, EncodableValue described);
+
+ interface EncodableValue
+ {
+ void encode(Encoder encoder);
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java
new file mode 100644
index 0000000000..c9cc0b72c3
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedEightWriter.java
@@ -0,0 +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;
+ }
+
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java
new file mode 100644
index 0000000000..164a869299
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedFourWriter.java
@@ -0,0 +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;
+ }
+
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedOneWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedOneWriter.java
new file mode 100644
index 0000000000..805b0743a4
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedOneWriter.java
@@ -0,0 +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.codec;
+
+import java.nio.ByteBuffer;
+
+
+public abstract class FixedOneWriter implements ValueWriter
+{
+ protected int _written = 2;
+ protected byte _value;
+
+ public int writeToBuffer(ByteBuffer buffer)
+ {
+
+ switch(_written)
+ {
+ case 0:
+ if(buffer.hasRemaining())
+ {
+ buffer.put(getFormatCode());
+ }
+ else
+ {
+ break;
+ }
+ case 1:
+ if(buffer.hasRemaining())
+ {
+ buffer.put(_value);
+ _written = 2;
+ }
+ else
+ {
+ _written = 1;
+ }
+
+ }
+
+ return 2;
+ }
+
+ protected abstract byte getFormatCode();
+
+ public boolean isComplete()
+ {
+ return _written == 2;
+ }
+
+ public boolean isCacheable()
+ {
+ return true;
+ }
+
+ public void setValue(final T value)
+ {
+ _written = 0;
+ _value = convertToByte(value);
+ }
+
+ protected abstract byte convertToByte(final T value);
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java
new file mode 100644
index 0000000000..20334595db
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedSixteenWriter.java
@@ -0,0 +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;
+ }
+
+
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java
new file mode 100644
index 0000000000..f6da0490a6
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FixedTwoWriter.java
@@ -0,0 +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();
+
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java
new file mode 100644
index 0000000000..200fead74e
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatTypeConstructor.java
@@ -0,0 +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);
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java
new file mode 100644
index 0000000000..823e33c3f8
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FloatWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java
new file mode 100644
index 0000000000..dbf9306366
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/FrameWriter.java
@@ -0,0 +1,245 @@
+/*
+ *
+ * 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:
+
+ _typeWriter.setValue(_frame.getFrameBody());
+
+ int payloadLength = _payload == null ? 0 : _payload.remaining();
+
+ _size = _typeWriter.writeToBuffer(remaining > 8
+ ? (ByteBuffer)buffer.duplicate().position(buffer.position()+8)
+ : ByteBuffer.wrap(EMPTY_BYTE_ARRAY)) + 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();
+ _typeWriter = _registry.getValueWriter(frameBody);
+ _payload = frame.getPayload() == null ? null : frame.getPayload().duplicate();
+ }
+}
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java
new file mode 100644
index 0000000000..b3e774de5c
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntTypeConstructor.java
@@ -0,0 +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);
+
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java
new file mode 100644
index 0000000000..91c3151494
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/IntegerWriter.java
@@ -0,0 +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);
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java
new file mode 100644
index 0000000000..3e0164705f
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java
@@ -0,0 +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;
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java
new file mode 100644
index 0000000000..b9a4509a04
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongTypeConstructor.java
@@ -0,0 +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);
+
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java
new file mode 100644
index 0000000000..984775cc74
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/LongWriter.java
@@ -0,0 +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);
+ }
+
+}
\ No newline at end of file
diff --git a/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java
new file mode 100644
index 0000000000..b239d4a397
--- /dev/null
+++ b/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/MapWriter.java
@@ -0,0 +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