My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.security.PrivilegedActionException;
import java.util.Properties;
import java.util.Vector;

/**
*
* J2ME/J9 compatibility module.
*
* <p>
* <b><u>Your application should not use this class directly.</u></b>
*/
public class UtilsJavaSE {

static final boolean javaSECompiledOut = false;

static class StackTraceLocation {

public String className;

public String methodName;

public String fileName;

public int lineNumber;
}

static interface JavaSE5Features {

public void clearProperty(String propertyName);

}

static final int javaSpecificationVersion = getJavaSpecificationVersion();

static boolean java13 = false;

static boolean java14 = false;

static boolean detectJava5Helper = true;

static JavaSE5Features java5Helper;

static final boolean ibmJ9midp = detectJ9midp();

static final boolean canCallNotLoadedNativeMethod = !ibmJ9midp;

private UtilsJavaSE() {

}

private static boolean detectJ9midp() {
String ibmJ9config;
try {
ibmJ9config = System.getProperty("com.ibm.oti.configuration");
} catch (SecurityException webstart) {
return false;
}
return (ibmJ9config != null) && (ibmJ9config.indexOf("midp") != -1);
}

private static int getJavaSpecificationVersion() {
try {
String javaV = System.getProperty("java.specification.version");
if ((javaV == null) || (javaV.length() < 3)) {
return 0;
}
return Integer.valueOf(javaV.substring(2, 3)).intValue();
} catch (Throwable e) {
return 0;
}
}

private static void detectJava5Helper() {
if (java13 || ibmJ9midp || (!detectJava5Helper) || (javaSpecificationVersion < 5)) {
return;
}
detectJava5Helper = false;
try {
Class klass = Class.forName("com.intel.bluetooth.UtilsJavaSE5");
java5Helper = (JavaSE5Features) klass.newInstance();
DebugLog.debug("Use java 1.5+ features:", vmInfo());
} catch (Throwable e) {
}
}

static StackTraceLocation getLocation(Vector fqcnSet) {
if (java13 || ibmJ9midp) {
return null;
}
if (!javaSECompiledOut) {
if (!java14) {
try {
Class.forName("java.lang.StackTraceElement");
java14 = true;
DebugLog.debug("Java 1.4+ detected:", vmInfo());
} catch (ClassNotFoundException e) {
java13 = true;
return null;
}
}
try {
return getLocationJava14(fqcnSet);
} catch (Throwable e) {
java13 = true;
}
}
return null;
}

static String vmInfo() {
try {
return System.getProperty("java.version") + "; " + System.getProperty("java.vm.name") + "; "
+ System.getProperty("java.vendor");
} catch (SecurityException ignore) {
return "";
}
}

private static StackTraceLocation getLocationJava14(Vector fqcnSet) {
if (!UtilsJavaSE.javaSECompiledOut) {
StackTraceElement[] ste = new Throwable().getStackTrace();
for (int i = 0; i < ste.length - 1; i++) {
if (fqcnSet.contains(ste[i].getClassName())) {
String nextClassName = ste[i + 1].getClassName();
if (nextClassName.startsWith("java.") || nextClassName.startsWith("sun.")) {
continue;
}
if (!fqcnSet.contains(nextClassName)) {
StackTraceElement st = ste[i + 1];
StackTraceLocation loc = new StackTraceLocation();
loc.className = st.getClassName();
loc.methodName = st.getMethodName();
loc.fileName = st.getFileName();
loc.lineNumber = st.getLineNumber();
return loc;
}
}
}
}
return null;
}

/**
* Marks the thread as a daemon thread. The Java Virtual Machine exits when
* the only threads running are all daemon threads.
*
* @see java.lang.Thread#setDaemon(boolean)
*/
public static void threadSetDaemon(Thread thread) {
try {
if ((!javaSECompiledOut) && (!ibmJ9midp)) {
thread.setDaemon(true);
}
} catch (Throwable javaJ9) {
}
}

static boolean runtimeAddShutdownHook(Thread thread) {
try {
// since Java 1.3
if (!javaSECompiledOut) {
if (ibmJ9midp) {
IBMJ9Helper.addShutdownClass(thread);
return true;
} else {
Runtime.getRuntime().addShutdownHook(thread);
return true;
}
}
} catch (Throwable java12) {
}
return false;
}

static void runtimeRemoveShutdownHook(Thread thread) {
try {
// since Java 1.3
if ((!javaSECompiledOut) && (!ibmJ9midp)) {
Runtime.getRuntime().removeShutdownHook(thread);
}
} catch (Throwable java12) {
}
}

static void setSystemProperty(String propertyName, String propertyValue) {
if (ibmJ9midp) {
return;
}
boolean propertySet = false;
try {
Properties props = System.getProperties();
if (propertyValue != null) {
props.put(propertyName, propertyValue);
propertySet = propertyValue.equals(System.getProperty(propertyName));
} else {
props.remove(propertyName);
propertySet = (System.getProperty(propertyName) == null);
}
} catch (SecurityException e) {
}
if (!propertySet) {
// Fall back if security managed allow to change only specific key
try {
if (propertyValue != null) {
System.setProperty(propertyName, propertyValue);
} else {
detectJava5Helper();
if (java5Helper != null) {
java5Helper.clearProperty(propertyName);
}
}
} catch (Throwable java11) {
}
}
}

public static Throwable initCause(Throwable throwable, Throwable cause) {
if ((!java14) || (cause == null)) {
return throwable;
}
try {
return throwable.initCause(cause);
} catch (Throwable j9pp10) {
return throwable;
}

}

/**
* Support for JBM J9 PPRO 10
*/
static boolean isCurrentThreadInterrupted() {
if (ibmJ9midp) {
return false;
}
return Thread.interrupted();
}

/**
* Support for JBM J9 PPRO 10
*/
static Throwable getCause(PrivilegedActionException e) {
try {
return e.getCause();
} catch (Throwable j9pp10) {
}
// Use older function
try {
return e.getException();
} catch (Throwable ignore) {
}
return null;
}
}

Change log

r3054 by skarzhevskyy on Aug 11, 2010   Diff
Loïc LAMBERT fix for JavaVer detection
Go to: 
Project members, sign in to write a code review

Older revisions

r2994 by skarzhevskyy on Jun 22, 2009   Diff
Android patches by Dennis Munsie
r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
All revisions of this file

File info

Size: 6894 bytes, 280 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting