KickJava   Java API By Example, From Geeks To Geeks.

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


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: ViewEjbBean.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.webadmin.clienttools;
46
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Properties JavaDoc;
52
53 import javax.naming.Context JavaDoc;
54 import javax.naming.InitialContext JavaDoc;
55
56 import org.openejb.webadmin.HttpRequest;
57 import org.openejb.webadmin.HttpResponse;
58 import org.openejb.webadmin.HttpSession;
59 import org.openejb.webadmin.WebAdminBean;
60 import org.openejb.alt.assembler.classic.ContainerInfo;
61 import org.openejb.alt.assembler.classic.EnterpriseBeanInfo;
62 import org.openejb.alt.config.ConfigurationFactory;
63 import org.openejb.core.DeploymentInfo;
64
65 /**
66  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
67  */

68 public class ViewEjbBean extends WebAdminBean implements Constants {
69
70     public void preProcess(HttpRequest request, HttpResponse response)
71         throws IOException JavaDoc {
72     }
73
74     public void postProcess(HttpRequest request, HttpResponse response)
75         throws IOException JavaDoc {
76     }
77
78     public void writeHtmlTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
79         out.write("Client Tools -- EJB Viewer");
80     }
81
82     public void writePageTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
83         out.write("EJB Viewer");
84     }
85
86     public void writeBody(PrintWriter JavaDoc out) throws IOException JavaDoc {
87         try {
88             String JavaDoc ejb = request.getQueryParameter("ejb");
89             if (ejb == null) {
90                 ContainerInfo[] cnt = ConfigurationFactory.sys.containerSystem.containers;
91                 for (int i = 0; i < cnt.length; i++) {
92                     EnterpriseBeanInfo[] beans = cnt[i].ejbeans;
93                     for (int x = 0; x < beans.length; x++) {
94                         EnterpriseBeanInfo bean = beans[x];
95                         out.print("<a HREF='"+VIEW_EJB+"?ejb="+bean.ejbDeploymentId+"'>"+ejbImg+"&nbsp;&nbsp;"+bean.ejbDeploymentId+"</a><br>");
96                     }
97                 }
98             } else {
99                 printEjb(ejb, out, request.getSession());
100             }
101         } catch (Exception JavaDoc e) {
102
103             out.println("FAIL: ");
104             out.print(e.getMessage());
105             // throw e;
106
//return;
107
}
108     }
109
110     public void printEjb(String JavaDoc name, PrintWriter JavaDoc out, HttpSession session)
111         throws Exception JavaDoc {
112         
113         
114         String JavaDoc id =
115             (name.startsWith("/")) ? name.substring(1, name.length()) : name;
116         org.openejb.DeploymentInfo ejb =
117             org.openejb.OpenEJB.getDeploymentInfo(id);
118
119         
120         if (ejb == null) {
121             out.print("No such EJB: " + id);
122             return;
123         }
124         String JavaDoc type = null;
125
126         switch (ejb.getComponentType()) {
127             case org.openejb.core.DeploymentInfo.CMP_ENTITY :
128                 type = "EntityBean with Container-Managed Persistence";
129                 break;
130             case org.openejb.core.DeploymentInfo.BMP_ENTITY :
131                 type = "EntityBean with Bean-Managed Persistence";
132                 break;
133             case org.openejb.core.DeploymentInfo.STATEFUL :
134                 type = "Stateful SessionBean";
135                 break;
136             case org.openejb.core.DeploymentInfo.STATELESS :
137                 type = "Stateless SessionBean";
138                 break;
139             default :
140                 type = "Unkown Bean Type";
141                 break;
142         }
143         out.print("<b>" + type + "</b><br>");
144         out.print("<table>");
145         printRow("JNDI Name", name, out);
146         
147         
148         boolean hasLocal = ejb.getLocalInterface() != null;
149         boolean hasRemote = ejb.getRemoteInterface() != null;
150
151         String JavaDoc remoteInterfaceClassRef;
152         String JavaDoc homeInterfaceClassRef;
153         if (hasRemote){
154             remoteInterfaceClassRef = getClassRef(ejb.getRemoteInterface());
155             homeInterfaceClassRef = getClassRef(ejb.getHomeInterface());
156         } else {
157             remoteInterfaceClassRef = "none";
158             homeInterfaceClassRef = "none";
159         }
160         
161         printRow("Remote Interface",remoteInterfaceClassRef,out);
162         printRow("Home Interface", homeInterfaceClassRef, out);
163
164         if (hasLocal){
165             String JavaDoc clzz = getClassRef(ejb.getLocalInterface());
166             printRow("Local Interface",clzz,out);
167             clzz = getClassRef(ejb.getLocalHomeInterface());
168             printRow("LocalHome Interface",clzz,out);
169         }
170         
171         printRow("Bean Class", getClassRef(ejb.getBeanClass()), out);
172
173         if (ejb.getComponentType() == DeploymentInfo.BMP_ENTITY
174             || ejb.getComponentType() == DeploymentInfo.CMP_ENTITY) {
175             printRow("Primary Key", getClassRef(ejb.getPrimaryKeyClass()), out);
176         }
177
178         out.print("</table>");
179         out.print("<br><br><b>Actions:</b><br>");
180         out.print("<table>");
181
182         // Browse JNDI with this ejb
183
//javax.servlet.http.HttpSession session = this.session;
184
HashMap JavaDoc objects = (HashMap JavaDoc) session.getAttribute("objects");
185         if (objects == null) {
186             objects = new HashMap JavaDoc();
187             session.setAttribute("objects", objects);
188         }
189
190         InitialContext JavaDoc ctx;
191         Properties JavaDoc p = new Properties JavaDoc();
192
193         p.put(
194             Context.INITIAL_CONTEXT_FACTORY,
195             "org.openejb.client.LocalInitialContextFactory");
196         p.put("openejb.loader", "embed");
197
198         ctx = new InitialContext JavaDoc(p);
199         
200
201         if (hasRemote){
202             Object JavaDoc obj = ctx.lookup(name);
203             String JavaDoc objID = ejb.getHomeInterface().getName() + "@" + obj.hashCode();
204             objects.put(objID, obj);
205             String JavaDoc invokerURL =
206                 "<a HREF='"
207                     + INVOKE_OBJ
208                     + "?obj="
209                     + objID
210                     + "'>Invoke this EJB's home interface</a>";
211             printRow(pepperImg, invokerURL, out);
212         }
213         if (hasLocal){
214             Object JavaDoc obj = ctx.lookup(name+"Local");
215             String JavaDoc objID = ejb.getLocalHomeInterface().getName() + "@" + obj.hashCode();
216             objects.put(objID, obj);
217             String JavaDoc invokerURL =
218                 "<a HREF='"
219                     + INVOKE_OBJ
220                     + "?obj="
221                     + objID
222                     + "'>Invoke this EJB's local home interface</a>";
223             printRow(pepperImg, invokerURL, out);
224         }
225
226         Context JavaDoc enc = ((org.openejb.core.DeploymentInfo) ejb).getJndiEnc();
227         String JavaDoc ctxID = "enc" + enc.hashCode();
228         session.setAttribute(ctxID, enc);
229         String JavaDoc jndiURL =
230             "<a HREF='"
231                 + VIEW_JNDI
232                 + "?ctx="
233                 + ctxID
234                 + "'>Browse this EJB's private JNDI namespace</a>";
235         printRow(pepperImg, jndiURL, out);
236         out.print("</table>");
237
238     }
239
240     protected void printRow(String JavaDoc col1, String JavaDoc col2, PrintWriter JavaDoc out)
241         throws IOException JavaDoc {
242         out.print("<tr><td><font size='2'>");
243         out.print(col1);
244         out.print("</font></td><td><font size='2'>");
245         out.print(col2);
246         out.print("</font></td></tr>");
247     }
248
249     public String JavaDoc getClassRef(Class JavaDoc clazz) throws Exception JavaDoc {
250         String JavaDoc name = clazz.getName();
251         return "<a HREF='"
252             + VIEW_CLASS
253             + "?class="
254             + name
255             + "'>"
256             + name
257             + "</a>";
258     }
259
260     public String JavaDoc getShortClassRef(Class JavaDoc clazz) throws Exception JavaDoc {
261         if (clazz.isPrimitive()) {
262             return "<font color='gray'>" + clazz.getName() + "</font>";
263         } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) {
264             return "<font color='gray'>"
265                 + clazz.getComponentType()
266                 + "[]</font>";
267         } else if (clazz.isArray()) {
268             String JavaDoc name = clazz.getComponentType().getName();
269             int dot = name.lastIndexOf(".") + 1;
270             String JavaDoc shortName = name.substring(dot, name.length());
271             return "<a HREF='"
272                 + VIEW_CLASS
273                 + "?class="
274                 + name
275                 + "'>"
276                 + shortName
277                 + "[]</a>";
278         } else {
279             String JavaDoc name = clazz.getName();
280             int dot = name.lastIndexOf(".") + 1;
281             String JavaDoc shortName = name.substring(dot, name.length());
282             return "<a HREF='"
283                 + VIEW_CLASS
284                 + "?class="
285                 + name
286                 + "'>"
287                 + shortName
288                 + "</a>";
289         }
290     }
291 }
292
Popular Tags