org.jvnet.mimepull.WeakDataFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jersey-min Show documentation
Show all versions of jersey-min Show documentation
jersey-min is a rebundled (minmal) verison of Jersey as one OSGi bundle.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.mimepull;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Removing files based on this
* article
*
* @author Jitendra Kotamraju
*/
final class WeakDataFile extends WeakReference {
private static final Logger LOGGER = Logger.getLogger(WeakDataFile.class.getName());
//private static final int MAX_ITERATIONS = 2;
private static ReferenceQueue refQueue = new ReferenceQueue();
private static List refList = new ArrayList();
private final File file;
private final RandomAccessFile raf;
private static boolean hasCleanUpExecutor = false;
static {
CleanUpExecutorFactory executorFactory = CleanUpExecutorFactory.newInstance();
if (executorFactory!=null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Initializing clean up executor for MIMEPULL: {0}", executorFactory.getClass().getName());
}
Executor executor = executorFactory.getExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
WeakDataFile weak;
while (true) {
try {
weak = (WeakDataFile) refQueue.remove();
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
}
weak.close();
} catch (InterruptedException e) {
}
}
}
});
hasCleanUpExecutor = true;
}
}
WeakDataFile(DataFile df, File file) {
super(df, refQueue);
refList.add(this);
this.file = file;
try {
raf = new RandomAccessFile(file, "rw");
} catch(IOException ioe) {
throw new MIMEParsingException(ioe);
}
if (!hasCleanUpExecutor) {
drainRefQueueBounded();
}
}
synchronized void read(long pointer, byte[] buf, int offset, int length ) {
try {
raf.seek(pointer);
raf.readFully(buf, offset, length);
} catch(IOException ioe) {
throw new MIMEParsingException(ioe);
}
}
synchronized long writeTo(long pointer, byte[] data, int offset, int length) {
try {
raf.seek(pointer);
raf.write(data, offset, length);
return raf.getFilePointer(); // Update pointer for next write
} catch(IOException ioe) {
throw new MIMEParsingException(ioe);
}
}
void close() {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Deleting file = {0}", file.getName());
}
refList.remove(this);
try {
raf.close();
boolean deleted = file.delete();
if (!deleted) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "File {0} was not deleted", file.getAbsolutePath());
}
}
} catch(IOException ioe) {
throw new MIMEParsingException(ioe);
}
}
void renameTo(File f) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Moving file={0} to={1}", new Object[]{file, f});
}
refList.remove(this);
try {
raf.close();
boolean renamed = file.renameTo(f);
if (!renamed) {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "File {0} was not moved to {1}", new Object[] {file.getAbsolutePath(), f.getAbsolutePath()});
}
}
} catch(IOException ioe) {
throw new MIMEParsingException(ioe);
}
}
static void drainRefQueueBounded() {
WeakDataFile weak;
while (( weak = (WeakDataFile) refQueue.poll()) != null ) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Cleaning file = {0} from reference queue.", weak.file);
}
weak.close();
}
}
}