View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2008 Vlad Skarzhevskyy
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: UtilsJavaSE.java 2476 2008-12-01 17:41:59Z skarzhevskyy $
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   * J2ME/J9 compatibility module.
36   *
37   * <p>
38   * <b><u>Your application should not use this class directly.</u></b>
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 	 * Marks the thread as a daemon thread. The Java Virtual Machine exits when
173 	 * the only threads running are all daemon threads.
174 	 *
175 	 * @see java.lang.Thread#setDaemon(boolean)
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 			// since Java 1.3
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 			// since Java 1.3
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 			// Fall back if security managed allow to change only specific key
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 	 * Support for JBM J9 PPRO 10
259 	 */
260 	static boolean isCurrentThreadInterrupted() {
261 		if (ibmJ9midp) {
262 			return false;
263 		}
264 		return Thread.interrupted();
265 	}
266 
267 	/**
268 	 * Support for JBM J9 PPRO 10
269 	 */
270 	static Throwable getCause(PrivilegedActionException e) {
271 		try {
272 			return e.getCause();
273 		} catch (Throwable j9pp10) {
274 		}
275 		// Use older function
276 		try {
277 			return e.getException();
278 		} catch (Throwable ignore) {
279 		}
280 		return null;
281 	}
282 }