jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r330226 - in /incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit: core/value/BLOBFileValue.java core/xml/TargetImportHandler.java util/TransientFileFactory.java
Date Wed, 02 Nov 2005 10:04:35 GMT
Author: stefan
Date: Wed Nov  2 02:04:28 2005
New Revision: 330226

URL: http://svn.apache.org/viewcvs?rev=330226&view=rev
Log:
improved internal temp file handling by introducing new utility class TransientFileFactory

Added:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
  (with props)
Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/TargetImportHandler.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/value/BLOBFileValue.java?rev=330226&r1=330225&r2=330226&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/value/BLOBFileValue.java
Wed Nov  2 02:04:28 2005
@@ -18,7 +18,9 @@
 
 import org.apache.jackrabbit.core.fs.FileSystemException;
 import org.apache.jackrabbit.core.fs.FileSystemResource;
+import org.apache.jackrabbit.util.TransientFileFactory;
 import org.apache.jackrabbit.util.ISO8601;
+import org.apache.jackrabbit.util.TransientFileFactory;
 import org.apache.log4j.Logger;
 
 import javax.jcr.PropertyType;
@@ -125,9 +127,8 @@
                 } else if (len + read > MAX_BUFFER_SIZE) {
                     // threshold for keeping data in memory exceeded;
                     // create temp file and spool buffer contents
-                    spoolFile = File.createTempFile("bin", null);
-                    // @todo FIXME see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513817
-                    spoolFile.deleteOnExit();
+                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+                    spoolFile = fileFactory.createTransientFile("bin", null, null);
                     out = new FileOutputStream(spoolFile);
                     out.write(buffer, 0, len);
                     out.write(spoolBuffer, 0, read);

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/TargetImportHandler.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/TargetImportHandler.java?rev=330226&r1=330225&r2=330226&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/TargetImportHandler.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/TargetImportHandler.java
Wed Nov  2 02:04:28 2005
@@ -17,6 +17,8 @@
 package org.apache.jackrabbit.core.xml;
 
 import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.util.TransientFileFactory;
+import org.apache.jackrabbit.util.TransientFileFactory;
 import org.apache.log4j.Logger;
 import org.xml.sax.helpers.DefaultHandler;
 
@@ -71,7 +73,7 @@
      * appendable.
      * <p/>
      * <b>Important:</b> Note that in order to free resources
-     * <code>{@link #dispose()}</code> should be called as soon as an an
+     * <code>{@link #dispose()}</code> should be called as soon as an
      * <code>AppendableValue</code> object is not used anymore.
      */
     public interface AppendableValue extends Importer.TextValue {
@@ -149,7 +151,7 @@
      * by a temporary file if its size exceeds a certain limit.
      * <p/>
      * <b>Important:</b> Note that in order to free resources
-     * <code>{@link #dispose()}</code> should be called as soon as an
+     * <code>{@link #dispose()}</code> should be called as soon as
      * <code>BufferedStringValue</code> instance is not used anymore.
      */
     protected class BufferedStringValue implements AppendableValue {
@@ -258,7 +260,8 @@
                 if (bufferPos + length > MAX_BUFFER_SIZE) {
                     // threshold for keeping data in memory exceeded;
                     // create temp file and spool buffer contents
-                    tmpFile = File.createTempFile("txt", null);
+                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+                    tmpFile = fileFactory.createTransientFile("txt", null, null);
                     writer = new FileWriter(tmpFile);
                     writer.write(buffer, 0, bufferPos);
                     writer.write(chars, start, length);
@@ -304,7 +307,6 @@
                 bufferPos = 0;
             } else if (tmpFile != null) {
                 writer.close();
-                tmpFile.delete();
                 tmpFile = null;
                 writer = null;
             } else {

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java?rev=330226&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
(added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
Wed Nov  2 02:04:28 2005
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.ref.PhantomReference;
+import java.lang.ref.ReferenceQueue;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * The <code>TransientFileFactory</code> utility class can be used to create
+ * <i>transient</i> files, i.e. temporary files that are automatically
+ * removed once the associated <code>File</code> object is reclaimed by the
+ * garbage collector.
+ * <p/>
+ * File deletion is handled by a low-priority background thread.
+ * <p/>
+ */
+public class TransientFileFactory {
+
+    /**
+     * The singleton factory instance
+     */
+    private static TransientFileFactory INSTANCE;
+
+    /**
+     * Queue where <code>MoribundFileReference</code> instances will be enqueued
+     * once the associated target <code>File</code> objects have been gc'ed.
+     */
+    private ReferenceQueue phantomRefQueue = new ReferenceQueue();
+
+    /**
+     * Collection of <code>MoribundFileReference</code> instances currently
+     * being tracked.
+     */
+    private Collection trackedRefs = new ArrayList();
+
+    /**
+     * The reaper thread responsible for removing files awaiting deletion
+     */
+    private final Thread reaper;
+
+    /**
+     * Returns the singleton <code>TransientFileFactory</code> instance.
+     */
+    public static TransientFileFactory getInstance() {
+        synchronized (TransientFileFactory.class) {
+            if (INSTANCE == null) {
+                INSTANCE = new TransientFileFactory();
+            }
+            return INSTANCE;
+        }
+    }
+
+    /**
+     * Hidden constructor.
+     */
+    private TransientFileFactory() {
+        // instantiate & start low priority reaper thread
+        reaper = new ReaperThread("Transient File Reaper");
+        reaper.setPriority(Thread.MIN_PRIORITY);
+        reaper.setDaemon(true);
+        reaper.start();
+        // register shutdownhook for final cleaning up
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            public void run() {
+                for (Iterator it = trackedRefs.iterator(); it.hasNext();) {
+                    MoribundFileReference fileRef = (MoribundFileReference) it.next();
+                    fileRef.delete();
+                }
+            }
+        });
+    }
+
+    //------------------------------------------------------< factory methods >
+    /**
+     * Same as {@link File#createTempFile(String, String, File)} except that
+     * the newly-created file will be automatically deleted once the
+     * returned <code>File</code> object has been gc'ed.
+     *
+     * @param prefix    The prefix string to be used in generating the file's
+     *                  name; must be at least three characters long
+     * @param suffix    The suffix string to be used in generating the file's
+     *                  name; may be <code>null</code>, in which case the
+     *                  suffix <code>".tmp"</code> will be used
+     * @param directory The directory in which the file is to be created, or
+     *                  <code>null</code> if the default temporary-file
+     *                  directory is to be used
+     * @return the newly-created empty file
+     * @throws IOException If a file could not be created
+     */
+    public File createTransientFile(String prefix, String suffix, File directory)
+            throws IOException {
+        File f = File.createTempFile(prefix, suffix, directory);
+        trackedRefs.add(new MoribundFileReference(f, phantomRefQueue));
+        return f;
+    }
+
+    //--------------------------------------------------------< inner classes >
+    /**
+     * The reaper thread that will remove the files that are ready for deletion.
+     */
+    private class ReaperThread extends Thread {
+
+        ReaperThread(String name) {
+            super(name);
+        }
+
+        /**
+         * Run the reaper thread that will delete files as their associated
+         * marker objects are reclaimed by the garbage collector.
+         */
+        public void run() {
+            while (true) {
+                MoribundFileReference fileRef = null;
+                try {
+                    // wait until a MoribundFileReference is ready for deletion
+                    fileRef = (MoribundFileReference) phantomRefQueue.remove();
+                } catch (Exception e) {
+                    // silently ignore...
+                    continue;
+                }
+                // delete target
+                fileRef.delete();
+                fileRef.clear();
+                trackedRefs.remove(fileRef);
+            }
+        }
+    }
+
+    /**
+     * Tracker object for a file pending deletion.
+     */
+    private class MoribundFileReference extends PhantomReference {
+
+        /**
+         * The full path to the file being tracked.
+         */
+        private String path;
+
+        /**
+         * Constructs an instance of this class from the supplied parameters.
+         *
+         * @param file  The file to be tracked.
+         * @param queue The queue on to which the tracker will be pushed.
+         */
+        MoribundFileReference(File file, ReferenceQueue queue) {
+            super(file, queue);
+            this.path = file.getPath();
+        }
+
+        /**
+         * Deletes the file associated with this instance.
+         *
+         * @return <code>true</code> if the file was deleted successfully;
+         *         <code>false</code> otherwise.
+         */
+        boolean delete() {
+            return new File(path).delete();
+        }
+    }
+}

Propchange: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/util/TransientFileFactory.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url



Mime
View raw message