
org.apache.openejb.util.Debug Maven / Gradle / Ivy
/*
* 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.
*/
package org.apache.openejb.util;
import org.apache.openejb.loader.IO;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* @version $Rev: 1436644 $ $Date: 2013-01-21 22:52:49 +0100 (Mon, 21 Jan 2013) $
*/
public class Debug {
public static String printStackTrace(Throwable t) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(baos));
return new String(baos.toByteArray());
}
public static Map contextToMap(Context context) throws NamingException {
Map map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
contextToMap(context, "", map);
return map;
}
public static void contextToMap(Context context, String baseName, Map results) throws NamingException {
NamingEnumeration namingEnumeration = context.listBindings("");
while (namingEnumeration.hasMoreElements()) {
Binding binding = namingEnumeration.nextElement();
String name = binding.getName();
String fullName = baseName + name;
Object object = binding.getObject();
results.put(fullName, object);
if (object instanceof Context) {
contextToMap((Context) object, fullName + "/", results);
}
}
}
public static Map printContext(Context context) throws NamingException {
return printContext(context, System.out);
}
public static Map printContext(Context context, PrintStream out) throws NamingException {
Map map = contextToMap(context);
for (Entry entry : map.entrySet()) {
out.println(entry.getKey() + "=" + entry.getValue().getClass().getName());
}
return map;
}
public static Map printContextValues(Context context) throws NamingException {
return printContextValues(context, System.out);
}
public static Map printContextValues(Context context, PrintStream out) throws NamingException {
Map map = contextToMap(context);
for (Entry entry : map.entrySet()) {
out.println(entry.getKey() + "=" + entry.getValue());
}
return map;
}
public static List getFields(Class clazz){
if (clazz == null) return Collections.EMPTY_LIST;
List fields = new ArrayList();
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
fields.addAll(getFields(clazz.getSuperclass()));
return fields;
}
public static class Trace {
private static final Trace trace = new Trace();
private final Map elements = new LinkedHashMap();
private final List events = new ArrayList();
private static class Event {
private final long time = System.currentTimeMillis();
private final List elements;
private Event(List elements) {
this.elements = Collections.unmodifiableList(elements);
}
@Override
public String toString() {
return "" +
"time=" + time +
", elements=" + elements +
" ";
}
}
public static void record() {
try {
final File file = new File("/tmp/trace.html");
final OutputStream write = IO.write(file);
report(write);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void report(OutputStream write) {
final PrintStream stream = new PrintStream(write);
report(stream);
stream.close();
}
public static void report(PrintStream stream) {
trace.print(stream);
stream.print("
");
for (Trace.Event event : trace.events) {
stream.println(event);
}
stream.print("
");
}
public static void mark() {
Throwable throwable = new Exception().fillInStackTrace();
List stackTraceElements = new ArrayList(Arrays.asList(throwable.getStackTrace()));
Collections.reverse(stackTraceElements);
Iterator iterator = stackTraceElements.iterator();
while (iterator.hasNext()) {
StackTraceElement element = iterator.next();
if (!element.getClassName().startsWith("org.apache")) iterator.remove();
if (element.getClassName().endsWith("Debug") && element.getMethodName().equals("mark")) iterator.remove();
}
trace.link(stackTraceElements);
}
public void print(PrintStream out) {
Set seen = new HashSet();
for (Node node : elements.values()) {
if (node.parent == null) {
out.println("");
print(seen, out, node, "- ");
out.println("
");
}
}
}
private void print(Set seen, PrintStream out, Node node, String s) {
if (!seen.add(node)) return;
out.print("\n");
StackTraceElement e = node.getElement();
out.printf("%s %s (%s)\n", escape(e.getMethodName()), reverse(e.getClassName()), e.getLineNumber());
if (node.children.size()> 0) {
out.println("");
for (Node child : node.children) {
print(seen, out, child, s);
}
out.println("
");
}
out.print(" \n");
}
private String escape(String methodName) {
return methodName.replace("<","<").replace(">",">");
}
private void printTxt(Set seen, PrintStream out, Node node, String s) {
if (!seen.add(node)) return;
out.print(s);
StackTraceElement e = node.getElement();
out.printf("**%s** *%s* (%s)\n", e.getMethodName(), reverse(e.getClassName()), e.getLineNumber());
s = " " + s;
for (Node child : node.children) {
print(seen, out, child, s);
}
}
private String reverse2(String className) {
List list = Arrays.asList(className.split("\\."));
Collections.reverse(list);
String string = Join.join(".", list);
string = string.replaceAll("(.*?)(\\..*)","$1$2");
return string;
}
private String reverse(String string) {
string = string.replaceAll("(.*)\\.([^.]+)","$2 $1");
return string;
}
public static class Node {
private Node parent;
private final String trace;
private final StackTraceElement element;
private final List children = new ArrayList();
public Node(StackTraceElement element) {
this.element = element;
this.trace = element.toString();
}
public String getTrace() {
return trace;
}
public StackTraceElement getElement() {
return element;
}
public Node addChild(Node node) {
node.parent = this;
children.add(node);
return node;
}
}
public static void reset() {
trace.events.clear();
trace.elements.clear();
}
public void link(List elements) {
events.add(new Event(elements));
Iterator iterator = elements.iterator();
if (!iterator.hasNext()) return;
Node parent = get(iterator.next());
while (iterator.hasNext()) {
parent = parent.addChild(get(iterator.next()));
}
}
private Node get(StackTraceElement element) {
String key = element.toString();
Node node = elements.get(key);
if (node == null) {
node = new Node(element);
elements.put(key, node);
}
return node;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy