All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.pageserializer.kryo.DebuggingKryo Maven / Gradle / Ivy

There is a newer version: 8.0.0-M9
Show newest version
package org.wicketstuff.pageserializer.kryo;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.util.lang.Args;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.javakaffee.kryoserializers.KryoReflectionFactorySupport;

/**
 * An extension of {@link KryoReflectionFactorySupport} that logs the serialized objects and the
 * current size of the buffer after the write. Additionally provides the functionality to blacklist
 * the serialization of specific classes.
 */
public class DebuggingKryo extends KryoReflectionFactorySupport
{

	private final static Logger LOG = LoggerFactory.getLogger(DebuggingKryo.class);

	private final List> blackList;

	public DebuggingKryo()
	{
		blackList = new ArrayList>();
	}

	public DebuggingKryo blacklist(final Class... classes)
	{
		Args.notNull(classes, "classes");

		for (Class cls : classes)
		{
			blackList.add(cls);
		}
		return this;
	}

	@Override
	public void writeClassAndObject(ByteBuffer buffer, Object object)
	{

		if (object != null)
		{
			Class target = object.getClass();
			for (Class cls : blackList)
			{
				if (cls.isAssignableFrom(target))
				{
					throw new IllegalArgumentException("Should not serialize class with type: " +
						cls.getName());
				}
			}
		}
		super.writeClassAndObject(buffer, object);

		if (object != null)
		{
			LOG.error("Wrote '{}' bytes for object: '{}'", buffer.position(), object.getClass());
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy