J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.misc; |
| 27 | |
| 28 | import java.util.jar.JarFile; |
| 29 | import java.io.Console; |
| 30 | import java.io.File; |
| 31 | import java.io.FileDescriptor; |
| 32 | |
| 33 | /** A repository of "shared secrets", which are a mechanism for |
| 34 | calling implementation-private methods in another package without |
| 35 | using reflection. A package-private class implements a public |
| 36 | interface and provides the ability to call package-private methods |
| 37 | within that package; the object implementing that interface is |
| 38 | provided through a third package to which access is restricted. |
| 39 | This framework avoids the primary disadvantage of using reflection |
| 40 | for this purpose, namely the loss of compile-time checking. */ |
| 41 | |
| 42 | public class SharedSecrets { |
| 43 | private static final Unsafe unsafe = Unsafe.getUnsafe(); |
| 44 | private static JavaUtilJarAccess javaUtilJarAccess; |
| 45 | private static JavaLangAccess javaLangAccess; |
| 46 | private static JavaIOAccess javaIOAccess; |
| 47 | private static JavaIODeleteOnExitAccess javaIODeleteOnExitAccess; |
| 48 | private static JavaNetAccess javaNetAccess; |
| 49 | private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess; |
| 50 | |
| 51 | public static JavaUtilJarAccess javaUtilJarAccess() { |
| 52 | if (javaUtilJarAccess == null) { |
| 53 | // Ensure JarFile is initialized; we know that that class |
| 54 | // provides the shared secret |
| 55 | unsafe.ensureClassInitialized(JarFile.class); |
| 56 | } |
| 57 | return javaUtilJarAccess; |
| 58 | } |
| 59 | |
| 60 | public static void setJavaUtilJarAccess(JavaUtilJarAccess access) { |
| 61 | javaUtilJarAccess = access; |
| 62 | } |
| 63 | |
| 64 | public static void setJavaLangAccess(JavaLangAccess jla) { |
| 65 | javaLangAccess = jla; |
| 66 | } |
| 67 | |
| 68 | public static JavaLangAccess getJavaLangAccess() { |
| 69 | return javaLangAccess; |
| 70 | } |
| 71 | |
| 72 | public static void setJavaNetAccess(JavaNetAccess jna) { |
| 73 | javaNetAccess = jna; |
| 74 | } |
| 75 | |
| 76 | public static JavaNetAccess getJavaNetAccess() { |
| 77 | return javaNetAccess; |
| 78 | } |
| 79 | |
| 80 | public static void setJavaIOAccess(JavaIOAccess jia) { |
| 81 | javaIOAccess = jia; |
| 82 | } |
| 83 | |
| 84 | public static JavaIOAccess getJavaIOAccess() { |
| 85 | if (javaIOAccess == null) { |
| 86 | unsafe.ensureClassInitialized(Console.class); |
| 87 | } |
| 88 | return javaIOAccess; |
| 89 | } |
| 90 | |
| 91 | public static void setJavaIODeleteOnExitAccess(JavaIODeleteOnExitAccess jida) { |
| 92 | javaIODeleteOnExitAccess = jida; |
| 93 | } |
| 94 | |
| 95 | public static JavaIODeleteOnExitAccess getJavaIODeleteOnExitAccess() { |
| 96 | if (javaIODeleteOnExitAccess == null) { |
| 97 | unsafe.ensureClassInitialized(File.class); |
| 98 | } |
| 99 | return javaIODeleteOnExitAccess; |
| 100 | } |
| 101 | |
| 102 | public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) { |
| 103 | javaIOFileDescriptorAccess = jiofda; |
| 104 | } |
| 105 | |
| 106 | public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() { |
| 107 | if (javaIOFileDescriptorAccess == null) |
| 108 | unsafe.ensureClassInitialized(FileDescriptor.class); |
| 109 | |
| 110 | return javaIOFileDescriptorAccess; |
| 111 | } |
| 112 | |
| 113 | } |