Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, 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 */
26 package com.sun.xml.internal.org.jvnet.mimepull;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.RandomAccessFile;
31 import java.lang.ref.ReferenceQueue;
32 import java.lang.ref.WeakReference;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.concurrent.Executor;
36 import java.util.logging.Logger;
38 /**
39 * Removing files based on this
40 * <a href="http://java.sun.com/developer/technicalArticles/javase/finalization/">article</a>
41 *
42 * @author Jitendra Kotamraju
43 */
44 final class WeakDataFile extends WeakReference<DataFile> {
46 private static final Logger LOGGER = Logger.getLogger(WeakDataFile.class.getName());
47 //private static final int MAX_ITERATIONS = 2;
48 private static ReferenceQueue<DataFile> refQueue = new ReferenceQueue<DataFile>();
49 private static List<WeakDataFile> refList = new ArrayList<WeakDataFile>();
50 private final File file;
51 private final RandomAccessFile raf;
52 private static boolean hasCleanUpExecutor = false;
53 static {
54 CleanUpExecutorFactory executorFactory = CleanUpExecutorFactory.newInstance();
55 if (executorFactory!=null) {
56 LOGGER.fine("Initializing clean up executor for MIMEPULL: "
57 + executorFactory.getClass().getName());
58 Executor executor = executorFactory.getExecutor();
59 executor.execute(new Runnable() {
60 public void run() {
61 WeakDataFile weak = null;
62 while (true) {
63 try {
64 weak = (WeakDataFile) refQueue.remove();
65 LOGGER.fine("Cleaning file = "+weak.file+" from reference queue.");
66 weak.close();
67 } catch (InterruptedException e) {
68 }
69 }
70 }
71 });
72 hasCleanUpExecutor = true;
73 }
74 }
76 WeakDataFile(DataFile df, File file) {
77 super(df, refQueue);
78 refList.add(this);
79 this.file = file;
80 try {
81 raf = new RandomAccessFile(file, "rw");
82 } catch(IOException ioe) {
83 throw new MIMEParsingException(ioe);
84 }
85 if (!hasCleanUpExecutor) {
86 drainRefQueueBounded();
87 }
88 }
90 synchronized void read(long pointer, byte[] buf, int offset, int length ) {
91 try {
92 raf.seek(pointer);
93 raf.readFully(buf, offset, length);
94 } catch(IOException ioe) {
95 throw new MIMEParsingException(ioe);
96 }
97 }
99 synchronized long writeTo(long pointer, byte[] data, int offset, int length) {
100 try {
101 raf.seek(pointer);
102 raf.write(data, offset, length);
103 return raf.getFilePointer(); // Update pointer for next write
104 } catch(IOException ioe) {
105 throw new MIMEParsingException(ioe);
106 }
107 }
109 void close() {
110 LOGGER.fine("Deleting file = "+file.getName());
111 refList.remove(this);
112 try {
113 raf.close();
114 file.delete();
115 } catch(IOException ioe) {
116 throw new MIMEParsingException(ioe);
117 }
118 }
120 void renameTo(File f) {
121 LOGGER.fine("Moving file="+file+" to="+f);
122 refList.remove(this);
123 try {
124 raf.close();
125 file.renameTo(f);
126 } catch(IOException ioe) {
127 throw new MIMEParsingException(ioe);
128 }
130 }
132 static void drainRefQueueBounded() {
133 WeakDataFile weak = null;
134 while (( weak = (WeakDataFile) refQueue.poll()) != null ) {
135 LOGGER.fine("Cleaning file = "+weak.file+" from reference queue.");
136 weak.close();
137 }
138 }
139 }