All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openejb.util.Debug Maven / Gradle / Ivy

The newest version!
/*
 * 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$ $Date$
 */
public class Debug {

    public static String printStackTrace(final Throwable t) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        t.printStackTrace(new PrintStream(baos));
        return new String(baos.toByteArray());
    }

    public static Map contextToMap(final Context context) throws NamingException {
        final Map map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
        contextToMap(context, "", map);
        return map;
    }

    public static void contextToMap(final Context context, final String baseName, final Map results) throws NamingException {
        final NamingEnumeration namingEnumeration = context.listBindings("");
        while (namingEnumeration.hasMoreElements()) {
            final Binding binding = namingEnumeration.nextElement();
            final String name = binding.getName();
            final String fullName = baseName + name;
            final Object object = binding.getObject();
            results.put(fullName, object);
            if (object instanceof Context) {
                contextToMap((Context) object, fullName + "/", results);
            }
        }
    }

    public static Map printContext(final Context context) throws NamingException {
        return printContext(context, System.out);
    }

    public static Map printContext(final Context context, final PrintStream out) throws NamingException {
        final Map map = contextToMap(context);
        for (final Entry entry : map.entrySet()) {
            out.println(entry.getKey() + "=" + entry.getValue().getClass().getName());
        }
        return map;
    }

    public static Map printContextValues(final Context context) throws NamingException {
        return printContextValues(context, System.out);
    }

    public static Map printContextValues(final Context context, final PrintStream out) throws NamingException {
        final Map map = contextToMap(context);
        for (final Entry entry : map.entrySet()) {
            out.println(entry.getKey() + "=" + entry.getValue());
        }
        return map;
    }

    public static List getFields(final Class clazz) {
        if (clazz == null) {
            return Collections.EMPTY_LIST;
        }

        final 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 final class Event {
            private final long time = System.currentTimeMillis();
            private final List elements;

            private Event(final 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 (final FileNotFoundException e) { e.printStackTrace(); } } public static void report(final OutputStream write) { final PrintStream stream = new PrintStream(write); report(stream); stream.close(); } public static void report(final PrintStream stream) { trace.print(stream); stream.print("
      "); for (final Trace.Event event : trace.events) { stream.println(event); } stream.print("
    "); } public static void mark() { final Throwable throwable = new Exception().fillInStackTrace(); final List stackTraceElements = new ArrayList(Arrays.asList(throwable.getStackTrace())); Collections.reverse(stackTraceElements); final Iterator iterator = stackTraceElements.iterator(); while (iterator.hasNext()) { final 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(final PrintStream out) { final Set seen = new HashSet(); for (final Node node : elements.values()) { if (node.parent == null) { out.println("
      "); print(seen, out, node, "- "); out.println("
    "); } } } private void print(final Set seen, final PrintStream out, final Node node, final String s) { if (!seen.add(node)) { return; } out.print("
  • \n"); final 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 (final Node child : node.children) { print(seen, out, child, s); } out.println("
    "); } out.print("
  • \n"); } private String escape(final String methodName) { return methodName.replace("<", "<").replace(">", ">"); } private void printTxt(final Set seen, final PrintStream out, final Node node, String s) { if (!seen.add(node)) { return; } out.print(s); final StackTraceElement e = node.getElement(); out.printf("**%s** *%s* (%s)\n", e.getMethodName(), reverse(e.getClassName()), e.getLineNumber()); s = " " + s; for (final Node child : node.children) { print(seen, out, child, s); } } private String reverse2(final String className) { final 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(final StackTraceElement element) { this.element = element; this.trace = element.toString(); } public String getTrace() { return trace; } public StackTraceElement getElement() { return element; } public Node addChild(final Node node) { node.parent = this; children.add(node); return node; } } public static void reset() { trace.events.clear(); trace.elements.clear(); } public void link(final List elements) { events.add(new Event(elements)); final 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(final StackTraceElement element) { final 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