ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.mimepull; ohair@286: ohair@286: import java.io.File; ohair@286: import java.io.IOException; ohair@286: import java.io.RandomAccessFile; ohair@286: import java.lang.ref.ReferenceQueue; ohair@286: import java.lang.ref.WeakReference; ohair@286: import java.util.ArrayList; ohair@286: import java.util.List; ohair@286: import java.util.concurrent.Executor; alanb@368: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Removing files based on this ohair@286: * article ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: final class WeakDataFile extends WeakReference { ohair@286: ohair@286: private static final Logger LOGGER = Logger.getLogger(WeakDataFile.class.getName()); ohair@286: //private static final int MAX_ITERATIONS = 2; ohair@286: private static ReferenceQueue refQueue = new ReferenceQueue(); ohair@286: private static List refList = new ArrayList(); ohair@286: private final File file; ohair@286: private final RandomAccessFile raf; ohair@286: private static boolean hasCleanUpExecutor = false; ohair@286: static { ohair@286: CleanUpExecutorFactory executorFactory = CleanUpExecutorFactory.newInstance(); ohair@286: if (executorFactory!=null) { alanb@368: if (LOGGER.isLoggable(Level.FINE)) { alanb@368: LOGGER.log(Level.FINE, "Initializing clean up executor for MIMEPULL: {0}", executorFactory.getClass().getName()); alanb@368: } ohair@286: Executor executor = executorFactory.getExecutor(); ohair@286: executor.execute(new Runnable() { alanb@368: @Override ohair@286: public void run() { alanb@368: WeakDataFile weak; ohair@286: while (true) { ohair@286: try { ohair@286: weak = (WeakDataFile) refQueue.remove(); alanb@368: if (LOGGER.isLoggable(Level.FINE)) { alanb@368: LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file); alanb@368: } ohair@286: weak.close(); ohair@286: } catch (InterruptedException e) { ohair@286: } ohair@286: } ohair@286: } ohair@286: }); ohair@286: hasCleanUpExecutor = true; ohair@286: } ohair@286: } ohair@286: ohair@286: WeakDataFile(DataFile df, File file) { ohair@286: super(df, refQueue); ohair@286: refList.add(this); ohair@286: this.file = file; ohair@286: try { ohair@286: raf = new RandomAccessFile(file, "rw"); ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: if (!hasCleanUpExecutor) { ohair@286: drainRefQueueBounded(); ohair@286: } ohair@286: } ohair@286: ohair@286: synchronized void read(long pointer, byte[] buf, int offset, int length ) { ohair@286: try { ohair@286: raf.seek(pointer); ohair@286: raf.readFully(buf, offset, length); ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: } ohair@286: ohair@286: synchronized long writeTo(long pointer, byte[] data, int offset, int length) { ohair@286: try { ohair@286: raf.seek(pointer); ohair@286: raf.write(data, offset, length); ohair@286: return raf.getFilePointer(); // Update pointer for next write ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: } ohair@286: ohair@286: void close() { alanb@368: if (LOGGER.isLoggable(Level.FINE)) { alanb@368: LOGGER.log(Level.FINE, "Deleting file = {0}", file.getName()); alanb@368: } ohair@286: refList.remove(this); ohair@286: try { ohair@286: raf.close(); alanb@368: boolean deleted = file.delete(); alanb@368: if (!deleted) { alanb@368: if (LOGGER.isLoggable(Level.INFO)) { alanb@368: LOGGER.log(Level.INFO, "File {0} was not deleted", file.getAbsolutePath()); alanb@368: } alanb@368: } ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: } ohair@286: ohair@286: void renameTo(File f) { alanb@368: if (LOGGER.isLoggable(Level.FINE)) { alanb@368: LOGGER.log(Level.FINE, "Moving file={0} to={1}", new Object[]{file, f}); alanb@368: } ohair@286: refList.remove(this); ohair@286: try { ohair@286: raf.close(); alanb@368: boolean renamed = file.renameTo(f); alanb@368: if (!renamed) { alanb@368: if (LOGGER.isLoggable(Level.INFO)) { alanb@368: LOGGER.log(Level.INFO, "File {0} was not moved to {1}", new Object[] {file.getAbsolutePath(), f.getAbsolutePath()}); alanb@368: } alanb@368: } ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: static void drainRefQueueBounded() { alanb@368: WeakDataFile weak; ohair@286: while (( weak = (WeakDataFile) refQueue.poll()) != null ) { alanb@368: if (LOGGER.isLoggable(Level.FINE)) { alanb@368: LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file); alanb@368: } ohair@286: weak.close(); ohair@286: } ohair@286: } ohair@286: }