Skip to content

Commit

Permalink
inspection listener
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmosmann committed Oct 22, 2012
1 parent 2819dc6 commit 0cf3fc0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 15 deletions.
Expand Up @@ -2,8 +2,11 @@

public interface ISerializationListener {

void start(Object object);

void begin(int position, Object object);

void end(int position, Object object);

void end(Object object);
}
Expand Up @@ -13,10 +13,10 @@
public class InspectingKryo extends KryoReflectionFactorySupport {

private final static Logger LOG = LoggerFactory.getLogger(InspectingKryo.class);
private final ISerializationListener serializingListener;
private final InspectionKryoSerializer parent;

public InspectingKryo(ISerializationListener serializingListener) {
this.serializingListener = serializingListener;
public InspectingKryo(InspectionKryoSerializer parent) {
this.parent = parent;
}

@Override
Expand Down Expand Up @@ -81,11 +81,11 @@ public void writeObjectOrNull(Output output, Object object,
}

private void before(Output output, Object object) {
serializingListener.begin(output.position(),object);
parent.serializingListener().begin(output.position(),object);
}

private void after(Output output, Object object) {
serializingListener.end(output.position(),object);
parent.serializingListener().end(output.position(),object);
}

}
@@ -0,0 +1,35 @@
package org.wicketstuff.pageserializer.kryo2.inspecting;

import org.apache.wicket.util.lang.Bytes;
import org.wicketstuff.pageserializer.kryo2.KryoSerializer;

import com.esotericsoftware.kryo.Kryo;

public class InspectionKryoSerializer extends KryoSerializer {

private final ISerializationListener serializingListener;

public InspectionKryoSerializer(Bytes size, ISerializationListener serializingListener) {
super(size);
this.serializingListener = serializingListener;
}

@Override
protected Kryo createKryo() {
return new InspectingKryo(this);
}

@Override
public byte[] serialize(Object object) {
try {
serializingListener.start(object);
return super.serialize(object);
} finally {
serializingListener.end(object);
}
}

protected final ISerializationListener serializingListener() {
return serializingListener;
}
}
Expand Up @@ -8,6 +8,12 @@ public class LoggingSerializationListener implements ISerializationListener {
private final static Logger LOG = LoggerFactory
.getLogger(LoggingSerializationListener.class);

@Override
public void start(Object object) {
LOG.error("Start for object: '{}'",
object.getClass());
}

@Override
public void begin(int position, Object object) {
LOG.error("Start at '{}' byte for object: '{}'", position,
Expand All @@ -19,5 +25,11 @@ public void end(int position, Object object) {
LOG.error("End at '{}' bytes for object: '{}'", position,
object != null ? object.getClass() : "NULL");
}

@Override
public void end(Object object) {
LOG.error("End for object: '{}'",
object.getClass());
}

}
Expand Up @@ -6,29 +6,23 @@

import junit.framework.Assert;

import org.apache.wicket.util.lang.Bytes;
import org.junit.Test;

import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class InspectingKryoTest {

@Test
public void writeStringList() {
List<String> stringList=new ArrayList<String>();
stringList.addAll(Arrays.asList("One","2","III"));

InspectingKryo kryo=new InspectingKryo(new LoggingSerializationListener());
InspectionKryoSerializer kryo=new InspectionKryoSerializer(Bytes.bytes(100),new LoggingSerializationListener());

Output buffer = new Output(100);
kryo.writeClassAndObject(buffer, stringList);
byte[] data=buffer.toBytes();
buffer.clear();
System.runFinalization();
byte[] data=kryo.serialize(stringList);

Assert.assertNotNull(data);

List<String> readBack=(List<String>) kryo.readClassAndObject(new Input(data));
List<String> readBack=(List<String>) kryo.deserialize(data);

Assert.assertNotNull(readBack);
Assert.assertEquals(stringList, readBack);
Expand Down

0 comments on commit 0cf3fc0

Please sign in to comment.