src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/WeakDataFile.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/WeakDataFile.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.org.jvnet.mimepull;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.IOException;
    1.33 +import java.io.RandomAccessFile;
    1.34 +import java.lang.ref.ReferenceQueue;
    1.35 +import java.lang.ref.WeakReference;
    1.36 +import java.util.ArrayList;
    1.37 +import java.util.List;
    1.38 +import java.util.concurrent.Executor;
    1.39 +import java.util.logging.Level;
    1.40 +import java.util.logging.Logger;
    1.41 +
    1.42 +/**
    1.43 + * Removing files based on this
    1.44 + * <a href="http://java.sun.com/developer/technicalArticles/javase/finalization/">article</a>
    1.45 + *
    1.46 + * @author Jitendra Kotamraju
    1.47 + */
    1.48 +final class WeakDataFile extends WeakReference<DataFile> {
    1.49 +
    1.50 +    private static final Logger LOGGER = Logger.getLogger(WeakDataFile.class.getName());
    1.51 +    //private static final int MAX_ITERATIONS = 2;
    1.52 +    private static ReferenceQueue<DataFile> refQueue = new ReferenceQueue<DataFile>();
    1.53 +    private static List<WeakDataFile> refList = new ArrayList<WeakDataFile>();
    1.54 +    private final File file;
    1.55 +    private final RandomAccessFile raf;
    1.56 +    private static boolean hasCleanUpExecutor = false;
    1.57 +    static {
    1.58 +        CleanUpExecutorFactory executorFactory = CleanUpExecutorFactory.newInstance();
    1.59 +        if (executorFactory!=null) {
    1.60 +            if (LOGGER.isLoggable(Level.FINE)) {
    1.61 +                LOGGER.log(Level.FINE, "Initializing clean up executor for MIMEPULL: {0}", executorFactory.getClass().getName());
    1.62 +            }
    1.63 +            Executor executor = executorFactory.getExecutor();
    1.64 +            executor.execute(new Runnable() {
    1.65 +                @Override
    1.66 +                public void run() {
    1.67 +                    WeakDataFile weak;
    1.68 +                    while (true) {
    1.69 +                        try {
    1.70 +                            weak = (WeakDataFile) refQueue.remove();
    1.71 +                            if (LOGGER.isLoggable(Level.FINE)) {
    1.72 +                                LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
    1.73 +                            }
    1.74 +                            weak.close();
    1.75 +                        } catch (InterruptedException e) {
    1.76 +                        }
    1.77 +                    }
    1.78 +                }
    1.79 +            });
    1.80 +            hasCleanUpExecutor = true;
    1.81 +        }
    1.82 +    }
    1.83 +
    1.84 +    WeakDataFile(DataFile df, File file) {
    1.85 +        super(df, refQueue);
    1.86 +        refList.add(this);
    1.87 +        this.file = file;
    1.88 +        try {
    1.89 +            raf = new RandomAccessFile(file, "rw");
    1.90 +        } catch(IOException ioe) {
    1.91 +            throw new MIMEParsingException(ioe);
    1.92 +        }
    1.93 +        if (!hasCleanUpExecutor) {
    1.94 +            drainRefQueueBounded();
    1.95 +        }
    1.96 +    }
    1.97 +
    1.98 +    synchronized void read(long pointer, byte[] buf, int offset, int length ) {
    1.99 +        try {
   1.100 +            raf.seek(pointer);
   1.101 +            raf.readFully(buf, offset, length);
   1.102 +        } catch(IOException ioe) {
   1.103 +            throw new MIMEParsingException(ioe);
   1.104 +        }
   1.105 +    }
   1.106 +
   1.107 +    synchronized long writeTo(long pointer, byte[] data, int offset, int length) {
   1.108 +        try {
   1.109 +            raf.seek(pointer);
   1.110 +            raf.write(data, offset, length);
   1.111 +            return raf.getFilePointer();    // Update pointer for next write
   1.112 +        } catch(IOException ioe) {
   1.113 +            throw new MIMEParsingException(ioe);
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    void close() {
   1.118 +        if (LOGGER.isLoggable(Level.FINE)) {
   1.119 +            LOGGER.log(Level.FINE, "Deleting file = {0}", file.getName());
   1.120 +        }
   1.121 +        refList.remove(this);
   1.122 +        try {
   1.123 +            raf.close();
   1.124 +            boolean deleted = file.delete();
   1.125 +            if (!deleted) {
   1.126 +                if (LOGGER.isLoggable(Level.INFO)) {
   1.127 +                    LOGGER.log(Level.INFO, "File {0} was not deleted", file.getAbsolutePath());
   1.128 +                }
   1.129 +            }
   1.130 +        } catch(IOException ioe) {
   1.131 +            throw new MIMEParsingException(ioe);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    void renameTo(File f) {
   1.136 +        if (LOGGER.isLoggable(Level.FINE)) {
   1.137 +            LOGGER.log(Level.FINE, "Moving file={0} to={1}", new Object[]{file, f});
   1.138 +        }
   1.139 +        refList.remove(this);
   1.140 +        try {
   1.141 +            raf.close();
   1.142 +            boolean renamed = file.renameTo(f);
   1.143 +            if (!renamed) {
   1.144 +                if (LOGGER.isLoggable(Level.INFO)) {
   1.145 +                    LOGGER.log(Level.INFO, "File {0} was not moved to {1}", new Object[] {file.getAbsolutePath(), f.getAbsolutePath()});
   1.146 +                }
   1.147 +            }
   1.148 +        } catch(IOException ioe) {
   1.149 +            throw new MIMEParsingException(ioe);
   1.150 +        }
   1.151 +
   1.152 +    }
   1.153 +
   1.154 +    static void drainRefQueueBounded() {
   1.155 +        WeakDataFile weak;
   1.156 +        while (( weak = (WeakDataFile) refQueue.poll()) != null ) {
   1.157 +            if (LOGGER.isLoggable(Level.FINE)) {
   1.158 +                LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
   1.159 +            }
   1.160 +            weak.close();
   1.161 +        }
   1.162 +    }
   1.163 +}

mercurial