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 JavaLangRefAccess javaLangRefAccess;
  49     private static JavaIOAccess javaIOAccess;
  50     private static JavaNetAccess javaNetAccess;
  51     private static JavaNetInetAddressAccess javaNetInetAddressAccess;
  52     private static JavaNetHttpCookieAccess javaNetHttpCookieAccess;
  53     private static JavaNioAccess javaNioAccess;
  54     private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
  55     private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess;
  56     private static JavaSecurityAccess javaSecurityAccess;
  57     private static JavaUtilZipFileAccess javaUtilZipFileAccess;
  58     private static JavaAWTAccess javaAWTAccess;
  59     private static JavaAWTFontAccess javaAWTFontAccess;
  60     private static JavaBeansAccess javaBeansAccess;
  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         if (javaNetAccess == null)
  97             unsafe.ensureClassInitialized(java.net.URLClassLoader.class);
  98         return javaNetAccess;
  99     }
 100 
 101     public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
 102         javaNetInetAddressAccess = jna;
 103     }
 104 
 105     public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
 106         return javaNetInetAddressAccess;
 107     }
 108 
 109     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
 110         javaNetHttpCookieAccess = a;
 111     }
 112 
 113     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
 114         if (javaNetHttpCookieAccess == null)
 115             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
 116         return javaNetHttpCookieAccess;
 117     }
 118 
 119     public static void setJavaNioAccess(JavaNioAccess jna) {
 120         javaNioAccess = jna;
 121     }
 122 
 123     public static JavaNioAccess getJavaNioAccess() {
 124         if (javaNioAccess == null) {
 125             // Ensure java.nio.ByteOrder is initialized; we know that
 126             // this class initializes java.nio.Bits that provides the
 127             // shared secret.
 128             unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
 129         }
 130         return javaNioAccess;
 131     }
 132 
 133     public static void setJavaIOAccess(JavaIOAccess jia) {
 134         javaIOAccess = jia;
 135     }
 136 
 137     public static JavaIOAccess getJavaIOAccess() {
 138         if (javaIOAccess == null) {
 139             unsafe.ensureClassInitialized(Console.class);
 140         }
 141         return javaIOAccess;
 142     }
 143 
 144     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 145         javaIOFileDescriptorAccess = jiofda;
 146     }
 147 
 148     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 149         if (javaIOFileDescriptorAccess == null)
 150             unsafe.ensureClassInitialized(FileDescriptor.class);
 151 
 152         return javaIOFileDescriptorAccess;
 153     }
 154 
 155     public static void setJavaSecurityProtectionDomainAccess
 156         (JavaSecurityProtectionDomainAccess jspda) {
 157             javaSecurityProtectionDomainAccess = jspda;
 158     }
 159 
 160     public static JavaSecurityProtectionDomainAccess
 161         getJavaSecurityProtectionDomainAccess() {
 162             if (javaSecurityProtectionDomainAccess == null)
 163                 unsafe.ensureClassInitialized(ProtectionDomain.class);
 164             return javaSecurityProtectionDomainAccess;
 165     }
 166 
 167     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 168         javaSecurityAccess = jsa;
 169     }
 170 
 171     public static JavaSecurityAccess getJavaSecurityAccess() {
 172         if (javaSecurityAccess == null) {
 173             unsafe.ensureClassInitialized(AccessController.class);
 174         }
 175         return javaSecurityAccess;
 176     }
 177 
 178     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 179         if (javaUtilZipFileAccess == null)
 180             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 181         return javaUtilZipFileAccess;
 182     }
 183 
 184     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 185         javaUtilZipFileAccess = access;
 186     }
 187 
 188     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 189         javaAWTAccess = jaa;
 190     }
 191 
 192     public static JavaAWTAccess getJavaAWTAccess() {
 193         // this may return null in which case calling code needs to
 194         // provision for.
 195         return javaAWTAccess;
 196     }
 197 
 198     public static void setJavaAWTFontAccess(JavaAWTFontAccess jafa) {
 199         javaAWTFontAccess = jafa;
 200     }
 201 
 202     public static JavaAWTFontAccess getJavaAWTFontAccess() {
 203         // this may return null in which case calling code needs to
 204         // provision for.
 205         return javaAWTFontAccess;
 206     }
 207 
 208     public static JavaBeansAccess getJavaBeansAccess() {
 209         return javaBeansAccess;
 210     }
 211 
 212     public static void setJavaBeansAccess(JavaBeansAccess access) {
 213         javaBeansAccess = access;
 214     }
 215 }