src/share/classes/sun/font/CreatedFontTracker.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 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


  96     public void set(File file, OutputStream os) {
  97         TempFileDeletionHook.set(file, os);
  98     }
  99 
 100     public void remove(File file) {
 101         TempFileDeletionHook.remove(file);
 102     }
 103 
 104     /**
 105      * Helper class for cleanup of temp files created while processing fonts.
 106      * Note that this only applies to createFont() from an InputStream object.
 107      */
 108     private static class TempFileDeletionHook {
 109         private static HashMap<File, OutputStream> files = new HashMap<>();
 110 
 111         private static Thread t = null;
 112         static void init() {
 113             if (t == null) {
 114                 // Add a shutdown hook to remove the temp file.
 115                 java.security.AccessController.doPrivileged(
 116                    new java.security.PrivilegedAction() {
 117                       public Object run() {
 118                           /* The thread must be a member of a thread group
 119                            * which will not get GCed before VM exit.
 120                            * Make its parent the top-level thread group.
 121                            */
 122                           ThreadGroup tg =
 123                               Thread.currentThread().getThreadGroup();
 124                           for (ThreadGroup tgn = tg;
 125                                tgn != null;
 126                                tg = tgn, tgn = tg.getParent());
 127                           t = new Thread(tg, new Runnable() {
 128                               public void run() {
 129                                   runHooks();
 130                               }
 131                           });
 132                           t.setContextClassLoader(null);
 133                           Runtime.getRuntime().addShutdownHook(t);
 134                           return null;
 135                       }
 136                    });


   1 /*
   2  * Copyright (c) 2008, 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


  96     public void set(File file, OutputStream os) {
  97         TempFileDeletionHook.set(file, os);
  98     }
  99 
 100     public void remove(File file) {
 101         TempFileDeletionHook.remove(file);
 102     }
 103 
 104     /**
 105      * Helper class for cleanup of temp files created while processing fonts.
 106      * Note that this only applies to createFont() from an InputStream object.
 107      */
 108     private static class TempFileDeletionHook {
 109         private static HashMap<File, OutputStream> files = new HashMap<>();
 110 
 111         private static Thread t = null;
 112         static void init() {
 113             if (t == null) {
 114                 // Add a shutdown hook to remove the temp file.
 115                 java.security.AccessController.doPrivileged(
 116                    new java.security.PrivilegedAction<Object>() {
 117                       public Object run() {
 118                           /* The thread must be a member of a thread group
 119                            * which will not get GCed before VM exit.
 120                            * Make its parent the top-level thread group.
 121                            */
 122                           ThreadGroup tg =
 123                               Thread.currentThread().getThreadGroup();
 124                           for (ThreadGroup tgn = tg;
 125                                tgn != null;
 126                                tg = tgn, tgn = tg.getParent());
 127                           t = new Thread(tg, new Runnable() {
 128                               public void run() {
 129                                   runHooks();
 130                               }
 131                           });
 132                           t.setContextClassLoader(null);
 133                           Runtime.getRuntime().addShutdownHook(t);
 134                           return null;
 135                       }
 136                    });