1 /*
   2  * Copyright (c) 2002, 2013, 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  */
  25 
  26 package sun.misc;
  27 
  28 import java.util.jar.JarFile;
  29 import java.io.Console;
  30 import java.io.FileDescriptor;
  31 import java.security.ProtectionDomain;
  32 
  33 import java.security.AccessController;
  34 
  35 /** A repository of "shared secrets", which are a mechanism for
  36     calling implementation-private methods in another package without
  37     using reflection. A package-private class implements a public
  38     interface and provides the ability to call package-private methods
  39     within that package; the object implementing that interface is
  40     provided through a third package to which access is restricted.
  41     This framework avoids the primary disadvantage of using reflection
  42     for this purpose, namely the loss of compile-time checking. */
  43 
  44 public class SharedSecrets {
  45     private static final Unsafe unsafe = Unsafe.getUnsafe();
  46     private static JavaUtilJarAccess javaUtilJarAccess;
  47     private static JavaLangAccess javaLangAccess;
  48     private static JavaIOAccess javaIOAccess;
  49     private static JavaNetAccess javaNetAccess;
  50     private static JavaNetHttpCookieAccess javaNetHttpCookieAccess;
  51     private static JavaNioAccess javaNioAccess;
  52     private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
  53     private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess;
  54     private static JavaSecurityAccess javaSecurityAccess;
  55     private static JavaUtilZipFileAccess javaUtilZipFileAccess;
  56     private static JavaAWTAccess javaAWTAccess;
  57 
  58     public static JavaUtilJarAccess javaUtilJarAccess() {
  59         if (javaUtilJarAccess == null) {
  60             // Ensure JarFile is initialized; we know that that class
  61             // provides the shared secret
  62             unsafe.ensureClassInitialized(JarFile.class);
  63         }
  64         return javaUtilJarAccess;
  65     }
  66 
  67     public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
  68         javaUtilJarAccess = access;
  69     }
  70 
  71     public static void setJavaLangAccess(JavaLangAccess jla) {
  72         javaLangAccess = jla;
  73     }
  74 
  75     public static JavaLangAccess getJavaLangAccess() {
  76         return javaLangAccess;
  77     }
  78 
  79     public static void setJavaNetAccess(JavaNetAccess jna) {
  80         javaNetAccess = jna;
  81     }
  82 
  83     public static JavaNetAccess getJavaNetAccess() {
  84         return javaNetAccess;
  85     }
  86 
  87     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
  88         javaNetHttpCookieAccess = a;
  89     }
  90 
  91     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
  92         if (javaNetHttpCookieAccess == null)
  93             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
  94         return javaNetHttpCookieAccess;
  95     }
  96 
  97     public static void setJavaNioAccess(JavaNioAccess jna) {
  98         javaNioAccess = jna;
  99     }
 100 
 101     public static JavaNioAccess getJavaNioAccess() {
 102         if (javaNioAccess == null) {
 103             // Ensure java.nio.ByteOrder is initialized; we know that
 104             // this class initializes java.nio.Bits that provides the
 105             // shared secret.
 106             unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
 107         }
 108         return javaNioAccess;
 109     }
 110 
 111     public static void setJavaIOAccess(JavaIOAccess jia) {
 112         javaIOAccess = jia;
 113     }
 114 
 115     public static JavaIOAccess getJavaIOAccess() {
 116         if (javaIOAccess == null) {
 117             unsafe.ensureClassInitialized(Console.class);
 118         }
 119         return javaIOAccess;
 120     }
 121 
 122     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 123         javaIOFileDescriptorAccess = jiofda;
 124     }
 125 
 126     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 127         if (javaIOFileDescriptorAccess == null)
 128             unsafe.ensureClassInitialized(FileDescriptor.class);
 129 
 130         return javaIOFileDescriptorAccess;
 131     }
 132 
 133     public static void setJavaSecurityProtectionDomainAccess
 134         (JavaSecurityProtectionDomainAccess jspda) {
 135             javaSecurityProtectionDomainAccess = jspda;
 136     }
 137 
 138     public static JavaSecurityProtectionDomainAccess
 139         getJavaSecurityProtectionDomainAccess() {
 140             if (javaSecurityProtectionDomainAccess == null)
 141                 unsafe.ensureClassInitialized(ProtectionDomain.class);
 142             return javaSecurityProtectionDomainAccess;
 143     }
 144 
 145     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 146         javaSecurityAccess = jsa;
 147     }
 148 
 149     public static JavaSecurityAccess getJavaSecurityAccess() {
 150         if (javaSecurityAccess == null) {
 151             unsafe.ensureClassInitialized(AccessController.class);
 152         }
 153         return javaSecurityAccess;
 154     }
 155 
 156     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 157         if (javaUtilZipFileAccess == null)
 158             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 159         return javaUtilZipFileAccess;
 160     }
 161 
 162     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 163         javaUtilZipFileAccess = access;
 164     }
 165 
 166     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 167         javaAWTAccess = jaa;
 168     }
 169 
 170     public static JavaAWTAccess getJavaAWTAccess() {
 171         // this may return null in which case calling code needs to
 172         // provision for.
 173         if (javaAWTAccess == null) {
 174             return null;
 175         }
 176         return javaAWTAccess;
 177     }
 178 }