net.sf.jasperreports.engine.util.VirtualizationSerializer Maven / Gradle / Ivy
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRVirtualizable;
import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer;
import net.sf.jasperreports.engine.virtualization.VirtualizationInput;
import net.sf.jasperreports.engine.virtualization.VirtualizationOutput;
/**
* @author Lucian Chirita ([email protected])
* @version $Id: VirtualizationSerializer.java 6168 2013-05-16 17:27:14Z lucianc $
*/
public class VirtualizationSerializer
{
protected static final int CLASSLOADER_IDX_NOT_SET = -1;
protected static boolean isAncestorClassLoader(ClassLoader loader)
{
for (
ClassLoader ancestor = JRAbstractLRUVirtualizer.class.getClassLoader();
ancestor != null;
ancestor = ancestor.getParent())
{
if (ancestor.equals(loader))
{
return true;
}
}
return false;
}
protected final Map classLoadersIndexes = new HashMap();
protected final List classLoadersList = new ArrayList();
protected final Map, Integer> classIndexes = new HashMap, Integer>();
protected final List> classes = new ArrayList>();
public int getClassloaderIdx(Class clazz)
{
ClassLoader classLoader = clazz.getClassLoader();
int loaderIdx;
if (clazz.isPrimitive()
|| classLoader == null
|| isAncestorClassLoader(classLoader))
{
loaderIdx = CLASSLOADER_IDX_NOT_SET;
}
else
{
Integer idx = classLoadersIndexes.get(classLoader);
if (idx == null)
{
idx = Integer.valueOf(classLoadersList.size());
classLoadersIndexes.put(classLoader, idx);
classLoadersList.add(classLoader);
}
loaderIdx = idx.intValue();
}
return loaderIdx;
}
public Class resolveClass(ObjectStreamClass desc, int loaderIdx) throws ClassNotFoundException
{
if (loaderIdx == CLASSLOADER_IDX_NOT_SET)
{
return null;
}
ClassLoader loader = classLoadersList.get(loaderIdx);
Class clazz = Class.forName(desc.getName(), false, loader);
return clazz;
}
public int getClassDescriptorIdx(Class clazz)
{
Integer classIdx = classIndexes.get(clazz);
if (classIdx == null)
{
classIdx = classIndexes.size();
classIndexes.put(clazz, classIdx);
classes.add(clazz);
}
return classIdx;
}
public Class getClassForDescriptorIdx(int descriptorIdx)
{
return classes.get(descriptorIdx);
}
public final void writeData(JRVirtualizable o, OutputStream out) throws IOException
{
VirtualizationOutput oos = new VirtualizationOutput(out, this, o.getContext());
Object virtualData = o.getVirtualData();
oos.writeJRObject(virtualData);
oos.flush();
}
public final void readData(JRVirtualizable o, InputStream in) throws IOException
{
@SuppressWarnings("resource")
VirtualizationInput ois = new VirtualizationInput(in, this, o.getContext());
o.setVirtualData(ois.readJRObject());
}
}