KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > clienttools > ViewJndiBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ViewJndiBean.java 2487 2006-02-22 22:05:03Z dblevins $
44  */

45 package org.openejb.webadmin.clienttools;
46
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.Properties JavaDoc;
50
51 import javax.ejb.EJBHome JavaDoc;
52 import javax.ejb.EJBLocalHome JavaDoc;
53 import javax.naming.Context JavaDoc;
54 import javax.naming.InitialContext JavaDoc;
55 import javax.naming.NameClassPair JavaDoc;
56 import javax.naming.NamingEnumeration JavaDoc;
57 import javax.naming.NamingException JavaDoc;
58
59 import org.openejb.webadmin.HttpRequest;
60 import org.openejb.webadmin.HttpResponse;
61 import org.openejb.webadmin.HttpSession;
62 import org.openejb.webadmin.WebAdminBean;
63
64 /**
65  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
66  */

67 public class ViewJndiBean extends WebAdminBean implements Constants {
68
69     
70     private HttpSession session;
71     private String JavaDoc selected;
72     private String JavaDoc ctxID;
73     private Context JavaDoc ctx;
74     
75     public void preProcess(HttpRequest request, HttpResponse response)
76         throws IOException JavaDoc {
77         session = request.getSession(true);
78         selected = request.getQueryParameter("selected");
79         if (selected == null) {
80             selected = "";
81         }
82         ctxID = request.getQueryParameter("ctx");
83         ctx = null;
84     }
85
86     public void postProcess(HttpRequest request, HttpResponse response)
87         throws IOException JavaDoc {
88     }
89
90     public void writeHtmlTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
91         out.write("Client Tools -- JNDI Viewer");
92     }
93
94     public void writePageTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
95         if (ctxID == null){
96             out.print("JNDI Environment Naming Context (ENC)");
97         } else if (ctxID.startsWith("enc")) {
98                 out.print("OpenEJB Global JNDI Namespace");
99         }
100     }
101
102     public void writeBody(PrintWriter JavaDoc out) throws IOException JavaDoc {
103
104         if (ctxID == null) {
105             Properties JavaDoc p = new Properties JavaDoc();
106             p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory");
107             p.put("openejb.loader", "embed");
108             try {
109                 ctx = new InitialContext JavaDoc( p );
110             } catch (NamingException JavaDoc e) {
111                 // TODO Auto-generated catch block
112
e.printStackTrace();
113             }
114             ctxID = null;
115             out.print("<b>OpenEJB Global JNDI Namespace</b><br><br>");
116         } else {
117             ctx = (Context JavaDoc)session.getAttribute(ctxID);
118             if (ctxID.startsWith("enc")) {
119                 out.print("This is the private namespace of an Enterprise JavaBean.");
120                 out.print("<BR><BR>");
121             }
122         }
123         
124         Node root = new RootNode();
125         try {
126             buildNode(root,ctx);
127
128             printNodes(root, out, "", selected);
129         } catch (Exception JavaDoc e) {
130             // TODO Auto-generated catch block
131
e.printStackTrace(out);
132         }
133     }
134             
135     class Node {
136         static final int CONTEXT = 1;
137         static final int BEAN = 2;
138         static final int OTHER = 3;
139         Node parent;
140         Node[] children = new Node[0];
141         String JavaDoc name;
142         int type = 0;
143
144         public String JavaDoc getID(){
145             if (parent instanceof RootNode) {
146                 return name;
147             } else {
148                 return parent.getID()+"/"+name;
149             }
150         }
151         public String JavaDoc getName(){
152             return name;
153         }
154         public int getType(){
155             return type;
156         }
157         public void addChild(Node child){
158             int len = children.length;
159             Node[] newChildren = new Node[len+1];
160             System.arraycopy(children,0,newChildren,0,len);
161             newChildren[len] = child;
162             children = newChildren;
163             child.parent = this;
164         }
165     }
166
167     class RootNode extends Node{
168         public String JavaDoc getID() {
169             return "";
170         }
171         public String JavaDoc getName() {
172             return "";
173         }
174         public int getType() {
175             return Node.CONTEXT;
176         }
177     }
178
179     public void buildNode(Node parent, Context JavaDoc ctx) throws Exception JavaDoc{
180         if (false) throw new NullPointerException JavaDoc();
181         NamingEnumeration JavaDoc enumeration = ctx.list( "" );
182         while (enumeration.hasMoreElements()){
183             NameClassPair JavaDoc pair = (NameClassPair JavaDoc)enumeration.next();
184             Node node = new Node();
185             parent.addChild(node);
186             node.name = pair.getName();
187             
188             Object JavaDoc obj = ctx.lookup(node.getName());
189             if ( obj instanceof Context JavaDoc ){
190                 node.type = Node.CONTEXT;
191                 buildNode(node,(Context JavaDoc)obj);
192             } else if (obj instanceof EJBHome JavaDoc || obj instanceof EJBLocalHome JavaDoc) {
193                 node.type = Node.BEAN;
194             } else {
195                 node.type = Node.OTHER;
196             }
197         }
198     }
199     
200     
201     
202     public void printNodes(Node node, PrintWriter JavaDoc out, String JavaDoc tabs, String JavaDoc selected) throws Exception JavaDoc {
203         switch (node.getType()) {
204             case Node.CONTEXT: printContextNode(node,out,tabs,selected); break;
205            case Node.BEAN: printBeanNode(node,out,tabs,selected); break;
206           default: printOtherNode(node,out,tabs,selected); break;
207         }
208         
209     }
210
211     public void printContextNode(Node node, PrintWriter JavaDoc out, String JavaDoc tabs, String JavaDoc selected) throws Exception JavaDoc {
212         String JavaDoc id = node.getID();
213         if ( selected.startsWith(id) ) {
214             if (ctxID != null) {
215                 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?ctx="+ctxID+"&selected="+id+"'>"+openImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
216             } else {
217                 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?selected="+id+"'>"+openImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
218             }
219             for (int i=0; i < node.children.length; i++){
220                 Node child = node.children[i];
221                 printNodes(child,out,tabs+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",selected);
222             }
223         } else {
224             if (ctxID != null) {
225                 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?ctx="+ctxID+"&selected="+id+"'>"+closedImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
226             } else {
227                 out.print(tabs+"<a HREF='"+VIEW_JNDI+"?selected="+id+"'>"+closedImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
228             }
229         }
230     }
231
232     public void printBeanNode(Node node, PrintWriter JavaDoc out, String JavaDoc tabs, String JavaDoc selected) throws Exception JavaDoc {
233         String JavaDoc id = node.getID();
234 // if (ctxID != null && ctxID.startsWith("enc")) {
235
// HACK!
236
try{
237                 Object JavaDoc ejb = ctx.lookup(id);
238                 Object JavaDoc handler = org.openejb.util.proxy.ProxyManager.getInvocationHandler(ejb);
239                 Object JavaDoc deploymentID = ((org.openejb.core.ivm.BaseEjbProxyHandler)handler).deploymentID;
240                 out.print(tabs+"<a HREF='"+VIEW_EJB+"?ejb="+deploymentID+"'>"+ejbImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
241             } catch (Exception JavaDoc e){
242                 out.print(tabs+ejbImg+"&nbsp;&nbsp;"+node.getName()+"<br>");
243             }
244 // } else {
245
// out.print(tabs+"<a HREF='"+VIEW_EJB+"?ejb="+id+"'>"+ejbImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
246
// }
247
}
248     
249     public void printOtherNode(Node node, PrintWriter JavaDoc out, String JavaDoc tabs, String JavaDoc selected) throws Exception JavaDoc {
250         String JavaDoc id = node.getID();
251         Object JavaDoc obj = ctx.lookup(id);
252         String JavaDoc clazz = obj.getClass().getName();
253         out.print(tabs+"<a HREF='"+VIEW_CLASS+"?class="+clazz+"'>"+javaImg+"&nbsp;&nbsp;"+node.getName()+"</a><br>");
254     }
255 }
256
Popular Tags