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: JavaSECommon.java 2650 2008-12-23 18:46:57Z skarzhevskyy $
24   */
25  package net.sf.bluecove.se;
26  
27  import java.io.File;
28  import java.io.FileWriter;
29  import java.io.OutputStreamWriter;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  import java.util.Iterator;
33  import java.util.Map;
34  
35  import net.sf.bluecove.Configuration;
36  
37  import org.bluecove.tester.log.Logger;
38  import org.bluecove.tester.util.CLDCStub;
39  import org.bluecove.tester.util.IOUtils;
40  import org.bluecove.tester.util.RuntimeDetect;
41  
42  public class JavaSECommon implements CLDCStub {
43  
44  	private static boolean initialized = false;
45  
46  	public static void initOnce() {
47  		if (initialized) {
48  			return;
49  		}
50  		initialized = true;
51  		Configuration.logTimeStamp = true;
52  		Logger.addAppender(new LoggerJavaSEAppender());
53  
54  		if (Configuration.serverAcceptWhileConnectedOnJavaSE) {
55  			Configuration.serverAcceptWhileConnected.setValue(true);
56  			// Configuration.testIgnoreNotWorkingServiceAttributes = false;
57  		}
58  
59  		RuntimeDetect.cldcStub = new JavaSECommon();
60  	}
61  
62  	public boolean canInterruptThread() {
63  		return true;
64  	}
65  
66  	public void interruptThread(Thread t) {
67  		if (t != null) {
68  			t.interrupt();
69  		}
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * 
75  	 * @see net.sf.bluecove.util.CLDCStub#createNamedThread(java.lang.Runnable, java.lang.String)
76  	 */
77  	public Thread createNamedThread(Runnable target, String name) {
78  		return new Thread(target, name);
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * 
84  	 * @see net.sf.bluecove.util.CLDCStub#setThreadLocalBluetoothStack(java.lang.Object)
85  	 */
86  	public void setThreadLocalBluetoothStack(Object id) {
87  		LocalDeviceManager.setThreadLocalBluetoothStack(id);
88  	}
89  
90  	public static boolean isJava5() {
91  		try {
92  			return java5Function();
93  		} catch (Throwable e) {
94  			return false;
95  		}
96  	}
97  
98  	static boolean java5Function() {
99  		return (Thread.currentThread().getStackTrace() != null);
100 	}
101 
102 	public static void threadDump() {
103 		SimpleDateFormat fmt = new SimpleDateFormat("MM-dd_HH-mm-ss");
104 		OutputStreamWriter out = null;
105 		try {
106 			File file = new File("ThreadDump-" + fmt.format(new Date()) + ".log");
107 			out = new FileWriter(file);
108 			Map traces = Thread.getAllStackTraces();
109 			for (Iterator iterator = traces.entrySet().iterator(); iterator.hasNext();) {
110 				Map.Entry entry = (Map.Entry) iterator.next();
111 				Thread thread = (Thread) entry.getKey();
112 				out.write("Thread= " + thread.getName() + " " + (thread.isDaemon() ? "daemon" : "") + " prio="
113 						+ thread.getPriority() + "id=" + thread.getId() + " " + thread.getState());
114 				out.write("\n");
115 
116 				StackTraceElement[] ste = (StackTraceElement[]) entry.getValue();
117 				for (int i = 0; i < ste.length; i++) {
118 					out.write("\t");
119 					out.write(ste[i].toString());
120 					out.write("\n");
121 				}
122 				out.write("---------------------------------\n");
123 			}
124 			out.close();
125 			out = null;
126 			Logger.info("Full ThreadDump created " + file.getAbsolutePath());
127 		} catch (Throwable ignore) {
128 		} finally {
129 			IOUtils.closeQuietly(out);
130 		}
131 	}
132 }