001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: J2eemanagementBaseServlet.java 8063 2006-02-27 12:59:05Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.j2eemanagement.servlets;
025:
026: import java.io.PrintWriter;
027:
028: import javax.management.j2ee.Management;
029: import javax.management.j2ee.ManagementHome;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.rmi.PortableRemoteObject;
033: import javax.servlet.http.HttpServlet;
034: import javax.servlet.http.HttpServletRequest;
035:
036: import org.objectweb.jonas.mejb.DomainManagementHome;
037:
038: /**
039: *
040: * @author Adriana Danes
041: *
042: * Base classe containing common code for the j2eeManagement servlets
043: */
044: public class J2eemanagementBaseServlet extends HttpServlet {
045: /** Printing constant*/
046: static final String APP_TITLE = "J2EE Management sample";
047: /**
048: * Standard J2EEManagement Bean
049: */
050: private Management mgmt = null;
051: /**
052: * Naming context
053: */
054: private Context initialContext = null;
055:
056: /**
057: * Header.
058: * @param pOut Printer
059: * @param pTitle Title to display
060: * @param pSubTitle Subtitle to display or null if not
061: */
062: protected void printHeader(PrintWriter pOut, String pTitle,
063: String pSubTitle) {
064:
065: pOut.println("<html>");
066: pOut.println("<head>");
067: pOut.println("<title>" + pTitle);
068: if (pSubTitle != null) {
069: pOut.println(" - " + pSubTitle);
070: }
071: pOut.println("</title>");
072: pOut.println("</head>");
073: pOut
074: .println("<body style=\"background : white; color : black;\">");
075: printHeaderTitle(pOut, pTitle, pSubTitle);
076: }
077:
078: /**
079: * Header title.
080: * @param pOut Printer
081: * @param pTitle Title to display
082: * @param pSubTitle Subtitle to display or null if not
083: */
084: protected void printHeaderTitle(PrintWriter pOut, String pTitle,
085: String pSubTitle) {
086:
087: pOut.println("<table width=\"100%\" border=\"0\">");
088: pOut.println("<tr>");
089: pOut
090: .println("<td rowspan=\"2\"><img src=\"img/logo_jonas.jpg\" alt=\"JOnAS Logo\"></td>");
091: pOut.println("<td align=\"center\"><h1>" + pTitle
092: + "</h1></td>");
093: pOut.println("</tr>");
094: pOut.println("<tr>");
095: if (pSubTitle != null) {
096: pOut.println("<td align=\"center\"><h1>" + pSubTitle
097: + "</h1></td>");
098: }
099: pOut.println("</tr>");
100: pOut.println("</table>");
101: }
102:
103: /**
104: * Footer navigation.
105: * @param pRequest Http request
106: * @param pOut Printer
107: */
108: protected void printNavigationFooter(HttpServletRequest pRequest,
109: PrintWriter pOut) {
110:
111: // index
112: String sViewIndex = pRequest.getContextPath();
113:
114: // Display
115: pOut
116: .print("<table border=\"1\" align=\"center\" cellspacing=\"0\" cellpadding=\"5\">");
117: pOut.print("<tr>");
118: pOut.print("<td align=\"center\"> <a href=\"" + sViewIndex
119: + "\">Index</a> </td>");
120: pOut.println("</tr></table>");
121: // Footer
122: printFooter(pOut);
123: }
124:
125: /**
126: * Footer.
127: * @param pOut Printer
128: */
129: protected void printFooter(PrintWriter pOut) {
130: pOut.println("</body>");
131: pOut.println("</html>");
132: }
133:
134: /**
135: * @param pRequest Client request
136: * @param pOut Writer for output
137: */
138: protected void doViewInit(HttpServletRequest pRequest,
139: PrintWriter pOut) {
140: // -----------------------------
141: // Get initial context
142: // -----------------------------
143: pOut.println("<h2>Initial context for domain management</h2>");
144: pOut.println("<ul>");
145: try {
146: initialContext = new InitialContext();
147: pOut.println("<li>Initial context OK</li>");
148: } catch (Exception e) {
149: pOut.print("<li>Cannot get initial context for JNDI: ");
150: pOut.println(e + "</li>");
151: pOut.println("</ul>");
152: printNavigationFooter(pRequest, pOut);
153: return;
154: }
155: pOut.println("</ul><br>");
156:
157: // -----------------------------
158: // Access to the MEJB
159: // -----------------------------
160: pOut.println("<h2>Create MEJB</h2>");
161: pOut.println("<ul>");
162:
163: // Connecting to the MEJB home through JNDI
164: ManagementHome mgmtHome = null;
165: DomainManagementHome domainMgmtHome = null;
166: try {
167: mgmtHome = (ManagementHome) PortableRemoteObject.narrow(
168: initialContext
169: .lookup("java:comp/env/ejb/mgmt/MEJB"),
170: ManagementHome.class);
171: } catch (Exception e) {
172: pOut
173: .println("<li>Cannot lookup java:comp/env/ejb/mgmt/MEJB: "
174: + e + "</li>");
175: pOut.println("</ul>");
176: printNavigationFooter(pRequest, pOut);
177: return;
178: }
179:
180: // Management bean creation
181: try {
182: mgmt = mgmtHome.create();
183: pOut.println("<li>MEJB created</li>");
184: } catch (Exception e) {
185: pOut.println("<li>Cannot create MEJB: " + e + "</li>");
186: pOut.println("</ul>");
187: printNavigationFooter(pRequest, pOut);
188: return;
189: }
190:
191: pOut.println("</ul><br/>");
192: }
193:
194: /**
195: * Simply View error.
196: * @param pError Message error
197: * @param pRequest Http request
198: * @param pOut Printer
199: */
200: protected void doViewError(String pError,
201: HttpServletRequest pRequest, PrintWriter pOut) {
202:
203: // Header
204: printHeader(pOut, APP_TITLE, "Error");
205: // Error message
206: pOut.println("<h2>" + pError + "</h2>");
207: // Return
208: pOut.println("<a href=\"" + pRequest.getContextPath()
209: + "\">Return</a>");
210: // Footer
211: printFooter(pOut);
212: }
213:
214: /**
215: * @return The management bean
216: */
217: public Management getMgmt() {
218: return mgmt;
219: }
220:
221: /**
222: * @return The initial naming context
223: */
224: public Context getInitialContext() {
225: return initialContext;
226: }
227: }
|