001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.webadmin.clienttools;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.util.Properties;
021:
022: import javax.ejb.EJBHome;
023: import javax.ejb.EJBLocalHome;
024: import javax.ejb.Stateless;
025: import javax.ejb.RemoteHome;
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NameClassPair;
029: import javax.naming.NamingEnumeration;
030: import javax.naming.NamingException;
031:
032: import org.apache.openejb.webadmin.HttpRequest;
033: import org.apache.openejb.webadmin.HttpResponse;
034: import org.apache.openejb.webadmin.HttpSession;
035: import org.apache.openejb.webadmin.WebAdminBean;
036: import org.apache.openejb.webadmin.HttpHome;
037:
038: /**
039: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
040: */
041: @Stateless(name="ClientTools/InvokeObject")
042: @RemoteHome(HttpHome.class)
043: public class ViewJndiBean extends WebAdminBean implements Constants {
044:
045: private HttpSession session;
046: private String selected;
047: private String ctxID;
048: private Context ctx;
049:
050: public void preProcess(HttpRequest request, HttpResponse response)
051: throws IOException {
052: session = request.getSession(true);
053: selected = request.getQueryParameter("selected");
054: if (selected == null) {
055: selected = "";
056: }
057: ctxID = request.getQueryParameter("ctx");
058: ctx = null;
059: }
060:
061: public void postProcess(HttpRequest request, HttpResponse response)
062: throws IOException {
063: }
064:
065: public void writeHtmlTitle(PrintWriter out) throws IOException {
066: out.write("Client Tools -- JNDI Viewer");
067: }
068:
069: public void writePageTitle(PrintWriter out) throws IOException {
070: if (ctxID == null) {
071: out.print("JNDI Environment Naming Context (ENC)");
072: } else if (ctxID.startsWith("enc")) {
073: out.print("OpenEJB Global JNDI Namespace");
074: }
075: }
076:
077: public void writeBody(PrintWriter out) throws IOException {
078:
079: if (ctxID == null) {
080: Properties p = new Properties();
081: p
082: .put(Context.INITIAL_CONTEXT_FACTORY,
083: "org.apache.openejb.client.LocalInitialContextFactory");
084: p.put("openejb.loader", "embed");
085: try {
086: ctx = new InitialContext(p);
087: } catch (NamingException e) {
088: // TODO Auto-generated catch block
089: e.printStackTrace();
090: }
091: ctxID = null;
092: out.print("<b>OpenEJB Global JNDI Namespace</b><br><br>");
093: } else {
094: ctx = (Context) session.getAttribute(ctxID);
095: if (ctxID.startsWith("enc")) {
096: out
097: .print("This is the private namespace of an Enterprise JavaBean.");
098: out.print("<BR><BR>");
099: }
100: }
101:
102: Node root = new RootNode();
103: try {
104: buildNode(root, ctx);
105:
106: printNodes(root, out, "", selected);
107: } catch (Exception e) {
108: // TODO Auto-generated catch block
109: e.printStackTrace(out);
110: }
111: }
112:
113: class Node {
114: static final int CONTEXT = 1;
115: static final int BEAN = 2;
116: static final int OTHER = 3;
117: Node parent;
118: Node[] children = new Node[0];
119: String name;
120: int type = 0;
121:
122: public String getID() {
123: if (parent instanceof RootNode) {
124: return name;
125: } else {
126: return parent.getID() + "/" + name;
127: }
128: }
129:
130: public String getName() {
131: return name;
132: }
133:
134: public int getType() {
135: return type;
136: }
137:
138: public void addChild(Node child) {
139: int len = children.length;
140: Node[] newChildren = new Node[len + 1];
141: System.arraycopy(children, 0, newChildren, 0, len);
142: newChildren[len] = child;
143: children = newChildren;
144: child.parent = this ;
145: }
146: }
147:
148: class RootNode extends Node {
149: public String getID() {
150: return "";
151: }
152:
153: public String getName() {
154: return "";
155: }
156:
157: public int getType() {
158: return Node.CONTEXT;
159: }
160: }
161:
162: public void buildNode(Node parent, Context ctx) throws Exception {
163: if (false)
164: throw new NullPointerException();
165: NamingEnumeration enumeration = ctx.list("");
166: while (enumeration.hasMoreElements()) {
167: NameClassPair pair = (NameClassPair) enumeration.next();
168: Node node = new Node();
169: parent.addChild(node);
170: node.name = pair.getName();
171:
172: Object obj = ctx.lookup(node.getName());
173: if (obj instanceof Context) {
174: node.type = Node.CONTEXT;
175: buildNode(node, (Context) obj);
176: } else if (obj instanceof EJBHome
177: || obj instanceof EJBLocalHome) {
178: node.type = Node.BEAN;
179: } else {
180: node.type = Node.OTHER;
181: }
182: }
183: }
184:
185: public void printNodes(Node node, PrintWriter out, String tabs,
186: String selected) throws Exception {
187: switch (node.getType()) {
188: case Node.CONTEXT:
189: printContextNode(node, out, tabs, selected);
190: break;
191: case Node.BEAN:
192: printBeanNode(node, out, tabs, selected);
193: break;
194: default:
195: printOtherNode(node, out, tabs, selected);
196: break;
197: }
198:
199: }
200:
201: public void printContextNode(Node node, PrintWriter out,
202: String tabs, String selected) throws Exception {
203: String id = node.getID();
204: if (selected.startsWith(id)) {
205: if (ctxID != null) {
206: out.print(tabs + "<a href='" + VIEW_JNDI + "?ctx="
207: + ctxID + "&selected=" + id + "'>" + openImg
208: + " " + node.getName() + "</a><br>");
209: } else {
210: out.print(tabs + "<a href='" + VIEW_JNDI + "?selected="
211: + id + "'>" + openImg + " "
212: + node.getName() + "</a><br>");
213: }
214: for (int i = 0; i < node.children.length; i++) {
215: Node child = node.children[i];
216: printNodes(child, out, tabs
217: + " ", selected);
218: }
219: } else {
220: if (ctxID != null) {
221: out.print(tabs + "<a href='" + VIEW_JNDI + "?ctx="
222: + ctxID + "&selected=" + id + "'>" + closedImg
223: + " " + node.getName() + "</a><br>");
224: } else {
225: out.print(tabs + "<a href='" + VIEW_JNDI + "?selected="
226: + id + "'>" + closedImg + " "
227: + node.getName() + "</a><br>");
228: }
229: }
230: }
231:
232: public void printBeanNode(Node node, PrintWriter out, String tabs,
233: String selected) throws Exception {
234: String id = node.getID();
235: // if (ctxID != null && ctxID.startsWith("enc")) {
236: // HACK!
237: try {
238: Object ejb = ctx.lookup(id);
239: Object handler = org.apache.openejb.util.proxy.ProxyManager
240: .getInvocationHandler(ejb);
241: Object deploymentID = ((org.apache.openejb.core.ivm.BaseEjbProxyHandler) handler).deploymentID;
242: out.print(tabs + "<a href='" + VIEW_EJB + "?ejb="
243: + deploymentID + "'>" + ejbImg + " "
244: + node.getName() + "</a><br>");
245: } catch (Exception e) {
246: out.print(tabs + ejbImg + " " + node.getName()
247: + "<br>");
248: }
249: // } else {
250: // out.print(tabs+"<a href='"+VIEW_EJB+"?ejb="+id+"'>"+ejbImg+" "+node.getName()+"</a><br>");
251: // }
252: }
253:
254: public void printOtherNode(Node node, PrintWriter out, String tabs,
255: String selected) throws Exception {
256: String id = node.getID();
257: Object obj = ctx.lookup(id);
258: String clazz = obj.getClass().getName();
259: out.print(tabs + "<a href='" + VIEW_CLASS + "?class=" + clazz
260: + "'>" + javaImg + " " + node.getName()
261: + "</a><br>");
262: }
263: }
|