diff --git a/src/main/java/com/profiler/util/DefaultTBaseLocator.java b/src/main/java/com/profiler/util/DefaultTBaseLocator.java index 10bab8ec1..d30d6d832 100644 --- a/src/main/java/com/profiler/util/DefaultTBaseLocator.java +++ b/src/main/java/com/profiler/util/DefaultTBaseLocator.java @@ -3,18 +3,18 @@ package com.profiler.util; import com.profiler.dto.*; import org.apache.thrift.TBase; -public class DefaultTBaseLocator implements TBaseLocator{ - @Override - public TBase lookup(Header header) { - short type = header.getType(); - switch (type) { - case 10: - return new JVMInfoThriftDTO(); - case 20: - return new RequestDataListThriftDTO(); - case 30: - return new RequestThriftDTO(); - } - throw new IllegalArgumentException("Unsupported type:" + type + " " + header); - } +public class DefaultTBaseLocator implements TBaseLocator { + @Override + public TBase lookup(Header header) { + short type = header.getType(); + switch (type) { + case 10: + return new JVMInfoThriftDTO(); + case 20: + return new RequestDataListThriftDTO(); + case 30: + return new RequestThriftDTO(); + } + throw new IllegalArgumentException("Unsupported type:" + type + " " + header); + } } diff --git a/src/main/java/com/profiler/util/HeaderTBaseDeserializer.java b/src/main/java/com/profiler/util/HeaderTBaseDeserializer.java index 5a68b3644..d98fc4438 100644 --- a/src/main/java/com/profiler/util/HeaderTBaseDeserializer.java +++ b/src/main/java/com/profiler/util/HeaderTBaseDeserializer.java @@ -6,89 +6,98 @@ import org.apache.thrift.TException; import org.apache.thrift.protocol.*; import org.apache.thrift.transport.TMemoryInputTransport; - public class HeaderTBaseDeserializer { - private final TProtocol protocol_; - private final TMemoryInputTransport trans_; + + private final TProtocol protocol_; + private final TMemoryInputTransport trans_; - /** - * Create a new TDeserializer that uses the TBinaryProtocol by default. - */ - public HeaderTBaseDeserializer() { - this(new TBinaryProtocol.Factory()); - } + /** + * Create a new TDeserializer that uses the TBinaryProtocol by default. + */ + public HeaderTBaseDeserializer() { + this(new TBinaryProtocol.Factory()); + } - /** - * Create a new TDeserializer. It will use the TProtocol specified by the - * factory that is passed in. - * - * @param protocolFactory Factory to create a protocol - */ - public HeaderTBaseDeserializer(TProtocolFactory protocolFactory) { - trans_ = new TMemoryInputTransport(); - protocol_ = protocolFactory.getProtocol(trans_); - } + /** + * Create a new TDeserializer. It will use the TProtocol specified by the + * factory that is passed in. + * + * @param protocolFactory + * Factory to create a protocol + */ + public HeaderTBaseDeserializer(TProtocolFactory protocolFactory) { + trans_ = new TMemoryInputTransport(); + protocol_ = protocolFactory.getProtocol(trans_); + } - /** - * Deserialize the Thrift object from a byte array. - * - * @param locator The object to read into - * @param bytes The array to read from - */ - public TBase deserialize(TBaseLocator locator, byte[] bytes) throws TException { - try { - trans_.reset(bytes); - Header header = readHeader(); - validate(header); - TBase base = locator.lookup(header); - base.read(protocol_); - return base; - } finally { - trans_.clear(); - protocol_.reset(); - } - } + /** + * Deserialize the Thrift object from a byte array. + * + * @param locator + * The object to read into + * @param bytes + * The array to read from + */ + public TBase deserialize(TBaseLocator locator, byte[] bytes) throws TException { + try { + trans_.reset(bytes); + Header header = readHeader(); + validate(header); + TBase base = locator.lookup(header); + base.read(protocol_); + return base; + } finally { + trans_.clear(); + protocol_.reset(); + } + } - private void validate(Header header) { - boolean accepted = HeaderUtil.validateSignature(header.getSignature()); - if(!accepted) { - throw new IllegalArgumentException("Invalid Signature:" + header); - } - } + private void validate(Header header) { + boolean accepted = HeaderUtil.validateSignature(header.getSignature()); + if (!accepted) { + throw new IllegalArgumentException("Invalid Signature:" + header); + } + } - private Header readHeader() throws TException { - byte signature = protocol_.readByte(); - byte version = protocol_.readByte(); - short type = protocol_.readI16(); - return new Header(signature, version, type); - } + private Header readHeader() throws TException { + byte signature = protocol_.readByte(); + byte version = protocol_.readByte(); + short type = protocol_.readI16(); + return new Header(signature, version, type); + } - /** - * Deserialize the Thrift object from a Java string, using a specified - * character set for decoding. - * - * @param base The object to read into - * @param data The string to read from - * @param charset Valid JVM charset - */ -// public void deserialize(TBase base, String data, String charset) throws TException { -// try { -// deserialize(base, data.getBytes(charset)); -// } catch (UnsupportedEncodingException uex) { -// throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); -// } finally { -// protocol_.reset(); -// } -// } + /** + * Deserialize the Thrift object from a Java string, using a specified + * character set for decoding. + * + * @param base + * The object to read into + * @param data + * The string to read from + * @param charset + * Valid JVM charset + */ + // public void deserialize(TBase base, String data, String charset) throws + // TException { + // try { + // deserialize(base, data.getBytes(charset)); + // } catch (UnsupportedEncodingException uex) { + // throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); + // } finally { + // protocol_.reset(); + // } + // } - /** - * Deserialize the Thrift object from a Java string, using the default JVM - * charset encoding. - * - * @param base The object to read into - * @param data The string to read from - */ -// public void fromString(TBase base, String data) throws TException { -// deserialize(base, data.getBytes()); -// } + /** + * Deserialize the Thrift object from a Java string, using the default JVM + * charset encoding. + * + * @param base + * The object to read into + * @param data + * The string to read from + */ + // public void fromString(TBase base, String data) throws TException { + // deserialize(base, data.getBytes()); + // } } diff --git a/src/main/java/com/profiler/util/HeaderTBaseSerializer.java b/src/main/java/com/profiler/util/HeaderTBaseSerializer.java index ecd78ab9d..759e7abad 100644 --- a/src/main/java/com/profiler/util/HeaderTBaseSerializer.java +++ b/src/main/java/com/profiler/util/HeaderTBaseSerializer.java @@ -3,7 +3,6 @@ package com.profiler.util; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; -import com.profiler.dto.Header; import org.apache.thrift.TBase; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; @@ -11,91 +10,97 @@ import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.protocol.TProtocolFactory; import org.apache.thrift.transport.TIOStreamTransport; +import com.profiler.dto.Header; + /** * Generic utility for easily serializing objects into a byte array or Java * String. - * + * */ public class HeaderTBaseSerializer { - /** - * This is the byte array that data is actually serialized into - */ - private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); + /** + * This is the byte array that data is actually serialized into + */ + private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); - /** - * This transport wraps that byte array - */ - private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_); + /** + * This transport wraps that byte array + */ + private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_); - /** - * Internal protocol used for serializing objects. - */ - private TProtocol protocol_; + /** + * Internal protocol used for serializing objects. + */ + private TProtocol protocol_; - /** - * Create a new TSerializer that uses the TBinaryProtocol by default. - */ - public HeaderTBaseSerializer() { - this(new TBinaryProtocol.Factory()); - } + /** + * Create a new TSerializer that uses the TBinaryProtocol by default. + */ + public HeaderTBaseSerializer() { + this(new TBinaryProtocol.Factory()); + } - /** - * Create a new TSerializer. It will use the TProtocol specified by the - * factory that is passed in. - * - * @param protocolFactory Factory to create a protocol - */ - public HeaderTBaseSerializer(TProtocolFactory protocolFactory) { - protocol_ = protocolFactory.getProtocol(transport_); - } + /** + * Create a new TSerializer. It will use the TProtocol specified by the + * factory that is passed in. + * + * @param protocolFactory + * Factory to create a protocol + */ + public HeaderTBaseSerializer(TProtocolFactory protocolFactory) { + protocol_ = protocolFactory.getProtocol(transport_); + } - /** - * Serialize the Thrift object into a byte array. The process is simple, - * just clear the byte array output, write the object into it, and grab the - * raw bytes. - * - * @param base The object to serialize - * @return Serialized object in byte[] format - */ - public byte[] serialize(Header header, TBase base) throws TException { - baos_.reset(); - writeHeader(header); - base.write(protocol_); - return baos_.toByteArray(); - } + /** + * Serialize the Thrift object into a byte array. The process is simple, + * just clear the byte array output, write the object into it, and grab the + * raw bytes. + * + * @param base + * The object to serialize + * @return Serialized object in byte[] format + */ + public byte[] serialize(Header header, TBase base) throws TException { + baos_.reset(); + writeHeader(header); + base.write(protocol_); + return baos_.toByteArray(); + } - private void writeHeader(Header header) throws TException { - protocol_.writeByte(header.getSignature()); - protocol_.writeByte(header.getVersion()); - protocol_.writeI16(header.getType()); - } + private void writeHeader(Header header) throws TException { + protocol_.writeByte(header.getSignature()); + protocol_.writeByte(header.getVersion()); + protocol_.writeI16(header.getType()); + } - /** - * Serialize the Thrift object into a Java string, using a specified - * character set for encoding. - * - * @param base The object to serialize - * @param charset Valid JVM charset - * @return Serialized object as a String - */ - public String toString(Header header, TBase base, String charset) throws TException { - try { - return new String(serialize(header, base), charset); - } catch (UnsupportedEncodingException uex) { - throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); - } - } + /** + * Serialize the Thrift object into a Java string, using a specified + * character set for encoding. + * + * @param base + * The object to serialize + * @param charset + * Valid JVM charset + * @return Serialized object as a String + */ + public String toString(Header header, TBase base, String charset) throws TException { + try { + return new String(serialize(header, base), charset); + } catch (UnsupportedEncodingException uex) { + throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); + } + } - /** - * Serialize the Thrift object into a Java string, using the default JVM - * charset encoding. - * - * @param base The object to serialize - * @return Serialized object as a String - */ - public String toString(Header header, TBase base) throws TException { - return new String(serialize(header, base)); - } + /** + * Serialize the Thrift object into a Java string, using the default JVM + * charset encoding. + * + * @param base + * The object to serialize + * @return Serialized object as a String + */ + public String toString(Header header, TBase base) throws TException { + return new String(serialize(header, base)); + } } -