Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 javax.naming.Binding;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.io.ByteArrayOutputStream;
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: 1161017 $ $Date: 2011-08-24 01:51:27 -0700 (Wed, 24 Aug 2011) $
*/
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();
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("