01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.test.servlet;
18:
19: import javax.naming.Context;
20: import javax.naming.InitialContext;
21: import javax.naming.NameClassPair;
22: import javax.naming.NamingException;
23: import javax.servlet.ServletException;
24: import javax.servlet.ServletOutputStream;
25: import javax.servlet.http.HttpServlet;
26: import javax.servlet.http.HttpServletRequest;
27: import javax.servlet.http.HttpServletResponse;
28: import java.io.IOException;
29: import java.util.Collections;
30: import java.util.Map;
31: import java.util.TreeMap;
32:
33: public class JndiServlet extends HttpServlet {
34: protected void doGet(HttpServletRequest request,
35: HttpServletResponse response) throws ServletException,
36: IOException {
37: response.setContentType("text/plain");
38: ServletOutputStream out = response.getOutputStream();
39:
40: Map<String, Object> bindings = new TreeMap<String, Object>(
41: String.CASE_INSENSITIVE_ORDER);
42: try {
43: Context context = (Context) new InitialContext()
44: .lookup("java:comp/");
45: addBindings("", bindings, context);
46: } catch (NamingException e) {
47: throw new ServletException(e);
48: }
49:
50: out.println("JNDI Context:");
51: for (Map.Entry<String, Object> entry : bindings.entrySet()) {
52: if (entry.getValue() != null) {
53: out.println(" " + entry.getKey() + "="
54: + entry.getValue());
55: } else {
56: out.println(" " + entry.getKey());
57: }
58: }
59: }
60:
61: private void addBindings(String path, Map<String, Object> bindings,
62: Context context) {
63: try {
64: for (NameClassPair pair : Collections
65: .list(context.list(""))) {
66: String name = pair.getName();
67: String className = pair.getClassName();
68: if ("org.apache.naming.resources.FileDirContext$FileResource"
69: .equals(className)) {
70: bindings.put(path + name, "<file>");
71: } else {
72: try {
73: Object value = context.lookup(name);
74: if (value instanceof Context) {
75: Context nextedContext = (Context) value;
76: bindings.put(path + name, "");
77: addBindings(path + name + "/", bindings,
78: nextedContext);
79: } else {
80: bindings.put(path + name, value);
81: }
82: } catch (NamingException e) {
83: // lookup failed
84: bindings.put(path + name, "ERROR: "
85: + e.getMessage());
86: }
87: }
88: }
89: } catch (NamingException e) {
90: bindings.put(path,
91: "ERROR: list bindings threw an exception: "
92: + e.getMessage());
93: }
94: }
95: }
|