Created
February 3, 2014 12:06
-
-
Save vhmolinar/8782776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Object deserialize(MessageType msgType, byte[] frame) { | |
TBase<?, ?> thriftObj; | |
if (msgType == MessageType.ErrorReport) { | |
thriftObj = new ErrorReport(); | |
} else { | |
thriftObj = ThriftMessageFactory.create(msgType); | |
if (thriftObj == null) | |
return ReportType.UNKNOWN_MESSAGE_TYPE; | |
} | |
TDeserializer deserializer = new TDeserializer(protoFactory); | |
try { | |
deserializer.deserialize(thriftObj, frame); | |
} catch (TException e) { | |
logger.error(e.getMessage() + " on message type: " + msgType, e); | |
return ReportType.UNSERIALIZABLE_MESSAGE; | |
} | |
return thriftObj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.neoex.db_communication.data_acess.utils.serializers; | |
import java.nio.ByteBuffer; | |
import me.prettyprint.cassandra.serializers.AbstractSerializer; | |
import org.apache.thrift.TBase; | |
import org.apache.thrift.TDeserializer; | |
import org.apache.thrift.TException; | |
import org.apache.thrift.TSerializer; | |
import org.apache.thrift.protocol.TBinaryProtocol; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
@SuppressWarnings("rawtypes") | |
public class ThriftSerializer<T extends TBase> extends AbstractSerializer<T> { | |
private Class<T> type; | |
private final static Logger log = LoggerFactory.getLogger( ThriftSerializer.class ); | |
/** | |
* Construtor | |
*/ | |
public ThriftSerializer( Class<T> type ) { | |
this.type = type; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* me.prettyprint.cassandra.serializers.AbstractSerializer#toByteBuffer( | |
* java.lang.Object) | |
*/ | |
@Override | |
public ByteBuffer toByteBuffer( T obj ) { | |
TSerializer serializer = new TSerializer( new TBinaryProtocol.Factory() ); | |
try { | |
byte[] bytes = serializer.serialize( obj ); | |
return ByteBuffer.wrap( bytes ); | |
} catch ( TException e ) { | |
log.error( e.getMessage(), e ); | |
return null; | |
} | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* me.prettyprint.cassandra.serializers.AbstractSerializer#fromByteBuffer | |
* (java.nio.ByteBuffer) | |
*/ | |
@Override | |
public T fromByteBuffer( ByteBuffer byteBuffer ) { | |
TDeserializer deserializer = new TDeserializer( new TBinaryProtocol.Factory() ); | |
byte[] bytes = new byte[ byteBuffer.remaining() ]; | |
byteBuffer.get( bytes, 0, bytes.length ); | |
T result; | |
try { | |
result = this.type.newInstance(); | |
deserializer.deserialize( result, bytes ); | |
return result; | |
} catch ( InstantiationException | IllegalAccessException | TException e ) { | |
log.error( e.getMessage(), e ); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment