KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2005 Works, Inc. http://www.works.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  * Works, Inc.
24  * 6034 West Courtyard Drive
25  * Suite 210
26  * Austin, TX 78730-5032
27  * USA
28  * http://www.works.com/
29  */

30
31 /*
32  * Licensed to JasperSoft Corporation under a Contributer Agreement
33  */

34 package net.sf.jasperreports.engine.fill;
35
36 import java.io.BufferedInputStream JavaDoc;
37 import java.io.BufferedOutputStream JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.FileInputStream JavaDoc;
40 import java.io.FileNotFoundException JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import net.sf.jasperreports.engine.JRRuntimeException;
45 import net.sf.jasperreports.engine.JRVirtualizable;
46 import net.sf.jasperreports.engine.util.JRProperties;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51 /**
52  * Virtualizes data to the filesystem. When this object is finalized, it removes
53  * the swap files it makes. The virtualized objects have references to this
54  * object, so finalization does not occur until this object and the objects
55  * using it are only weakly referenced.
56  *
57  * @author John Bindel
58  * @version $Id: JRFileVirtualizer.java 1327 2006-07-06 16:43:55 +0300 (Thu, 06 Jul 2006) teodord $
59  */

60 public class JRFileVirtualizer extends JRAbstractLRUVirtualizer {
61     
62     private static final Log log = LogFactory.getLog(JRFileVirtualizer.class);
63
64     
65     /**
66      * Property used to decide whether {@link File#deleteOnExit() deleteOnExit} should be requested
67      * for temporary files created by the virtualizer.
68      * <p>
69      * Calling {@link File#deleteOnExit() File.deleteOnExit()} will accumulate JVM process memory
70      * (see this <a HREF="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817">bug</a>), and this
71      * should abviously be avoided in long-running applications.
72      * <p>
73      * Temporary files will be deleted by explicitly calling {@link #cleanup() cleanup()} or from the virtualizer
74      * <code>finalize()</code> method.
75      */

76     public static final String JavaDoc PROPERTY_TEMP_FILES_SET_DELETE_ON_EXIT = JRProperties.PROPERTY_PREFIX + "virtualizer.files.delete.on.exit";
77
78     private final String JavaDoc directory;
79
80     /**
81      * Uses the process's working directory as the location to store files.
82      *
83      * @param maxSize
84      * the maximum size (in JRVirtualizable objects) of the paged in
85      * cache.
86      */

87     public JRFileVirtualizer(int maxSize) {
88         this(maxSize, null);
89     }
90
91     /**
92      * @param maxSize
93      * the maximum size (in JRVirtualizable objects) of the paged in
94      * cache.
95      * @param directory
96      * the base directory in the filesystem where the paged out data
97      * is to be stored
98      */

99     public JRFileVirtualizer(int maxSize, String JavaDoc directory) {
100         super(maxSize);
101         
102         this.directory = directory;
103     }
104
105     private String JavaDoc makeFilename(JRVirtualizable o) {
106         String JavaDoc uid = o.getUID();
107         return "virt" + uid;
108     }
109
110     private String JavaDoc makeFilename(String JavaDoc virtualId) {
111         return "virt" + virtualId;
112     }
113
114     protected void pageOut(JRVirtualizable o) throws IOException JavaDoc {
115         // Store data to a file.
116
String JavaDoc filename = makeFilename(o);
117         File JavaDoc file = new File JavaDoc(directory, filename);
118         
119         if (file.createNewFile()) {
120             if (JRProperties.getBooleanProperty(PROPERTY_TEMP_FILES_SET_DELETE_ON_EXIT)) {
121                 file.deleteOnExit();
122             }
123
124             FileOutputStream JavaDoc fos = null;
125             try {
126                 fos = new FileOutputStream JavaDoc(file);
127                 BufferedOutputStream JavaDoc bufferedOut = new BufferedOutputStream JavaDoc(fos);
128                 writeData(o, bufferedOut);
129             }
130             catch (FileNotFoundException JavaDoc e) {
131                 log.error("Error virtualizing object", e);
132                 throw new JRRuntimeException(e);
133             }
134             finally {
135                 if (fos != null) {
136                     fos.close();
137                 }
138             }
139         } else {
140             if (!isReadOnly(o)) {
141                 throw new IllegalStateException JavaDoc(
142                         "Cannot virtualize data because the file \"" + filename
143                                 + "\" already exists.");
144             }
145         }
146     }
147
148     protected void pageIn(JRVirtualizable o) throws IOException JavaDoc {
149         // Load data from a file.
150
String JavaDoc filename = makeFilename(o);
151         File JavaDoc file = new File JavaDoc(directory, filename);
152
153         FileInputStream JavaDoc fis = null;
154         try {
155             fis = new FileInputStream JavaDoc(file);
156             BufferedInputStream JavaDoc bufferedIn = new BufferedInputStream JavaDoc(fis);
157             readData(o, bufferedIn);
158         }
159         catch (FileNotFoundException JavaDoc e) {
160             log.error("Error devirtualizing object", e);
161             throw new JRRuntimeException(e);
162         }
163         finally {
164             if (fis != null) {
165                 fis.close();
166             }
167         }
168
169         if (!isReadOnly(o)) {
170             // Wait until we know it worked before tossing the data.
171
file.delete();
172         }
173     }
174
175     protected void dispose(String JavaDoc virtualId) {
176         String JavaDoc filename = makeFilename(virtualId);
177         File JavaDoc file = new File JavaDoc(directory, filename);
178         file.delete();
179     }
180     
181     
182     /**
183      * Called when we are done with the virtualizer and wish to
184      * cleanup any resources it has.
185      */

186     public synchronized void cleanup()
187     {
188         disposeAll();
189         reset();
190     }
191 }
192
Popular Tags