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.main;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.util.Arrays;
021: import java.util.HashMap;
022: import java.util.List;
023:
024: import org.apache.openejb.DeploymentInfo;
025: import org.apache.openejb.OpenEJB;
026: import org.apache.openejb.core.CoreDeploymentInfo;
027: import org.apache.openejb.spi.ContainerSystem;
028: import org.apache.openejb.loader.SystemInstance;
029: import org.apache.openejb.webadmin.HttpRequest;
030: import org.apache.openejb.webadmin.HttpResponse;
031: import org.apache.openejb.webadmin.WebAdminBean;
032: import org.apache.openejb.webadmin.HttpHome;
033: import org.apache.openejb.assembler.classic.EjbReferenceInfo;
034: import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
035: import org.apache.openejb.assembler.classic.EnvEntryInfo;
036: import org.apache.openejb.assembler.classic.JndiEncInfo;
037: import org.apache.openejb.assembler.classic.ResourceReferenceInfo;
038: import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
039: import org.apache.openejb.assembler.classic.AppInfo;
040: import org.apache.openejb.assembler.classic.EjbJarInfo;
041: import org.apache.openejb.assembler.classic.EjbLocalReferenceInfo;
042: import org.apache.openejb.assembler.classic.ResourceEnvReferenceInfo;
043: import org.apache.openejb.assembler.classic.PersistenceUnitReferenceInfo;
044: import org.apache.openejb.assembler.classic.PersistenceContextReferenceInfo;
045: import org.apache.openejb.assembler.classic.ServiceReferenceInfo;
046: import org.apache.openejb.util.StringUtilities;
047:
048: import javax.ejb.Stateless;
049: import javax.ejb.RemoteHome;
050:
051: /**
052: * A bean which lists all deployed beans.
053: *
054: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
055: * @author <a href="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
056: */
057: @Stateless(name="Webadmin/DeploymentList")
058: @RemoteHome(HttpHome.class)
059: public class DeploymentListBean extends WebAdminBean {
060: private HashMap deploymentIdIndex;
061: private HashMap containerIdIndex;
062:
063: /** Creates a new instance of DeploymentListBean */
064: public void ejbCreate() {
065: this .section = "DeploymentList";
066: }
067:
068: /** called after all content is written to the browser
069: * @param request the http request
070: * @param response the http response
071: * @throws IOException if an exception is thrown
072: *
073: */
074: public void postProcess(HttpRequest request, HttpResponse response)
075: throws IOException {
076: }
077:
078: /** called before any content is written to the browser
079: * @param request the http request
080: * @param response the http response
081: * @throws IOException if an exception is thrown
082: *
083: */
084: public void preProcess(HttpRequest request, HttpResponse response)
085: throws IOException {
086: createIndexes();
087: }
088:
089: /** writes the main body content to the broswer. This content is inside a <code><p></code> block
090: *
091: *
092: * @param body the output to write to
093: * @exception IOException if an exception is thrown
094: *
095: */
096: public void writeBody(PrintWriter body) throws IOException {
097: String id = request.getQueryParameter("id");
098: if (id != null) {
099: showDeployment(body, id);
100: } else {
101: printDeployments(body);
102: }
103: }
104:
105: private void createIndexes() {
106: deploymentIdIndex = new HashMap();
107: containerIdIndex = new HashMap();
108: OpenEjbConfiguration configuration = SystemInstance.get()
109: .getComponent(OpenEjbConfiguration.class);
110: for (AppInfo appInfo : configuration.containerSystem.applications) {
111: for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
112: for (EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
113: deploymentIdIndex.put(bean.ejbDeploymentId, bean);
114: }
115: }
116: }
117: }
118:
119: private void showDeployment(PrintWriter body, String id)
120: throws IOException {
121: EnterpriseBeanInfo bean = getBeanInfo(id);
122:
123: // TODO:0: Inform the user the id is bad
124: if (bean == null)
125: return;
126:
127: // Assuming things are good
128: body = response.getPrintWriter();
129:
130: body.println("<h2>General</h2><br>");
131: body.println("<table width=\"100%\" border=\"1\">");
132: body.println("<tr bgcolor=\"#5A5CB8\">");
133: body
134: .println("<td><font face=\"arial\" color=\"white\">ID</font></td>");
135: body
136: .println("<td><font color=\"white\">" + id
137: + "</font></td>");
138: body.println("</tr>");
139:
140: SystemInstance system = SystemInstance.get();
141: ContainerSystem containerSystem = system
142: .getComponent(ContainerSystem.class);
143:
144: CoreDeploymentInfo di = (CoreDeploymentInfo) containerSystem
145: .getDeploymentInfo(id);
146:
147: printRow("Name", bean.ejbName, body);
148: printRow(
149: "Description",
150: StringUtilities
151: .replaceNullOrBlankStringWithNonBreakingSpace(bean.description),
152: body);
153:
154: String type = null;
155:
156: switch (di.getComponentType()) {
157: case CMP_ENTITY:
158: type = "EntityBean with Container-Managed Persistence";
159: break;
160: case BMP_ENTITY:
161: type = "EntityBean with Bean-Managed Persistence";
162: break;
163: case STATEFUL:
164: type = "Stateful SessionBean";
165: break;
166: case STATELESS:
167: type = "Stateless SessionBean";
168: break;
169: default:
170: type = "Unkown Bean Type";
171: break;
172: }
173:
174: printRow("Bean Type", type, body);
175: printRow("Bean Class", bean.ejbClass, body);
176: printRow("Home Interface", bean.home, body);
177: printRow("Remote Interface", bean.remote, body);
178: printRow("Jar location", bean.codebase, body);
179:
180: //String container = URLEncoder.encode("" + di.getContainer().getContainerID());
181: String container = (String) di.getContainer().getContainerID();
182: printRow("Deployed in", container, body);
183:
184: body.println("</table>");
185:
186: JndiEncInfo enc = bean.jndiEnc;
187:
188: body.println("<h2>JNDI Environment Details</h2><br>");
189: body.println("<table width=\"100%\" border=\"1\">");
190: body.println("<tr bgcolor=\"#5A5CB8\">");
191: body
192: .println("<td><font face=\"arial\" color=\"white\">JNDI Name</font></td>");
193: body
194: .println("<td><font face=\"arial\" color=\"white\">Value</font></td>");
195: body
196: .println("<td><font face=\"arial\" color=\"white\">Type</font></td>");
197: body.println("</tr>");
198:
199: for (EnvEntryInfo info : enc.envEntries) {
200: printRow(info.name, info.value, info.type, body);
201: }
202:
203: for (EjbLocalReferenceInfo info : enc.ejbLocalReferences) {
204: printRow(info.referenceName, info.ejbDeploymentId,
205: info.homeType, body);
206: }
207:
208: for (EjbReferenceInfo info : enc.ejbReferences) {
209: printRow(info.referenceName, info.ejbDeploymentId,
210: info.homeType, body);
211: }
212:
213: for (ResourceReferenceInfo info : enc.resourceRefs) {
214: printRow(info.referenceName, info.resourceID,
215: info.referenceType, body);
216: }
217:
218: for (ResourceEnvReferenceInfo info : enc.resourceEnvRefs) {
219: printRow(info.resourceEnvRefName, info.resourceID,
220: info.resourceEnvRefType, body);
221: }
222:
223: for (PersistenceUnitReferenceInfo info : enc.persistenceUnitRefs) {
224: printRow(info.referenceName, info.persistenceUnitName, "",
225: body);
226: }
227:
228: for (PersistenceContextReferenceInfo info : enc.persistenceContextRefs) {
229: printRow(info.referenceName, info.persistenceUnitName, "",
230: body);
231: }
232:
233: for (ServiceReferenceInfo info : enc.serviceRefs) {
234: printRow(info.referenceName, "<not-supported>", "", body);
235: }
236:
237: body.println("</table>");
238: }
239:
240: private EnterpriseBeanInfo getBeanInfo(String id) {
241: return (EnterpriseBeanInfo) deploymentIdIndex.get(id);
242: }
243:
244: private void printDeployments(PrintWriter out) throws IOException {
245: SystemInstance system = SystemInstance.get();
246: ContainerSystem containerSystem = system
247: .getComponent(ContainerSystem.class);
248:
249: DeploymentInfo[] deployments = containerSystem.deployments();
250: String[] deploymentString = new String[deployments.length];
251: out.println("<table width=\"100%\" border=\"1\">");
252: out.println("<tr bgcolor=\"#5A5CB8\">");
253: out
254: .println("<td><font color=\"white\">Deployment ID</font></td>");
255: out.println("</tr>");
256:
257: if (deployments.length > 0) {
258: for (int i = 0; i < deployments.length; i++) {
259: deploymentString[i] = (String) deployments[i]
260: .getDeploymentID();
261: }
262: Arrays.sort(deploymentString);
263:
264: for (int i = 0; i < deploymentString.length; i++) {
265: if (i % 2 == 1) {
266: out.println("<tr bgcolor=\"#c9c5fe\">");
267: } else {
268: out.println("<tr>");
269: }
270:
271: out.print("<td><span class=\"bodyBlack\">");
272: out.print("<a href=\"DeploymentList?id="
273: + deploymentString[i] + "\">");
274: out.print(deploymentString[i]);
275: out.print("</a>");
276: out.println("</span></td></tr>");
277: }
278: }
279: out.println("</table>");
280: }
281:
282: /** Write the TITLE of the HTML document. This is the part
283: * that goes into the <code><head><title>
284: * </title></head></code> tags
285: *
286: * @param body the output to write to
287: * @exception IOException of an exception is thrown
288: *
289: */
290: public void writeHtmlTitle(PrintWriter body) throws IOException {
291: body.println(HTML_TITLE);
292: }
293:
294: /** Write the title of the page. This is displayed right
295: * above the main block of content.
296: *
297: * @param body the output to write to
298: * @exception IOException if an exception is thrown
299: *
300: */
301: public void writePageTitle(PrintWriter body) throws IOException {
302: body.println("EnterpriseBean Details");
303: }
304:
305: /** Write the sub items for this bean in the left navigation bar of
306: * the page. This should look somthing like the one below:
307: *
308: * <code>
309: * <tr>
310: * <td valign="top" align="left">
311: * <a href="system?show=deployments"><span class="subMenuOff">
312: * Deployments
313: * </span>
314: * </a></td>
315: * </tr>
316: * </code>
317: *
318: * Alternately, the bean can use the method formatSubMenuItem(..) which
319: * will create HTML like the one above
320: *
321: * @param body the output to write to
322: * @exception IOException if an exception is thrown
323: *
324: */
325: public void writeSubMenuItems(PrintWriter body) throws IOException {
326: }
327:
328: }
|