< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.org.jvnet.mimepull;
  27 



  28 import java.util.concurrent.TimeUnit;
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.RandomAccessFile;
  33 import java.lang.ref.ReferenceQueue;
  34 import java.lang.ref.WeakReference;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.concurrent.ScheduledExecutorService;
  38 import java.util.logging.Level;
  39 import java.util.logging.Logger;
  40 
  41 /**
  42  * Removing files based on this
  43  * <a href="http://java.sun.com/developer/technicalArticles/javase/finalization/">article</a>
  44  *
  45  * @author Jitendra Kotamraju
  46  */
  47 final class WeakDataFile extends WeakReference<DataFile> {


 116         try {
 117             raf.close();
 118             boolean deleted = file.delete();
 119             if (!deleted) {
 120                 if (LOGGER.isLoggable(Level.INFO)) {
 121                     LOGGER.log(Level.INFO, "File {0} was not deleted", file.getAbsolutePath());
 122                 }
 123             }
 124         } catch(IOException ioe) {
 125             throw new MIMEParsingException(ioe);
 126         }
 127     }
 128 
 129     void renameTo(File f) {
 130         if (LOGGER.isLoggable(Level.FINE)) {
 131             LOGGER.log(Level.FINE, "Moving file={0} to={1}", new Object[]{file, f});
 132         }
 133         refList.remove(this);
 134         try {
 135             raf.close();
 136             boolean renamed = file.renameTo(f);

 137             if (!renamed) {
 138                 if (LOGGER.isLoggable(Level.INFO)) {
 139                     LOGGER.log(Level.INFO, "File {0} was not moved to {1}", new Object[] {file.getAbsolutePath(), f.getAbsolutePath()});

 140                 }
 141             }
 142         } catch(IOException ioe) {
 143             throw new MIMEParsingException(ioe);
 144         }
 145 
 146     }
 147 
 148     static void drainRefQueueBounded() {
 149         WeakDataFile weak;
 150         while (( weak = (WeakDataFile) refQueue.poll()) != null ) {
 151             if (LOGGER.isLoggable(Level.FINE)) {
 152                 LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
 153             }
 154             weak.close();
 155         }
 156     }
 157 
 158 private static class CleanupRunnable implements Runnable {
 159     @Override
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.org.jvnet.mimepull;
  27 
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.StandardCopyOption;
  31 import java.util.concurrent.TimeUnit;
  32 
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.io.RandomAccessFile;
  36 import java.lang.ref.ReferenceQueue;
  37 import java.lang.ref.WeakReference;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.concurrent.ScheduledExecutorService;
  41 import java.util.logging.Level;
  42 import java.util.logging.Logger;
  43 
  44 /**
  45  * Removing files based on this
  46  * <a href="http://java.sun.com/developer/technicalArticles/javase/finalization/">article</a>
  47  *
  48  * @author Jitendra Kotamraju
  49  */
  50 final class WeakDataFile extends WeakReference<DataFile> {


 119         try {
 120             raf.close();
 121             boolean deleted = file.delete();
 122             if (!deleted) {
 123                 if (LOGGER.isLoggable(Level.INFO)) {
 124                     LOGGER.log(Level.INFO, "File {0} was not deleted", file.getAbsolutePath());
 125                 }
 126             }
 127         } catch(IOException ioe) {
 128             throw new MIMEParsingException(ioe);
 129         }
 130     }
 131 
 132     void renameTo(File f) {
 133         if (LOGGER.isLoggable(Level.FINE)) {
 134             LOGGER.log(Level.FINE, "Moving file={0} to={1}", new Object[]{file, f});
 135         }
 136         refList.remove(this);
 137         try {
 138             raf.close();
 139             Path target = Files.move(file.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
 140             boolean renamed = f.toPath().equals(target);
 141             if (!renamed) {
 142                 if (LOGGER.isLoggable(Level.INFO)) {
 143                     throw new MIMEParsingException("File " + file.getAbsolutePath() +
 144                             " was not moved to " + f.getAbsolutePath());
 145                 }
 146             }
 147         } catch(IOException ioe) {
 148             throw new MIMEParsingException(ioe);
 149         }
 150 
 151     }
 152 
 153     static void drainRefQueueBounded() {
 154         WeakDataFile weak;
 155         while (( weak = (WeakDataFile) refQueue.poll()) != null ) {
 156             if (LOGGER.isLoggable(Level.FINE)) {
 157                 LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
 158             }
 159             weak.close();
 160         }
 161     }
 162 
 163 private static class CleanupRunnable implements Runnable {
 164     @Override
< prev index next >