SharedSecrets.java revision 51b1b6997fd3f980076b8081f7f1165ccc2a4008
1/*
2 * Copyright (c) 2002, 2013, 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
26package sun.misc;
27
28import java.util.jar.JarFile;
29import java.io.Console;
30import java.io.FileDescriptor;
31import java.security.ProtectionDomain;
32import java.util.zip.Adler32;
33import javax.security.auth.kerberos.KeyTab;
34
35import 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
46public class SharedSecrets {
47    private static final Unsafe unsafe = Unsafe.getUnsafe();
48    private static JavaUtilJarAccess javaUtilJarAccess;
49    private static JavaLangAccess javaLangAccess;
50    private static JavaIOAccess javaIOAccess;
51    private static JavaNetAccess javaNetAccess;
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 JavaxSecurityAuthKerberosAccess javaxSecurityAuthKerberosAccess;
58    private static JavaUtilZipAccess javaUtilZipAccess;
59    private static JavaUtilZipFileAccess javaUtilZipFileAccess;
60    private static JavaAWTAccess javaAWTAccess;
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 setJavaNetAccess(JavaNetAccess jna) {
84        javaNetAccess = jna;
85    }
86
87    public static JavaNetAccess getJavaNetAccess() {
88        return javaNetAccess;
89    }
90
91    public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
92        javaNetHttpCookieAccess = a;
93    }
94
95    public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
96        if (javaNetHttpCookieAccess == null)
97            unsafe.ensureClassInitialized(java.net.HttpCookie.class);
98        return javaNetHttpCookieAccess;
99    }
100
101    public static void setJavaNioAccess(JavaNioAccess jna) {
102        javaNioAccess = jna;
103    }
104
105    public static JavaNioAccess getJavaNioAccess() {
106        if (javaNioAccess == null) {
107            // Ensure java.nio.ByteOrder is initialized; we know that
108            // this class initializes java.nio.Bits that provides the
109            // shared secret.
110            unsafe.ensureClassInitialized(java.nio.ByteOrder.class);
111        }
112        return javaNioAccess;
113    }
114
115    public static void setJavaIOAccess(JavaIOAccess jia) {
116        javaIOAccess = jia;
117    }
118
119    public static JavaIOAccess getJavaIOAccess() {
120        if (javaIOAccess == null) {
121            unsafe.ensureClassInitialized(Console.class);
122        }
123        return javaIOAccess;
124    }
125
126    public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
127        javaIOFileDescriptorAccess = jiofda;
128    }
129
130    public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
131        if (javaIOFileDescriptorAccess == null)
132            unsafe.ensureClassInitialized(FileDescriptor.class);
133
134        return javaIOFileDescriptorAccess;
135    }
136
137    public static void setJavaSecurityProtectionDomainAccess
138        (JavaSecurityProtectionDomainAccess jspda) {
139            javaSecurityProtectionDomainAccess = jspda;
140    }
141
142    public static JavaSecurityProtectionDomainAccess
143        getJavaSecurityProtectionDomainAccess() {
144            if (javaSecurityProtectionDomainAccess == null)
145                unsafe.ensureClassInitialized(ProtectionDomain.class);
146            return javaSecurityProtectionDomainAccess;
147    }
148
149    public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
150        javaSecurityAccess = jsa;
151    }
152
153    public static JavaSecurityAccess getJavaSecurityAccess() {
154        if (javaSecurityAccess == null) {
155            unsafe.ensureClassInitialized(AccessController.class);
156        }
157        return javaSecurityAccess;
158    }
159
160    public static void setJavaxSecurityAuthKerberosAccess
161            (JavaxSecurityAuthKerberosAccess jsaka) {
162        javaxSecurityAuthKerberosAccess = jsaka;
163    }
164
165    public static JavaxSecurityAuthKerberosAccess
166            getJavaxSecurityAuthKerberosAccess() {
167        if (javaxSecurityAuthKerberosAccess == null)
168            unsafe.ensureClassInitialized(KeyTab.class);
169        return javaxSecurityAuthKerberosAccess;
170    }
171
172    public static void setJavaUtilZipAccess(JavaUtilZipAccess access) {
173        javaUtilZipAccess = access;
174    }
175
176    public static JavaUtilZipAccess getJavaUtilZipAccess() {
177        if (javaUtilZipAccess == null) {
178            unsafe.ensureClassInitialized(Adler32.class);
179        }
180        return javaUtilZipAccess;
181    }
182
183    public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
184        if (javaUtilZipFileAccess == null)
185            unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
186        return javaUtilZipFileAccess;
187    }
188
189    public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
190        javaUtilZipFileAccess = access;
191    }
192
193    public static void setJavaAWTAccess(JavaAWTAccess jaa) {
194        javaAWTAccess = jaa;
195    }
196
197    public static JavaAWTAccess getJavaAWTAccess() {
198        // this may return null in which case calling code needs to
199        // provision for.
200        if (javaAWTAccess == null || javaAWTAccess.getContext() == null) {
201            return null;
202        }
203        return javaAWTAccess;
204    }
205}
206