001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.fill;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.util.Collections;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import net.sf.jasperreports.engine.JRVirtualizable;
038: import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer;
039: import net.sf.jasperreports.engine.util.JRSwapFile;
040:
041: /**
042: * A virtualizer that uses a single swap file to serialize virtual data.
043: *
044: * @author Lucian Chirita (lucianc@users.sourceforge.net)
045: * @version $Id: JRSwapFileVirtualizer.java 1256 2006-05-15 09:00:34Z lucianc $
046: */
047: public class JRSwapFileVirtualizer extends JRAbstractLRUVirtualizer {
048: private final JRSwapFile swap;
049: private final boolean swapOwner;
050: private final Map handles;
051:
052: /**
053: * Creates a virtualizer that uses a swap file.
054: * <p>
055: * The virtualizer will be considered the owner of the swap file.
056: *
057: * @param maxSize the maximum size (in JRVirtualizable objects) of the paged in cache.
058: * @param swap the swap file to use for data virtualization
059: */
060: public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap) {
061: this (maxSize, swap, true);
062: }
063:
064: /**
065: * Creates a virtualizer that uses a swap file.
066: *
067: * @param maxSize the maximum size (in JRVirtualizable objects) of the paged in cache.
068: * @param swap the swap file to use for data virtualization
069: * @param swapOwner whether the virtualizer is the owner (single user) of the swap file.
070: * If <code>true</code>, the virtualizer will dispose the swap file on
071: * {@link #cleanup() cleanup}.
072: */
073: public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap,
074: boolean swapOwner) {
075: super (maxSize);
076:
077: this .swap = swap;
078: this .swapOwner = swapOwner;
079: handles = Collections.synchronizedMap(new HashMap());
080: }
081:
082: protected void pageOut(JRVirtualizable o) throws IOException {
083: if (!handles.containsKey(o.getUID())) {
084: ByteArrayOutputStream bout = new ByteArrayOutputStream(3000);
085: writeData(o, bout);
086: byte[] data = bout.toByteArray();
087:
088: JRSwapFile.SwapHandle handle = swap.write(data);
089: handles.put(o.getUID(), handle);
090: } else {
091: if (!isReadOnly(o)) {
092: throw new IllegalStateException(
093: "Cannot virtualize data because the data for object UID \""
094: + o.getUID() + "\" already exists.");
095: }
096: }
097: }
098:
099: protected void pageIn(JRVirtualizable o) throws IOException {
100: JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles
101: .get(o.getUID());
102: byte[] data = swap.read(handle, !isReadOnly(o));
103:
104: readData(o, new ByteArrayInputStream(data));
105:
106: if (!isReadOnly(o)) {
107: handles.remove(o.getUID());
108: }
109: }
110:
111: protected void dispose(String id) {
112: JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles
113: .remove(id);
114: if (handle != null) {
115: swap.free(handle);
116: }
117: }
118:
119: /**
120: * Disposes the swap file used if this virtualizer owns it.
121: * @see #JRSwapFileVirtualizer(int, JRSwapFile, boolean)
122: */
123: public void cleanup() {
124: handles.clear();
125: if (swapOwner) {
126: swap.dispose();
127: }
128: }
129: }
|