diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/AbstractCommand.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/AbstractCommand.cs index da83bf85a3..47e7e6be56 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/AbstractCommand.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/AbstractCommand.cs @@ -15,11 +15,10 @@ * limitations under the License. */ using System; -using OpenWire.Client; -using OpenWire.Client.Commands; -using OpenWire.Client.Core; +using ActiveMQ.OpenWire; +using ActiveMQ.OpenWire.Commands; -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire { /// /// Summary description for AbstractCommand. diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/BaseDataStreamMarshaller.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/BaseDataStreamMarshaller.cs index ccbb59141f..fc1c87fa50 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/BaseDataStreamMarshaller.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/BaseDataStreamMarshaller.cs @@ -1,1025 +1,1025 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System; -using System.IO; -using System.Net; -using System.Text; - -using OpenWire.Client.Commands; -using OpenWire.Client.Core; -using OpenWire.Client.IO; -using System.Collections; - -namespace OpenWire.Client.Core -{ - /// - /// A base class with useful implementation inheritence methods - /// for creating marshallers of the OpenWire protocol - /// - public abstract class BaseDataStreamMarshaller - { - public const byte NULL = 0; - public const byte BOOLEAN_TYPE = 1; - public const byte BYTE_TYPE = 2; - public const byte CHAR_TYPE = 3; - public const byte SHORT_TYPE = 4; - public const byte INTEGER_TYPE = 5; - public const byte LONG_TYPE = 6; - public const byte DOUBLE_TYPE = 7; - public const byte FLOAT_TYPE = 8; - public const byte STRING_TYPE = 9; - public const byte BYTE_ARRAY_TYPE = 10; - - private static String[] HEX_TABLE = new String[]{ - "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", - "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", - "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", - "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", - "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", - "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", - "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", - "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", - "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", - "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", - "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", - "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", - "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", - "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", - "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", - "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff", - }; - - public abstract DataStructure CreateObject(); - public abstract byte GetDataStructureType(); - - public virtual int TightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) - { - return 0; - } - public virtual void TightMarshal2( - OpenWireFormat wireFormat, - Object o, - BinaryWriter dataOut, - BooleanStream bs) - { - } - - public virtual void TightUnmarshal( - OpenWireFormat wireFormat, - Object o, - BinaryReader dataIn, - BooleanStream bs) - { - } - - - protected virtual DataStructure TightUnmarshalNestedObject( - OpenWireFormat wireFormat, - BinaryReader dataIn, - BooleanStream bs) - { - return wireFormat.TightUnmarshalNestedObject(dataIn, bs); - } - - protected virtual int TightMarshalNestedObject1( - OpenWireFormat wireFormat, - DataStructure o, - BooleanStream bs) - { - return wireFormat.TightMarshalNestedObject1(o, bs); - } - - protected virtual void TightMarshalNestedObject2( - OpenWireFormat wireFormat, - DataStructure o, - BinaryWriter dataOut, - BooleanStream bs) - { - wireFormat.TightMarshalNestedObject2(o, dataOut, bs); - } - - protected virtual DataStructure TightUnmarshalCachedObject( - OpenWireFormat wireFormat, - BinaryReader dataIn, - BooleanStream bs) - { - /* - if (wireFormat.isCacheEnabled()) { - if (bs.ReadBoolean()) { - short index = dataInReadShort(dataIn)Int16(); - DataStructure value = wireFormat.UnmarshalNestedObject(dataIn, bs); - wireFormat.setInUnmarshallCache(index, value); - return value; - } else { - short index = ReadShort(dataIn); - return wireFormat.getFromUnmarshallCache(index); - } - } else { - return wireFormat.UnmarshalNestedObject(dataIn, bs); - } - */ - return wireFormat.TightUnmarshalNestedObject(dataIn, bs); - } - - protected virtual int TightMarshalCachedObject1( - OpenWireFormat wireFormat, - DataStructure o, - BooleanStream bs) - { - /* - if (wireFormat.isCacheEnabled()) { - Short index = wireFormat.getMarshallCacheIndex(o); - bs.WriteBoolean(index == null); - if (index == null) { - int rc = wireFormat.Marshal1NestedObject(o, bs); - wireFormat.addToMarshallCache(o); - return 2 + rc; - } else { - return 2; - } - } else { - return wireFormat.Marshal1NestedObject(o, bs); - } - */ - return wireFormat.TightMarshalNestedObject1(o, bs); - } - - protected virtual void TightMarshalCachedObject2( - OpenWireFormat wireFormat, - DataStructure o, - BinaryWriter dataOut, - BooleanStream bs) - { - /* - if (wireFormat.isCacheEnabled()) { - Short index = wireFormat.getMarshallCacheIndex(o); - if (bs.ReadBoolean()) { - WriteShort(index.shortValue(), dataOut); - wireFormat.Marshal2NestedObject(o, dataOut, bs); - } else { - WriteShort(index.shortValue(), dataOut); - } - } else { - wireFormat.Marshal2NestedObject(o, dataOut, bs); - } - */ - wireFormat.TightMarshalNestedObject2(o, dataOut, bs); - } - - - - protected virtual String TightUnmarshalString(BinaryReader dataIn, BooleanStream bs) - { - if (bs.ReadBoolean()) - { - if (bs.ReadBoolean()) - { - return ReadAsciiString(dataIn); - } - else - { - return ReadUTF8(dataIn); - } - } - else - { - return null; - } - } - - protected virtual String ReadAsciiString(BinaryReader dataIn) - { - int size = ReadShort(dataIn); - byte[] data = new byte[size]; - dataIn.Read(data, 0, size); - char[] text = new char[size]; - for (int i = 0; i < size; i++) - { - text[i] = (char) data[i]; - } - return new String(text); - } - - protected virtual int TightMarshalString1(String value, BooleanStream bs) - { - bs.WriteBoolean(value != null); - if (value != null) - { - int strlen = value.Length; - - // TODO until we get UTF8 working, lets just force ASCII - bs.WriteBoolean(true); - return strlen + 2; - - - /* - int utflen = 0; - int c = 0; - bool isOnlyAscii = true; - char[] charr = value.ToCharArray(); - for (int i = 0; i < strlen; i++) - { - c = charr[i]; - if ((c >= 0x0001) && (c <= 0x007F)) - { - utflen++; - } - else if (c > 0x07FF) - { - utflen += 3; - isOnlyAscii = false; - } - else - { - isOnlyAscii = false; - utflen += 2; - } - } - - if (utflen >= Int16.MaxValue) - throw new IOException("Encountered a String value that is too long to encode."); - - bs.WriteBoolean(isOnlyAscii); - return utflen + 2; - */ - } - else - { - return 0; - } - } - - public static void TightMarshalString2(String value, BinaryWriter dataOut, BooleanStream bs) - { - if (bs.ReadBoolean()) - { - // If we verified it only holds ascii values - if (bs.ReadBoolean()) - { - WriteShort((short) value.Length, dataOut); - // now lets write the bytes - char[] chars = value.ToCharArray(); - for (int i = 0; i < chars.Length; i++) - { - WriteByte((byte) chars[i], dataOut); - } - } - else - { - // TODO how should we properly write a String so that Java will grok it??? - dataOut.Write(value); - } - } - } - - public static byte ReadByte(BinaryReader dataIn) - { - return dataIn.ReadByte(); - } - - public static char ReadChar(BinaryReader dataIn) - { - return (char) ReadShort(dataIn); - } - - public static short ReadShort(BinaryReader dataIn) - { - return SwitchEndian(dataIn.ReadInt16()); - } - - public static int ReadInt(BinaryReader dataIn) - { - return SwitchEndian(dataIn.ReadInt32()); - } - - public static long ReadLong(BinaryReader dataIn) - { - return SwitchEndian(dataIn.ReadInt64()); - } - - public static void WriteByte(byte value, BinaryWriter dataOut) - { - dataOut.Write(value); - } - - public static void WriteChar(char value, BinaryWriter dataOut) - { - dataOut.Write(SwitchEndian((short) value)); - } - - public static void WriteShort(short value, BinaryWriter dataOut) - { - dataOut.Write(SwitchEndian(value)); - } - - public static void WriteInt(int value, BinaryWriter dataOut) - { - dataOut.Write(SwitchEndian(value)); - } - - public static void WriteLong(long value, BinaryWriter dataOut) - { - dataOut.Write(SwitchEndian(value)); - } - - - /// - /// Switches from one endian to the other - /// - public static int SwitchEndian(int x) - { - return ((x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24)); - } - - public static short SwitchEndian(short x) - { - int low = x & 0xff; - int high = x & 0xff00; - return(short)(high >> 8 | low << 8); - } - - public static long SwitchEndian(long x) - { - long answer = 0; - for (int i = 0; i < 8; i++) - { - long lowest = x & 0xff; - x >>= 8; - answer <<= 8; - answer += lowest; - } - return answer; - } - - public virtual int TightMarshalLong1(OpenWireFormat wireFormat, long o, BooleanStream bs) - { - if (o == 0L) - { - bs.WriteBoolean(false); - bs.WriteBoolean(false); - return 0; - } - else - { - ulong ul = (ulong) o; - if ((ul & 0xFFFFFFFFFFFF0000ul) == 0L) - { - bs.WriteBoolean(false); - bs.WriteBoolean(true); - return 2; - } - else if ((ul & 0xFFFFFFFF00000000ul) == 0L) - { - bs.WriteBoolean(true); - bs.WriteBoolean(false); - return 4; - } - else - { - bs.WriteBoolean(true); - bs.WriteBoolean(true); - return 8; - } - } - } - - public virtual void TightMarshalLong2( - OpenWireFormat wireFormat, - long o, - BinaryWriter dataOut, - BooleanStream bs) - { - if (bs.ReadBoolean()) - { - if (bs.ReadBoolean()) - { - WriteLong(o, dataOut); - } - else - { - WriteInt((int) o, dataOut); - } - } - else - { - if (bs.ReadBoolean()) - { - WriteShort((short) o, dataOut); - } - } - } - public virtual long TightUnmarshalLong(OpenWireFormat wireFormat, BinaryReader dataIn, BooleanStream bs) - { - if (bs.ReadBoolean()) - { - if (bs.ReadBoolean()) - { - return ReadLong(dataIn); - } - else - { - return ReadInt(dataIn); - } - } - else - { - if (bs.ReadBoolean()) - { - return ReadShort(dataIn); - } - else - { - return 0; - } - } - } - protected virtual int TightMarshalObjectArray1( - OpenWireFormat wireFormat, - DataStructure[] objects, - BooleanStream bs) - { - if (objects != null) - { - int rc = 0; - bs.WriteBoolean(true); - rc += 2; - for (int i = 0; i < objects.Length; i++) - { - rc += TightMarshalNestedObject1(wireFormat, objects[i], bs); - } - return rc; - } - else - { - bs.WriteBoolean(false); - return 0; - } - } - - protected virtual void TightMarshalObjectArray2( - OpenWireFormat wireFormat, - DataStructure[] objects, - BinaryWriter dataOut, - BooleanStream bs) - { - if (bs.ReadBoolean()) - { - WriteShort((short) objects.Length, dataOut); - for (int i = 0; i < objects.Length; i++) - { - TightMarshalNestedObject2(wireFormat, objects[i], dataOut, bs); - } - } - } - - protected virtual byte[] ReadBytes(BinaryReader dataIn, bool flag) - { - if (flag) - { - int size = ReadInt(dataIn); - return dataIn.ReadBytes(size); - } - else - { - return null; - } - } - - protected virtual byte[] ReadBytes(BinaryReader dataIn) - { - int size = ReadInt(dataIn); - return dataIn.ReadBytes(size); - } - - protected virtual byte[] ReadBytes(BinaryReader dataIn, int size) - { - return dataIn.ReadBytes(size); - } - - protected virtual void WriteBytes(byte[] command, BinaryWriter dataOut) - { - WriteInt(command.Length, dataOut); - dataOut.Write(command); - } - - protected virtual BrokerError TightUnmarshalBrokerError( - OpenWireFormat wireFormat, - BinaryReader dataIn, - BooleanStream bs) - { - if (bs.ReadBoolean()) - { - BrokerError answer = new BrokerError(); - - answer.ExceptionClass = TightUnmarshalString(dataIn, bs); - answer.Message = TightUnmarshalString(dataIn, bs); - if (wireFormat.StackTraceEnabled) - { - short length = ReadShort(dataIn); - StackTraceElement[] stackTrace = new StackTraceElement[length]; - for (int i = 0; i < stackTrace.Length; i++) - { - StackTraceElement element = new StackTraceElement(); - element.ClassName = TightUnmarshalString(dataIn, bs); - element.MethodName = TightUnmarshalString(dataIn, bs); - element.FileName = TightUnmarshalString(dataIn, bs); - element.LineNumber = ReadInt(dataIn); - stackTrace[i] = element; - } - answer.StackTraceElements = stackTrace; - answer.Cause = TightUnmarshalBrokerError(wireFormat, dataIn, bs); - } - return answer; - } - else - { - return null; - } - } - - protected int TightMarshalBrokerError1(OpenWireFormat wireFormat, BrokerError o, BooleanStream bs) - { - if (o == null) - { - bs.WriteBoolean(false); - return 0; - } - else - { - int rc = 0; - bs.WriteBoolean(true); - rc += TightMarshalString1(o.ExceptionClass, bs); - rc += TightMarshalString1(o.Message, bs); - if (wireFormat.StackTraceEnabled) - { - rc += 2; - StackTraceElement[] stackTrace = o.StackTraceElements; - for (int i = 0; i < stackTrace.Length; i++) - { - StackTraceElement element = stackTrace[i]; - rc += TightMarshalString1(element.ClassName, bs); - rc += TightMarshalString1(element.MethodName, bs); - rc += TightMarshalString1(element.FileName, bs); - rc += 4; - } - rc += TightMarshalBrokerError1(wireFormat, o.Cause, bs); - } - - return rc; - } - } - - protected void TightMarshalBrokerError2( - OpenWireFormat wireFormat, - BrokerError o, - BinaryWriter dataOut, - BooleanStream bs) - { - if (bs.ReadBoolean()) - { - TightMarshalString2(o.ExceptionClass, dataOut, bs); - TightMarshalString2(o.Message, dataOut, bs); - if (wireFormat.StackTraceEnabled) - { - StackTraceElement[] stackTrace = o.StackTraceElements; - WriteShort((short) stackTrace.Length, dataOut); - - for (int i = 0; i < stackTrace.Length; i++) - { - StackTraceElement element = stackTrace[i]; - TightMarshalString2(element.ClassName, dataOut, bs); - TightMarshalString2(element.MethodName, dataOut, bs); - TightMarshalString2(element.FileName, dataOut, bs); - WriteInt(element.LineNumber, dataOut); - } - TightMarshalBrokerError2(wireFormat, o.Cause, dataOut, bs); - } - } - } - - /// - /// Marshals the primitive type map to a byte array - /// - public static byte[] MarshalPrimitiveMap(IDictionary map) - { - if (map == null) - { - return null; - } - else - { - MemoryStream memoryStream = new MemoryStream(); - MarshalPrimitiveMap(map, new BinaryWriter(memoryStream)); - return memoryStream.GetBuffer(); - } - } - public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut) - { - if (map == null) - { - WriteInt(-1, dataOut); - } - else - { - WriteInt(map.Count, dataOut); - foreach (DictionaryEntry entry in map) - { - String name = (String) entry.Key; - WriteUTF8(name, dataOut); - Object value = entry.Value; - MarshalPrimitive(dataOut, value); - } - }} - - - - /// - /// Unmarshals the primitive type map from the given byte array - /// - public static IDictionary UnmarshalPrimitiveMap(byte[] data) - { - if (data == null) - { - return new Hashtable(); - } - else - { - return UnmarshalPrimitiveMap(new BinaryReader(new MemoryStream(data))); - } - } - - public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn) - { - int size = ReadInt(dataIn); - if (size < 0) - { - return null; - } - else - { - IDictionary answer = new Hashtable(size); - for (int i=0; i < size; i++) - { - String name = ReadUTF8(dataIn); - answer[name] = UnmarshalPrimitive(dataIn); - } - return answer; - } - - } - - public static void MarshalPrimitive(BinaryWriter dataOut, Object value) - { - if (value == null) - { - WriteByte(NULL, dataOut); - } - else if (value is bool) - { - WriteByte(BOOLEAN_TYPE, dataOut); - WriteBoolean((bool) value, dataOut); - } - else if (value is byte) - { - WriteByte(BYTE_TYPE, dataOut); - WriteByte(((Byte)value), dataOut); - } - else if (value is char) - { - WriteByte(CHAR_TYPE, dataOut); - WriteChar((char) value, dataOut); - } - else if (value is short) - { - WriteByte(SHORT_TYPE, dataOut); - WriteShort((short) value, dataOut); - } - else if (value is int) - { - WriteByte(INTEGER_TYPE, dataOut); - WriteInt((int) value, dataOut); - } - else if (value is long) - { - WriteByte(LONG_TYPE, dataOut); - WriteLong((long) value, dataOut); - } - else if (value is float) - { - WriteByte(FLOAT_TYPE, dataOut); - WriteFloat((float) value, dataOut); - } - else if (value is double) - { - WriteByte(DOUBLE_TYPE, dataOut); - WriteDouble((double) value, dataOut); - } - else if (value is byte[]) - { - byte[] data = (byte[]) value; - WriteByte(BYTE_ARRAY_TYPE, dataOut); - WriteInt(data.Length, dataOut); - dataOut.Write(data); - } - else if (value is string) - { - WriteByte(STRING_TYPE, dataOut); - WriteUTF8((string) value, dataOut); - } - else - { - throw new IOException("Object is not a primitive: " + value); - } - } - - public static Object UnmarshalPrimitive(BinaryReader dataIn) - { - Object value=null; - switch (ReadByte(dataIn)) - { - case BYTE_TYPE: - value = ReadByte(dataIn); - break; - case BOOLEAN_TYPE: - value = ReadBoolean(dataIn); - break; - case CHAR_TYPE: - value = ReadChar(dataIn); - break; - case SHORT_TYPE: - value = ReadShort(dataIn); - break; - case INTEGER_TYPE: - value = ReadInt(dataIn); - break; - case LONG_TYPE: - value = ReadLong(dataIn); - break; - case FLOAT_TYPE: - value = ReadFloat(dataIn); - break; - case DOUBLE_TYPE: - value = ReadDouble(dataIn); - break; - case BYTE_ARRAY_TYPE: - int size = ReadInt(dataIn); - byte[] data = new byte[size]; - dataIn.Read(data, 0, size); - value = data; - break; - case STRING_TYPE: - value = ReadUTF8(dataIn); - break; - } - return value; - } - - private static Object ReadDouble(BinaryReader dataIn) - { - // TODO: Implement this method - return dataIn.ReadDouble(); - } - - /// - /// Method ReadFloat - /// - private static Object ReadFloat(BinaryReader dataIn) - { - // TODO: Implement this method - return (float) dataIn.ReadDouble(); - } - - private static Object ReadBoolean(BinaryReader dataIn) - { - // TODO: Implement this method - return dataIn.ReadBoolean(); - } - - private static void WriteDouble(double value, BinaryWriter dataOut) - { - // TODO: Implement this method - dataOut.Write(value); - } - - private static void WriteFloat(float value, BinaryWriter dataOut) - { - // TODO: Implement this method - dataOut.Write(value); - } - - private static void WriteBoolean(bool value, BinaryWriter dataOut) - { - // TODO: Implement this method - dataOut.Write(value); - } - - - public static void WriteUTF8(String text, BinaryWriter dataOut) - { - if (text != null) - { - int strlen = text.Length; - int utflen = 0; - int c, count = 0; - - char[] charr = text.ToCharArray(); - - for (int i = 0; i < strlen; i++) - { - c = charr[i]; - if ((c >= 0x0001) && (c <= 0x007F)) - { - utflen++; - } - else if (c > 0x07FF) - { - utflen += 3; - } - else - { - utflen += 2; - } - } - - WriteInt(utflen, dataOut); - byte[] bytearr = new byte[utflen]; - /* - byte[] bytearr = new byte[utflen + 4]; - bytearr[count++] = (byte) ((utflen >>> 24) & 0xFF); - bytearr[count++] = (byte) ((utflen >>> 16) & 0xFF); - bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); - bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); - */ - for (int i = 0; i < strlen; i++) - { - c = charr[i]; - if ((c >= 0x0001) && (c <= 0x007F)) - { - bytearr[count++] = (byte) c; - } - else if (c > 0x07FF) - { - bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); - bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); - bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); - } - else - { - bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); - bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); - } - } - dataOut.Write(bytearr); - - } - else - { - WriteInt(-1, dataOut); - } - } - - public static String ReadUTF8(BinaryReader dataIn) - { - int utflen = ReadInt(dataIn); - if (utflen > -1) - { - StringBuilder str = new StringBuilder(utflen); - - byte[] bytearr = new byte[utflen]; - int c, char2, char3; - int count = 0; - - dataIn.Read(bytearr, 0, utflen); - - while (count < utflen) - { - c = bytearr[count] & 0xff; - switch (c >> 4) - { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - count++; - str.Append((char) c); - break; - case 12: - case 13: - /* 110x xxxx 10xx xxxx */ - count += 2; - if (count > utflen) - { - throw CreateDataFormatException(); - } - char2 = bytearr[count - 1]; - if ((char2 & 0xC0) != 0x80) - { - throw CreateDataFormatException(); - } - str.Append((char) (((c & 0x1F) << 6) | (char2 & 0x3F))); - break; - case 14: - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - count += 3; - if (count > utflen) - { - throw CreateDataFormatException(); - } - char2 = bytearr[count - 2]; - char3 = bytearr[count - 1]; - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - { - throw CreateDataFormatException(); - } - str.Append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0))); - break; - default : - /* 10xx xxxx, 1111 xxxx */ - throw CreateDataFormatException(); - } - } -// The number of chars produced may be less than utflen - return str.ToString(); - } - else - { - return null; - } - } - - private static Exception CreateDataFormatException() - { - // TODO: implement a better exception - return new Exception("Data format error!"); - } - - - /// - /// Converts the object to a String - /// - public static string ToString(MessageId id) - { - return ToString(id.ProducerId) + ":" + id.ProducerSequenceId; - } - /// - /// Converts the object to a String - /// - public static string ToString(ProducerId id) - { - return id.ConnectionId + ":" + id.SessionId + ":" + id.Value; - } - - - /// - /// Converts the given transaction ID into a String - /// - public static String ToString(TransactionId txnId) - { - if (txnId is LocalTransactionId) - { - LocalTransactionId ltxnId = (LocalTransactionId) txnId; - return "" + ltxnId.Value; - } - else if (txnId is XATransactionId) - { - XATransactionId xaTxnId = (XATransactionId) txnId; - return "XID:" + xaTxnId.FormatId + ":" + ToHexFromBytes(xaTxnId.GlobalTransactionId) + ":" + ToHexFromBytes(xaTxnId.BranchQualifier); - } - return null; - } - - /// - /// Creates the byte array into hexidecimal - /// - public static String ToHexFromBytes(byte[] data) - { - StringBuilder buffer = new StringBuilder(data.Length * 2); - for (int i = 0; i < data.Length; i++) - { - buffer.Append(HEX_TABLE[0xFF & data[i]]); - } - return buffer.ToString(); - } - - } -} - +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Collections; + +using ActiveMQ.OpenWire; +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire + +{ + /// + /// A base class with useful implementation inheritence methods + /// for creating marshallers of the OpenWire protocol + /// + public abstract class BaseDataStreamMarshaller + { + public const byte NULL = 0; + public const byte BOOLEAN_TYPE = 1; + public const byte BYTE_TYPE = 2; + public const byte CHAR_TYPE = 3; + public const byte SHORT_TYPE = 4; + public const byte INTEGER_TYPE = 5; + public const byte LONG_TYPE = 6; + public const byte DOUBLE_TYPE = 7; + public const byte FLOAT_TYPE = 8; + public const byte STRING_TYPE = 9; + public const byte BYTE_ARRAY_TYPE = 10; + + private static String[] HEX_TABLE = new String[]{ + "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", + "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", + "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", + "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", + "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", + "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", + "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", + "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", + "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", + "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", + "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", + "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", + "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", + "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", + "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", + "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff", + }; + + public abstract DataStructure CreateObject(); + public abstract byte GetDataStructureType(); + + public virtual int TightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs) + { + return 0; + } + public virtual void TightMarshal2( + OpenWireFormat wireFormat, + Object o, + BinaryWriter dataOut, + BooleanStream bs) + { + } + + public virtual void TightUnmarshal( + OpenWireFormat wireFormat, + Object o, + BinaryReader dataIn, + BooleanStream bs) + { + } + + + protected virtual DataStructure TightUnmarshalNestedObject( + OpenWireFormat wireFormat, + BinaryReader dataIn, + BooleanStream bs) + { + return wireFormat.TightUnmarshalNestedObject(dataIn, bs); + } + + protected virtual int TightMarshalNestedObject1( + OpenWireFormat wireFormat, + DataStructure o, + BooleanStream bs) + { + return wireFormat.TightMarshalNestedObject1(o, bs); + } + + protected virtual void TightMarshalNestedObject2( + OpenWireFormat wireFormat, + DataStructure o, + BinaryWriter dataOut, + BooleanStream bs) + { + wireFormat.TightMarshalNestedObject2(o, dataOut, bs); + } + + protected virtual DataStructure TightUnmarshalCachedObject( + OpenWireFormat wireFormat, + BinaryReader dataIn, + BooleanStream bs) + { + /* + if (wireFormat.isCacheEnabled()) { + if (bs.ReadBoolean()) { + short index = dataInReadShort(dataIn)Int16(); + DataStructure value = wireFormat.UnmarshalNestedObject(dataIn, bs); + wireFormat.setInUnmarshallCache(index, value); + return value; + } else { + short index = ReadShort(dataIn); + return wireFormat.getFromUnmarshallCache(index); + } + } else { + return wireFormat.UnmarshalNestedObject(dataIn, bs); + } + */ + return wireFormat.TightUnmarshalNestedObject(dataIn, bs); + } + + protected virtual int TightMarshalCachedObject1( + OpenWireFormat wireFormat, + DataStructure o, + BooleanStream bs) + { + /* + if (wireFormat.isCacheEnabled()) { + Short index = wireFormat.getMarshallCacheIndex(o); + bs.WriteBoolean(index == null); + if (index == null) { + int rc = wireFormat.Marshal1NestedObject(o, bs); + wireFormat.addToMarshallCache(o); + return 2 + rc; + } else { + return 2; + } + } else { + return wireFormat.Marshal1NestedObject(o, bs); + } + */ + return wireFormat.TightMarshalNestedObject1(o, bs); + } + + protected virtual void TightMarshalCachedObject2( + OpenWireFormat wireFormat, + DataStructure o, + BinaryWriter dataOut, + BooleanStream bs) + { + /* + if (wireFormat.isCacheEnabled()) { + Short index = wireFormat.getMarshallCacheIndex(o); + if (bs.ReadBoolean()) { + WriteShort(index.shortValue(), dataOut); + wireFormat.Marshal2NestedObject(o, dataOut, bs); + } else { + WriteShort(index.shortValue(), dataOut); + } + } else { + wireFormat.Marshal2NestedObject(o, dataOut, bs); + } + */ + wireFormat.TightMarshalNestedObject2(o, dataOut, bs); + } + + + + protected virtual String TightUnmarshalString(BinaryReader dataIn, BooleanStream bs) + { + if (bs.ReadBoolean()) + { + if (bs.ReadBoolean()) + { + return ReadAsciiString(dataIn); + } + else + { + return ReadUTF8(dataIn); + } + } + else + { + return null; + } + } + + protected virtual String ReadAsciiString(BinaryReader dataIn) + { + int size = ReadShort(dataIn); + byte[] data = new byte[size]; + dataIn.Read(data, 0, size); + char[] text = new char[size]; + for (int i = 0; i < size; i++) + { + text[i] = (char) data[i]; + } + return new String(text); + } + + protected virtual int TightMarshalString1(String value, BooleanStream bs) + { + bs.WriteBoolean(value != null); + if (value != null) + { + int strlen = value.Length; + + // TODO until we get UTF8 working, lets just force ASCII + bs.WriteBoolean(true); + return strlen + 2; + + + /* + int utflen = 0; + int c = 0; + bool isOnlyAscii = true; + char[] charr = value.ToCharArray(); + for (int i = 0; i < strlen; i++) + { + c = charr[i]; + if ((c >= 0x0001) && (c <= 0x007F)) + { + utflen++; + } + else if (c > 0x07FF) + { + utflen += 3; + isOnlyAscii = false; + } + else + { + isOnlyAscii = false; + utflen += 2; + } + } + + if (utflen >= Int16.MaxValue) + throw new IOException("Encountered a String value that is too long to encode."); + + bs.WriteBoolean(isOnlyAscii); + return utflen + 2; + */ + } + else + { + return 0; + } + } + + public static void TightMarshalString2(String value, BinaryWriter dataOut, BooleanStream bs) + { + if (bs.ReadBoolean()) + { + // If we verified it only holds ascii values + if (bs.ReadBoolean()) + { + WriteShort((short) value.Length, dataOut); + // now lets write the bytes + char[] chars = value.ToCharArray(); + for (int i = 0; i < chars.Length; i++) + { + WriteByte((byte) chars[i], dataOut); + } + } + else + { + // TODO how should we properly write a String so that Java will grok it??? + dataOut.Write(value); + } + } + } + + public static byte ReadByte(BinaryReader dataIn) + { + return dataIn.ReadByte(); + } + + public static char ReadChar(BinaryReader dataIn) + { + return (char) ReadShort(dataIn); + } + + public static short ReadShort(BinaryReader dataIn) + { + return SwitchEndian(dataIn.ReadInt16()); + } + + public static int ReadInt(BinaryReader dataIn) + { + return SwitchEndian(dataIn.ReadInt32()); + } + + public static long ReadLong(BinaryReader dataIn) + { + return SwitchEndian(dataIn.ReadInt64()); + } + + public static void WriteByte(byte value, BinaryWriter dataOut) + { + dataOut.Write(value); + } + + public static void WriteChar(char value, BinaryWriter dataOut) + { + dataOut.Write(SwitchEndian((short) value)); + } + + public static void WriteShort(short value, BinaryWriter dataOut) + { + dataOut.Write(SwitchEndian(value)); + } + + public static void WriteInt(int value, BinaryWriter dataOut) + { + dataOut.Write(SwitchEndian(value)); + } + + public static void WriteLong(long value, BinaryWriter dataOut) + { + dataOut.Write(SwitchEndian(value)); + } + + + /// + /// Switches from one endian to the other + /// + public static int SwitchEndian(int x) + { + return ((x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24)); + } + + public static short SwitchEndian(short x) + { + int low = x & 0xff; + int high = x & 0xff00; + return(short)(high >> 8 | low << 8); + } + + public static long SwitchEndian(long x) + { + long answer = 0; + for (int i = 0; i < 8; i++) + { + long lowest = x & 0xff; + x >>= 8; + answer <<= 8; + answer += lowest; + } + return answer; + } + + public virtual int TightMarshalLong1(OpenWireFormat wireFormat, long o, BooleanStream bs) + { + if (o == 0L) + { + bs.WriteBoolean(false); + bs.WriteBoolean(false); + return 0; + } + else + { + ulong ul = (ulong) o; + if ((ul & 0xFFFFFFFFFFFF0000ul) == 0L) + { + bs.WriteBoolean(false); + bs.WriteBoolean(true); + return 2; + } + else if ((ul & 0xFFFFFFFF00000000ul) == 0L) + { + bs.WriteBoolean(true); + bs.WriteBoolean(false); + return 4; + } + else + { + bs.WriteBoolean(true); + bs.WriteBoolean(true); + return 8; + } + } + } + + public virtual void TightMarshalLong2( + OpenWireFormat wireFormat, + long o, + BinaryWriter dataOut, + BooleanStream bs) + { + if (bs.ReadBoolean()) + { + if (bs.ReadBoolean()) + { + WriteLong(o, dataOut); + } + else + { + WriteInt((int) o, dataOut); + } + } + else + { + if (bs.ReadBoolean()) + { + WriteShort((short) o, dataOut); + } + } + } + public virtual long TightUnmarshalLong(OpenWireFormat wireFormat, BinaryReader dataIn, BooleanStream bs) + { + if (bs.ReadBoolean()) + { + if (bs.ReadBoolean()) + { + return ReadLong(dataIn); + } + else + { + return ReadInt(dataIn); + } + } + else + { + if (bs.ReadBoolean()) + { + return ReadShort(dataIn); + } + else + { + return 0; + } + } + } + protected virtual int TightMarshalObjectArray1( + OpenWireFormat wireFormat, + DataStructure[] objects, + BooleanStream bs) + { + if (objects != null) + { + int rc = 0; + bs.WriteBoolean(true); + rc += 2; + for (int i = 0; i < objects.Length; i++) + { + rc += TightMarshalNestedObject1(wireFormat, objects[i], bs); + } + return rc; + } + else + { + bs.WriteBoolean(false); + return 0; + } + } + + protected virtual void TightMarshalObjectArray2( + OpenWireFormat wireFormat, + DataStructure[] objects, + BinaryWriter dataOut, + BooleanStream bs) + { + if (bs.ReadBoolean()) + { + WriteShort((short) objects.Length, dataOut); + for (int i = 0; i < objects.Length; i++) + { + TightMarshalNestedObject2(wireFormat, objects[i], dataOut, bs); + } + } + } + + protected virtual byte[] ReadBytes(BinaryReader dataIn, bool flag) + { + if (flag) + { + int size = ReadInt(dataIn); + return dataIn.ReadBytes(size); + } + else + { + return null; + } + } + + protected virtual byte[] ReadBytes(BinaryReader dataIn) + { + int size = ReadInt(dataIn); + return dataIn.ReadBytes(size); + } + + protected virtual byte[] ReadBytes(BinaryReader dataIn, int size) + { + return dataIn.ReadBytes(size); + } + + protected virtual void WriteBytes(byte[] command, BinaryWriter dataOut) + { + WriteInt(command.Length, dataOut); + dataOut.Write(command); + } + + protected virtual BrokerError TightUnmarshalBrokerError( + OpenWireFormat wireFormat, + BinaryReader dataIn, + BooleanStream bs) + { + if (bs.ReadBoolean()) + { + BrokerError answer = new BrokerError(); + + answer.ExceptionClass = TightUnmarshalString(dataIn, bs); + answer.Message = TightUnmarshalString(dataIn, bs); + if (wireFormat.StackTraceEnabled) + { + short length = ReadShort(dataIn); + StackTraceElement[] stackTrace = new StackTraceElement[length]; + for (int i = 0; i < stackTrace.Length; i++) + { + StackTraceElement element = new StackTraceElement(); + element.ClassName = TightUnmarshalString(dataIn, bs); + element.MethodName = TightUnmarshalString(dataIn, bs); + element.FileName = TightUnmarshalString(dataIn, bs); + element.LineNumber = ReadInt(dataIn); + stackTrace[i] = element; + } + answer.StackTraceElements = stackTrace; + answer.Cause = TightUnmarshalBrokerError(wireFormat, dataIn, bs); + } + return answer; + } + else + { + return null; + } + } + + protected int TightMarshalBrokerError1(OpenWireFormat wireFormat, BrokerError o, BooleanStream bs) + { + if (o == null) + { + bs.WriteBoolean(false); + return 0; + } + else + { + int rc = 0; + bs.WriteBoolean(true); + rc += TightMarshalString1(o.ExceptionClass, bs); + rc += TightMarshalString1(o.Message, bs); + if (wireFormat.StackTraceEnabled) + { + rc += 2; + StackTraceElement[] stackTrace = o.StackTraceElements; + for (int i = 0; i < stackTrace.Length; i++) + { + StackTraceElement element = stackTrace[i]; + rc += TightMarshalString1(element.ClassName, bs); + rc += TightMarshalString1(element.MethodName, bs); + rc += TightMarshalString1(element.FileName, bs); + rc += 4; + } + rc += TightMarshalBrokerError1(wireFormat, o.Cause, bs); + } + + return rc; + } + } + + protected void TightMarshalBrokerError2( + OpenWireFormat wireFormat, + BrokerError o, + BinaryWriter dataOut, + BooleanStream bs) + { + if (bs.ReadBoolean()) + { + TightMarshalString2(o.ExceptionClass, dataOut, bs); + TightMarshalString2(o.Message, dataOut, bs); + if (wireFormat.StackTraceEnabled) + { + StackTraceElement[] stackTrace = o.StackTraceElements; + WriteShort((short) stackTrace.Length, dataOut); + + for (int i = 0; i < stackTrace.Length; i++) + { + StackTraceElement element = stackTrace[i]; + TightMarshalString2(element.ClassName, dataOut, bs); + TightMarshalString2(element.MethodName, dataOut, bs); + TightMarshalString2(element.FileName, dataOut, bs); + WriteInt(element.LineNumber, dataOut); + } + TightMarshalBrokerError2(wireFormat, o.Cause, dataOut, bs); + } + } + } + + /// + /// Marshals the primitive type map to a byte array + /// + public static byte[] MarshalPrimitiveMap(IDictionary map) + { + if (map == null) + { + return null; + } + else + { + MemoryStream memoryStream = new MemoryStream(); + MarshalPrimitiveMap(map, new BinaryWriter(memoryStream)); + return memoryStream.GetBuffer(); + } + } + public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut) + { + if (map == null) + { + WriteInt(-1, dataOut); + } + else + { + WriteInt(map.Count, dataOut); + foreach (DictionaryEntry entry in map) + { + String name = (String) entry.Key; + WriteUTF8(name, dataOut); + Object value = entry.Value; + MarshalPrimitive(dataOut, value); + } + }} + + + + /// + /// Unmarshals the primitive type map from the given byte array + /// + public static IDictionary UnmarshalPrimitiveMap(byte[] data) + { + if (data == null) + { + return new Hashtable(); + } + else + { + return UnmarshalPrimitiveMap(new BinaryReader(new MemoryStream(data))); + } + } + + public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn) + { + int size = ReadInt(dataIn); + if (size < 0) + { + return null; + } + else + { + IDictionary answer = new Hashtable(size); + for (int i=0; i < size; i++) + { + String name = ReadUTF8(dataIn); + answer[name] = UnmarshalPrimitive(dataIn); + } + return answer; + } + + } + + public static void MarshalPrimitive(BinaryWriter dataOut, Object value) + { + if (value == null) + { + WriteByte(NULL, dataOut); + } + else if (value is bool) + { + WriteByte(BOOLEAN_TYPE, dataOut); + WriteBoolean((bool) value, dataOut); + } + else if (value is byte) + { + WriteByte(BYTE_TYPE, dataOut); + WriteByte(((Byte)value), dataOut); + } + else if (value is char) + { + WriteByte(CHAR_TYPE, dataOut); + WriteChar((char) value, dataOut); + } + else if (value is short) + { + WriteByte(SHORT_TYPE, dataOut); + WriteShort((short) value, dataOut); + } + else if (value is int) + { + WriteByte(INTEGER_TYPE, dataOut); + WriteInt((int) value, dataOut); + } + else if (value is long) + { + WriteByte(LONG_TYPE, dataOut); + WriteLong((long) value, dataOut); + } + else if (value is float) + { + WriteByte(FLOAT_TYPE, dataOut); + WriteFloat((float) value, dataOut); + } + else if (value is double) + { + WriteByte(DOUBLE_TYPE, dataOut); + WriteDouble((double) value, dataOut); + } + else if (value is byte[]) + { + byte[] data = (byte[]) value; + WriteByte(BYTE_ARRAY_TYPE, dataOut); + WriteInt(data.Length, dataOut); + dataOut.Write(data); + } + else if (value is string) + { + WriteByte(STRING_TYPE, dataOut); + WriteUTF8((string) value, dataOut); + } + else + { + throw new IOException("Object is not a primitive: " + value); + } + } + + public static Object UnmarshalPrimitive(BinaryReader dataIn) + { + Object value=null; + switch (ReadByte(dataIn)) + { + case BYTE_TYPE: + value = ReadByte(dataIn); + break; + case BOOLEAN_TYPE: + value = ReadBoolean(dataIn); + break; + case CHAR_TYPE: + value = ReadChar(dataIn); + break; + case SHORT_TYPE: + value = ReadShort(dataIn); + break; + case INTEGER_TYPE: + value = ReadInt(dataIn); + break; + case LONG_TYPE: + value = ReadLong(dataIn); + break; + case FLOAT_TYPE: + value = ReadFloat(dataIn); + break; + case DOUBLE_TYPE: + value = ReadDouble(dataIn); + break; + case BYTE_ARRAY_TYPE: + int size = ReadInt(dataIn); + byte[] data = new byte[size]; + dataIn.Read(data, 0, size); + value = data; + break; + case STRING_TYPE: + value = ReadUTF8(dataIn); + break; + } + return value; + } + + private static Object ReadDouble(BinaryReader dataIn) + { + // TODO: Implement this method + return dataIn.ReadDouble(); + } + + /// + /// Method ReadFloat + /// + private static Object ReadFloat(BinaryReader dataIn) + { + // TODO: Implement this method + return (float) dataIn.ReadDouble(); + } + + private static Object ReadBoolean(BinaryReader dataIn) + { + // TODO: Implement this method + return dataIn.ReadBoolean(); + } + + private static void WriteDouble(double value, BinaryWriter dataOut) + { + // TODO: Implement this method + dataOut.Write(value); + } + + private static void WriteFloat(float value, BinaryWriter dataOut) + { + // TODO: Implement this method + dataOut.Write(value); + } + + private static void WriteBoolean(bool value, BinaryWriter dataOut) + { + // TODO: Implement this method + dataOut.Write(value); + } + + + public static void WriteUTF8(String text, BinaryWriter dataOut) + { + if (text != null) + { + int strlen = text.Length; + int utflen = 0; + int c, count = 0; + + char[] charr = text.ToCharArray(); + + for (int i = 0; i < strlen; i++) + { + c = charr[i]; + if ((c >= 0x0001) && (c <= 0x007F)) + { + utflen++; + } + else if (c > 0x07FF) + { + utflen += 3; + } + else + { + utflen += 2; + } + } + + WriteInt(utflen, dataOut); + byte[] bytearr = new byte[utflen]; + /* + byte[] bytearr = new byte[utflen + 4]; + bytearr[count++] = (byte) ((utflen >>> 24) & 0xFF); + bytearr[count++] = (byte) ((utflen >>> 16) & 0xFF); + bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); + bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); + */ + for (int i = 0; i < strlen; i++) + { + c = charr[i]; + if ((c >= 0x0001) && (c <= 0x007F)) + { + bytearr[count++] = (byte) c; + } + else if (c > 0x07FF) + { + bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); + bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); + bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); + } + else + { + bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); + bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); + } + } + dataOut.Write(bytearr); + + } + else + { + WriteInt(-1, dataOut); + } + } + + public static String ReadUTF8(BinaryReader dataIn) + { + int utflen = ReadInt(dataIn); + if (utflen > -1) + { + StringBuilder str = new StringBuilder(utflen); + + byte[] bytearr = new byte[utflen]; + int c, char2, char3; + int count = 0; + + dataIn.Read(bytearr, 0, utflen); + + while (count < utflen) + { + c = bytearr[count] & 0xff; + switch (c >> 4) + { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + /* 0xxxxxxx */ + count++; + str.Append((char) c); + break; + case 12: + case 13: + /* 110x xxxx 10xx xxxx */ + count += 2; + if (count > utflen) + { + throw CreateDataFormatException(); + } + char2 = bytearr[count - 1]; + if ((char2 & 0xC0) != 0x80) + { + throw CreateDataFormatException(); + } + str.Append((char) (((c & 0x1F) << 6) | (char2 & 0x3F))); + break; + case 14: + /* 1110 xxxx 10xx xxxx 10xx xxxx */ + count += 3; + if (count > utflen) + { + throw CreateDataFormatException(); + } + char2 = bytearr[count - 2]; + char3 = bytearr[count - 1]; + if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) + { + throw CreateDataFormatException(); + } + str.Append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0))); + break; + default : + /* 10xx xxxx, 1111 xxxx */ + throw CreateDataFormatException(); + } + } +// The number of chars produced may be less than utflen + return str.ToString(); + } + else + { + return null; + } + } + + private static Exception CreateDataFormatException() + { + // TODO: implement a better exception + return new Exception("Data format error!"); + } + + + /// + /// Converts the object to a String + /// + public static string ToString(MessageId id) + { + return ToString(id.ProducerId) + ":" + id.ProducerSequenceId; + } + /// + /// Converts the object to a String + /// + public static string ToString(ProducerId id) + { + return id.ConnectionId + ":" + id.SessionId + ":" + id.Value; + } + + + /// + /// Converts the given transaction ID into a String + /// + public static String ToString(TransactionId txnId) + { + if (txnId is LocalTransactionId) + { + LocalTransactionId ltxnId = (LocalTransactionId) txnId; + return "" + ltxnId.Value; + } + else if (txnId is XATransactionId) + { + XATransactionId xaTxnId = (XATransactionId) txnId; + return "XID:" + xaTxnId.FormatId + ":" + ToHexFromBytes(xaTxnId.GlobalTransactionId) + ":" + ToHexFromBytes(xaTxnId.BranchQualifier); + } + return null; + } + + /// + /// Creates the byte array into hexidecimal + /// + public static String ToHexFromBytes(byte[] data) + { + StringBuilder buffer = new StringBuilder(data.Length * 2); + for (int i = 0; i < data.Length; i++) + { + buffer.Append(HEX_TABLE[0xFF & data[i]]); + } + return buffer.ToString(); + } + + } +} + diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/BooleanStream.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/BooleanStream.cs index b441afa5b6..ca897318ca 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/BooleanStream.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/BooleanStream.cs @@ -17,11 +17,11 @@ using System; using System.IO; -using OpenWire.Client.Commands; -using OpenWire.Client.Core; -using OpenWire.Client.IO; +using ActiveMQ.OpenWire; +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire -namespace OpenWire.Client.Core { /// /// Represents a stream of boolean flags diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/BrokerError.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/BrokerError.cs index a58a2a02f4..7fb110c151 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/BrokerError.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/BrokerError.cs @@ -16,10 +16,10 @@ */ using System; using System.Text; -using OpenWire.Client.Core; using System.IO; -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire + { public struct StackTraceElement { diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/Command.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/Command.cs index dc7fdd9f35..6078ef2aa0 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/Command.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/Command.cs @@ -15,9 +15,9 @@ * limitations under the License. */ using System; -using OpenWire.Client.Core; -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire + { /// /// An OpenWire command diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructure.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructure.cs index 4b50374ca5..0955c2f71b 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructure.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructure.cs @@ -15,9 +15,9 @@ * limitations under the License. */ using System; -using OpenWire.Client.Core; -namespace OpenWire.Client.Core { +namespace ActiveMQ.OpenWire +{ /// /// An OpenWire command /// diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructureSupport.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructureSupport.cs index 129531b616..eced31e18a 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructureSupport.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/DataStructureSupport.cs @@ -1,39 +1,40 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System; -using OpenWire.Client; -using OpenWire.Client.Commands; -using OpenWire.Client.Core; - -namespace OpenWire.Client.Core { - /// - /// Summary description for DataStructureSupport. - /// - public abstract class DataStructureSupport : DataStructure { - - protected DataStructureSupport() { - } - - public virtual byte GetDataStructureType() { - return 0; - } - - public virtual bool IsMarshallAware() { - return false; - } - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; + +using ActiveMQ.OpenWire; +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire +{ + /// + /// Summary description for DataStructureSupport. + /// + public abstract class DataStructureSupport : DataStructure { + + protected DataStructureSupport() { + } + + public virtual byte GetDataStructureType() { + return 0; + } + + public virtual bool IsMarshallAware() { + return false; + } + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/Dispatcher.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/Dispatcher.cs index d2131c1547..3164253f18 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/Dispatcher.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/Dispatcher.cs @@ -1,124 +1,124 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System.Collections; -using OpenWire.Client.Commands; -using System; -using OpenWire.Client; -using System.Threading; - -namespace OpenWire.Client.Core -{ - /// - /// Handles the multi-threaded dispatching between the transport and the consumers - /// - public class Dispatcher - { - Queue queue = new Queue(); - Object semaphore = new Object(); - ArrayList messagesToRedeliver = new ArrayList(); - - /// - /// Whem we start a transaction we must redeliver any rolled back messages - /// - public void RedeliverRolledBackMessages() { - lock (semaphore) - { - Queue replacement = new Queue(queue.Count + messagesToRedeliver.Count); - foreach (ActiveMQMessage element in messagesToRedeliver) { - replacement.Enqueue(element); - } - messagesToRedeliver.Clear(); - - while (queue.Count > 0) - { - ActiveMQMessage element = (ActiveMQMessage) queue.Dequeue(); - replacement.Enqueue(element); - } - queue = replacement; - Monitor.PulseAll(semaphore); - } - } - - /// - /// Redeliver the given message, putting it at the head of the queue - /// - public void Redeliver(ActiveMQMessage message) - { - lock (semaphore) { - messagesToRedeliver.Add(message); - } - } - - /// - /// Method Enqueue - /// - public void Enqueue(ActiveMQMessage message) - { - lock (semaphore) - { - queue.Enqueue(message); - Monitor.PulseAll(semaphore); - } - } - - /// - /// Method DequeueNoWait - /// - public IMessage DequeueNoWait() - { - lock (semaphore) - { - if (queue.Count > 0) - { - return (IMessage) queue.Dequeue(); - } - } - return null; - } - - /// - /// Method Dequeue - /// - public IMessage Dequeue(int timeout) - { - lock (semaphore) - { - if (queue.Count == 0) - { - Monitor.Wait(semaphore, timeout); - } - if (queue.Count > 0) - { - return (IMessage) queue.Dequeue(); - } - } - return null; - } - - /// - /// Method Dequeue - /// - public IMessage Dequeue() - { - lock (semaphore) - { - return (IMessage) queue.Dequeue(); - } - } - - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System.Collections; +using System; +using System.Threading; + +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire +{ + /// + /// Handles the multi-threaded dispatching between the transport and the consumers + /// + public class Dispatcher + { + Queue queue = new Queue(); + Object semaphore = new Object(); + ArrayList messagesToRedeliver = new ArrayList(); + + /// + /// Whem we start a transaction we must redeliver any rolled back messages + /// + public void RedeliverRolledBackMessages() { + lock (semaphore) + { + Queue replacement = new Queue(queue.Count + messagesToRedeliver.Count); + foreach (ActiveMQMessage element in messagesToRedeliver) { + replacement.Enqueue(element); + } + messagesToRedeliver.Clear(); + + while (queue.Count > 0) + { + ActiveMQMessage element = (ActiveMQMessage) queue.Dequeue(); + replacement.Enqueue(element); + } + queue = replacement; + Monitor.PulseAll(semaphore); + } + } + + /// + /// Redeliver the given message, putting it at the head of the queue + /// + public void Redeliver(ActiveMQMessage message) + { + lock (semaphore) { + messagesToRedeliver.Add(message); + } + } + + /// + /// Method Enqueue + /// + public void Enqueue(ActiveMQMessage message) + { + lock (semaphore) + { + queue.Enqueue(message); + Monitor.PulseAll(semaphore); + } + } + + /// + /// Method DequeueNoWait + /// + public IMessage DequeueNoWait() + { + lock (semaphore) + { + if (queue.Count > 0) + { + return (IMessage) queue.Dequeue(); + } + } + return null; + } + + /// + /// Method Dequeue + /// + public IMessage Dequeue(int timeout) + { + lock (semaphore) + { + if (queue.Count == 0) + { + Monitor.Wait(semaphore, timeout); + } + if (queue.Count > 0) + { + return (IMessage) queue.Dequeue(); + } + } + return null; + } + + /// + /// Method Dequeue + /// + public IMessage Dequeue() + { + lock (semaphore) + { + return (IMessage) queue.Dequeue(); + } + } + + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/FutureResponse.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/FutureResponse.cs index 57d29d87ca..a8e5209919 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/FutureResponse.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/FutureResponse.cs @@ -17,10 +17,9 @@ using System; using System.Threading; -using OpenWire.Client; -using OpenWire.Client.Commands; +using ActiveMQ.OpenWire.Commands; -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire { /// /// Handles asynchronous responses diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/ISynchronization.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/ISynchronization.cs index 1fb0fe059b..efc4de913f 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/ISynchronization.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/ISynchronization.cs @@ -1,36 +1,36 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -namespace OpenWire.Client.Core -{ - public interface ISynchronization - { - /// - /// Called before a commit - /// - void BeforeCommit(); - - /// - /// Called after a commit - /// - void AfterCommit(); - - /// - /// Called after a transaction rollback - /// - void AfterRollback(); - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +namespace ActiveMQ.OpenWire +{ + public interface ISynchronization + { + /// + /// Called before a commit + /// + void BeforeCommit(); + + /// + /// Called after a commit + /// + void AfterCommit(); + + /// + /// Called after a transaction rollback + /// + void AfterRollback(); + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/ITransport.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/ITransport.cs index ad18686354..83562a2ee6 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/ITransport.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/ITransport.cs @@ -16,11 +16,9 @@ */ using System; -using OpenWire.Client; -using OpenWire.Client.Commands; -using OpenWire.Client.Core; +using ActiveMQ.OpenWire.Commands; -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire { public delegate void CommandHandler(ITransport sender, Command command); diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/MarshallAware.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/MarshallAware.cs index 7525538c64..2234a1ca37 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/MarshallAware.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/MarshallAware.cs @@ -1,39 +1,36 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System; -using System.IO; - -using OpenWire.Client.Commands; -using OpenWire.Client.Core; -using OpenWire.Client.IO; - -namespace OpenWire.Client.Core { - /// - /// Represents a marshallable entity - /// - public interface MarshallAware { - - void BeforeMarshall(OpenWireFormat wireFormat); - void AfterMarshall(OpenWireFormat wireFormat); - - void BeforeUnmarshall(OpenWireFormat wireFormat); - void AfterUnmarshall(OpenWireFormat wireFormat); - - void SetMarshalledForm(OpenWireFormat wireFormat, byte[] data); - byte[] GetMarshalledForm(OpenWireFormat wireFormat); - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.IO; + +namespace ActiveMQ.OpenWire +{ +/// + /// Represents a marshallable entity + /// + public interface MarshallAware { + + void BeforeMarshall(OpenWireFormat wireFormat); + void AfterMarshall(OpenWireFormat wireFormat); + + void BeforeUnmarshall(OpenWireFormat wireFormat); + void AfterUnmarshall(OpenWireFormat wireFormat); + + void SetMarshalledForm(OpenWireFormat wireFormat, byte[] data); + byte[] GetMarshalledForm(OpenWireFormat wireFormat); + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/MessagePropertyHelper.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/MessagePropertyHelper.cs index 46258688d9..9a55c6f2e8 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/MessagePropertyHelper.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/MessagePropertyHelper.cs @@ -1,57 +1,55 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -*/ -using System; -using System.Collections; - -using OpenWire.Client; -using OpenWire.Client.Core; -using OpenWire.Client.Commands; - -namespace OpenWire.Client.Core -{ - public delegate object PropertyGetter(ActiveMQMessage message); - public delegate void PropertySetter(ActiveMQMessage message, object value); - - public class MessagePropertyHelper - { - private IDictionary setters = new Hashtable(); - private IDictionary getters = new Hashtable(); - - public MessagePropertyHelper() - { - // TODO find all of the JMS properties via introspection - } - - - public object GetObjectProperty(ActiveMQMessage message, string name) { - object getter = getters[name]; - if (getter != null) { - } - return message.Properties[name]; - } - - public void SetObjectProperty(ActiveMQMessage message, string name, object value) { - PropertySetter setter = (PropertySetter) setters[name]; - if (setter != null) { - setter(message, value); - } - else { - message.Properties[name] = value; - } - } - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +using System; +using System.Collections; + +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire +{ + public delegate object PropertyGetter(ActiveMQMessage message); + public delegate void PropertySetter(ActiveMQMessage message, object value); + + public class MessagePropertyHelper + { + private IDictionary setters = new Hashtable(); + private IDictionary getters = new Hashtable(); + + public MessagePropertyHelper() + { + // TODO find all of the JMS properties via introspection + } + + + public object GetObjectProperty(ActiveMQMessage message, string name) { + object getter = getters[name]; + if (getter != null) { + } + return message.Properties[name]; + } + + public void SetObjectProperty(ActiveMQMessage message, string name, object value) { + PropertySetter setter = (PropertySetter) setters[name]; + if (setter != null) { + setter(message, value); + } + else { + message.Properties[name] = value; + } + } + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/OpenWireFormat.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/OpenWireFormat.cs index 90965e6ee4..0c82b7a556 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/OpenWireFormat.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/OpenWireFormat.cs @@ -1,224 +1,223 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System; -using System.IO; - -using OpenWire.Client.Commands; -using OpenWire.Client.Core; -using OpenWire.Client.IO; - -namespace OpenWire.Client.Core -{ - /// - /// Represents the wire format - /// - public class OpenWireFormat - { - static private char[] MAGIC = new char[] { 'A', 'c', 't', 'i', 'v', 'e', 'M', 'Q' }; - - private BaseDataStreamMarshaller[] dataMarshallers; - private const byte NULL_TYPE = 0; - private WireFormatInfo wireFormatInfo = new WireFormatInfo(); - - public OpenWireFormat() - { - // lets configure the wire format - wireFormatInfo.Magic = CreateMagicBytes(); - wireFormatInfo.Version = 1; - wireFormatInfo.StackTraceEnabled = true; - wireFormatInfo.TcpNoDelayEnabled = true; - wireFormatInfo.PrefixPacketSize = true; - wireFormatInfo.TightEncodingEnabled = true; - - dataMarshallers = new BaseDataStreamMarshaller[256]; - MarshallerFactory factory = new MarshallerFactory(); - factory.configure(this); - } - - public WireFormatInfo WireFormatInfo { - get { - return wireFormatInfo; - } - } - - public bool StackTraceEnabled { - get { - return wireFormatInfo.StackTraceEnabled; - } - } - - public void addMarshaller(BaseDataStreamMarshaller marshaller) - { - byte type = marshaller.GetDataStructureType(); - dataMarshallers[type & 0xFF] = marshaller; - } - - public void Marshal(Object o, BinaryWriter ds) - { - int size = 1; - if (o != null) - { - DataStructure c = (DataStructure) o; - byte type = c.GetDataStructureType(); - BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; - if (dsm == null) - throw new IOException("Unknown data type: " + type); - - BooleanStream bs = new BooleanStream(); - size += dsm.TightMarshal1(this, c, bs); - size += bs.MarshalledSize(); - - BaseDataStreamMarshaller.WriteInt(size, ds); - BaseDataStreamMarshaller.WriteByte(type, ds); - bs.Marshal(ds); - dsm.TightMarshal2(this, c, ds, bs); - } - else - { - BaseDataStreamMarshaller.WriteInt(size, ds); - BaseDataStreamMarshaller.WriteByte(NULL_TYPE, ds); - } - } - - public Object Unmarshal(BinaryReader dis) - { - // lets ignore the size of the packet - BaseDataStreamMarshaller.ReadInt(dis); - - // first byte is the type of the packet - byte dataType = BaseDataStreamMarshaller.ReadByte(dis); - if (dataType != NULL_TYPE) - { - BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[dataType & 0xFF]; - if (dsm == null) - throw new IOException("Unknown data type: " + dataType); - //Console.WriteLine("Parsing type: " + dataType + " with: " + dsm); - Object data = dsm.CreateObject(); - BooleanStream bs = new BooleanStream(); - bs.Unmarshal(dis); - dsm.TightUnmarshal(this, data, dis, bs); - return data; - } - else - { - return null; - } - } - - public int TightMarshalNestedObject1(DataStructure o, BooleanStream bs) - { - bs.WriteBoolean(o != null); - if (o == null) - return 0; - - if (o.IsMarshallAware()) - { - MarshallAware ma = (MarshallAware) o; - byte[] sequence = ma.GetMarshalledForm(this); - bs.WriteBoolean(sequence != null); - if (sequence != null) - { - return 1 + sequence.Length; - } - } - - byte type = o.GetDataStructureType(); - if (type == 0) { - throw new IOException("No valid data structure type for: " + o + " of type: " + o.GetType()); - } - BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; - if (dsm == null) - throw new IOException("Unknown data type: " + type); - //Console.WriteLine("Marshalling type: " + type + " with structure: " + o); - return 1 + dsm.TightMarshal1(this, o, bs); - } - - public void TightMarshalNestedObject2(DataStructure o, BinaryWriter ds, BooleanStream bs) - { - if (!bs.ReadBoolean()) - return ; - - byte type = o.GetDataStructureType(); - BaseDataStreamMarshaller.WriteByte(type, ds); - - if (o.IsMarshallAware() && bs.ReadBoolean()) - { - MarshallAware ma = (MarshallAware) o; - byte[] sequence = ma.GetMarshalledForm(this); - ds.Write(sequence, 0, sequence.Length); - } - else - { - - BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; - if (dsm == null) - throw new IOException("Unknown data type: " + type); - dsm.TightMarshal2(this, o, ds, bs); - } - } - - public DataStructure TightUnmarshalNestedObject(BinaryReader dis, BooleanStream bs) - { - if (bs.ReadBoolean()) - { - - byte dataType = BaseDataStreamMarshaller.ReadByte(dis); - BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[dataType & 0xFF]; - if (dsm == null) - throw new IOException("Unknown data type: " + dataType); - DataStructure data = dsm.CreateObject(); - - if (data.IsMarshallAware() && bs.ReadBoolean()) - { - BaseDataStreamMarshaller.ReadInt(dis); - BaseDataStreamMarshaller.ReadByte(dis); - - BooleanStream bs2 = new BooleanStream(); - bs2.Unmarshal(dis); - dsm.TightUnmarshal(this, data, dis, bs2); - - // TODO: extract the sequence from the dis and associate it. - // MarshallAware ma = (MarshallAware)data - // ma.setCachedMarshalledForm(this, sequence); - } - else - { - dsm.TightUnmarshal(this, data, dis, bs); - } - - return data; - } - else - { - return null; - } - } - - /// - /// Method CreateMagicBytes - /// - private byte[] CreateMagicBytes() - { - byte[] answer = new byte[MAGIC.Length]; - for (int i = 0; i < answer.Length; i++) - { - answer[i] = (byte) MAGIC[i]; - } - return answer; - } - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.IO; + +using ActiveMQ.OpenWire.Commands; +using ActiveMQ.OpenWire.V1; + +namespace ActiveMQ.OpenWire +{ + /// + /// Represents the wire format + /// + public class OpenWireFormat + { + static private char[] MAGIC = new char[] { 'A', 'c', 't', 'i', 'v', 'e', 'M', 'Q' }; + + private BaseDataStreamMarshaller[] dataMarshallers; + private const byte NULL_TYPE = 0; + private WireFormatInfo wireFormatInfo = new WireFormatInfo(); + + public OpenWireFormat() + { + // lets configure the wire format + wireFormatInfo.Magic = CreateMagicBytes(); + wireFormatInfo.Version = 1; + wireFormatInfo.StackTraceEnabled = true; + wireFormatInfo.TcpNoDelayEnabled = true; + wireFormatInfo.PrefixPacketSize = true; + wireFormatInfo.TightEncodingEnabled = true; + + dataMarshallers = new BaseDataStreamMarshaller[256]; + MarshallerFactory factory = new MarshallerFactory(); + factory.configure(this); + } + + public WireFormatInfo WireFormatInfo { + get { + return wireFormatInfo; + } + } + + public bool StackTraceEnabled { + get { + return wireFormatInfo.StackTraceEnabled; + } + } + + public void addMarshaller(BaseDataStreamMarshaller marshaller) + { + byte type = marshaller.GetDataStructureType(); + dataMarshallers[type & 0xFF] = marshaller; + } + + public void Marshal(Object o, BinaryWriter ds) + { + int size = 1; + if (o != null) + { + DataStructure c = (DataStructure) o; + byte type = c.GetDataStructureType(); + BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; + if (dsm == null) + throw new IOException("Unknown data type: " + type); + + BooleanStream bs = new BooleanStream(); + size += dsm.TightMarshal1(this, c, bs); + size += bs.MarshalledSize(); + + BaseDataStreamMarshaller.WriteInt(size, ds); + BaseDataStreamMarshaller.WriteByte(type, ds); + bs.Marshal(ds); + dsm.TightMarshal2(this, c, ds, bs); + } + else + { + BaseDataStreamMarshaller.WriteInt(size, ds); + BaseDataStreamMarshaller.WriteByte(NULL_TYPE, ds); + } + } + + public Object Unmarshal(BinaryReader dis) + { + // lets ignore the size of the packet + BaseDataStreamMarshaller.ReadInt(dis); + + // first byte is the type of the packet + byte dataType = BaseDataStreamMarshaller.ReadByte(dis); + if (dataType != NULL_TYPE) + { + BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[dataType & 0xFF]; + if (dsm == null) + throw new IOException("Unknown data type: " + dataType); + //Console.WriteLine("Parsing type: " + dataType + " with: " + dsm); + Object data = dsm.CreateObject(); + BooleanStream bs = new BooleanStream(); + bs.Unmarshal(dis); + dsm.TightUnmarshal(this, data, dis, bs); + return data; + } + else + { + return null; + } + } + + public int TightMarshalNestedObject1(DataStructure o, BooleanStream bs) + { + bs.WriteBoolean(o != null); + if (o == null) + return 0; + + if (o.IsMarshallAware()) + { + MarshallAware ma = (MarshallAware) o; + byte[] sequence = ma.GetMarshalledForm(this); + bs.WriteBoolean(sequence != null); + if (sequence != null) + { + return 1 + sequence.Length; + } + } + + byte type = o.GetDataStructureType(); + if (type == 0) { + throw new IOException("No valid data structure type for: " + o + " of type: " + o.GetType()); + } + BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; + if (dsm == null) + throw new IOException("Unknown data type: " + type); + //Console.WriteLine("Marshalling type: " + type + " with structure: " + o); + return 1 + dsm.TightMarshal1(this, o, bs); + } + + public void TightMarshalNestedObject2(DataStructure o, BinaryWriter ds, BooleanStream bs) + { + if (!bs.ReadBoolean()) + return ; + + byte type = o.GetDataStructureType(); + BaseDataStreamMarshaller.WriteByte(type, ds); + + if (o.IsMarshallAware() && bs.ReadBoolean()) + { + MarshallAware ma = (MarshallAware) o; + byte[] sequence = ma.GetMarshalledForm(this); + ds.Write(sequence, 0, sequence.Length); + } + else + { + + BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[type & 0xFF]; + if (dsm == null) + throw new IOException("Unknown data type: " + type); + dsm.TightMarshal2(this, o, ds, bs); + } + } + + public DataStructure TightUnmarshalNestedObject(BinaryReader dis, BooleanStream bs) + { + if (bs.ReadBoolean()) + { + + byte dataType = BaseDataStreamMarshaller.ReadByte(dis); + BaseDataStreamMarshaller dsm = (BaseDataStreamMarshaller) dataMarshallers[dataType & 0xFF]; + if (dsm == null) + throw new IOException("Unknown data type: " + dataType); + DataStructure data = dsm.CreateObject(); + + if (data.IsMarshallAware() && bs.ReadBoolean()) + { + BaseDataStreamMarshaller.ReadInt(dis); + BaseDataStreamMarshaller.ReadByte(dis); + + BooleanStream bs2 = new BooleanStream(); + bs2.Unmarshal(dis); + dsm.TightUnmarshal(this, data, dis, bs2); + + // TODO: extract the sequence from the dis and associate it. + // MarshallAware ma = (MarshallAware)data + // ma.setCachedMarshalledForm(this, sequence); + } + else + { + dsm.TightUnmarshal(this, data, dis, bs); + } + + return data; + } + else + { + return null; + } + } + + /// + /// Method CreateMagicBytes + /// + private byte[] CreateMagicBytes() + { + byte[] answer = new byte[MAGIC.Length]; + for (int i = 0; i < answer.Length; i++) + { + answer[i] = (byte) MAGIC[i]; + } + return answer; + } + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/PrimitiveMap.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/PrimitiveMap.cs index 8f49387418..43524383da 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/PrimitiveMap.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/PrimitiveMap.cs @@ -1,241 +1,238 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -*/ -using System; -using System.Collections; - -using OpenWire.Client; -using OpenWire.Client.Core; - -namespace OpenWire.Client.Core -{ - /// - /// A default implementation of IPrimitiveMap - /// - public class PrimitiveMap : IPrimitiveMap - { - private IDictionary dictionary = new Hashtable(); - - - /// - /// Unmarshalls the map from the given data or if the data is null just - /// return an empty map - /// - public static PrimitiveMap Unmarshal(byte[] data) - { - PrimitiveMap answer = new PrimitiveMap(); - answer.dictionary = BaseDataStreamMarshaller.UnmarshalPrimitiveMap(data); - return answer; - } - - public byte[] Marshal() - { - return BaseDataStreamMarshaller.MarshalPrimitiveMap(dictionary); - } - - - public void Clear() - { - dictionary.Clear(); - } - - public bool Contains(Object key) - { - return dictionary.Contains(key); - } - - public void Remove(Object key) - { - dictionary.Remove(key); - } - - - public int Count - { - get { - return dictionary.Count; - } - } - - public ICollection Keys - { - get { - return dictionary.Keys; - } - } - - public ICollection Values - { - get { - return dictionary.Values; - } - } - - public object this[string key] - { - get { - return GetValue(key); - } - set { - CheckValidType(value); - SetValue(key, value); - } - } - - public string GetString(string key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(string)); - return (string) value; - } - - public void SetString(string key, string value) - { - SetValue(key, value); - } - - public bool GetBool(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(bool)); - return (bool) value; - } - - public void SetByte(String key, bool value) - { - SetValue(key, value); - } - - public byte GetByte(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(byte)); - return (byte) value; - } - - public void SetByte(String key, byte value) - { - SetValue(key, value); - } - - public char GetChar(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(char)); - return (char) value; - } - - public void SetChar(String key, char value) - { - SetValue(key, value); - } - - public short GetShort(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(short)); - return (short) value; - } - - public void SetShort(String key, short value) - { - SetValue(key, value); - } - - public int GetInt(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(int)); - return (int) value; - } - - public void SetInt(String key, int value) - { - SetValue(key, value); - } - - public long GetLong(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(long)); - return (long) value; - } - - public void SetLong(String key, long value) - { - SetValue(key, value); - } - - public float GetFloat(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(float)); - return (float) value; - } - - public void SetFloat(String key, float value) - { - SetValue(key, value); - } - - public double GetDouble(String key) - { - Object value = GetValue(key); - CheckValueType(value, typeof(double)); - return (double) value; - } - - public void SetDouble(String key, double value) - { - SetValue(key, value); - } - - - - - protected virtual void SetValue(String key, Object value) - { - dictionary[key] = value; - } - - - protected virtual Object GetValue(String key) - { - return dictionary[key]; - } - - protected virtual void CheckValueType(Object value, Type type) - { - if (! type.IsInstanceOfType(value)) - { - throw new OpenWireException("Expected type: " + type.Name + " but was: " + value); - } - } - - protected virtual void CheckValidType(Object value) - { - if (value != null) - { - Type type = value.GetType(); - if (! type.IsPrimitive && !type.IsValueType && !type.IsAssignableFrom(typeof(string))) - { - throw new OpenWireException("Invalid type: " + type.Name + " for value: " + value); - } - } - } - - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*/ +using System; +using System.Collections; + +namespace ActiveMQ.OpenWire +{ + /// + /// A default implementation of IPrimitiveMap + /// + public class PrimitiveMap : IPrimitiveMap + { + private IDictionary dictionary = new Hashtable(); + + + /// + /// Unmarshalls the map from the given data or if the data is null just + /// return an empty map + /// + public static PrimitiveMap Unmarshal(byte[] data) + { + PrimitiveMap answer = new PrimitiveMap(); + answer.dictionary = BaseDataStreamMarshaller.UnmarshalPrimitiveMap(data); + return answer; + } + + public byte[] Marshal() + { + return BaseDataStreamMarshaller.MarshalPrimitiveMap(dictionary); + } + + + public void Clear() + { + dictionary.Clear(); + } + + public bool Contains(Object key) + { + return dictionary.Contains(key); + } + + public void Remove(Object key) + { + dictionary.Remove(key); + } + + + public int Count + { + get { + return dictionary.Count; + } + } + + public ICollection Keys + { + get { + return dictionary.Keys; + } + } + + public ICollection Values + { + get { + return dictionary.Values; + } + } + + public object this[string key] + { + get { + return GetValue(key); + } + set { + CheckValidType(value); + SetValue(key, value); + } + } + + public string GetString(string key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(string)); + return (string) value; + } + + public void SetString(string key, string value) + { + SetValue(key, value); + } + + public bool GetBool(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(bool)); + return (bool) value; + } + + public void SetByte(String key, bool value) + { + SetValue(key, value); + } + + public byte GetByte(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(byte)); + return (byte) value; + } + + public void SetByte(String key, byte value) + { + SetValue(key, value); + } + + public char GetChar(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(char)); + return (char) value; + } + + public void SetChar(String key, char value) + { + SetValue(key, value); + } + + public short GetShort(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(short)); + return (short) value; + } + + public void SetShort(String key, short value) + { + SetValue(key, value); + } + + public int GetInt(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(int)); + return (int) value; + } + + public void SetInt(String key, int value) + { + SetValue(key, value); + } + + public long GetLong(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(long)); + return (long) value; + } + + public void SetLong(String key, long value) + { + SetValue(key, value); + } + + public float GetFloat(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(float)); + return (float) value; + } + + public void SetFloat(String key, float value) + { + SetValue(key, value); + } + + public double GetDouble(String key) + { + Object value = GetValue(key); + CheckValueType(value, typeof(double)); + return (double) value; + } + + public void SetDouble(String key, double value) + { + SetValue(key, value); + } + + + + + protected virtual void SetValue(String key, Object value) + { + dictionary[key] = value; + } + + + protected virtual Object GetValue(String key) + { + return dictionary[key]; + } + + protected virtual void CheckValueType(Object value, Type type) + { + if (! type.IsInstanceOfType(value)) + { + throw new OpenWireException("Expected type: " + type.Name + " but was: " + value); + } + } + + protected virtual void CheckValidType(Object value) + { + if (value != null) + { + Type type = value.GetType(); + if (! type.IsPrimitive && !type.IsValueType && !type.IsAssignableFrom(typeof(string))) + { + throw new OpenWireException("Invalid type: " + type.Name + " for value: " + value); + } + } + } + + } +} diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/SocketTransport.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/SocketTransport.cs index ed58030747..6a511182fd 100755 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/SocketTransport.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/SocketTransport.cs @@ -22,13 +22,9 @@ using System.Net.Sockets; using System.Text; using System.Threading; +using ActiveMQ.OpenWire.Commands; -using OpenWire.Client; -using OpenWire.Client.Commands; -using OpenWire.Client.Core; -using OpenWire.Client.IO; - -namespace OpenWire.Client.Core +namespace ActiveMQ.OpenWire { /// diff --git a/openwire-dotnet/src/ActiveMQ/OpenWire/TransactionContext.cs b/openwire-dotnet/src/ActiveMQ/OpenWire/TransactionContext.cs index 7556c89e9b..f3fb5c5283 100644 --- a/openwire-dotnet/src/ActiveMQ/OpenWire/TransactionContext.cs +++ b/openwire-dotnet/src/ActiveMQ/OpenWire/TransactionContext.cs @@ -1,110 +1,110 @@ -/* - * Copyright 2006 The Apache Software Foundation or its licensors, as - * applicable. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -using System.Collections; -using OpenWire.Client.Commands; -using System; -using OpenWire.Client; - -namespace OpenWire.Client.Core -{ - public enum TransactionType - { - Begin = 0, Prepare = 1, CommitOnePhase = 2, CommitTwoPhase = 3, Rollback = 4, Recover=5, Forget = 6, End = 7 - } - - public class TransactionContext - { - private TransactionId transactionId; - private Session session; - private ArrayList synchronizations = new ArrayList(); - - public TransactionContext(Session session) { - this.session = session; - } - - public TransactionId TransactionId - { - get { return transactionId; } - } - - /// - /// Method AddSynchronization - /// - public void AddSynchronization(ISynchronization synchronization) - { - synchronizations.Add(synchronization); - } - - - public void Begin() - { - if (transactionId == null) - { - transactionId = session.Connection.CreateLocalTransactionId(); - - TransactionInfo info = new TransactionInfo(); - info.ConnectionId = session.Connection.ConnectionId; - info.TransactionId = transactionId; - info.Type = (int) TransactionType.Begin; - session.Connection.OneWay(info); - } - } - - - public void Rollback() - { - if (transactionId != null) - { - TransactionInfo info = new TransactionInfo(); - info.ConnectionId = session.Connection.ConnectionId; - info.TransactionId = transactionId; - info.Type = (int) TransactionType.Rollback; - - transactionId = null; - session.Connection.OneWay(info); - } - - foreach (ISynchronization synchronization in synchronizations) { - synchronization.AfterRollback(); - } - synchronizations.Clear(); - } - - public void Commit() - { - foreach (ISynchronization synchronization in synchronizations) { - synchronization.BeforeCommit(); - } - - if (transactionId != null) - { - TransactionInfo info = new TransactionInfo(); - info.ConnectionId = session.Connection.ConnectionId; - info.TransactionId = transactionId; - info.Type = (int) TransactionType.CommitOnePhase; - - transactionId = null; - session.Connection.OneWay(info); - } - - foreach (ISynchronization synchronization in synchronizations) { - synchronization.AfterCommit(); - } - synchronizations.Clear(); - } - } -} +/* + * Copyright 2006 The Apache Software Foundation or its licensors, as + * applicable. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System.Collections; +using System; + +using ActiveMQ.OpenWire.Commands; + +namespace ActiveMQ.OpenWire +{ + public enum TransactionType + { + Begin = 0, Prepare = 1, CommitOnePhase = 2, CommitTwoPhase = 3, Rollback = 4, Recover=5, Forget = 6, End = 7 + } + + public class TransactionContext + { + private TransactionId transactionId; + private Session session; + private ArrayList synchronizations = new ArrayList(); + + public TransactionContext(Session session) { + this.session = session; + } + + public TransactionId TransactionId + { + get { return transactionId; } + } + + /// + /// Method AddSynchronization + /// + public void AddSynchronization(ISynchronization synchronization) + { + synchronizations.Add(synchronization); + } + + + public void Begin() + { + if (transactionId == null) + { + transactionId = session.Connection.CreateLocalTransactionId(); + + TransactionInfo info = new TransactionInfo(); + info.ConnectionId = session.Connection.ConnectionId; + info.TransactionId = transactionId; + info.Type = (int) TransactionType.Begin; + session.Connection.OneWay(info); + } + } + + + public void Rollback() + { + if (transactionId != null) + { + TransactionInfo info = new TransactionInfo(); + info.ConnectionId = session.Connection.ConnectionId; + info.TransactionId = transactionId; + info.Type = (int) TransactionType.Rollback; + + transactionId = null; + session.Connection.OneWay(info); + } + + foreach (ISynchronization synchronization in synchronizations) { + synchronization.AfterRollback(); + } + synchronizations.Clear(); + } + + public void Commit() + { + foreach (ISynchronization synchronization in synchronizations) { + synchronization.BeforeCommit(); + } + + if (transactionId != null) + { + TransactionInfo info = new TransactionInfo(); + info.ConnectionId = session.Connection.ConnectionId; + info.TransactionId = transactionId; + info.Type = (int) TransactionType.CommitOnePhase; + + transactionId = null; + session.Connection.OneWay(info); + } + + foreach (ISynchronization synchronization in synchronizations) { + synchronization.AfterCommit(); + } + synchronizations.Clear(); + } + } +}