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.HashMap;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.ejb.Stateless;
027: import javax.ejb.RemoteHome;
028:
029: import org.apache.openejb.webadmin.HttpRequest;
030: import org.apache.openejb.webadmin.HttpResponse;
031: import org.apache.openejb.webadmin.HttpSession;
032: import org.apache.openejb.webadmin.WebAdminBean;
033: import org.apache.openejb.webadmin.HttpHome;
034: import org.apache.openejb.assembler.classic.ContainerInfo;
035: import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
036: import org.apache.openejb.config.ConfigurationFactory;
037: import org.apache.openejb.core.CoreDeploymentInfo;
038: import org.apache.openejb.assembler.classic.ContainerInfo;
039: import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
040: import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
041: import org.apache.openejb.assembler.classic.AppInfo;
042: import org.apache.openejb.assembler.classic.EjbJarInfo;
043: import org.apache.openejb.config.ConfigurationFactory;
044: import org.apache.openejb.loader.SystemInstance;
045: import org.apache.openejb.spi.ContainerSystem;
046: import org.apache.openejb.DeploymentInfo;
047: import org.apache.openejb.BeanType;
048:
049: /**
050: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
051: */
052: @Stateless(name="ClientTools/ViewEjb")
053: @RemoteHome(HttpHome.class)
054: public class ViewEjbBean extends WebAdminBean implements Constants {
055:
056: public void preProcess(HttpRequest request, HttpResponse response)
057: throws IOException {
058: }
059:
060: public void postProcess(HttpRequest request, HttpResponse response)
061: throws IOException {
062: }
063:
064: public void writeHtmlTitle(PrintWriter out) throws IOException {
065: out.write("Client Tools -- EJB Viewer");
066: }
067:
068: public void writePageTitle(PrintWriter out) throws IOException {
069: out.write("EJB Viewer");
070: }
071:
072: public void writeBody(PrintWriter out) throws IOException {
073: try {
074: String ejb = request.getQueryParameter("ejb");
075: if (ejb == null) {
076: OpenEjbConfiguration configuration = SystemInstance
077: .get().getComponent(OpenEjbConfiguration.class);
078: for (AppInfo appInfo : configuration.containerSystem.applications) {
079: for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
080: for (EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
081: out
082: .print("<a href='" + VIEW_EJB
083: + "?ejb="
084: + bean.ejbDeploymentId
085: + "'>" + ejbImg
086: + " "
087: + bean.ejbDeploymentId
088: + "</a><br>");
089: }
090: }
091: }
092: } else {
093: printEjb(ejb, out, request.getSession());
094: }
095: } catch (Exception e) {
096:
097: out.println("FAIL: ");
098: out.print(e.getMessage());
099: // throw e;
100: //return;
101: }
102: }
103:
104: public void printEjb(String name, PrintWriter out,
105: HttpSession session) throws Exception {
106:
107: SystemInstance system = SystemInstance.get();
108: ContainerSystem containerSystem = system
109: .getComponent(ContainerSystem.class);
110:
111: String id = (name.startsWith("/")) ? name.substring(1, name
112: .length()) : name;
113:
114: org.apache.openejb.DeploymentInfo ejb = containerSystem
115: .getDeploymentInfo(id);
116:
117: if (ejb == null) {
118: out.print("No such EJB: " + id);
119: return;
120: }
121: String type = null;
122:
123: switch (ejb.getComponentType()) {
124: case CMP_ENTITY:
125: type = "EntityBean with Container-Managed Persistence";
126: break;
127: case BMP_ENTITY:
128: type = "EntityBean with Bean-Managed Persistence";
129: break;
130: case STATEFUL:
131: type = "Stateful SessionBean";
132: break;
133: case STATELESS:
134: type = "Stateless SessionBean";
135: break;
136: default:
137: type = "Unkown Bean Type";
138: break;
139: }
140: out.print("<b>" + type + "</b><br>");
141: out.print("<table>");
142: printRow("JNDI Name", name, out);
143:
144: boolean hasLocal = ejb.getLocalInterface() != null;
145: boolean hasRemote = ejb.getRemoteInterface() != null;
146:
147: String remoteInterfaceClassRef;
148: String homeInterfaceClassRef;
149: if (hasRemote) {
150: remoteInterfaceClassRef = getClassRef(ejb
151: .getRemoteInterface());
152: homeInterfaceClassRef = getClassRef(ejb.getHomeInterface());
153: } else {
154: remoteInterfaceClassRef = "none";
155: homeInterfaceClassRef = "none";
156: }
157:
158: printRow("Remote Interface", remoteInterfaceClassRef, out);
159: printRow("Home Interface", homeInterfaceClassRef, out);
160:
161: if (hasLocal) {
162: String clzz = getClassRef(ejb.getLocalInterface());
163: printRow("Local Interface", clzz, out);
164: clzz = getClassRef(ejb.getLocalHomeInterface());
165: printRow("LocalHome Interface", clzz, out);
166: }
167:
168: printRow("Bean Class", getClassRef(ejb.getBeanClass()), out);
169:
170: if (ejb.getComponentType() == BeanType.BMP_ENTITY
171: || ejb.getComponentType() == BeanType.CMP_ENTITY) {
172: printRow("Primary Key", getClassRef(ejb
173: .getPrimaryKeyClass()), out);
174: }
175:
176: out.print("</table>");
177: out.print("<br><br><b>Actions:</b><br>");
178: out.print("<table>");
179:
180: // Browse JNDI with this ejb
181: //javax.servlet.http.HttpSession session = this.session;
182: HashMap objects = (HashMap) session.getAttribute("objects");
183: if (objects == null) {
184: objects = new HashMap();
185: session.setAttribute("objects", objects);
186: }
187:
188: InitialContext ctx;
189: Properties p = new Properties();
190:
191: p.put(Context.INITIAL_CONTEXT_FACTORY,
192: "org.apache.openejb.client.LocalInitialContextFactory");
193: p.put("openejb.loader", "embed");
194:
195: ctx = new InitialContext(p);
196:
197: if (hasRemote) {
198: Object obj = ctx.lookup(name);
199: String objID = ejb.getHomeInterface().getName() + "@"
200: + obj.hashCode();
201: objects.put(objID, obj);
202: String invokerURL = "<a href='" + INVOKE_OBJ + "?obj="
203: + objID + "'>Invoke this EJB's home interface</a>";
204: printRow(pepperImg, invokerURL, out);
205: }
206: if (hasLocal) {
207: Object obj = ctx.lookup(name + "Local");
208: String objID = ejb.getLocalHomeInterface().getName() + "@"
209: + obj.hashCode();
210: objects.put(objID, obj);
211: String invokerURL = "<a href='" + INVOKE_OBJ + "?obj="
212: + objID
213: + "'>Invoke this EJB's local home interface</a>";
214: printRow(pepperImg, invokerURL, out);
215: }
216:
217: Context enc = ((org.apache.openejb.core.CoreDeploymentInfo) ejb)
218: .getJndiEnc();
219: String ctxID = "enc" + enc.hashCode();
220: session.setAttribute(ctxID, enc);
221: String jndiURL = "<a href='" + VIEW_JNDI + "?ctx=" + ctxID
222: + "'>Browse this EJB's private JNDI namespace</a>";
223: printRow(pepperImg, jndiURL, out);
224: out.print("</table>");
225:
226: }
227:
228: protected void printRow(String col1, String col2, PrintWriter out)
229: throws IOException {
230: out.print("<tr><td><font size='2'>");
231: out.print(col1);
232: out.print("</font></td><td><font size='2'>");
233: out.print(col2);
234: out.print("</font></td></tr>");
235: }
236:
237: public String getClassRef(Class clazz) throws Exception {
238: String name = clazz.getName();
239: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
240: + name + "</a>";
241: }
242:
243: public String getShortClassRef(Class clazz) throws Exception {
244: if (clazz.isPrimitive()) {
245: return "<font color='gray'>" + clazz.getName() + "</font>";
246: } else if (clazz.isArray()
247: && clazz.getComponentType().isPrimitive()) {
248: return "<font color='gray'>" + clazz.getComponentType()
249: + "[]</font>";
250: } else if (clazz.isArray()) {
251: String name = clazz.getComponentType().getName();
252: int dot = name.lastIndexOf(".") + 1;
253: String shortName = name.substring(dot, name.length());
254: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
255: + shortName + "[]</a>";
256: } else {
257: String name = clazz.getName();
258: int dot = name.lastIndexOf(".") + 1;
259: String shortName = name.substring(dot, name.length());
260: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
261: + shortName + "</a>";
262: }
263: }
264: }
|