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     private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
  62 
  63     public static JavaUtilJarAccess javaUtilJarAccess() {
  64         if (javaUtilJarAccess == null) {
  65             // Ensure JarFile is initialized; we know that that class
  66             // provides the shared secret
  67             unsafe.ensureClassInitialized(JarFile.class);
  68         }
  69         return javaUtilJarAccess;
  70     }
  71 
  72     public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
  73         javaUtilJarAccess = access;
  74     }
  75 
  76     public static void setJavaLangAccess(JavaLangAccess jla) {
  77         javaLangAccess = jla;
  78     }
  79 
  80     public static JavaLangAccess getJavaLangAccess() {
  81         return javaLangAccess;
  82     }
  83 
  84     public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
  85         javaLangRefAccess = jlra;
  86     }
  87 
  88     public static JavaLangRefAccess getJavaLangRefAccess() {
  89         return javaLangRefAccess;
  90     }
  91 
  92     public static void setJavaNetAccess(JavaNetAccess jna) {
  93         javaNetAccess = jna;
  94     }
  95 
  96     public static JavaNetAccess getJavaNetAccess() {
  97         return javaNetAccess;
  98     }
  99 
 100     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
 101         javaNetHttpCookieAccess = a;
 102     }
 103 
 104     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
 105         if (javaNetHttpCookieAccess == null)
 106             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
 107         return javaNetHttpCookieAccess;
 108     }
 109 
 110     public static void setJavaNioAccess(JavaNioAccess jna) {
 111         javaNioAccess = jna;
 112     }
 113 
 114     public static JavaNioAccess getJavaNioAccess() {
 115         if (javaNioAccess == null) {
 116             // Ensure java.nio.ByteOrder is initialized; we know that
 117             // this class initializes java.nio.Bits that provides the
 118             // shared secret.
 119             unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
 120         }
 121         return javaNioAccess;
 122     }
 123 
 124     public static void setJavaIOAccess(JavaIOAccess jia) {
 125         javaIOAccess = jia;
 126     }
 127 
 128     public static JavaIOAccess getJavaIOAccess() {
 129         if (javaIOAccess == null) {
 130             unsafe.ensureClassInitialized(Console.class);
 131         }
 132         return javaIOAccess;
 133     }
 134 
 135     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 136         javaIOFileDescriptorAccess = jiofda;
 137     }
 138 
 139     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 140         if (javaIOFileDescriptorAccess == null)
 141             unsafe.ensureClassInitialized(FileDescriptor.class);
 142 
 143         return javaIOFileDescriptorAccess;
 144     }
 145 
 146     public static void setJavaOISAccess(JavaOISAccess access) {
 147         javaOISAccess = access;
 148     }
 149 
 150     public static JavaOISAccess getJavaOISAccess() {
 151         if (javaOISAccess == null)
 152             unsafe.ensureClassInitialized(ObjectInputStream.class);
 153 
 154         return javaOISAccess;
 155     }
 156 
 157 
 158     public static void setJavaSecurityProtectionDomainAccess
 159         (JavaSecurityProtectionDomainAccess jspda) {
 160             javaSecurityProtectionDomainAccess = jspda;
 161     }
 162 
 163     public static JavaSecurityProtectionDomainAccess
 164         getJavaSecurityProtectionDomainAccess() {
 165             if (javaSecurityProtectionDomainAccess == null)
 166                 unsafe.ensureClassInitialized(ProtectionDomain.class);
 167             return javaSecurityProtectionDomainAccess;
 168     }
 169 
 170     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 171         javaSecurityAccess = jsa;
 172     }
 173 
 174     public static JavaSecurityAccess getJavaSecurityAccess() {
 175         if (javaSecurityAccess == null) {
 176             unsafe.ensureClassInitialized(AccessController.class);
 177         }
 178         return javaSecurityAccess;
 179     }
 180 
 181     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 182         if (javaUtilZipFileAccess == null)
 183             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 184         return javaUtilZipFileAccess;
 185     }
 186 
 187     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 188         javaUtilZipFileAccess = access;
 189     }
 190 
 191     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 192         javaAWTAccess = jaa;
 193     }
 194 
 195     public static JavaAWTAccess getJavaAWTAccess() {
 196         // this may return null in which case calling code needs to
 197         // provision for.
 198         if (javaAWTAccess == null) {
 199             return null;
 200         }
 201         return javaAWTAccess;
 202     }
 203 
 204     public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
 205         if (javaObjectInputStreamAccess == null) {
 206             unsafe.ensureClassInitialized(ObjectInputStream.class);
 207         }
 208         return javaObjectInputStreamAccess;
 209     }
 210 
 211     public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
 212         javaObjectInputStreamAccess = access;
 213     }
 214 }