KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRSwapFileVirtualizer


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.fill;
29
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import net.sf.jasperreports.engine.JRVirtualizable;
38 import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer;
39 import net.sf.jasperreports.engine.util.JRSwapFile;
40
41
42 /**
43  * A virtualizer that uses a single swap file to serialize virtual data.
44  *
45  * @author Lucian Chirita (lucianc@users.sourceforge.net)
46  * @version $Id: JRSwapFileVirtualizer.java 1256 2006-05-15 12:00:34 +0300 (Mon, 15 May 2006) lucianc $
47  */

48 public class JRSwapFileVirtualizer extends JRAbstractLRUVirtualizer
49 {
50     private final JRSwapFile swap;
51     private final boolean swapOwner;
52     private final Map JavaDoc handles;
53     
54     
55     /**
56      * Creates a virtualizer that uses a swap file.
57      * <p>
58      * The virtualizer will be considered the owner of the swap file.
59      *
60      * @param maxSize the maximum size (in JRVirtualizable objects) of the paged in cache.
61      * @param swap the swap file to use for data virtualization
62      */

63     public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap)
64     {
65         this(maxSize, swap, true);
66     }
67
68     
69     /**
70      * Creates a virtualizer that uses a swap file.
71      *
72      * @param maxSize the maximum size (in JRVirtualizable objects) of the paged in cache.
73      * @param swap the swap file to use for data virtualization
74      * @param swapOwner whether the virtualizer is the owner (single user) of the swap file.
75      * If <code>true</code>, the virtualizer will dispose the swap file on
76      * {@link #cleanup() cleanup}.
77      */

78     public JRSwapFileVirtualizer(int maxSize, JRSwapFile swap, boolean swapOwner)
79     {
80         super(maxSize);
81
82         this.swap = swap;
83         this.swapOwner = swapOwner;
84         handles = Collections.synchronizedMap(new HashMap JavaDoc());
85     }
86
87     protected void pageOut(JRVirtualizable o) throws IOException JavaDoc
88     {
89         if (!handles.containsKey(o.getUID()))
90         {
91             ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc(3000);
92             writeData(o, bout);
93             byte[] data = bout.toByteArray();
94             
95             JRSwapFile.SwapHandle handle = swap.write(data);
96             handles.put(o.getUID(), handle);
97         }
98         else
99         {
100             if (!isReadOnly(o))
101             {
102                 throw new IllegalStateException JavaDoc("Cannot virtualize data because the data for object UID \"" + o.getUID() + "\" already exists.");
103             }
104         }
105     }
106
107     protected void pageIn(JRVirtualizable o) throws IOException JavaDoc
108     {
109         JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles.get(o.getUID());
110         byte[] data = swap.read(handle, !isReadOnly(o));
111
112         readData(o, new ByteArrayInputStream JavaDoc(data));
113         
114         if (!isReadOnly(o))
115         {
116             handles.remove(o.getUID());
117         }
118     }
119
120     protected void dispose(String JavaDoc id)
121     {
122         JRSwapFile.SwapHandle handle = (JRSwapFile.SwapHandle) handles.remove(id);
123         if (handle != null)
124         {
125             swap.free(handle);
126         }
127     }
128
129     
130     /**
131      * Disposes the swap file used if this virtualizer owns it.
132      * @see #JRSwapFileVirtualizer(int, JRSwapFile, boolean)
133      */

134     public void cleanup()
135     {
136         handles.clear();
137         if (swapOwner)
138         {
139             swap.dispose();
140         }
141     }
142 }
143
Popular Tags