8c7fd6f142d4a4a6b58c2167ce7074e6bc94e246
[jpf-core.git] / src / classes / sun / misc / SharedSecrets.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package sun.misc;
19
20 import java.io.File;
21 import java.io.FileDescriptor;
22 import java.io.ObjectInputStream;
23 import java.util.jar.JarFile;
24
25 /**
26  * This is a backdoor mechanism in Java 6 to allow (some sort of)
27  * controlled access to internals between packages, using
28  * sun.misc.* interfaces (e.g. JavaLangAccess) that are anonymously
29  * instantiated within the exporting package (e.g. java.lang), and
30  * obtained via SharedSecrets, which in turn obtains the
31  * instances from sun.misc.Unsafe. For most packages these interface
32  * objects are created on demand by static init of some key classes of
33  * these packages that call the SharedSecrets setters
34  * (except for JavaLangAccess and JavaNetAccess)
35  *
36  * Since this is used from within the standard libraries of Java 6, we need
37  * some sort of support, but we don't want to break Java 1.5 yet by introducing
38  * lots of Java 6 dependencies, which would force us to duplicate their code
39  * even though it might be pure Java (like java.io.Console).
40  *
41  * This is a can of worms, which we only open partially to support
42  * EnumSets for both Java 1.5 and 6. We make the cut at java.* packages -
43  * if the backdoor interfaces/types require anything outside sun.* packages,
44  * we leave it out.
45  *
46  * All of this is hopefully going away when we drop Java 1.5 support, and is
47  * to be replaced by some native peers providing the required native calls
48  */
49 public class SharedSecrets {
50   private static final Unsafe unsafe = Unsafe.getUnsafe();
51
52   private static JavaUtilJarAccess javaUtilJarAccess;
53   private static JavaLangAccess javaLangAccess;
54   private static JavaIOAccess javaIOAccess;
55   private static JavaIODeleteOnExitAccess javaIODeleteOnExitAccess;
56   private static JavaNetAccess javaNetAccess;
57   private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
58   private static JavaNioAccess javaNioAccess;
59   private static JavaAWTAccess javaAWTAccess;
60   private static JavaOISAccess javaOISAccess;
61   // removed in Java8u161
62   // private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
63
64   // (required for EnumSet ops)
65   public static JavaLangAccess getJavaLangAccess() {
66     return javaLangAccess;
67   }
68   // automatically called by java.lang.System clinit
69   public static void setJavaLangAccess(JavaLangAccess jla) {
70     javaLangAccess = jla;
71   }
72
73
74   public static void setJavaNetAccess(JavaNetAccess jna) {
75     javaNetAccess = jna;
76   }
77   // automatically called by java.net.URLClassLoader clinit
78   public static JavaNetAccess getJavaNetAccess() {
79     return javaNetAccess;
80   }
81
82
83   public static JavaUtilJarAccess javaUtilJarAccess() {
84     if (javaUtilJarAccess == null) {
85       unsafe.ensureClassInitialized(JarFile.class);
86     }
87     return javaUtilJarAccess;
88   }
89   public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
90     javaUtilJarAccess = access;
91   }
92
93
94   public static void setJavaIOAccess(JavaIOAccess jia) {
95     javaIOAccess = jia;
96   }
97   // this is normally done by java.io.Console, which is not in Java 1.5
98   // since this is a rather big beast with lost of bytecode, we don't add
99   // this for now
100   public static JavaIOAccess getJavaIOAccess() {
101     if (javaIOAccess == null) {
102       //unsafe.ensureClassInitialized(Console.class);
103       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOAccess() not supported yet");
104     }
105     return javaIOAccess;
106   }
107
108   
109   public static void setJavaNioAccess(JavaNioAccess a) {
110     javaNioAccess = a;
111   }
112   public static JavaNioAccess getJavaNioAccess() {
113     if (javaNioAccess == null) {
114       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaNioAccess() not supported yet");
115     }
116     return javaNioAccess;
117   }
118
119   
120   public static void setJavaIODeleteOnExitAccess(JavaIODeleteOnExitAccess jida) {
121     javaIODeleteOnExitAccess = jida;
122   }
123
124   public static JavaIODeleteOnExitAccess getJavaIODeleteOnExitAccess() {
125     if (javaIODeleteOnExitAccess == null) {
126       unsafe.ensureClassInitialized(File.class);
127     }
128     return javaIODeleteOnExitAccess;
129   }
130
131   public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
132     javaIOFileDescriptorAccess = jiofda;
133   }
134   public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
135     if (javaIOFileDescriptorAccess == null) {
136       unsafe.ensureClassInitialized(FileDescriptor.class);
137       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess() not supported yet");
138     }
139
140     return javaIOFileDescriptorAccess;
141   }
142 /*
143   public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
144     if (javaObjectInputStreamAccess == null) {
145       unsafe.ensureClassInitialized(ObjectInputStream.class);
146       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaObjectInputStreamAccess() not supported yet");
147     }
148     
149     return javaObjectInputStreamAccess;
150   }
151
152   public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
153     javaObjectInputStreamAccess = access;
154   }
155 */  
156   public static void setJavaAWTAccess (JavaAWTAccess jaa){
157     javaAWTAccess = jaa;
158   }
159   public static JavaAWTAccess getJavaAWTAccess(){
160     return javaAWTAccess;
161   }
162
163   public static void setJavaOISAccess(JavaOISAccess access) {
164     javaOISAccess = access;
165   }
166
167   public static JavaOISAccess getJavaOISAccess() {
168     if (javaOISAccess == null) {
169       unsafe.ensureClassInitialized(ObjectInputStream.class);
170       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaOISAccess() not supported yet");
171     }
172
173     return javaOISAccess;
174   }
175 }