Initial import
[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   private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
62
63   // (required for EnumSet ops)
64   public static JavaLangAccess getJavaLangAccess() {
65     return javaLangAccess;
66   }
67   // automatically called by java.lang.System clinit
68   public static void setJavaLangAccess(JavaLangAccess jla) {
69     javaLangAccess = jla;
70   }
71
72
73   public static void setJavaNetAccess(JavaNetAccess jna) {
74     javaNetAccess = jna;
75   }
76   // automatically called by java.net.URLClassLoader clinit
77   public static JavaNetAccess getJavaNetAccess() {
78     return javaNetAccess;
79   }
80
81
82   public static JavaUtilJarAccess javaUtilJarAccess() {
83     if (javaUtilJarAccess == null) {
84       unsafe.ensureClassInitialized(JarFile.class);
85     }
86     return javaUtilJarAccess;
87   }
88   public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
89     javaUtilJarAccess = access;
90   }
91
92
93   public static void setJavaIOAccess(JavaIOAccess jia) {
94     javaIOAccess = jia;
95   }
96   // this is normally done by java.io.Console, which is not in Java 1.5
97   // since this is a rather big beast with lost of bytecode, we don't add
98   // this for now
99   public static JavaIOAccess getJavaIOAccess() {
100     if (javaIOAccess == null) {
101       //unsafe.ensureClassInitialized(Console.class);
102       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOAccess() not supported yet");
103     }
104     return javaIOAccess;
105   }
106
107   
108   public static void setJavaNioAccess(JavaNioAccess a) {
109     javaNioAccess = a;
110   }
111   public static JavaNioAccess getJavaNioAccess() {
112     if (javaNioAccess == null) {
113       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaNioAccess() not supported yet");
114     }
115     return javaNioAccess;
116   }
117
118   
119   public static void setJavaIODeleteOnExitAccess(JavaIODeleteOnExitAccess jida) {
120     javaIODeleteOnExitAccess = jida;
121   }
122
123   public static JavaIODeleteOnExitAccess getJavaIODeleteOnExitAccess() {
124     if (javaIODeleteOnExitAccess == null) {
125       unsafe.ensureClassInitialized(File.class);
126     }
127     return javaIODeleteOnExitAccess;
128   }
129
130   public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
131     javaIOFileDescriptorAccess = jiofda;
132   }
133   public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
134     if (javaIOFileDescriptorAccess == null) {
135       unsafe.ensureClassInitialized(FileDescriptor.class);
136       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess() not supported yet");
137     }
138
139     return javaIOFileDescriptorAccess;
140   }
141
142   public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
143     if (javaObjectInputStreamAccess == null) {
144       unsafe.ensureClassInitialized(ObjectInputStream.class);
145       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaObjectInputStreamAccess() not supported yet");
146     }
147     
148     return javaObjectInputStreamAccess;
149   }
150
151   public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
152     javaObjectInputStreamAccess = access;
153   }
154   
155   public static void setJavaAWTAccess (JavaAWTAccess jaa){
156     javaAWTAccess = jaa;
157   }
158   public static JavaAWTAccess getJavaAWTAccess(){
159     return javaAWTAccess;
160   }
161
162   public static void setJavaOISAccess(JavaOISAccess access) {
163     javaOISAccess = access;
164   }
165
166   public static JavaOISAccess getJavaOISAccess() {
167     if (javaOISAccess == null) {
168       unsafe.ensureClassInitialized(ObjectInputStream.class);
169       throw new UnsupportedOperationException("sun.misc.SharedSecrets.getJavaOISAccess() not supported yet");
170     }
171
172     return javaOISAccess;
173   }
174 }