1 /*
   2  * Copyright (c) 2002, 2019, 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.access;
  27 
  28 import javax.crypto.SealedObject;
  29 import java.io.ObjectInputFilter;
  30 import java.lang.module.ModuleDescriptor;
  31 import java.util.ResourceBundle;
  32 import java.util.jar.JarFile;
  33 import java.io.Console;
  34 import java.io.FileDescriptor;
  35 import java.io.FilePermission;
  36 import java.io.ObjectInputStream;
  37 import java.io.RandomAccessFile;
  38 import java.security.ProtectionDomain;
  39 import java.security.Signature;
  40 import jdk.internal.misc.Unsafe;
  41 
  42 /** A repository of "shared secrets", which are a mechanism for
  43     calling implementation-private methods in another package without
  44     using reflection. A package-private class implements a public
  45     interface and provides the ability to call package-private methods
  46     within that package; the object implementing that interface is
  47     provided through a third package to which access is restricted.
  48     This framework avoids the primary disadvantage of using reflection
  49     for this purpose, namely the loss of compile-time checking. */
  50 
  51 public class SharedSecrets {
  52     private static final Unsafe unsafe = Unsafe.getUnsafe();
  53     private static JavaAWTAccess javaAWTAccess;
  54     private static JavaAWTFontAccess javaAWTFontAccess;
  55     private static JavaBeansAccess javaBeansAccess;
  56     private static JavaLangAccess javaLangAccess;

  57     private static JavaLangInvokeAccess javaLangInvokeAccess;
  58     private static JavaLangModuleAccess javaLangModuleAccess;
  59     private static JavaLangRefAccess javaLangRefAccess;
  60     private static JavaLangReflectAccess javaLangReflectAccess;
  61     private static JavaIOAccess javaIOAccess;
  62     private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
  63     private static JavaIOFilePermissionAccess javaIOFilePermissionAccess;
  64     private static JavaIORandomAccessFileAccess javaIORandomAccessFileAccess;
  65     private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
  66     private static JavaObjectInputFilterAccess javaObjectInputFilterAccess;
  67     private static JavaNetInetAddressAccess javaNetInetAddressAccess;
  68     private static JavaNetHttpCookieAccess javaNetHttpCookieAccess;
  69     private static JavaNetSocketAccess javaNetSocketAccess;
  70     private static JavaNetUriAccess javaNetUriAccess;
  71     private static JavaNetURLAccess javaNetURLAccess;
  72     private static JavaNetURLClassLoaderAccess javaNetURLClassLoaderAccess;
  73     private static JavaNioAccess javaNioAccess;
  74     private static JavaUtilJarAccess javaUtilJarAccess;


  75     private static JavaUtilZipFileAccess javaUtilZipFileAccess;
  76     private static JavaUtilResourceBundleAccess javaUtilResourceBundleAccess;
  77     private static JavaSecurityAccess javaSecurityAccess;





  78     private static JavaSecuritySignatureAccess javaSecuritySignatureAccess;
  79     private static JavaxCryptoSealedObjectAccess javaxCryptoSealedObjectAccess;
  80 
  81     public static JavaUtilJarAccess javaUtilJarAccess() {
  82         if (javaUtilJarAccess == null) {
  83             // Ensure JarFile is initialized; we know that this class
  84             // provides the shared secret
  85             unsafe.ensureClassInitialized(JarFile.class);
  86         }
  87         return javaUtilJarAccess;
  88     }
  89 
  90     public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
  91         javaUtilJarAccess = access;
  92     }
  93 
  94     public static void setJavaLangAccess(JavaLangAccess jla) {
  95         javaLangAccess = jla;
  96     }
  97 
  98     public static JavaLangAccess getJavaLangAccess() {
  99         return javaLangAccess;
 100     }
 101 
 102     public static void setJavaLangInvokeAccess(JavaLangInvokeAccess jlia) {
 103         javaLangInvokeAccess = jlia;
 104     }
 105 
 106     public static JavaLangInvokeAccess getJavaLangInvokeAccess() {
 107         if (javaLangInvokeAccess == null) {
 108             try {
 109                 Class<?> c = Class.forName("java.lang.invoke.MethodHandleImpl");
 110                 unsafe.ensureClassInitialized(c);
 111             } catch (ClassNotFoundException e) {};
 112         }
 113         return javaLangInvokeAccess;
 114     }
 115 
 116     public static void setJavaLangModuleAccess(JavaLangModuleAccess jlrma) {
 117         javaLangModuleAccess = jlrma;
 118     }
 119 
 120     public static JavaLangModuleAccess getJavaLangModuleAccess() {
 121         if (javaLangModuleAccess == null) {
 122             unsafe.ensureClassInitialized(ModuleDescriptor.class);
 123         }
 124         return javaLangModuleAccess;
 125     }
 126 
 127     public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
 128         javaLangRefAccess = jlra;
 129     }
 130 
 131     public static JavaLangRefAccess getJavaLangRefAccess() {
 132         return javaLangRefAccess;
 133     }
 134 
 135     public static void setJavaLangReflectAccess(JavaLangReflectAccess jlra) {
 136         javaLangReflectAccess = jlra;
 137     }
 138 
 139     public static JavaLangReflectAccess getJavaLangReflectAccess() {
 140         return javaLangReflectAccess;
 141     }
 142 
 143     public static void setJavaNetUriAccess(JavaNetUriAccess jnua) {
 144         javaNetUriAccess = jnua;
 145     }
 146 
 147     public static JavaNetUriAccess getJavaNetUriAccess() {
 148         if (javaNetUriAccess == null)
 149             unsafe.ensureClassInitialized(java.net.URI.class);
 150         return javaNetUriAccess;
 151     }
 152 
 153     public static void setJavaNetURLAccess(JavaNetURLAccess jnua) {
 154         javaNetURLAccess = jnua;
 155     }
 156 
 157     public static JavaNetURLAccess getJavaNetURLAccess() {
 158         if (javaNetURLAccess == null)
 159             unsafe.ensureClassInitialized(java.net.URL.class);
 160         return javaNetURLAccess;
 161     }
 162 
 163     public static void setJavaNetURLClassLoaderAccess(JavaNetURLClassLoaderAccess jnua) {
 164         javaNetURLClassLoaderAccess = jnua;
 165     }
 166 
 167     public static JavaNetURLClassLoaderAccess getJavaNetURLClassLoaderAccess() {
 168         if (javaNetURLClassLoaderAccess == null)
 169             unsafe.ensureClassInitialized(java.net.URLClassLoader.class);
 170         return javaNetURLClassLoaderAccess;
 171     }
 172 
 173     public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
 174         javaNetInetAddressAccess = jna;
 175     }
 176 
 177     public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
 178         if (javaNetInetAddressAccess == null)
 179             unsafe.ensureClassInitialized(java.net.InetAddress.class);
 180         return javaNetInetAddressAccess;
 181     }
 182 
 183     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
 184         javaNetHttpCookieAccess = a;
 185     }
 186 
 187     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
 188         if (javaNetHttpCookieAccess == null)
 189             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
 190         return javaNetHttpCookieAccess;
 191     }
 192 
 193     public static void setJavaNetSocketAccess(JavaNetSocketAccess jnsa) {
 194         javaNetSocketAccess = jnsa;
 195     }
 196 
 197     public static JavaNetSocketAccess getJavaNetSocketAccess() {
 198         if (javaNetSocketAccess == null)
 199             unsafe.ensureClassInitialized(java.net.ServerSocket.class);
 200         return javaNetSocketAccess;
 201     }
 202 
 203     public static void setJavaNioAccess(JavaNioAccess jna) {
 204         javaNioAccess = jna;
 205     }
 206 
 207     public static JavaNioAccess getJavaNioAccess() {
 208         if (javaNioAccess == null) {
 209             // Ensure java.nio.Buffer is initialized, which provides the
 210             // shared secret.
 211             unsafe.ensureClassInitialized(java.nio.Buffer.class);
 212         }
 213         return javaNioAccess;
 214     }
 215 
 216     public static void setJavaIOAccess(JavaIOAccess jia) {
 217         javaIOAccess = jia;
 218     }
 219 
 220     public static JavaIOAccess getJavaIOAccess() {
 221         if (javaIOAccess == null) {
 222             unsafe.ensureClassInitialized(Console.class);
 223         }
 224         return javaIOAccess;
 225     }
 226 
 227     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 228         javaIOFileDescriptorAccess = jiofda;
 229     }
 230 
 231     public static JavaIOFilePermissionAccess getJavaIOFilePermissionAccess() {
 232         if (javaIOFilePermissionAccess == null)
 233             unsafe.ensureClassInitialized(FilePermission.class);
 234 
 235         return javaIOFilePermissionAccess;
 236     }
 237 
 238     public static void setJavaIOFilePermissionAccess(JavaIOFilePermissionAccess jiofpa) {
 239         javaIOFilePermissionAccess = jiofpa;
 240     }
 241 
 242     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 243         if (javaIOFileDescriptorAccess == null)
 244             unsafe.ensureClassInitialized(FileDescriptor.class);
 245 
 246         return javaIOFileDescriptorAccess;
 247     }
 248 
 249     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 250         javaSecurityAccess = jsa;
 251     }
 252 
 253     public static JavaSecurityAccess getJavaSecurityAccess() {
 254         if (javaSecurityAccess == null) {
 255             unsafe.ensureClassInitialized(ProtectionDomain.class);
 256         }
 257         return javaSecurityAccess;
 258     }
 259 
 260     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 261         if (javaUtilZipFileAccess == null)
 262             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 263         return javaUtilZipFileAccess;
 264     }
 265 
 266     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 267         javaUtilZipFileAccess = access;
 268     }
 269 
 270     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 271         javaAWTAccess = jaa;
 272     }
 273 
 274     public static JavaAWTAccess getJavaAWTAccess() {
 275         // this may return null in which case calling code needs to
 276         // provision for.
 277         return javaAWTAccess;
 278     }
 279 
 280     public static void setJavaAWTFontAccess(JavaAWTFontAccess jafa) {
 281         javaAWTFontAccess = jafa;
 282     }
 283 
 284     public static JavaAWTFontAccess getJavaAWTFontAccess() {
 285         // this may return null in which case calling code needs to
 286         // provision for.
 287         return javaAWTFontAccess;
 288     }
 289 
 290     public static JavaBeansAccess getJavaBeansAccess() {
 291         return javaBeansAccess;
 292     }
 293 
 294     public static void setJavaBeansAccess(JavaBeansAccess access) {
 295         javaBeansAccess = access;
 296     }
 297 
 298     public static JavaUtilResourceBundleAccess getJavaUtilResourceBundleAccess() {
 299         if (javaUtilResourceBundleAccess == null)
 300             unsafe.ensureClassInitialized(ResourceBundle.class);
 301         return javaUtilResourceBundleAccess;
 302     }
 303 
 304     public static void setJavaUtilResourceBundleAccess(JavaUtilResourceBundleAccess access) {
 305         javaUtilResourceBundleAccess = access;
 306     }
 307 
 308     public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
 309         if (javaObjectInputStreamAccess == null) {
 310             unsafe.ensureClassInitialized(ObjectInputStream.class);
 311         }
 312         return javaObjectInputStreamAccess;
 313     }
 314 
 315     public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
 316         javaObjectInputStreamAccess = access;
 317     }
 318 
 319     public static JavaObjectInputFilterAccess getJavaObjectInputFilterAccess() {
 320         if (javaObjectInputFilterAccess == null) {
 321             unsafe.ensureClassInitialized(ObjectInputFilter.Config.class);
 322         }
 323         return javaObjectInputFilterAccess;
 324     }
 325 
 326     public static void setJavaObjectInputFilterAccess(JavaObjectInputFilterAccess access) {
 327         javaObjectInputFilterAccess = access;
 328     }
 329 
 330     public static void setJavaIORandomAccessFileAccess(JavaIORandomAccessFileAccess jirafa) {
 331         javaIORandomAccessFileAccess = jirafa;
 332     }
 333 
 334     public static JavaIORandomAccessFileAccess getJavaIORandomAccessFileAccess() {
 335         if (javaIORandomAccessFileAccess == null) {
 336             unsafe.ensureClassInitialized(RandomAccessFile.class);
 337         }
 338         return javaIORandomAccessFileAccess;
 339     }
 340 
 341     public static void setJavaSecuritySignatureAccess(JavaSecuritySignatureAccess jssa) {
 342         javaSecuritySignatureAccess = jssa;
 343     }
 344 
 345     public static JavaSecuritySignatureAccess getJavaSecuritySignatureAccess() {
 346         if (javaSecuritySignatureAccess == null) {
 347             unsafe.ensureClassInitialized(Signature.class);
 348         }
 349         return javaSecuritySignatureAccess;
 350     }
 351 
 352     public static void setJavaxCryptoSealedObjectAccess(JavaxCryptoSealedObjectAccess jcsoa) {
 353         javaxCryptoSealedObjectAccess = jcsoa;
 354     }
 355 
 356     public static JavaxCryptoSealedObjectAccess getJavaxCryptoSealedObjectAccess() {
 357         if (javaxCryptoSealedObjectAccess == null) {
 358             unsafe.ensureClassInitialized(SealedObject.class);
 359         }
 360         return javaxCryptoSealedObjectAccess;
 361     }
 362 }
--- EOF ---