diff options
author | Robert Greig <rgreig@apache.org> | 2006-12-22 20:32:43 +0000 |
---|---|---|
committer | Robert Greig <rgreig@apache.org> | 2006-12-22 20:32:43 +0000 |
commit | 56e155dc210a1e46796b18f179a00a56ae52a0e7 (patch) | |
tree | 9add891a6b632f7d1eeefeb2019079efce7739df /java/common/src | |
parent | a71fc76d0f605d1faa76f8aec7b4498c168aec46 (diff) | |
download | qpid-python-56e155dc210a1e46796b18f179a00a56ae52a0e7.tar.gz |
QPID-229 : Patch supplied by Rob Godfrey - Change implementation of FieldTable
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@489748 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src')
11 files changed, 1525 insertions, 1716 deletions
diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQType.java b/java/common/src/main/java/org/apache/qpid/framing/AMQType.java new file mode 100644 index 0000000000..4bce1ca5f0 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQType.java @@ -0,0 +1,688 @@ +/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.qpid.framing;
+
+import org.apache.mina.common.ByteBuffer;
+
+public enum AMQType
+{
+
+
+ //AMQP FieldTable Wire Types
+
+ DECIMAL('D')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+ },
+
+ UNSIGNED_SHORT('S')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ },
+
+ UNSIGNED_INT('I')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+
+ public Object toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+ },
+
+ UNSIGNED_LONG('L')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+
+ public Long toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+ },
+
+ EXTTENDED('D')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+
+ public Object toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+ },
+
+ TIMESTAMP('T')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+
+ public Object toNativeValue(Object value)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ // TODO : fixme
+ throw new UnsupportedOperationException();
+ }
+ },
+
+ BINARY('x')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return 1 + (value == null ? 0 : ((byte[]) value).length);
+ }
+
+
+ public Object toNativeValue(Object value)
+ {
+ if((value instanceof byte[]) || (value == null))
+ {
+ return value;
+ }
+ else
+ {
+ throw new IllegalArgumentException("Value: " + value + " (" + value.getClass().getName() +
+ ") cannot be converted to byte[]");
+ }
+ }
+
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeBytes(buffer, (byte[]) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readBytes(buffer);
+ }
+
+ },
+
+ ASCII_STRING('c')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedLongStringLength((String) value);
+ }
+
+
+ public String toNativeValue(Object value)
+ {
+ if (value != null)
+ {
+ return value.toString();
+ }
+ else
+ {
+ throw new NullPointerException("Cannot convert: null to String.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeLongStringBytes(buffer, (String) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readLongString(buffer);
+ }
+
+ },
+
+ WIDE_STRING('C')
+ {
+ public int getEncodingSize(Object value)
+ {
+ // FIXME: use proper charset encoder
+ return EncodingUtils.encodedLongStringLength((String) value);
+ }
+
+
+ public String toNativeValue(Object value)
+ {
+ if (value != null)
+ {
+ return value.toString();
+ }
+ else
+ {
+ throw new NullPointerException("Cannot convert: null to String.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeLongStringBytes(buffer, (String) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readLongString(buffer);
+ }
+ },
+
+ NULL_STRING('n')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ return 0;
+ }
+
+
+ public String toNativeValue(Object value)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to null String.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return null;
+ }
+ },
+
+ BOOLEAN('t')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedBooleanLength();
+ }
+
+
+ public Object toNativeValue(Object value)
+ {
+ if (value instanceof Boolean)
+ {
+ return (Boolean) value;
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Boolean.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to boolean.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeBoolean(buffer, (Boolean) value);
+ }
+
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readBoolean(buffer);
+ }
+ },
+
+ BYTE('b')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedByteLength();
+ }
+
+
+ public Byte toNativeValue(Object value)
+ {
+ if (value instanceof Byte)
+ {
+ return (Byte) value;
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Byte.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to byte.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeByte(buffer, (Byte) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readByte(buffer);
+ }
+ },
+
+ ASCII_CHARACTER('k')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedCharLength();
+ }
+
+
+ public Character toNativeValue(Object value)
+ {
+ if (value instanceof Character)
+ {
+ return (Character) value;
+ }
+ else if (value == null)
+ {
+ throw new NullPointerException("Cannot convert null into char");
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to char.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeChar(buffer, (Character) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readChar(buffer);
+ }
+
+ },
+
+ SHORT('s')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedShortLength();
+ }
+
+
+ public Short toNativeValue(Object value)
+ {
+ if (value instanceof Short)
+ {
+ return (Short) value;
+ }
+ else if (value instanceof Byte)
+ {
+ return ((Byte) value).shortValue();
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Short.valueOf((String)value);
+ }
+
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to short.");
+ }
+
+
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeShort(buffer, (Short) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readShort(buffer);
+ }
+ },
+
+ INT('i')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedIntegerLength();
+ }
+
+ public Integer toNativeValue(Object value)
+ {
+ if (value instanceof Integer)
+ {
+ return (Integer) value;
+ }
+ else if (value instanceof Short)
+ {
+ return ((Short) value).intValue();
+ }
+ else if (value instanceof Byte)
+ {
+ return ((Byte) value).intValue();
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Integer.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to int.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeInteger(buffer, (Integer) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readInteger(buffer);
+ }
+ },
+
+ LONG('l')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedLongLength();
+ }
+
+ public Object toNativeValue(Object value)
+ {
+ if(value instanceof Long)
+ {
+ return (Long) value;
+ }
+ else if (value instanceof Integer)
+ {
+ return ((Integer) value).longValue();
+ }
+ else if (value instanceof Short)
+ {
+ return ((Short) value).longValue();
+ }
+ else if (value instanceof Byte)
+ {
+ return ((Byte) value).longValue();
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Long.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to long.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeLong(buffer, (Long) value);
+ }
+
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readLong(buffer);
+ }
+ },
+
+ FLOAT('f')
+ {
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedFloatLength();
+ }
+
+
+ public Float toNativeValue(Object value)
+ {
+ if (value instanceof Float)
+ {
+ return (Float) value;
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Float.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to float.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeFloat(buffer, (Float) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readFloat(buffer);
+ }
+ },
+
+ DOUBLE('d')
+ {
+
+ public int getEncodingSize(Object value)
+ {
+ return EncodingUtils.encodedDoubleLength();
+ }
+
+
+ public Double toNativeValue(Object value)
+ {
+ if (value instanceof Double)
+ {
+ return (Double) value;
+ }
+ else if (value instanceof Float)
+ {
+ return ((Float) value).doubleValue();
+ }
+ else if ((value instanceof String) || (value == null))
+ {
+ return Double.valueOf((String)value);
+ }
+ else
+ {
+ throw new NumberFormatException("Cannot convert: " + value + "(" +
+ value.getClass().getName() + ") to double.");
+ }
+ }
+
+ public void writeValueImpl(Object value, ByteBuffer buffer)
+ {
+ EncodingUtils.writeDouble(buffer, (Double) value);
+ }
+
+ public Object readValueFromBuffer(ByteBuffer buffer)
+ {
+ return EncodingUtils.readDouble(buffer);
+ }
+ };
+
+ private final byte _identifier;
+
+ AMQType(char identifier)
+ {
+ _identifier = (byte) identifier;
+ }
+
+ public final byte identifier()
+ {
+ return _identifier;
+ }
+
+
+ public abstract int getEncodingSize(Object value);
+
+ public abstract Object toNativeValue(Object value);
+
+ public AMQTypedValue asTypedValue(Object value)
+ {
+ return new AMQTypedValue(this, toNativeValue(value));
+ }
+
+ public void writeToBuffer(Object value, ByteBuffer buffer)
+ {
+ buffer.put((byte)identifier());
+ writeValueImpl(value, buffer);
+ }
+
+ abstract void writeValueImpl(Object value, ByteBuffer buffer);
+
+ abstract Object readValueFromBuffer(ByteBuffer buffer);
+}
diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQTypeMap.java b/java/common/src/main/java/org/apache/qpid/framing/AMQTypeMap.java new file mode 100644 index 0000000000..e24fd7efeb --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQTypeMap.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.framing;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class AMQTypeMap
+{
+ public static Map<Byte, AMQType> _reverseTypeMap = new HashMap<Byte, AMQType>();
+
+ static
+ {
+ for(AMQType type : AMQType.values())
+ {
+ _reverseTypeMap.put(type.identifier(), type);
+ }
+ }
+
+ public static AMQType getType(Byte identifier)
+ {
+ return _reverseTypeMap.get(identifier);
+ }
+
+}
diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java b/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java new file mode 100644 index 0000000000..b29c23c2a2 --- /dev/null +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQTypedValue.java @@ -0,0 +1,54 @@ +package org.apache.qpid.framing;
+
+import org.apache.mina.common.ByteBuffer;
+
+public class AMQTypedValue
+{
+ private final AMQType _type;
+ private final Object _value;
+
+
+ public AMQTypedValue(AMQType type, Object value)
+ {
+ if(type == null)
+ {
+ throw new NullPointerException("Cannot create a typed value with null type");
+ }
+ _type = type;
+ _value = type.toNativeValue(value);
+ }
+
+ private AMQTypedValue(AMQType type, ByteBuffer buffer)
+ {
+ _type = type;
+ _value = type.readValueFromBuffer( buffer );
+ }
+
+
+ public AMQType getType()
+ {
+ return _type;
+ }
+
+ public Object getValue()
+ {
+ return _value;
+ }
+
+
+ public void writeToBuffer(ByteBuffer buffer)
+ {
+ _type.writeToBuffer(_value,buffer);
+ }
+
+ public int getEncodingSize()
+ {
+ return _type.getEncodingSize(_value);
+ }
+
+ public static AMQTypedValue readFromBuffer(ByteBuffer buffer)
+ {
+ AMQType type = AMQTypeMap.getType(buffer.get());
+ return new AMQTypedValue(type, buffer);
+ }
+}
diff --git a/java/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java b/java/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java index 46dff9ffa8..8a1bf7fc75 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java +++ b/java/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java @@ -48,6 +48,19 @@ public class EncodingUtils } } + public static int encodedShortStringLength(AMQShortString s) + { + if (s == null) + { + return 1; + } + else + { + return (short) (1 + s.length()); + } + } + + public static int encodedLongStringLength(String s) { if (s == null) @@ -88,12 +101,12 @@ public class EncodingUtils { if (table == null) { - // size is encoded as 4 octets + // length is encoded as 4 octets return 4; } else { - // size of the table plus 4 octets for the size + // length of the table plus 4 octets for the length return (int) table.getEncodedSize() + 4; } } @@ -104,6 +117,20 @@ public class EncodingUtils return 0; } + public static void writeShortStringBytes(ByteBuffer buffer, AMQShortString s) + { + + if (s != null) + { + writeBytes(buffer, s.getBytes()); + } + else + { + // really writing out unsigned byte + buffer.put((byte) 0); + } + } + public static void writeShortStringBytes(ByteBuffer buffer, String s) { if (s != null) @@ -312,6 +339,24 @@ public class EncodingUtils return null; } + public static AMQShortString readShortStringAsAMQShortString(ByteBuffer buffer) + { + short length = buffer.getUnsigned(); + if (length == 0) + { + return null; + } + else + { + byte[] stringBytes = new byte[length]; + buffer.get(stringBytes, 0, length); + + return new AMQShortString(stringBytes); + } + } + + + public static String readShortString(ByteBuffer buffer) { short length = buffer.getUnsigned(); @@ -448,7 +493,7 @@ public class EncodingUtils byte[] from = new byte[size]; // Is this not the same. - //bb.get(from, 0, size); + //bb.get(from, 0, length); for (int i = 0; i < size; i++) { from[i] = bb.get(i); diff --git a/java/common/src/main/java/org/apache/qpid/framing/FieldTable.java b/java/common/src/main/java/org/apache/qpid/framing/FieldTable.java index 193c7adf1c..4d88009076 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/FieldTable.java +++ b/java/common/src/main/java/org/apache/qpid/framing/FieldTable.java @@ -1,98 +1,612 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. * - * */ package org.apache.qpid.framing; +import org.apache.log4j.Logger; import org.apache.mina.common.ByteBuffer; +import org.apache.qpid.AMQPInvalidClassException; -import java.util.Map; -import java.util.Enumeration; +import java.util.*; -public interface FieldTable extends Map +//extends FieldTable +public class FieldTable { - void writeToBuffer(ByteBuffer buffer); - - void setFromBuffer(ByteBuffer buffer, long length) throws AMQFrameDecodingException; - - byte[] getDataAsBytes(); - - public long getEncodedSize(); - - Object put(Object key, Object value); - - Object remove(Object key); - - - public Enumeration getPropertyNames(); - - public boolean propertyExists(String propertyName); - - //Getters - - public Boolean getBoolean(String string); - - public Byte getByte(String string); - - public Short getShort(String string); - - public Integer getInteger(String string); - - public Long getLong(String string); - - public Float getFloat(String string); - - public Double getDouble(String string); - - public String getString(String string); - - public Character getCharacter(String string); - - public byte[] getBytes(String string); - - public Object getObject(String string); - - // Setters - public Object setBoolean(String string, boolean b); - - public Object setByte(String string, byte b); - - public Object setShort(String string, short i); - - public Object setInteger(String string, int i); - - public Object setLong(String string, long l); - - public Object setFloat(String string, float v); - - public Object setDouble(String string, double v); - - public Object setString(String string, String string1); - - public Object setChar(String string, char c); - - public Object setBytes(String string, byte[] bytes); - - public Object setBytes(String string, byte[] bytes, int start, int length); - - public Object setObject(String string, Object object); - - public boolean isNullStringValue(String name); + private static final Logger _logger = Logger.getLogger(FieldTable.class); + + private LinkedHashMap<String, AMQTypedValue> _properties; + + public FieldTable() + { + super(); + _properties = new LinkedHashMap<String, AMQTypedValue>(); + + } + + + + /** + * Construct a new field table. + * + * @param buffer the buffer from which to read data. The length byte must be read already + * @param length the length of the field table. Must be > 0. + * @throws AMQFrameDecodingException if there is an error decoding the table + */ + public FieldTable(ByteBuffer buffer, long length) throws AMQFrameDecodingException + { + this(); + setFromBuffer(buffer, length); + } + + + + public Boolean getBoolean(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.BOOLEAN)) + { + return (Boolean) value.getValue(); + } + else + { + return null; + } + } + + public Byte getByte(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.BYTE)) + { + return (Byte) value.getValue(); + } + else + { + return null; + } + } + + public Short getShort(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.SHORT)) + { + return (Short) value.getValue(); + } + else + { + return null; + } + } + + public Integer getInteger(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.INT)) + { + return (Integer) value.getValue(); + } + else + { + return null; + } + } + + public Long getLong(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.LONG)) + { + return (Long) value.getValue(); + } + else + { + return null; + } + } + + public Float getFloat(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.FLOAT)) + { + return (Float) value.getValue(); + } + else + { + return null; + } + } + + public Double getDouble(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.DOUBLE)) + { + return (Double) value.getValue(); + } + else + { + return null; + } + } + + public String getString(String string) + { + AMQTypedValue value = _properties.get(string); + if ((value != null) && ((value.getType() == AMQType.WIDE_STRING) || + (value.getType() == AMQType.ASCII_STRING))) + { + return (String) value.getValue(); + } + + else if ((value != null) && (value.getValue() != null) && !(value.getValue() instanceof byte[])) + { + return String.valueOf(value.getValue()); + } + else + { + return null; + } + + } + + public Character getCharacter(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.ASCII_CHARACTER)) + { + return (Character) value.getValue(); + } + else + { + return null; + } + } + + public byte[] getBytes(String string) + { + AMQTypedValue value = _properties.get(string); + if (value != null && (value.getType() == AMQType.BINARY)) + { + return (byte[]) value.getValue(); + } + else + { + return null; + } + } + + public Object getObject(String string) + { + AMQTypedValue value = _properties.get(string); + if(value != null) + { + return value.getValue(); + } + else + { + return value; + } + + } + + // ************ Setters + + public Object setBoolean(String string, boolean b) + { + checkPropertyName(string); + return _properties.put(string, AMQType.BOOLEAN.asTypedValue(b)); + } + + public Object setByte(String string, byte b) + { + checkPropertyName(string); + return _properties.put(string, AMQType.BYTE.asTypedValue(b)); + } + + public Object setShort(String string, short i) + { + checkPropertyName(string); + return _properties.put(string, AMQType.SHORT.asTypedValue(i)); + } + + public Object setInteger(String string, int i) + { + checkPropertyName(string); + return _properties.put(string, AMQType.INT.asTypedValue(i)); + } + + public Object setLong(String string, long l) + { + checkPropertyName(string); + return _properties.put(string, AMQType.LONG.asTypedValue(l)); + } + + public Object setFloat(String string, float v) + { + checkPropertyName(string); + return _properties.put(string, AMQType.FLOAT.asTypedValue(v)); + } + + public Object setDouble(String string, double v) + { + checkPropertyName(string); + return _properties.put(string, AMQType.DOUBLE.asTypedValue(v)); + } + + public Object setString(String string, String value) + { + checkPropertyName(string); + if (value == null) + { + return _properties.put(string, AMQType.NULL_STRING.asTypedValue(null)); + } + else + { + //FIXME: determine string encoding and set either WIDE or ASCII string +// if () + { + return _properties.put(string, AMQType.WIDE_STRING.asTypedValue(value)); + } +// else +// { +// return _properties.put(string, AMQType.ASCII_STRING.asTypedValue(value)); +// } + } + } + + public Object setChar(String string, char c) + { + checkPropertyName(string); + return _properties.put(string, AMQType.ASCII_CHARACTER.asTypedValue(c)); + } + + public Object setBytes(String string, byte[] bytes) + { + checkPropertyName(string); + return _properties.put(string, AMQType.BINARY.asTypedValue(bytes)); + } + + public Object setBytes(String string, byte[] bytes, int start, int length) + { + checkPropertyName(string); + byte[] newBytes = new byte[length]; + System.arraycopy(bytes,start,newBytes,0,length); + return setBytes(string, bytes); + } + + + public Object setObject(String string, Object object) + { + if (object instanceof Boolean) + { + return setBoolean(string, (Boolean) object); + } + else if (object instanceof Byte) + { + return setByte(string, (Byte) object); + } + else if (object instanceof Short) + { + return setShort(string, (Short) object); + } + else if (object instanceof Integer) + { + return setInteger(string, (Integer) object); + } + else if (object instanceof Long) + { + return setLong(string, (Long) object); + } + else if (object instanceof Float) + { + return setFloat(string, (Float) object); + } + else if (object instanceof Double) + { + return setDouble(string, (Double) object); + } + else if (object instanceof String) + { + return setString(string, (String) object); + } + else if (object instanceof Character) + { + return setChar(string, (Character) object); + } + else if (object instanceof byte[]) + { + return setBytes(string, (byte[]) object); + } + + throw new AMQPInvalidClassException("Only Primatives objects allowed Object is:" + object.getClass()); + } + + + public boolean isNullStringValue(String name) + { + AMQTypedValue value = _properties.get(name); + return (value != null) && (value.getType() == AMQType.NULL_STRING); + } + + // ***** Methods + + public Enumeration getPropertyNames() + { + return Collections.enumeration(_properties.keySet()); + } + + public boolean propertyExists(String propertyName) + { + return itemExists(propertyName); + } + + public boolean itemExists(String string) + { + return _properties.containsKey(string); + } + + public String toString() + { + return _properties.toString(); + } + + + + private void checkPropertyName(String propertyName) + { + if (propertyName == null) + { + throw new IllegalArgumentException("Property name must not be null"); + } + else if ("".equals(propertyName)) + { + throw new IllegalArgumentException("Property name must not be the empty string"); + } + + checkIdentiferFormat(propertyName); + } + + + protected static void checkIdentiferFormat(String propertyName) + { +// AMQP Spec: 4.2.5.5 Field Tables +// Guidelines for implementers: +// * Field names MUST start with a letter, '$' or '#' and may continue with +// letters, '$' or '#', digits, or underlines, to a maximum length of 128 +// characters. +// * The server SHOULD validate field names and upon receiving an invalid +// field name, it SHOULD signal a connection exception with reply code +// 503 (syntax error). Conformance test: amq_wlp_table_01. +// * A peer MUST handle duplicate fields by using only the first instance. + + + // AMQP length limit + if (propertyName.length() > 128) + { + throw new IllegalArgumentException("AMQP limits property names to 128 characters"); + } + + // AMQ start character + if (!(Character.isLetter(propertyName.charAt(0)) + || propertyName.charAt(0) == '$' + || propertyName.charAt(0) == '#' + || propertyName.charAt(0) == '_')) // Not official AMQP added for JMS. + { + throw new IllegalArgumentException("Identifier '" + propertyName + "' does not start with a valid AMQP start character"); + } + } + + + // ************************* Byte Buffer Processing + + public void writeToBuffer(ByteBuffer buffer) + { + final boolean trace = _logger.isTraceEnabled(); + + if (trace) + { + _logger.trace("FieldTable::writeToBuffer: Writing encoded length of " + getEncodedSize() + "..."); + } + + EncodingUtils.writeUnsignedInteger(buffer, getEncodedSize()); + + putDataInBuffer(buffer); + } + + public byte[] getDataAsBytes() + { + final int encodedSize = (int) getEncodedSize(); + final ByteBuffer buffer = ByteBuffer.allocate(encodedSize); // FIXME XXX: Is cast a problem? + + putDataInBuffer(buffer); + + final byte[] result = new byte[encodedSize]; + buffer.flip(); + buffer.get(result); + buffer.release(); + return result; + } + + public long getEncodedSize() + { + int encodedSize = 0; + for(Map.Entry<String,AMQTypedValue> e : _properties.entrySet()) + { + encodedSize += EncodingUtils.encodedShortStringLength(e.getKey()); + encodedSize++; // the byte for the encoding Type + encodedSize += e.getValue().getEncodingSize(); + + } + return encodedSize; + } + + public void addAll(FieldTable fieldTable) + { + _properties.putAll(fieldTable._properties); + } + + + public static interface FieldTableElementProcessor + { + public boolean processElement(String propertyName, AMQTypedValue value); + public Object getResult(); + } + + public Object processOverElements(FieldTableElementProcessor processor) + { + for(Map.Entry<String,AMQTypedValue> e : _properties.entrySet()) + { + boolean result = processor.processElement(e.getKey(), e.getValue()); + if(!result) + { + break; + } + } + return processor.getResult(); + } + + + public int size() + { + return _properties.size(); + } + + public boolean isEmpty() + { + return _properties.isEmpty(); + } + + public boolean containsKey(String key) + { + return _properties.containsKey(key); + } + + public Set<String> keys() + { + return _properties.keySet(); + } + + + public Object get(Object key) + { + + return getObject((String)key); + } + + + public Object put(Object key, Object value) + { + return setObject(key.toString(), value); + } + + + public Object remove(String key) + { + AMQTypedValue value = _properties.remove(key); + return value == null ? null : value.getValue(); + } + + + + public void clear() + { + _properties.clear(); + } + + public Set keySet() + { + return _properties.keySet(); + } + + private void putDataInBuffer(ByteBuffer buffer) + { + + final Iterator<Map.Entry<String,AMQTypedValue>> it = _properties.entrySet().iterator(); + + //If there are values then write out the encoded Size... could check _encodedSize != 0 + // write out the total length, which we have kept up to date as data is added + + + while (it.hasNext()) + { + final Map.Entry<String,AMQTypedValue> me = it.next(); + try + { + if (_logger.isTraceEnabled()) + { + _logger.trace("Writing Property:" + me.getKey() + + " Type:" + me.getValue().getType() + + " Value:" + me.getValue().getValue()); + _logger.trace("Buffer Position:" + buffer.position() + + " Remaining:" + buffer.remaining()); + } + + + + //Write the actual parameter name + EncodingUtils.writeShortStringBytes(buffer, me.getKey()); + me.getValue().writeToBuffer(buffer); + } + catch (Exception e) + { + if (_logger.isTraceEnabled()) + { + _logger.trace("Exception thrown:" + e); + _logger.trace("Writing Property:" + me.getKey() + + " Type:" + me.getValue().getType() + + " Value:" + me.getValue().getValue()); + _logger.trace("Buffer Position:" + buffer.position() + + " Remaining:" + buffer.remaining()); + } + throw new RuntimeException(e); + } + } + } + + + public void setFromBuffer(ByteBuffer buffer, long length) throws AMQFrameDecodingException + { + final boolean trace = _logger.isTraceEnabled(); + + int sizeRead = 0; + while (sizeRead < length) + { + int sizeRemaining = buffer.remaining(); + final String key = EncodingUtils.readShortString(buffer); + AMQTypedValue value = AMQTypedValue.readFromBuffer(buffer); + sizeRead += (sizeRemaining - buffer.remaining()); + + if (trace) + { + _logger.trace("FieldTable::PropFieldTable(buffer," + length + "): Read type '" + value.getType() + "', key '" + key + "', value '" + value.getValue() + "' (now read " + sizeRead + " of " + length + " encoded bytes)..."); + } + + _properties.put(key,value); + } + + if (trace) + { + _logger.trace("FieldTable::FieldTable(buffer," + length + "): Done."); + } + } } diff --git a/java/common/src/main/java/org/apache/qpid/framing/FieldTableFactory.java b/java/common/src/main/java/org/apache/qpid/framing/FieldTableFactory.java index b1fcd8a20b..e9d75137ef 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/FieldTableFactory.java +++ b/java/common/src/main/java/org/apache/qpid/framing/FieldTableFactory.java @@ -26,16 +26,13 @@ public class FieldTableFactory { public static FieldTable newFieldTable() { - return new PropertyFieldTable(); + return new FieldTable(); } public static FieldTable newFieldTable(ByteBuffer byteBuffer, long length) throws AMQFrameDecodingException { - return new PropertyFieldTable(byteBuffer, length); + return new FieldTable(byteBuffer, length); } - public static FieldTable newFieldTable(String text) - { - return new PropertyFieldTable(text); - } + } diff --git a/java/common/src/main/java/org/apache/qpid/framing/JMSPropertyFieldTable.java b/java/common/src/main/java/org/apache/qpid/framing/JMSPropertyFieldTable.java index 142a689a01..d78034cf2f 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/JMSPropertyFieldTable.java +++ b/java/common/src/main/java/org/apache/qpid/framing/JMSPropertyFieldTable.java @@ -32,29 +32,11 @@ public class JMSPropertyFieldTable { private FieldTable _fieldtable; - public JMSPropertyFieldTable() - { - _fieldtable = new PropertyFieldTable(); - } - public JMSPropertyFieldTable(FieldTable table) { _fieldtable = table; } - public JMSPropertyFieldTable(ByteBuffer buffer, long length) throws JMSException - { - try - { - _fieldtable = new PropertyFieldTable(buffer, length); - } - catch (AMQFrameDecodingException e) - { - JMSException error = new JMSException(e.getMessage()); - error.setLinkedException(e); - throw error; - } - } private void checkPropertyName(String propertyName) { diff --git a/java/common/src/main/java/org/apache/qpid/framing/PropertyFieldTable.java b/java/common/src/main/java/org/apache/qpid/framing/PropertyFieldTable.java deleted file mode 100644 index 4b8f56e4e8..0000000000 --- a/java/common/src/main/java/org/apache/qpid/framing/PropertyFieldTable.java +++ /dev/null @@ -1,1281 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.framing; - -import org.apache.log4j.Logger; -import org.apache.mina.common.ByteBuffer; -import org.apache.qpid.AMQPInvalidClassException; - -import java.util.Collection; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.util.StringTokenizer; -import java.util.Vector; -import java.util.HashMap; - -//extends FieldTable -public class PropertyFieldTable implements FieldTable -{ - private static final Logger _logger = Logger.getLogger(PropertyFieldTable.class); - - private static final String BOOLEAN = "boolean"; - private static final String BYTE = "byte"; - private static final String BYTES = "bytes"; - private static final String SHORT = "short"; - private static final String INT = "int"; - private static final String LONG = "long"; - private static final String FLOAT = "float"; - private static final String DOUBLE = "double"; - private static final String STRING = "string"; - private static final String NULL_STRING = "nullstring"; - private static final String CHAR = "char"; - private static final String UNKNOWN = "unknown type"; - - private static final String PROPERTY_FIELD_TABLE_CLOSE_XML = "</PropertyFieldTable>"; - private static final String PROPERTY_FIELD_TABLE_OPEN_XML = "<PropertyFieldTable>"; - private static final String BYTES_CLOSE_XML = "</" + BYTES + ">"; - private static final String BYTES_OPEN_XML_START = "<" + BYTES; - - public static enum Prefix - { - //AMQP FieldTable Wire Types - AMQP_DECIMAL_PROPERTY_PREFIX('D'), - AMQP_UNSIGNED_SHORT_PROPERTY_PREFIX('S'), - AMQP_UNSIGNED_INT_PROPERTY_PREFIX('I'), - AMQP_UNSIGNED_LONG_PROPERTY_PREFIX('L'), - AMQP_DOUBLE_EXTTENDED_PROPERTY_PREFIX('D'), - - AMQP_TIMESTAMP_PROPERTY_PREFIX('T'), - AMQP_BINARY_PROPERTY_PREFIX('x'), - - //Strings - AMQP_ASCII_STRING_PROPERTY_PREFIX('c'), - AMQP_WIDE_STRING_PROPERTY_PREFIX('C'), - AMQP_NULL_STRING_PROPERTY_PREFIX('n'), - - //Java Primative Types - AMQP_BOOLEAN_PROPERTY_PREFIX('t'), - AMQP_BYTE_PROPERTY_PREFIX('b'), - AMQP_ASCII_CHARACTER_PROPERTY_PREFIX('k'), - AMQP_SHORT_PROPERTY_PREFIX('s'), - AMQP_INT_PROPERTY_PREFIX('i'), - AMQP_LONG_PROPERTY_PREFIX('l'), - AMQP_FLOAT_PROPERTY_PREFIX('f'), - AMQP_DOUBLE_PROPERTY_PREFIX('d'); - - private final char _identifier; - - Prefix(char identifier) - { - _identifier = identifier; - //_reverseTypeMap.put(identifier, this); - } - - public final char identifier() - { - return _identifier; - } - - } - - public static Map<Character, Prefix> _reverseTypeMap = new HashMap<Character, Prefix>(); - - static - { - for (Prefix p : Prefix.values()) - { - _reverseTypeMap.put(p.identifier(), p); - } - } - - private LinkedHashMap<String, Object> _properties; - private LinkedHashMap<String, Prefix> _propertyNamesTypeMap; - private long _encodedSize = 0; - - public PropertyFieldTable() - { - super(); - _properties = new LinkedHashMap<String, Object>(); - _propertyNamesTypeMap = new LinkedHashMap<String, Prefix>(); - } - - public PropertyFieldTable(String textFormat) - { - this(); - try - { - parsePropertyFieldTable(textFormat); - } - catch (Exception e) - { - _logger.warn("Unable to decode PropertyFieldTable format:" + textFormat); - throw new IllegalArgumentException("Unable to decode PropertyFieldTable format:" + textFormat); - } - } - - /** - * Construct a new field table. - * - * @param buffer the buffer from which to read data. The length byte must be read already - * @param length the length of the field table. Must be > 0. - * @throws AMQFrameDecodingException if there is an error decoding the table - */ - public PropertyFieldTable(ByteBuffer buffer, long length) throws AMQFrameDecodingException - { - this(); - setFromBuffer(buffer, length); - } - - // ************ Getters - private Object get(String propertyName, Prefix prefix) - { - //Retrieve the type associated with this name - Prefix type = _propertyNamesTypeMap.get(propertyName); - - if (type == null) - { - return null; - } - - if (type.equals(prefix)) - { - return _properties.get(propertyName); - } - else - { - return null; - } - } - - public Boolean getBoolean(String string) - { - Object o = get(string, Prefix.AMQP_BOOLEAN_PROPERTY_PREFIX); - if (o != null && o instanceof Boolean) - { - return (Boolean) o; - } - else - { - return null; - } - } - - public Byte getByte(String string) - { - Object o = get(string, Prefix.AMQP_BYTE_PROPERTY_PREFIX); - if (o != null) - { - return (Byte) o; - } - else - { - return null; - } - } - - public Short getShort(String string) - { - Object o = get(string, Prefix.AMQP_SHORT_PROPERTY_PREFIX); - if (o != null) - { - return (Short) o; - } - else - { - return null; - } - } - - public Integer getInteger(String string) - { - Object o = get(string, Prefix.AMQP_INT_PROPERTY_PREFIX); - if (o != null) - { - return (Integer) o; - } - else - { - return null; - } - } - - public Long getLong(String string) - { - Object o = get(string, Prefix.AMQP_LONG_PROPERTY_PREFIX); - if (o != null) - { - return (Long) o; - } - else - { - return null; - } - } - - public Float getFloat(String string) - { - Object o = get(string, Prefix.AMQP_FLOAT_PROPERTY_PREFIX); - if (o != null) - { - return (Float) o; - } - else - { - return null; - } - } - - public Double getDouble(String string) - { - Object o = get(string, Prefix.AMQP_DOUBLE_PROPERTY_PREFIX); - if (o != null) - { - return (Double) o; - } - else - { - return null; - } - } - - public String getString(String string) - { - Object o = get(string, Prefix.AMQP_ASCII_STRING_PROPERTY_PREFIX); - if (o != null) - { - return (String) o; - } - else - { - o = get(string, Prefix.AMQP_WIDE_STRING_PROPERTY_PREFIX); - if (o != null) - { - return (String) o; - } - else - { - - Prefix type = _propertyNamesTypeMap.get(string); - - if (type == null || type.equals(Prefix.AMQP_NULL_STRING_PROPERTY_PREFIX)) - { - return null; - } - else - { - switch (type) - { - case AMQP_ASCII_STRING_PROPERTY_PREFIX: - case AMQP_WIDE_STRING_PROPERTY_PREFIX: - case AMQP_BINARY_PROPERTY_PREFIX: - return null; - default: - case AMQP_BYTE_PROPERTY_PREFIX: - case AMQP_BOOLEAN_PROPERTY_PREFIX: - case AMQP_SHORT_PROPERTY_PREFIX: - case AMQP_INT_PROPERTY_PREFIX: - case AMQP_LONG_PROPERTY_PREFIX: - case AMQP_FLOAT_PROPERTY_PREFIX: - case AMQP_DOUBLE_PROPERTY_PREFIX: - return String.valueOf(_properties.get(string)); - case AMQP_ASCII_CHARACTER_PROPERTY_PREFIX: - Object value = _properties.get(string); - if (value == null) - { - throw new NullPointerException("null char cannot be converted to String"); - } - else - { - return String.valueOf(value); - } - } - } - } - } - } - - public Character getCharacter(String string) - { - Object o = get(string, Prefix.AMQP_ASCII_CHARACTER_PROPERTY_PREFIX); - if (o != null) - { - return (Character) o; - } - else - { - return null; - } - } - - public byte[] getBytes(String string) - { - Object o = get(string, Prefix.AMQP_BINARY_PROPERTY_PREFIX); - if (o != null) - { - return (byte[]) o; - } - else - { - return null; - } - } - - public Object getObject(String string) - { - return _properties.get(string); - } - - // ************ Setters - - public Object setBoolean(String string, boolean b) - { - return put(Prefix.AMQP_BOOLEAN_PROPERTY_PREFIX, string, b); - } - - public Object setByte(String string, byte b) - { - return put(Prefix.AMQP_BYTE_PROPERTY_PREFIX, string, b); - } - - public Object setShort(String string, short i) - { - return put(Prefix.AMQP_SHORT_PROPERTY_PREFIX, string, i); - } - - public Object setInteger(String string, int i) - { - return put(Prefix.AMQP_INT_PROPERTY_PREFIX, string, i); - } - - public Object setLong(String string, long l) - { - return put(Prefix.AMQP_LONG_PROPERTY_PREFIX, string, l); - } - - public Object setFloat(String string, float v) - { - return put(Prefix.AMQP_FLOAT_PROPERTY_PREFIX, string, v); - } - - public Object setDouble(String string, double v) - { - return put(Prefix.AMQP_DOUBLE_PROPERTY_PREFIX, string, v); - } - - public Object setString(String string, String string1) - { - if (string1 == null) - { - return put(Prefix.AMQP_NULL_STRING_PROPERTY_PREFIX, string, null); - } - else - { - //FIXME: determine string encoding and set either WIDE or ASCII string -// if () - { - return put(Prefix.AMQP_WIDE_STRING_PROPERTY_PREFIX, string, string1); - } -// else -// { -// return put(Prefix.AMQP_ASCII_STRING_PROPERTY_PREFIX, string, string1); -// } - } - } - - public Object setChar(String string, char c) - { - return put(Prefix.AMQP_ASCII_CHARACTER_PROPERTY_PREFIX, string, c); - } - - public Object setBytes(String string, byte[] bytes) - { - return setBytes(string, bytes, 0, bytes.length); - } - - public Object setBytes(String string, byte[] bytes, int start, int length) - { - return put(Prefix.AMQP_BINARY_PROPERTY_PREFIX, string, sizeByteArray(bytes, start, length)); - } - - private byte[] sizeByteArray(byte[] bytes, int start, int length) - { - byte[] resized = new byte[length]; - int newIndex = 0; - for (int oldIndex = start; oldIndex < length; oldIndex++) - { - resized[newIndex] = bytes[oldIndex]; - newIndex++; - } - - return resized; - } - - - public Object setObject(String string, Object object) - { - if (object instanceof Boolean) - { - return setBoolean(string, (Boolean) object); - } - else if (object instanceof Byte) - { - return setByte(string, (Byte) object); - } - else if (object instanceof Short) - { - return setShort(string, (Short) object); - } - else if (object instanceof Integer) - { - return setInteger(string, (Integer) object); - } - else if (object instanceof Long) - { - return setLong(string, (Long) object); - } - else if (object instanceof Float) - { - return setFloat(string, (Float) object); - } - else if (object instanceof Double) - { - return setDouble(string, (Double) object); - } - else if (object instanceof String) - { - return setString(string, (String) object); - } - else if (object instanceof Character) - { - return setChar(string, (Character) object); - } - else if (object instanceof byte[]) - { - return setBytes(string, (byte[]) object); - } - - throw new AMQPInvalidClassException("Only Primatives objects allowed Object is:" + object.getClass()); - } - - - public boolean isNullStringValue(String name) - { - return _properties.containsKey(name) && (_properties.get(name) == null) && - _propertyNamesTypeMap.get(name).equals(Prefix.AMQP_NULL_STRING_PROPERTY_PREFIX); - - - } - - // ***** Methods - - public Enumeration getPropertyNames() - { - Vector<String> names = new Vector<String>(); - - Iterator keys = _properties.keySet().iterator(); - - while (keys.hasNext()) - { - String key = (String) keys.next(); - - names.add(key); - } - - return names.elements(); - } - - public boolean propertyExists(String propertyName) - { - return itemExists(propertyName); - } - - public boolean itemExists(String string) - { - return _properties.containsKey(string); - } - - public String toString() - { - return valueOf(this); - } - - public static String valueOf(PropertyFieldTable table) - { - StringBuffer buf = new StringBuffer(PROPERTY_FIELD_TABLE_OPEN_XML); - - final Iterator it = table._properties.entrySet().iterator(); - - while (it.hasNext()) - { - final Map.Entry entry = (Map.Entry) it.next(); - final String propertyName = (String) entry.getKey(); - - buf.append('\n'); - buf.append(valueAsXML(table._propertyNamesTypeMap.get(propertyName), propertyName, entry.getValue())); - } - buf.append("\n"); - buf.append(PROPERTY_FIELD_TABLE_CLOSE_XML); - - return buf.toString(); - } - - private static String valueAsXML(Prefix type, String propertyName, Object value) - { - StringBuffer buf = new StringBuffer(); - // Start Tag - buf.append(propertyXML(type, propertyName, true)); - - // Value - if (type.equals(Prefix.AMQP_BINARY_PROPERTY_PREFIX)) - { - //remove '>' - buf.deleteCharAt(buf.length() - 1); - - byte[] bytes = (byte[]) value; - buf.append(" length='").append(bytes.length).append("'>"); - - buf.append(byteArrayToXML(propertyName, bytes)); - } - else - { - if (!type.equals(Prefix.AMQP_NULL_STRING_PROPERTY_PREFIX)) - { - buf.append(String.valueOf(value)); - } - } - //End Tag - buf.append(propertyXML(type, propertyName, false)); - - return buf.toString(); - } - - private void checkPropertyName(String propertyName) - { - if (propertyName == null) - { - throw new IllegalArgumentException("Property name must not be null"); - } - else if ("".equals(propertyName)) - { - throw new IllegalArgumentException("Property name must not be the empty string"); - } - - checkIdentiferFormat(propertyName); - } - - - protected static void checkIdentiferFormat(String propertyName) - { -// AMQP Spec: 4.2.5.5 Field Tables -// Guidelines for implementers: -// * Field names MUST start with a letter, '$' or '#' and may continue with -// letters, '$' or '#', digits, or underlines, to a maximum length of 128 -// characters. -// * The server SHOULD validate field names and upon receiving an invalid -// field name, it SHOULD signal a connection exception with reply code -// 503 (syntax error). Conformance test: amq_wlp_table_01. -// * A peer MUST handle duplicate fields by using only the first instance. - - // AMQP length limit - if (propertyName.length() > 128) - { - throw new IllegalArgumentException("AMQP limits property names to 128 characters"); - } - - // AMQ start character - if (!(Character.isLetter(propertyName.charAt(0)) - || propertyName.charAt(0) == '$' - || propertyName.charAt(0) == '#' - || propertyName.charAt(0) == '_')) // Not official AMQP added for JMS. - { - throw new IllegalArgumentException("Identifier '" + propertyName + "' does not start with a valid AMQP start character"); - } - } - - private static String propertyXML(Prefix type, String propertyName, boolean start) - { - StringBuffer buf = new StringBuffer(); - - if (start) - { - buf.append("<"); - } - else - { - buf.append("</"); - } - - switch (type) - { - case AMQP_BOOLEAN_PROPERTY_PREFIX: - buf.append(BOOLEAN); - break; - case AMQP_BYTE_PROPERTY_PREFIX: - buf.append(BYTE); - break; - case AMQP_BINARY_PROPERTY_PREFIX: - buf.append(BYTES); - break; - case AMQP_SHORT_PROPERTY_PREFIX: - buf.append(SHORT); - break; - case AMQP_INT_PROPERTY_PREFIX: - buf.append(INT); - break; - case AMQP_LONG_PROPERTY_PREFIX: - buf.append(LONG); - break; - case AMQP_FLOAT_PROPERTY_PREFIX: - buf.append(FLOAT); - break; - case AMQP_DOUBLE_PROPERTY_PREFIX: - buf.append(DOUBLE); - break; - case AMQP_NULL_STRING_PROPERTY_PREFIX: - buf.append(NULL_STRING); - break; - case AMQP_ASCII_STRING_PROPERTY_PREFIX: - case AMQP_WIDE_STRING_PROPERTY_PREFIX: - buf.append(STRING); - break; - case AMQP_ASCII_CHARACTER_PROPERTY_PREFIX: - buf.append(CHAR); - break; - default: - buf.append(UNKNOWN + " (identifier ").append(type.identifier()).append(")"); - break; - } - - if (start) - { - buf.append(" name='").append(propertyName).append("'"); - } - - buf.append(">"); - - return buf.toString(); - } - - private static String byteArrayToXML(String propertyName, byte[] bytes) - { - StringBuffer buf = new StringBuffer(); - - for (int index = 0; index < bytes.length; index++) - { - buf.append("\n"); - buf.append(propertyXML(Prefix.AMQP_BYTE_PROPERTY_PREFIX, propertyName + "[" + index + "]", true)); - buf.append(bytes[index]); - buf.append(propertyXML(Prefix.AMQP_BYTE_PROPERTY_PREFIX, propertyName + "[" + index + "]", false)); - } - buf.append("\n"); - return buf.toString(); - } - - private void processBytesXMLLine(String xmlline) - { - String propertyName = xmlline.substring(xmlline.indexOf('\'') + 1, - xmlline.indexOf('\'', xmlline.indexOf('\'') + 1)); - String value = xmlline.substring(xmlline.indexOf(">") + 1, - xmlline.indexOf("</")); - - Integer index = Integer.parseInt(propertyName.substring(propertyName.lastIndexOf("[") + 1, - propertyName.lastIndexOf("]"))); - propertyName = propertyName.substring(0, propertyName.lastIndexOf("[")); - - getBytes(propertyName)[index] = Byte.parseByte(value); - } - - private void parsePropertyFieldTable(String textFormat) - { - StringTokenizer tokenizer = new StringTokenizer(textFormat, "\n"); - - boolean finished = false; - boolean processing = false; - - boolean processing_bytes = false; - - if (!tokenizer.hasMoreTokens()) - { - throw new IllegalArgumentException("XML has no tokens to parse."); - } - - while (tokenizer.hasMoreTokens()) - { - String token = tokenizer.nextToken(); - - if (token.equals(PROPERTY_FIELD_TABLE_CLOSE_XML)) - { - processing = false; - finished = true; - } - if (token.equals(BYTES_CLOSE_XML)) - { - processing = false; - } - - if (token.equals(BYTES_CLOSE_XML)) - { - processing_bytes = false; - } - - if (processing) - { - processXMLLine(token); - } - else if (processing_bytes) - { - processBytesXMLLine(token); - } - - if (token.startsWith(BYTES_OPEN_XML_START)) - { - processing_bytes = true; - processing = false; - } - - if (token.equals(PROPERTY_FIELD_TABLE_OPEN_XML) || - token.equals(BYTES_CLOSE_XML)) - { - processing = true; - } - } - - if (!finished) - { - throw new IllegalArgumentException("XML was not in a valid format."); - } - - } - - private void processXMLLine(String xmlline) - { - // <<type> name='<property>'><value></<type>> - // <string name='message' >Message 99</string > - - String type = xmlline.substring(1, xmlline.indexOf(" ")); - - String propertyName = xmlline.substring(xmlline.indexOf('\'') + 1, - xmlline.indexOf('\'', xmlline.indexOf('\'') + 1)); - - String value = ""; - - if (!type.equals(BYTES)) - { - value = xmlline.substring(xmlline.indexOf(">") + 1, - xmlline.indexOf("</")); - } - - if (type.equals(BOOLEAN)) - { - setBoolean(propertyName, Boolean.parseBoolean(value)); - } - if (type.equals(BYTE)) - { - setByte(propertyName, Byte.parseByte(value)); - } - if (type.equals(BYTES)) - { - int headerEnd = xmlline.indexOf('>'); - String bytesHeader = xmlline.substring(0, headerEnd); - - //Extract length value - Integer length = Integer.parseInt(bytesHeader.substring( - bytesHeader.lastIndexOf("=") + 2 - , bytesHeader.lastIndexOf("'"))); - - - byte[] bytes = new byte[length]; - setBytes(propertyName, bytes); - - //Check if the line contains all the byte values - // This is needed as the XMLLine sent across the wire is the bytes value - - int byteStart = xmlline.indexOf('<', headerEnd); - - //Don't think this is required. - if (byteStart > 0) - { - while (!xmlline.startsWith(BYTES_CLOSE_XML, byteStart)) - { - //This should be the next byte line - int bytePrefixEnd = xmlline.indexOf('>', byteStart) + 1; - int byteEnd = xmlline.indexOf('>', bytePrefixEnd) + 1; - - String byteline = xmlline.substring(byteStart, byteEnd); - - processBytesXMLLine(byteline); - - byteStart = xmlline.indexOf('<', byteEnd); - } - } - - } - if (type.equals(SHORT)) - { - setShort(propertyName, Short.parseShort(value)); - } - if (type.equals(INT)) - { - setInteger(propertyName, Integer.parseInt(value)); - } - if (type.equals(LONG)) - { - setLong(propertyName, Long.parseLong(value)); - } - if (type.equals(FLOAT)) - { - setFloat(propertyName, Float.parseFloat(value)); - } - if (type.equals(DOUBLE)) - { - setDouble(propertyName, Double.parseDouble(value)); - } - if (type.equals(STRING) || type.equals(NULL_STRING)) - { - if (type.equals(NULL_STRING)) - { - value = null; - } - setString(propertyName, value); - } - if (type.equals(CHAR)) - { - setChar(propertyName, value.charAt(0)); - } - if (type.equals(UNKNOWN)) - { - _logger.warn("Ignoring unknown property value:" + xmlline); - } - } - - // ************************* Byte Buffer Processing - - public void writeToBuffer(ByteBuffer buffer) - { - final boolean trace = _logger.isTraceEnabled(); - - if (trace) - { - _logger.trace("FieldTable::writeToBuffer: Writing encoded size of " + _encodedSize + "..."); - } - - EncodingUtils.writeUnsignedInteger(buffer, _encodedSize); - - putDataInBuffer(buffer); - } - - public byte[] getDataAsBytes() - { - final ByteBuffer buffer = ByteBuffer.allocate((int) _encodedSize); // FIXME XXX: Is cast a problem? - - putDataInBuffer(buffer); - - final byte[] result = new byte[(int) _encodedSize]; - buffer.flip(); - buffer.get(result); - buffer.release(); - return result; - } - - - public int size() - { - return _properties.size(); - } - - public boolean isEmpty() - { - return _properties.isEmpty(); - } - - public boolean containsKey(Object key) - { - return _properties.containsKey(key); - } - - public boolean containsValue(Object value) - { - return _properties.containsValue(value); - } - - public Object get(Object key) - { - return _properties.get(key); - } - - - public Object put(Object key, Object value) - { - return setObject(key.toString(), value); - } - - protected Object put(Prefix type, String propertyName, Object value) - { - checkPropertyName(propertyName); - - //remove the previous value - Object previous = remove(propertyName); - - - if (_logger.isTraceEnabled()) - { - int valueSize = 0; - if (value != null) - { - valueSize = getEncodingSize(type, value); - } - _logger.trace("Put:" + propertyName + - " encoding size Now:" + _encodedSize + - " name size= " + EncodingUtils.encodedShortStringLength(propertyName) + - " value size= " + valueSize); - } - - //Add the size of the propertyName plus one for the type identifier - _encodedSize += EncodingUtils.encodedShortStringLength(propertyName) + 1; - - if (value != null) - { - //Add the size of the content - _encodedSize += getEncodingSize(type, value); - } - - //Store new values - _propertyNamesTypeMap.put(propertyName, type); - _properties.put(propertyName, value); - - return previous; - } - - public Object remove(Object key) - { - if (_properties.containsKey(key)) - { - final Object value = _properties.remove(key); - Prefix type = _propertyNamesTypeMap.remove(key); - // plus one for the type - _encodedSize -= EncodingUtils.encodedShortStringLength(((String) key)) + 1; - - // This check is, for now, unnecessary (we don't store null values). - if (value != null) - { - _encodedSize -= getEncodingSize(type, value); - } - return value; - } - else - { - return null; - } - } - - public void putAll(Map t) - { - Iterator it = t.keySet().iterator(); - - while (it.hasNext()) - { - Object key = it.next(); - put(key, t.get(key)); - } - } - - public void clear() - { - _encodedSize = 0; - _properties.clear(); - _propertyNamesTypeMap.clear(); - } - - public Set keySet() - { - return _properties.keySet(); - } - - public Collection values() - { - return _properties.values(); - } - - public Set entrySet() - { - return _properties.entrySet(); - } - - public long getEncodedSize() - { - return _encodedSize; - } - - - private void putDataInBuffer(ByteBuffer buffer) - { - - final Iterator it = _properties.entrySet().iterator(); - - //If there are values then write out the encoded Size... could check _encodedSize != 0 - // write out the total length, which we have kept up to date as data is added - - - while (it.hasNext()) - { - Map.Entry me = (Map.Entry) it.next(); - String propertyName = (String) me.getKey(); - - //The type value - Prefix type = _propertyNamesTypeMap.get(propertyName); - - Object value = me.getValue(); - try - { - if (_logger.isTraceEnabled()) - { - _logger.trace("Writing Property:" + propertyName + - " Type:" + type + - " Value:" + value); - _logger.trace("Buffer Position:" + buffer.position() + - " Remaining:" + buffer.remaining()); - } - - //Write the actual parameter name - EncodingUtils.writeShortStringBytes(buffer, propertyName); - - switch (type) - { - case AMQP_BOOLEAN_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_BOOLEAN_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeBoolean(buffer, (Boolean) value); - break; - case AMQP_BYTE_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_BYTE_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeByte(buffer, (Byte) value); - break; - case AMQP_SHORT_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_SHORT_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeShort(buffer, (Short) value); - break; - case AMQP_INT_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_INT_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeInteger(buffer, (Integer) value); - break; - case AMQP_UNSIGNED_INT_PROPERTY_PREFIX: // Currently we don't create these - buffer.put((byte) Prefix.AMQP_UNSIGNED_INT_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeUnsignedInteger(buffer, (Long) value); - break; - case AMQP_LONG_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_LONG_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeLong(buffer, (Long) value); - break; - case AMQP_FLOAT_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_FLOAT_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeFloat(buffer, (Float) value); - break; - case AMQP_DOUBLE_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_DOUBLE_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeDouble(buffer, (Double) value); - break; - case AMQP_NULL_STRING_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_NULL_STRING_PROPERTY_PREFIX.identifier()); - break; - case AMQP_WIDE_STRING_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_WIDE_STRING_PROPERTY_PREFIX.identifier()); - // FIXME: use proper charset encoder - EncodingUtils.writeLongStringBytes(buffer, (String) value); - break; - case AMQP_ASCII_STRING_PROPERTY_PREFIX: - //This is a simple ASCII string - buffer.put((byte) Prefix.AMQP_ASCII_STRING_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeLongStringBytes(buffer, (String) value); - break; - case AMQP_ASCII_CHARACTER_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_ASCII_CHARACTER_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeChar(buffer, (Character) value); - break; - case AMQP_BINARY_PROPERTY_PREFIX: - buffer.put((byte) Prefix.AMQP_BINARY_PROPERTY_PREFIX.identifier()); - EncodingUtils.writeBytes(buffer, (byte[]) value); - break; - default: - { - // Should never get here - throw new IllegalArgumentException("Key '" + propertyName + "': Unsupported type in field table, type: " + ((value == null) ? "null-object" : value.getClass())); - } - } - } - catch (Exception e) - { - if (_logger.isTraceEnabled()) - { - _logger.trace("Exception thrown:" + e); - _logger.trace("Writing Property:" + propertyName + - " Type:" + type + - " Value:" + value); - _logger.trace("Buffer Position:" + buffer.position() + - " Remaining:" + buffer.remaining()); - } - throw new RuntimeException(e); - } - } - } - - - public void setFromBuffer(ByteBuffer buffer, long length) throws AMQFrameDecodingException - { - final boolean trace = _logger.isTraceEnabled(); - - int sizeRead = 0; - while (sizeRead < length) - { - int sizeRemaining = buffer.remaining(); - final String key = EncodingUtils.readShortString(buffer); - - byte iType = buffer.get(); - - Character mapKey = new Character((char) iType); - Prefix type = _reverseTypeMap.get(mapKey); - - if (type == null) - { - String msg = "Field '" + key + "' - unsupported field table type: " + type + "."; - //some extra trace information... - msg += " (" + iType + "), length=" + length + ", sizeRead=" + sizeRead + ", sizeRemaining=" + sizeRemaining; - throw new AMQFrameDecodingException(msg); - } - Object value; - - switch (type) - { - case AMQP_BOOLEAN_PROPERTY_PREFIX: - value = EncodingUtils.readBoolean(buffer); - break; - case AMQP_BYTE_PROPERTY_PREFIX: - value = EncodingUtils.readByte(buffer); - break; - case AMQP_SHORT_PROPERTY_PREFIX: - value = EncodingUtils.readShort(buffer); - break; - case AMQP_INT_PROPERTY_PREFIX: - value = EncodingUtils.readInteger(buffer); - break; - case AMQP_UNSIGNED_INT_PROPERTY_PREFIX:// This will only fit in a long - //Change this type for java lookups - type = Prefix.AMQP_LONG_PROPERTY_PREFIX; - case AMQP_LONG_PROPERTY_PREFIX: - value = EncodingUtils.readLong(buffer); - break; - case AMQP_FLOAT_PROPERTY_PREFIX: - value = EncodingUtils.readFloat(buffer); - break; - case AMQP_DOUBLE_PROPERTY_PREFIX: - value = EncodingUtils.readDouble(buffer); - break; - case AMQP_WIDE_STRING_PROPERTY_PREFIX: - // FIXME: use proper charset encoder - case AMQP_ASCII_STRING_PROPERTY_PREFIX: - value = EncodingUtils.readLongString(buffer); - break; - case AMQP_NULL_STRING_PROPERTY_PREFIX: - value = null; - break; - case AMQP_ASCII_CHARACTER_PROPERTY_PREFIX: - value = EncodingUtils.readChar((buffer)); - break; - case AMQP_BINARY_PROPERTY_PREFIX: - value = EncodingUtils.readBytes(buffer); - break; - default: - String msg = "Internal error, the following type identifier is not handled: " + type; - throw new AMQFrameDecodingException(msg); - } - - sizeRead += (sizeRemaining - buffer.remaining()); - - if (trace) - { - _logger.trace("FieldTable::PropFieldTable(buffer," + length + "): Read type '" + type + "', key '" + key + "', value '" + value + "' (now read " + sizeRead + " of " + length + " encoded bytes)..."); - } - - put(type, key, value); - } - - if (trace) - { - _logger.trace("FieldTable::FieldTable(buffer," + length + "): Done."); - } - } - - /** - * @param type the type to calucluate encoding for - * @param value the property value - * @return integer - */ - private static int getEncodingSize(Prefix type, Object value) - { - int encodingSize = 0; - - switch (type) - { - case AMQP_BOOLEAN_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedBooleanLength(); - break; - case AMQP_BYTE_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedByteLength(); - break; - case AMQP_SHORT_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedShortLength(); - break; - case AMQP_INT_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedIntegerLength(); - break; - case AMQP_LONG_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedLongLength(); - break; - case AMQP_FLOAT_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedFloatLength(); - break; - case AMQP_DOUBLE_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedDoubleLength(); - break; - case AMQP_WIDE_STRING_PROPERTY_PREFIX: - // FIXME: use proper charset encoder - case AMQP_ASCII_STRING_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedLongStringLength((String) value); - break; -// This is not required as this method is never called if the value is null -// case AMQP_NULL_STRING_PROPERTY_PREFIX: -// // There is no need for additional size beyond the prefix -// break; - case AMQP_ASCII_CHARACTER_PROPERTY_PREFIX: - encodingSize = EncodingUtils.encodedCharLength(); - break; - case AMQP_BINARY_PROPERTY_PREFIX: - encodingSize = 1 + ((byte[]) value).length; - break; - default: - throw new IllegalArgumentException("Unsupported type in field table: " + value.getClass()); - } - - // the extra byte for the type indicator is calculated in the name - return encodingSize; - } -} diff --git a/java/common/src/test/java/org/apache/qpid/framing/BasicContentHeaderPropertiesTest.java b/java/common/src/test/java/org/apache/qpid/framing/BasicContentHeaderPropertiesTest.java index 66dd1b10ef..ffbdf730a9 100644 --- a/java/common/src/test/java/org/apache/qpid/framing/BasicContentHeaderPropertiesTest.java +++ b/java/common/src/test/java/org/apache/qpid/framing/BasicContentHeaderPropertiesTest.java @@ -31,7 +31,7 @@ public class BasicContentHeaderPropertiesTest extends TestCase { BasicContentHeaderProperties _testProperties; - PropertyFieldTable _testTable; + FieldTable _testTable; String _testString = "This is a test string"; int _testint = 666; @@ -45,11 +45,9 @@ public class BasicContentHeaderPropertiesTest extends TestCase public void setUp() { - HashMap _testMap = new HashMap(10); - _testMap.put("TestString", _testString); - _testMap.put("Testint", _testint); - _testTable = new PropertyFieldTable(); - _testTable.putAll(_testMap); + _testTable = new FieldTable(); + _testTable.setString("TestString", _testString); + _testTable.setInteger("Testint", _testint); _testProperties = new BasicContentHeaderProperties(); _testProperties.setHeaders(_testTable); } @@ -57,7 +55,7 @@ public class BasicContentHeaderPropertiesTest extends TestCase public void testGetPropertyListSize() { //needs a better test but at least we're exercising the code ! - // FT size is encoded in an int + // FT length is encoded in an int int expectedSize = EncodingUtils.encodedIntegerLength(); expectedSize += EncodingUtils.encodedShortStringLength("TestInt"); diff --git a/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java index 9cad31766b..94c97ef808 100644 --- a/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java +++ b/java/common/src/test/java/org/apache/qpid/framing/JMSPropertyFieldTableTest.java @@ -51,7 +51,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testReplacement() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); //Set a boolean value table1.setBoolean("value", true); @@ -73,7 +73,7 @@ public class JMSPropertyFieldTableTest extends TestCase public void testRemoval() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); //Set a boolean value table1.setBoolean("value", true); @@ -99,7 +99,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testBoolean() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setBoolean("value", true); Assert.assertTrue(table1.propertyExists("value")); @@ -184,7 +184,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value will return false @@ -197,7 +197,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testByte() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setByte("value", Byte.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -243,7 +243,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -266,7 +266,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testShort() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setShort("value", Short.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -323,7 +323,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -345,7 +345,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testDouble() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setDouble("value", Double.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -414,7 +414,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -437,7 +437,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testFloat() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setFloat("value", Float.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -500,7 +500,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -522,7 +522,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testInt() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setInteger("value", Integer.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -586,7 +586,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -608,7 +608,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testLong() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setLong("value", Long.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -679,7 +679,7 @@ public class JMSPropertyFieldTableTest extends TestCase //but after a remove it doesn't Assert.assertFalse(table1.propertyExists("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value @@ -700,7 +700,7 @@ public class JMSPropertyFieldTableTest extends TestCase * Calls all methods that can be used to check the table is empty * - getEncodedSize * - isEmpty - * - size + * - length * * @param table to check is empty */ @@ -716,7 +716,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testString() throws JMSException { - JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table1 = new JMSPropertyFieldTable(new FieldTable()); table1.setString("value", "Hello"); Assert.assertTrue(table1.propertyExists("value")); @@ -799,7 +799,7 @@ public class JMSPropertyFieldTableTest extends TestCase public void testValues() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); table.setBoolean("bool", true); table.setDouble("double", Double.MAX_VALUE); table.setFloat("float", Float.MAX_VALUE); @@ -842,7 +842,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameasNull() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); try { @@ -862,7 +862,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameasEmptyString() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); try { @@ -882,7 +882,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNamehasMaxLength() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); StringBuffer longPropertyName = new StringBuffer(129); @@ -910,7 +910,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameStartCharacterIsLetter() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); //Try a name that starts with a number try @@ -931,7 +931,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameContainsInvalidCharacter() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); //Try a name that starts with a number try @@ -953,7 +953,7 @@ public class JMSPropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameIsInvalid() throws JMSException { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); //Try a name that starts with a number try @@ -995,7 +995,7 @@ public class JMSPropertyFieldTableTest extends TestCase public void testSets() { - JMSPropertyFieldTable table = new JMSPropertyFieldTable(); + JMSPropertyFieldTable table = new JMSPropertyFieldTable(new FieldTable()); table.put("n1", "1"); table.put("n2", "2"); diff --git a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java b/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java index 5256c62054..c259d3ee8a 100644 --- a/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java +++ b/java/common/src/test/java/org/apache/qpid/framing/PropertyFieldTableTest.java @@ -37,39 +37,23 @@ public class PropertyFieldTableTest extends TestCase private static final Logger _logger = Logger.getLogger(PropertyFieldTableTest.class); - /** - * Test that modifying a byte[] after setting property doesn't change property - */ - public void testByteModification() - { - PropertyFieldTable table = new PropertyFieldTable(); - byte[] bytes = {99, 98, 97, 96, 95}; - table.setBytes("bytes", bytes); - bytes[0] = 1; - bytes[1] = 2; - bytes[2] = 3; - bytes[3] = 4; - bytes[4] = 5; - - assertBytesNotEqual(bytes, table.getBytes("bytes")); - } /** * Test that setting a similar named value replaces any previous value set on that name */ public void testReplacement() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); //Set a boolean value table1.setBoolean("value", true); - //Check size of table is correct (<Value length> + <type> + <Boolean length>) + //Check length of table is correct (<Value length> + <type> + <Boolean length>) int size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedBooleanLength(); Assert.assertEquals(size, table1.getEncodedSize()); // reset value to an integer table1.setInteger("value", Integer.MAX_VALUE); - // Check the size has changed accordingly (<Value length> + <type> + <Integer length>) + // Check the length has changed accordingly (<Value length> + <type> + <Integer length>) size = EncodingUtils.encodedShortStringLength("value") + 1 + EncodingUtils.encodedIntegerLength(); Assert.assertEquals(size, table1.getEncodedSize()); @@ -86,7 +70,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testBoolean() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setBoolean("value", true); Assert.assertTrue(table1.propertyExists("value")); @@ -107,19 +91,9 @@ public class PropertyFieldTableTest extends TestCase //except value as a string Assert.assertEquals("true", table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_BOOLEAN_PROPERTY_PREFIX, "value", null); - - // Should be able to get the null back - Assert.assertEquals(null, table1.getBoolean("value")); - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); - //but after a remove it doesn't - Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -132,7 +106,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testByte() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setByte("value", Byte.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -151,20 +125,8 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Byte.MAX_VALUE, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_BYTE_PROPERTY_PREFIX, "value", null); - - // Should be able to get the null back - Assert.assertEquals(null, table1.getByte("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); - //but after a remove it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -177,7 +139,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testShort() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setShort("value", Short.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -196,20 +158,8 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Short.MAX_VALUE, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_SHORT_PROPERTY_PREFIX, "value", null); - - // Should be able to get the null back - Assert.assertEquals(null, table1.getShort("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); - //but after a remove it doesn't - Assert.assertFalse(table1.containsKey("value")); - - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -223,7 +173,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testChar() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setChar("value", 'c'); Assert.assertTrue(table1.propertyExists("value")); @@ -242,26 +192,9 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("c", table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_ASCII_CHARACTER_PROPERTY_PREFIX, "value", null); - - try - { - table1.getString("value"); - fail("Should throw NullPointerException"); - } - catch (NullPointerException npe) - { - //Normal Path - } - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); - //but after a remove it doesn't - Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -275,7 +208,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testDouble() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setDouble("value", Double.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -293,20 +226,11 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Double.MAX_VALUE, table1.getString("value")); - - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_DOUBLE_PROPERTY_PREFIX, "value", null); - - Assert.assertEquals(null, table1.getDouble("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); //but after a remove it doesn't Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -320,7 +244,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testFloat() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setFloat("value", Float.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -339,19 +263,12 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Float.MAX_VALUE, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_FLOAT_PROPERTY_PREFIX, "value", null); - - Assert.assertEquals(null, table1.getFloat("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); table1.remove("value"); //but after a remove it doesn't Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -365,7 +282,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testInt() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setInteger("value", Integer.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -384,19 +301,12 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Integer.MAX_VALUE, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_INT_PROPERTY_PREFIX, "value", null); - - Assert.assertEquals(null, table1.getInteger("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); table1.remove("value"); //but after a remove it doesn't Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -410,7 +320,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testLong() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setLong("value", Long.MAX_VALUE); Assert.assertTrue(table1.propertyExists("value")); @@ -429,19 +339,12 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it. Assert.assertEquals("" + Long.MAX_VALUE, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_LONG_PROPERTY_PREFIX, "value", null); - - Assert.assertEquals(null, table1.getLong("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); table1.remove("value"); //but after a remove it doesn't Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -457,7 +360,7 @@ public class PropertyFieldTableTest extends TestCase { byte[] bytes = {99, 98, 97, 96, 95}; - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setBytes("value", bytes); Assert.assertTrue(table1.propertyExists("value")); @@ -476,19 +379,11 @@ public class PropertyFieldTableTest extends TestCase //... and a the string value of it is null Assert.assertEquals(null, table1.getString("value")); - //Try setting a null value and read it back - table1.put(PropertyFieldTable.Prefix.AMQP_BINARY_PROPERTY_PREFIX, "value", null); - - Assert.assertEquals(null, table1.getBytes("value")); - - //but still contains the value - Assert.assertTrue(table1.containsKey("value")); - table1.remove("value"); //but after a remove it doesn't Assert.assertFalse(table1.containsKey("value")); - // Table should now have zero size for encoding + // Table should now have zero length for encoding checkEmpty(table1); //Looking up an invalid value returns null @@ -499,19 +394,17 @@ public class PropertyFieldTableTest extends TestCase * Calls all methods that can be used to check the table is empty * - getEncodedSize * - isEmpty - * - size + * - length * * @param table to check is empty */ - private void checkEmpty(PropertyFieldTable table) + private void checkEmpty(FieldTable table) { Assert.assertEquals(0, table.getEncodedSize()); Assert.assertTrue(table.isEmpty()); Assert.assertEquals(0, table.size()); Assert.assertEquals(0, table.keySet().size()); - Assert.assertEquals(0, table.values().size()); - Assert.assertEquals(0, table.entrySet().size()); } @@ -521,7 +414,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testString() { - PropertyFieldTable table1 = new PropertyFieldTable(); + FieldTable table1 = new FieldTable(); table1.setString("value", "Hello"); Assert.assertTrue(table1.propertyExists("value")); @@ -562,79 +455,11 @@ public class PropertyFieldTableTest extends TestCase } - /** - * Test that the generated XML can be used to create a field table with the same values. - */ - public void testValidXML() - { - PropertyFieldTable table1 = new PropertyFieldTable(); - table1.setBoolean("bool", true); - table1.setByte("byte", Byte.MAX_VALUE); - byte[] bytes = {99, 98, 97, 96, 95}; - table1.setBytes("bytes", bytes); - table1.setChar("char", 'c'); - table1.setDouble("double", Double.MAX_VALUE); - table1.setFloat("float", Float.MAX_VALUE); - table1.setInteger("int", Integer.MAX_VALUE); - table1.setLong("long", Long.MAX_VALUE); - table1.setShort("short", Short.MAX_VALUE); - table1.setString("string", "Hello"); - table1.setString("null-string", null); - - table1.setObject("object-bool", true); - table1.setObject("object-byte", Byte.MAX_VALUE); - table1.setObject("object-bytes", bytes); - table1.setObject("object-char", 'c'); - table1.setObject("object-double", Double.MAX_VALUE); - table1.setObject("object-float", Float.MAX_VALUE); - table1.setObject("object-int", Integer.MAX_VALUE); - table1.setObject("object-long", Long.MAX_VALUE); - table1.setObject("object-short", Short.MAX_VALUE); - table1.setObject("object-string", "Hello"); - - Assert.assertEquals(21, table1.size()); - - String table1XML = table1.toString(); - - PropertyFieldTable table2 = new PropertyFieldTable(table1XML); - - Assert.assertEquals(table1XML, table2.toString()); - - //Check that when bytes is written out as a string with no new line between items that it is read in ok. - - } - - /** - * Test that invalid input throws the correct Exception - */ - public void testInvalidXML() - { - try - { - _logger.warn("Testing Invalid XML expecting IllegalArgumentException"); - new PropertyFieldTable("Rubbish"); - fail("IllegalArgumentException expected"); - } - catch (IllegalArgumentException iae) - { - //normal path - } - try - { - _logger.warn("Testing Invalid XML expecting IllegalArgumentException"); - new PropertyFieldTable(""); - fail("IllegalArgumentException expected"); - } - catch (IllegalArgumentException iae) - { - //normal path - } - } - + public void testKeyEnumeration() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); table.setLong("one", 1L); table.setLong("two", 2L); table.setLong("three", 3L); @@ -652,7 +477,7 @@ public class PropertyFieldTableTest extends TestCase public void testValues() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); table.setBoolean("bool", true); table.setByte("byte", Byte.MAX_VALUE); byte[] bytes = {99, 98, 97, 96, 95}; @@ -707,7 +532,7 @@ public class PropertyFieldTableTest extends TestCase { byte[] bytes = {99, 98, 97, 96, 95}; - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); table.setBoolean("bool", true); table.setByte("byte", Byte.MAX_VALUE); @@ -732,7 +557,7 @@ public class PropertyFieldTableTest extends TestCase try { - PropertyFieldTable table2 = new PropertyFieldTable(buffer, length); + FieldTable table2 = new FieldTable(buffer, length); Assert.assertEquals((Boolean) true, table2.getBoolean("bool")); Assert.assertEquals((Byte) Byte.MAX_VALUE, table2.getByte("byte")); @@ -756,7 +581,7 @@ public class PropertyFieldTableTest extends TestCase public void testEncodingSize() { - PropertyFieldTable result = new PropertyFieldTable(); + FieldTable result = new FieldTable(); int size = 0; result.setBoolean("boolean", true); @@ -847,31 +672,31 @@ public class PropertyFieldTableTest extends TestCase // public void testEncodingSize1() // { // PropertyFieldTable table = new PropertyFieldTable(); -// int size = 0; +// int length = 0; // result.put("one", 1L); -// size = EncodingUtils.encodedShortStringLength("one"); -// size += 1 + EncodingUtils.encodedLongLength(); -// assertEquals(size, result.getEncodedSize()); +// length = EncodingUtils.encodedShortStringLength("one"); +// length += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(length, result.getEncodedSize()); // // result.put("two", 2L); -// size += EncodingUtils.encodedShortStringLength("two"); -// size += 1 + EncodingUtils.encodedLongLength(); -// assertEquals(size, result.getEncodedSize()); +// length += EncodingUtils.encodedShortStringLength("two"); +// length += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(length, result.getEncodedSize()); // // result.put("three", 3L); -// size += EncodingUtils.encodedShortStringLength("three"); -// size += 1 + EncodingUtils.encodedLongLength(); -// assertEquals(size, result.getEncodedSize()); +// length += EncodingUtils.encodedShortStringLength("three"); +// length += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(length, result.getEncodedSize()); // // result.put("four", 4L); -// size += EncodingUtils.encodedShortStringLength("four"); -// size += 1 + EncodingUtils.encodedLongLength(); -// assertEquals(size, result.getEncodedSize()); +// length += EncodingUtils.encodedShortStringLength("four"); +// length += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(length, result.getEncodedSize()); // // result.put("five", 5L); -// size += EncodingUtils.encodedShortStringLength("five"); -// size += 1 + EncodingUtils.encodedLongLength(); -// assertEquals(size, result.getEncodedSize()); +// length += EncodingUtils.encodedShortStringLength("five"); +// length += 1 + EncodingUtils.encodedLongLength(); +// assertEquals(length, result.getEncodedSize()); // // //fixme should perhaps be expanded to incorporate all types. // @@ -907,7 +732,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testSetObject() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); //Try setting a non primative object @@ -920,7 +745,7 @@ public class PropertyFieldTableTest extends TestCase { //normal path } - // so size should be zero + // so length should be zero Assert.assertEquals(0, table.getEncodedSize()); } @@ -929,7 +754,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameasNull() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); try { @@ -940,7 +765,7 @@ public class PropertyFieldTableTest extends TestCase { //normal path } - // so size should be zero + // so length should be zero Assert.assertEquals(0, table.getEncodedSize()); } @@ -950,7 +775,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameasEmptyString() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); try { @@ -961,7 +786,7 @@ public class PropertyFieldTableTest extends TestCase { //normal path } - // so size should be zero + // so length should be zero Assert.assertEquals(0, table.getEncodedSize()); } @@ -971,7 +796,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testCheckPropertyNamehasMaxLength() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); StringBuffer longPropertyName = new StringBuffer(129); @@ -989,7 +814,7 @@ public class PropertyFieldTableTest extends TestCase { //normal path } - // so size should be zero + // so length should be zero Assert.assertEquals(0, table.getEncodedSize()); } @@ -999,7 +824,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameStartCharacterIsLetter() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); //Try a name that starts with a number try @@ -1011,7 +836,7 @@ public class PropertyFieldTableTest extends TestCase { //normal path } - // so size should be zero + // so length should be zero Assert.assertEquals(0, table.getEncodedSize()); } @@ -1021,7 +846,7 @@ public class PropertyFieldTableTest extends TestCase */ public void testCheckPropertyNameStartCharacterIsHashorDollar() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); //Try a name that starts with a number try @@ -1041,12 +866,10 @@ public class PropertyFieldTableTest extends TestCase */ public void testContents() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); table.put("StringProperty", "String"); - Assert.assertTrue(table.containsValue("String")); - Assert.assertEquals("String", table.get("StringProperty")); //Test Clear @@ -1062,7 +885,7 @@ public class PropertyFieldTableTest extends TestCase public void testSets() { - PropertyFieldTable table = new PropertyFieldTable(); + FieldTable table = new FieldTable(); table.put("n1", "1"); table.put("n2", "2"); @@ -1075,64 +898,10 @@ public class PropertyFieldTableTest extends TestCase Assert.assertFalse(iterator.hasNext()); - iterator = table.values().iterator(); - Assert.assertEquals("1", iterator.next()); - Assert.assertEquals("2", iterator.next()); - Assert.assertEquals("3", iterator.next()); - Assert.assertFalse(iterator.hasNext()); - - - iterator = table.entrySet().iterator(); - Map.Entry entry = (Map.Entry) iterator.next(); - Assert.assertEquals("n1", entry.getKey()); - Assert.assertEquals("1", entry.getValue()); - entry = (Map.Entry) iterator.next(); - Assert.assertEquals("n2", entry.getKey()); - Assert.assertEquals("2", entry.getValue()); - entry = (Map.Entry) iterator.next(); - Assert.assertEquals("n3", entry.getKey()); - Assert.assertEquals("3", entry.getValue()); - Assert.assertFalse(iterator.hasNext()); - } - /** - * Test that all the values are preserved after a putAll - */ - public void testPutAll() - { - Map map = new HashMap(); - - map.put("char", 'c'); - map.put("double", Double.MAX_VALUE); - map.put("float", Float.MAX_VALUE); - map.put("int", Integer.MAX_VALUE); - map.put("long", Long.MAX_VALUE); - map.put("short", Short.MAX_VALUE); - - PropertyFieldTable table = new PropertyFieldTable(); - - table.putAll(map); - - Assert.assertEquals(6, table.size()); - - Assert.assertTrue(table.containsKey("char")); - Assert.assertEquals('c', (char) table.getCharacter("char")); - Assert.assertTrue(table.containsKey("double")); - Assert.assertEquals(Double.MAX_VALUE, table.getDouble("double")); - Assert.assertTrue(table.containsKey("float")); - Assert.assertEquals(Float.MAX_VALUE, table.getFloat("float")); - Assert.assertTrue(table.containsKey("int")); - Assert.assertEquals(Integer.MAX_VALUE, (int) table.getInteger("int")); - Assert.assertTrue(table.containsKey("long")); - Assert.assertEquals(Long.MAX_VALUE, (long) table.getLong("long")); - Assert.assertTrue(table.containsKey("short")); - Assert.assertEquals(Short.MAX_VALUE, (short) table.getShort("short")); - Assert.assertEquals(Short.MAX_VALUE, (short) table.getShort("short")); - } - private void assertBytesEqual(byte[] expected, byte[] actual) { |