< prev index next >

src/java.desktop/share/classes/com/sun/imageio/stream/StreamCloser.java

Print this page




   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.imageio.stream;
  27 
  28 import sun.awt.util.ThreadGroupUtils;
  29 import sun.misc.ManagedLocalsThread;
  30 
  31 import java.io.IOException;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.util.Set;
  35 import java.util.WeakHashMap;
  36 import javax.imageio.stream.ImageInputStream;
  37 
  38 /**
  39  * This class provide means to properly close hanging
  40  * image input/output streams on VM shutdown.
  41  * This might be useful for proper cleanup such as removal
  42  * of temporary files.
  43  *
  44  * Addition of stream do not prevent it from being garbage collected
  45  * if no other references to it exists. Stream can be closed
  46  * explicitly without removal from StreamCloser queue.
  47  * Explicit removal from the queue only helps to save some memory.
  48  */
  49 public class StreamCloser {


  75                                 actions = set.toArray(actions);
  76                                 for (CloseAction ca : actions) {
  77                                     if (ca != null) {
  78                                         try {
  79                                             ca.performAction();
  80                                         } catch (IOException e) {
  81                                         }
  82                                     }
  83                                 }
  84                             }
  85                         }
  86                     }
  87                 };
  88 
  89                 AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
  90                     /* The thread must be a member of a thread group
  91                      * which will not get GCed before VM exit.
  92                      * Make its parent the top-level thread group.
  93                      */
  94                     ThreadGroup tg = ThreadGroupUtils.getRootThreadGroup();
  95                     streamCloser = new ManagedLocalsThread(tg,
  96                                                            streamCloserRunnable);
  97                     /* Set context class loader to null in order to avoid
  98                      * keeping a strong reference to an application classloader.
  99                      */
 100                     streamCloser.setContextClassLoader(null);
 101                     Runtime.getRuntime().addShutdownHook(streamCloser);
 102                     return null;
 103                 });
 104             }
 105         }
 106     }
 107 
 108     public static void removeFromQueue(CloseAction ca) {
 109         synchronized (StreamCloser.class) {
 110             if (toCloseQueue != null) {
 111                 toCloseQueue.remove(ca);
 112             }
 113         }
 114     }
 115 
 116     public static CloseAction createCloseAction(ImageInputStream iis) {


   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.imageio.stream;
  27 
  28 import sun.awt.util.ThreadGroupUtils;

  29 
  30 import java.io.IOException;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Set;
  34 import java.util.WeakHashMap;
  35 import javax.imageio.stream.ImageInputStream;
  36 
  37 /**
  38  * This class provide means to properly close hanging
  39  * image input/output streams on VM shutdown.
  40  * This might be useful for proper cleanup such as removal
  41  * of temporary files.
  42  *
  43  * Addition of stream do not prevent it from being garbage collected
  44  * if no other references to it exists. Stream can be closed
  45  * explicitly without removal from StreamCloser queue.
  46  * Explicit removal from the queue only helps to save some memory.
  47  */
  48 public class StreamCloser {


  74                                 actions = set.toArray(actions);
  75                                 for (CloseAction ca : actions) {
  76                                     if (ca != null) {
  77                                         try {
  78                                             ca.performAction();
  79                                         } catch (IOException e) {
  80                                         }
  81                                     }
  82                                 }
  83                             }
  84                         }
  85                     }
  86                 };
  87 
  88                 AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
  89                     /* The thread must be a member of a thread group
  90                      * which will not get GCed before VM exit.
  91                      * Make its parent the top-level thread group.
  92                      */
  93                     ThreadGroup tg = ThreadGroupUtils.getRootThreadGroup();
  94                     streamCloser = new Thread(tg, streamCloserRunnable,
  95                                               "StreamCloser", 0, false);
  96                     /* Set context class loader to null in order to avoid
  97                      * keeping a strong reference to an application classloader.
  98                      */
  99                     streamCloser.setContextClassLoader(null);
 100                     Runtime.getRuntime().addShutdownHook(streamCloser);
 101                     return null;
 102                 });
 103             }
 104         }
 105     }
 106 
 107     public static void removeFromQueue(CloseAction ca) {
 108         synchronized (StreamCloser.class) {
 109             if (toCloseQueue != null) {
 110                 toCloseQueue.remove(ca);
 111             }
 112         }
 113     }
 114 
 115     public static CloseAction createCloseAction(ImageInputStream iis) {
< prev index next >