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