1 /*
   2  * Copyright (c) 2002, 2015, 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 jdk.internal.misc;
  27 
  28 import java.util.jar.JarFile;
  29 import java.io.Console;
  30 import java.io.FileDescriptor;
  31 import java.security.ProtectionDomain;
  32 import java.security.AccessController;
  33 import jdk.internal.misc.Unsafe;
  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 JavaLangInvokeAccess javaLangInvokeAccess;
  49     private static JavaLangRefAccess javaLangRefAccess;
  50     private static JavaIOAccess javaIOAccess;
  51     private static JavaNetAccess javaNetAccess;
  52     private static JavaNetInetAddressAccess javaNetInetAddressAccess;
  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 JavaAWTFontAccess javaAWTFontAccess;
  61     private static JavaBeansAccess javaBeansAccess;
  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 setJavaLangInvokeAccess(JavaLangInvokeAccess jlia) {
  85         javaLangInvokeAccess = jlia;
  86     }
  87 
  88     public static JavaLangInvokeAccess getJavaLangInvokeAccess() {
  89         if (javaLangInvokeAccess == null) {
  90             try {
  91                 Class<?> c = Class.forName("java.lang.invoke.MemberName");
  92                 unsafe.ensureClassInitialized(c);
  93             } catch (ClassNotFoundException e) {};
  94         }
  95         return javaLangInvokeAccess;
  96     }
  97 
  98     public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
  99         javaLangRefAccess = jlra;
 100     }
 101 
 102     public static JavaLangRefAccess getJavaLangRefAccess() {
 103         return javaLangRefAccess;
 104     }
 105 
 106     public static void setJavaNetAccess(JavaNetAccess jna) {
 107         javaNetAccess = jna;
 108     }
 109 
 110     public static JavaNetAccess getJavaNetAccess() {
 111         if (javaNetAccess == null)
 112             unsafe.ensureClassInitialized(java.net.URLClassLoader.class);
 113         return javaNetAccess;
 114     }
 115 
 116     public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
 117         javaNetInetAddressAccess = jna;
 118     }
 119 
 120     public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
 121         return javaNetInetAddressAccess;
 122     }
 123 
 124     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
 125         javaNetHttpCookieAccess = a;
 126     }
 127 
 128     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
 129         if (javaNetHttpCookieAccess == null)
 130             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
 131         return javaNetHttpCookieAccess;
 132     }
 133 
 134     public static void setJavaNioAccess(JavaNioAccess jna) {
 135         javaNioAccess = jna;
 136     }
 137 
 138     public static JavaNioAccess getJavaNioAccess() {
 139         if (javaNioAccess == null) {
 140             // Ensure java.nio.ByteOrder is initialized; we know that
 141             // this class initializes java.nio.Bits that provides the
 142             // shared secret.
 143             unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
 144         }
 145         return javaNioAccess;
 146     }
 147 
 148     public static void setJavaIOAccess(JavaIOAccess jia) {
 149         javaIOAccess = jia;
 150     }
 151 
 152     public static JavaIOAccess getJavaIOAccess() {
 153         if (javaIOAccess == null) {
 154             unsafe.ensureClassInitialized(Console.class);
 155         }
 156         return javaIOAccess;
 157     }
 158 
 159     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 160         javaIOFileDescriptorAccess = jiofda;
 161     }
 162 
 163     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 164         if (javaIOFileDescriptorAccess == null)
 165             unsafe.ensureClassInitialized(FileDescriptor.class);
 166 
 167         return javaIOFileDescriptorAccess;
 168     }
 169 
 170     public static void setJavaSecurityProtectionDomainAccess
 171         (JavaSecurityProtectionDomainAccess jspda) {
 172             javaSecurityProtectionDomainAccess = jspda;
 173     }
 174 
 175     public static JavaSecurityProtectionDomainAccess
 176         getJavaSecurityProtectionDomainAccess() {
 177             if (javaSecurityProtectionDomainAccess == null)
 178                 unsafe.ensureClassInitialized(ProtectionDomain.class);
 179             return javaSecurityProtectionDomainAccess;
 180     }
 181 
 182     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 183         javaSecurityAccess = jsa;
 184     }
 185 
 186     public static JavaSecurityAccess getJavaSecurityAccess() {
 187         if (javaSecurityAccess == null) {
 188             unsafe.ensureClassInitialized(AccessController.class);
 189         }
 190         return javaSecurityAccess;
 191     }
 192 
 193     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 194         if (javaUtilZipFileAccess == null)
 195             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 196         return javaUtilZipFileAccess;
 197     }
 198 
 199     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 200         javaUtilZipFileAccess = access;
 201     }
 202 
 203     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 204         javaAWTAccess = jaa;
 205     }
 206 
 207     public static JavaAWTAccess getJavaAWTAccess() {
 208         // this may return null in which case calling code needs to
 209         // provision for.
 210         return javaAWTAccess;
 211     }
 212 
 213     public static void setJavaAWTFontAccess(JavaAWTFontAccess jafa) {
 214         javaAWTFontAccess = jafa;
 215     }
 216 
 217     public static JavaAWTFontAccess getJavaAWTFontAccess() {
 218         // this may return null in which case calling code needs to
 219         // provision for.
 220         return javaAWTFontAccess;
 221     }
 222 
 223     public static JavaBeansAccess getJavaBeansAccess() {
 224         return javaBeansAccess;
 225     }
 226 
 227     public static void setJavaBeansAccess(JavaBeansAccess access) {
 228         javaBeansAccess = access;
 229     }
 230 }