1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.intel.bluetooth;
26
27 import java.security.PrivilegedActionException;
28 import java.util.Properties;
29 import java.util.Vector;
30
31 import com.ibm.oti.vm.VM;
32
33
34
35
36
37
38
39
40 public class UtilsJavaSE {
41
42 static final boolean javaSECompiledOut = false;
43
44 static class StackTraceLocation {
45
46 public String className;
47
48 public String methodName;
49
50 public String fileName;
51
52 public int lineNumber;
53 }
54
55 static interface JavaSE5Features {
56
57 public void clearProperty(String propertyName);
58
59 }
60
61 static final int javaSpecificationVersion = getJavaSpecificationVersion();
62
63 static boolean java13 = false;
64
65 static boolean java14 = false;
66
67 static boolean detectJava5Helper = true;
68
69 static JavaSE5Features java5Helper;
70
71 static final boolean ibmJ9midp = detectJ9midp();
72
73 static final boolean canCallNotLoadedNativeMethod = !ibmJ9midp;
74
75 private UtilsJavaSE() {
76
77 }
78
79 private static boolean detectJ9midp() {
80 String ibmJ9config;
81 try {
82 ibmJ9config = System.getProperty("com.ibm.oti.configuration");
83 } catch (SecurityException webstart) {
84 return false;
85 }
86 return (ibmJ9config != null) && (ibmJ9config.indexOf("midp") != -1);
87 }
88
89 private static int getJavaSpecificationVersion() {
90 try {
91 String javaV = System.getProperty("java.specification.version");
92 if ((javaV == null) || (javaV.length() < 3)) {
93 return 0;
94 }
95 return Integer.valueOf(javaV.charAt(2)).intValue();
96 } catch (Throwable e) {
97 return 0;
98 }
99 }
100
101 private static void detectJava5Helper() {
102 if (java13 || ibmJ9midp || (!detectJava5Helper) || (javaSpecificationVersion < 5)) {
103 return;
104 }
105 detectJava5Helper = false;
106 try {
107 Class klass = Class.forName("com.intel.bluetooth.UtilsJavaSE5");
108 java5Helper = (JavaSE5Features) klass.newInstance();
109 DebugLog.debug("Use java 1.5+ features:", vmInfo());
110 } catch (Throwable e) {
111 }
112 }
113
114 static StackTraceLocation getLocation(Vector fqcnSet) {
115 if (java13 || ibmJ9midp) {
116 return null;
117 }
118 if (!javaSECompiledOut) {
119 if (!java14) {
120 try {
121 Class.forName("java.lang.StackTraceElement");
122 java14 = true;
123 DebugLog.debug("Java 1.4+ detected:", vmInfo());
124 } catch (ClassNotFoundException e) {
125 java13 = true;
126 return null;
127 }
128 }
129 try {
130 return getLocationJava14(fqcnSet);
131 } catch (Throwable e) {
132 java13 = true;
133 }
134 }
135 return null;
136 }
137
138 static String vmInfo() {
139 try {
140 return System.getProperty("java.version") + "; " + System.getProperty("java.vm.name") + "; "
141 + System.getProperty("java.vendor");
142 } catch (SecurityException ignore) {
143 return "";
144 }
145 }
146
147 private static StackTraceLocation getLocationJava14(Vector fqcnSet) {
148 if (!UtilsJavaSE.javaSECompiledOut) {
149 StackTraceElement[] ste = new Throwable().getStackTrace();
150 for (int i = 0; i < ste.length - 1; i++) {
151 if (fqcnSet.contains(ste[i].getClassName())) {
152 String nextClassName = ste[i + 1].getClassName();
153 if (nextClassName.startsWith("java.") || nextClassName.startsWith("sun.")) {
154 continue;
155 }
156 if (!fqcnSet.contains(nextClassName)) {
157 StackTraceElement st = ste[i + 1];
158 StackTraceLocation loc = new StackTraceLocation();
159 loc.className = st.getClassName();
160 loc.methodName = st.getMethodName();
161 loc.fileName = st.getFileName();
162 loc.lineNumber = st.getLineNumber();
163 return loc;
164 }
165 }
166 }
167 }
168 return null;
169 }
170
171
172
173
174
175
176
177 public static void threadSetDaemon(Thread thread) {
178 try {
179 if ((!javaSECompiledOut) && (!ibmJ9midp)) {
180 thread.setDaemon(true);
181 }
182 } catch (Throwable javaJ9) {
183 }
184 }
185
186 static boolean runtimeAddShutdownHook(Thread thread) {
187 try {
188
189 if (!javaSECompiledOut) {
190 if (ibmJ9midp) {
191 VM.addShutdownClass(thread);
192 return true;
193 } else {
194 Runtime.getRuntime().addShutdownHook(thread);
195 return true;
196 }
197 }
198 } catch (Throwable java12) {
199 }
200 return false;
201 }
202
203 static void runtimeRemoveShutdownHook(Thread thread) {
204 try {
205
206 if ((!javaSECompiledOut) && (!ibmJ9midp)) {
207 Runtime.getRuntime().removeShutdownHook(thread);
208 }
209 } catch (Throwable java12) {
210 }
211 }
212
213 static void setSystemProperty(String propertyName, String propertyValue) {
214 if (ibmJ9midp) {
215 return;
216 }
217 boolean propertySet = false;
218 try {
219 Properties props = System.getProperties();
220 if (propertyValue != null) {
221 props.put(propertyName, propertyValue);
222 propertySet = propertyValue.equals(System.getProperty(propertyName));
223 } else {
224 props.remove(propertyName);
225 propertySet = (System.getProperty(propertyName) == null);
226 }
227 } catch (SecurityException e) {
228 }
229 if (!propertySet) {
230
231 try {
232 if (propertyValue != null) {
233 System.setProperty(propertyName, propertyValue);
234 } else {
235 detectJava5Helper();
236 if (java5Helper != null) {
237 java5Helper.clearProperty(propertyName);
238 }
239 }
240 } catch (Throwable java11) {
241 }
242 }
243 }
244
245 public static Throwable initCause(Throwable throwable, Throwable cause) {
246 if ((!java14) || (cause == null)) {
247 return throwable;
248 }
249 try {
250 return throwable.initCause(cause);
251 } catch (Throwable j9pp10) {
252 return throwable;
253 }
254
255 }
256
257
258
259
260 static boolean isCurrentThreadInterrupted() {
261 if (ibmJ9midp) {
262 return false;
263 }
264 return Thread.interrupted();
265 }
266
267
268
269
270 static Throwable getCause(PrivilegedActionException e) {
271 try {
272 return e.getCause();
273 } catch (Throwable j9pp10) {
274 }
275
276 try {
277 return e.getException();
278 } catch (Throwable ignore) {
279 }
280 return null;
281 }
282 }