01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.util;
17:
18: import javax.naming.Context;
19: import javax.naming.NamingException;
20: import javax.naming.Binding;
21: import javax.naming.NamingEnumeration;
22: import java.io.ByteArrayOutputStream;
23: import java.io.PrintStream;
24: import java.util.Map;
25: import java.util.TreeMap;
26: import java.util.Iterator;
27:
28: /**
29: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
30: */
31: public class Debug {
32:
33: public static String printStackTrace(Throwable t) {
34: ByteArrayOutputStream baos = new ByteArrayOutputStream();
35: t.printStackTrace(new PrintStream(baos));
36: return new String(baos.toByteArray());
37: }
38:
39: public static Map<String, Object> contextToMap(Context context)
40: throws NamingException {
41: Map<String, Object> map = new TreeMap<String, Object>();
42: contextToMap(context, "", map);
43: return map;
44: }
45:
46: public static void contextToMap(Context context, String baseName,
47: Map<String, Object> results) throws NamingException {
48: NamingEnumeration<Binding> namingEnumeration = context
49: .listBindings("");
50: while (namingEnumeration.hasMoreElements()) {
51: Binding binding = namingEnumeration.nextElement();
52: String name = binding.getName();
53: String fullName = baseName + name;
54: Object object = binding.getObject();
55: results.put(fullName, object);
56: if (object instanceof Context) {
57: contextToMap((Context) object, fullName + "/", results);
58: }
59: }
60: }
61:
62: public static Map<String, Object> printContext(Context context)
63: throws NamingException {
64: return printContext(context, System.out);
65: }
66:
67: public static Map<String, Object> printContext(Context context,
68: PrintStream out) throws NamingException {
69: Map<String, Object> map = contextToMap(context);
70: for (Iterator<Map.Entry<String, Object>> iterator = map
71: .entrySet().iterator(); iterator.hasNext();) {
72: Map.Entry<String, Object> entry = iterator.next();
73: out.println(entry.getKey() + "="
74: + entry.getValue().getClass().getName());
75: }
76: return map;
77: }
78: }
|