KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > WebAdminBean


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: WebAdminBean.java 2487 2006-02-22 22:05:03Z dblevins $
44  */

45 package org.openejb.webadmin;
46
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.PrintWriter JavaDoc;
50 import java.net.URL JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import javax.ejb.CreateException JavaDoc;
57 import javax.ejb.SessionContext JavaDoc;
58 import javax.naming.Context JavaDoc;
59 import javax.naming.NameClassPair JavaDoc;
60 import javax.naming.NamingEnumeration JavaDoc;
61
62 /** This is the template web admin bean to extend from. It contains all the functionality for the webadministration. To use
63  * this class, simply sub-class it:<br><br>
64  *
65  * <code>
66  * public class MyBean extends WebAdminBean {
67  * ...
68  * }
69  * </code>
70  * <br><br>
71  * and declare the following methods:<br><br>
72  *
73  * <code>
74  * public void ejbCreate() {}<br>
75  * public void preProcess(HttpRequest request, HttpResponse response) throws IOException {}<br>
76  * public void postProcess(HttpRequest request, HttpResponse response) throws IOException {}<br>
77  * public void writeBody(PrintWriter body) throws IOException {}<br>
78  * public void writeHtmlTitle(PrintWriter body) throws IOException {}<br>
79  * public void writePageTitle(PrintWriter body) throws IOException {}<br>
80  * public void writeSubMenuItems(PrintWriter body) throws IOException {}<br>
81  * </code>
82  *
83  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
84  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
85  */

86 public abstract class WebAdminBean implements HttpBean {
87     /** used for the session context
88      */

89     protected SessionContext JavaDoc ejbContext;
90     /** the substitue
91      */

92     public static final int SUBSTITUTE = 26;
93     /** the navigation sections
94      */

95     public static HashMap JavaDoc sections;
96     /** the menu section
97      */

98     protected String JavaDoc section = "";
99     /** the HTTP request
100      */

101     protected HttpRequest request;
102     /** the HTTP response
103      */

104     protected HttpResponse response;
105     /** the standard title */
106     public static final String JavaDoc HTML_TITLE = "OpenEJB Web Administration Console";
107
108
109     /** the main method of this bean, it takes care of the processing
110      * @param request the http request
111      * @param response the http response
112      * @throws IOException if an exception is thrown
113      */

114     public void onMessage(HttpRequest request, HttpResponse response) throws IOException JavaDoc{
115         this.request = request;
116         this.response = response;
117
118         preProcess(request, response);
119         
120         // Assuming things are good
121
java.io.PrintWriter JavaDoc body = response.getPrintWriter();
122         InputStream JavaDoc template = getTemplate();
123
124         // Write till PAGETITLE
125
writeTemplate(body, template);
126         writeHtmlTitle(body);
127         
128         // Write till TOP_NAV_BAR
129
writeTemplate(body, template);
130         writeTopNavBar(body);
131         
132         // Write till LEFT_NAV_BAR
133
writeTemplate(body, template);
134         writeLeftNavBar(body);
135
136         // Write till TITLE
137
writeTemplate(body, template);
138         writePageTitle(body);
139         
140         // Write till BODY
141
writeTemplate(body, template);
142         writeBody(body);
143
144         // Write till FOOTER
145
writeTemplate(body, template);
146         writeFooter(body);
147         
148         // Write the rest
149
writeTemplate(body, template);
150         postProcess(request, response);
151     }
152
153     /** called before any content is written to the browser
154      * @param request the http request
155      * @param response the http response
156      * @throws IOException if an exception is thrown
157      */

158     public abstract void preProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc;
159     /** called after all content is written to the browser
160      * @param request the http request
161      * @param response the http response
162      * @throws IOException if an exception is thrown
163      */

164     public abstract void postProcess(HttpRequest request, HttpResponse response) throws IOException JavaDoc;
165     
166     /** Write the TITLE of the HTML document. This is the part
167      * that goes into the <code>&lt;head&gt;&lt;title&gt;
168      * &lt;/title&gt;&lt;/head&gt;</code> tags
169      *
170      * @param body the output to write to
171      * @exception IOException of an exception is thrown
172      *
173      */

174     public abstract void writeHtmlTitle(PrintWriter JavaDoc body) throws IOException JavaDoc;
175
176     /** Write the title of the page. This is displayed right
177      * above the main block of content.
178      *
179      * @param body the output to write to
180      * @exception IOException if an exception is thrown
181      */

182     public abstract void writePageTitle(PrintWriter JavaDoc body) throws IOException JavaDoc;
183
184     /** Write the top navigation bar of the page. This should look somthing
185      * like the one below:
186      *
187      * <code>
188      * &lt;a HREF="system?show=server"&gt;
189      * &lt;span class="menuTopOff"&gt;Remote Server&lt;/span&gt;
190      * &lt;/a&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
191      * &lt;a HREF="system?show=containers"&gt;
192      * &lt;span class="menuTopOff"&gt;Containers&lt;/span&gt;
193      * &lt;/a&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
194      * &lt;a HREF="system?show=deployments"&gt;
195      * &lt;span class="menuTopOff"&gt;Deployments&lt;/span&gt;
196      * &lt;/a&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
197      * &lt;a HREF="system?show=logs"&gt;
198      * &lt;span class="menuTopOff"&gt;Logs&lt;/span&gt;
199      * &lt;/a&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
200      * </code>
201      *
202      * @param body the output to write to
203      * @exception IOException if an exception is thrown
204      */

205     public void writeTopNavBar(PrintWriter JavaDoc body) throws IOException JavaDoc{
206 // for (int i=0; i < navSections.length; i+=2){
207
// body.print("<a HREF=\"");
208
// body.print(navSections[i]);
209
// body.print("\" class=\"menuTopOff\">");
210
// body.print(navSections[i+1]);
211
// if(i == (navSections.length-2))
212
// body.print("</a>");
213
// else
214
// body.print("</a> | ");
215
// }
216
}
217     
218      /** Write the left navigation bar of the page. This should look somthing
219      * like the one below:
220      *
221      * <code>
222      * &lt;tr&gt;
223      * &lt;td valign="top" align="left"&gt;
224      * &lt;span class="subMenuOn"&gt;
225      * Admin
226      * &lt;/span&gt;
227      * &lt;/td&gt;
228      * &lt;/tr&gt;
229      * &lt;tr&gt;
230      * &lt;td valign="top" align="left"&gt;
231      * &lt;a HREF="system?show=status"&gt;&lt;span class="subMenuOff"&gt;
232      * &nbsp;&nbsp;&nbsp;Status
233      * &lt;/span&gt;
234      * &lt;/a&gt;&lt;/td&gt;
235      * &lt;/tr&gt;
236      * &lt;tr&gt;
237      * &lt;&lt;td valign="top" align="left"&gt;
238      * &lt;a HREF="system?show=deployments"&gt;&lt;span class="subMenuOff"&gt;
239      * &nbsp;&nbsp;&nbsp;Deployments
240      * &lt;/span&gt;
241      * &lt;/a&gt;&lt;/td&gt;
242      * &lt;/tr&gt;
243      * </code>
244      *
245       * @param body the output to write to
246       * @exception IOException if an exception is thrown
247      */

248     public void writeLeftNavBar(PrintWriter JavaDoc body) throws IOException JavaDoc{
249         Object JavaDoc[] entries = sections.entrySet().toArray();
250         
251         for (int i = 0; i < entries.length; i++) {
252             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries[i];
253             String JavaDoc section = (String JavaDoc)entry.getKey();
254             String JavaDoc[] subSections = (String JavaDoc[])entry.getValue();
255             
256             body.println("<tr><td valign=\"top\" align=\"left\">");
257             body.print("<span class=\"subMenuOn\">");
258             body.print(section);
259             body.print("</td></tr>");
260             
261             for (int j=0; j < subSections.length; j+=2){
262                 String JavaDoc name = subSections[j];
263                 String JavaDoc url = subSections[j+1];
264                 
265                 body.print("<tr>");
266                 body.print("<td valign=\"top\" align=\"left\">");
267                 body.print("<a HREF=\"/");
268                 body.print(section);
269                 body.print('/');
270                 body.print(url);
271                 body.print("\" class=\"subMenuOff\">");
272                 body.print("&nbsp;&nbsp;&nbsp;");
273                 body.print(name);
274                 body.print("</a></td></tr>");
275             }
276         }
277     }
278     
279
280     /** formats a sub menu item for the left navigation
281      * @param itemName the name for display
282      * @param url the url to link
283      * @return the html that is formatted
284      */

285     public String JavaDoc formatSubMenuItem(String JavaDoc itemName, String JavaDoc url){
286         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
287
288         return buff.toString();
289     }
290
291     /** writes the main body content to the broswer. This content is inside a <code>&lt;p&gt;</code> block
292      *
293      *
294      * @param body the output to write to
295      * @exception IOException if an exception is thrown
296      */

297     public abstract void writeBody(PrintWriter JavaDoc body) throws IOException JavaDoc;
298
299     /** Write the footer
300      *
301      * @param body the output to write to
302      * @exception IOException if an exception is thrown
303      */

304     public void writeFooter(PrintWriter JavaDoc body) throws IOException JavaDoc{
305         body.print(footer);
306     }
307     
308     /** the footer
309      */

310     protected static String JavaDoc footer = getFooter();
311
312     /** gets a footer for the document
313      * @return the footer string
314      */

315     public static String JavaDoc getFooter(){
316         StringBuffer JavaDoc out = new StringBuffer JavaDoc(100);
317         try {
318             Properties JavaDoc openejbProps = new Properties JavaDoc();
319             openejbProps.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
320             out.append( "<a HREF=\""+openejbProps.get("url")+"\">OpenEJB</a> ");
321             out.append( openejbProps.get( "version" ) +"<br>");
322             out.append( "build: "+openejbProps.get( "date" )+"-"+openejbProps.get( "time" ));
323         } catch (java.io.IOException JavaDoc e) {
324         }
325
326         return out.toString();
327     }
328
329     /** writes a template from the input stream to the output stream
330      * @param out the output to write to
331      * @param template the template to read
332      * @throws IOException if an exception is thrown
333      */

334     public void writeTemplate(PrintWriter JavaDoc out, InputStream JavaDoc template) throws IOException JavaDoc{
335         int b = template.read();
336         //System.out.println("[] read");
337
while (b != -1 && b != SUBSTITUTE) {
338             out.write( b );
339             b = template.read();
340         }
341         //System.out.println("[] done reading");
342
}
343
344     /** gets an html template which is the content of the pages written to the browser
345      * @throws IOException if an exception is thrown
346      * @return the template
347      */

348     public InputStream JavaDoc getTemplate() throws IOException JavaDoc{
349         //System.out.println("[] get template");
350
return new URL JavaDoc( "resource:/htdocs/template.html" ).openConnection().getInputStream();
351     }
352
353     /** initalizes the left and top menu navigation
354      */

355     public HashMap JavaDoc initNavSections(){
356         HashMap JavaDoc sections = new HashMap JavaDoc();
357         try{
358             Context JavaDoc ctx = org.openejb.OpenEJB.getJNDIContext();
359             ctx = (Context JavaDoc) ctx.lookup("openejb/ejb");
360             NamingEnumeration JavaDoc enumeration = ctx.list("");
361             //System.out.println("\n\nENUM "+enumeration);
362

363             if ( enumeration == null){
364                 return sections;
365             }
366
367             while (enumeration.hasMore()) {
368                 NameClassPair JavaDoc entry = (NameClassPair JavaDoc)enumeration.next();
369                 //System.out.println("ITEM NAME "+entry.getName());
370
//System.out.println("ITEM CLASS "+entry.getClassName());
371
if ( !entry.getClassName().equals("org.openejb.core.ivm.naming.IvmContext") ) {
372                     continue;
373                 }
374                 
375                 Context JavaDoc subCtx = (Context JavaDoc) ctx.lookup(entry.getName());
376                 String JavaDoc[] subSections = getSubsections(subCtx);
377                 if (subSections.length > 0){
378                     sections.put(entry.getName(), subSections );
379                 }
380             }
381         } catch (Exception JavaDoc e){
382             e.printStackTrace();
383         }
384         return sections;
385     }
386
387     private String JavaDoc[] getSubsections(Context JavaDoc ctx){
388         ArrayList JavaDoc sections = new ArrayList JavaDoc();
389         try{
390             NamingEnumeration JavaDoc enumeration = ctx.list("");
391             
392             if ( enumeration == null){
393                 return new String JavaDoc[0];
394             }
395
396             while (enumeration.hasMore()) {
397                 NameClassPair JavaDoc entry = (NameClassPair JavaDoc)enumeration.next();
398                 //System.out.println("ITEM NAME "+entry.getName());
399
//System.out.println("ITEM CLASS "+entry.getClassName());
400
if ( !entry.getClassName().equals("org.openejb.core.stateless.EncReference") ) {
401                     continue;
402                 }
403
404                 if ( entry.getName().startsWith("Default") ) {
405                     continue;
406                 }
407
408                 Object JavaDoc obj = ctx.lookup(entry.getName());
409                 if (obj instanceof HttpHome){
410                     String JavaDoc beanName = entry.getName();
411                     sections.add(beanName);
412                     sections.add(beanName);
413                 }
414             }
415         } catch (Exception JavaDoc e){
416             e.printStackTrace();
417         }
418         return (String JavaDoc[]) sections.toArray(new String JavaDoc[0]);
419     }
420     /** prints a table row similar to this
421      *
422      * &lt;tr&gt;
423      * &lt;td&gt;some info&lt;/td&gt;
424      * &lt;td&gt;some more info&lt;/td&gt;
425      * &lt;/tr&gt;
426      * @param col1 the first column
427      * @param col2 the second column
428      * @param out the output to write to
429      * @throws IOException if an exception is thrown
430      */

431     protected void printRow(String JavaDoc col1, String JavaDoc col2, PrintWriter JavaDoc out) throws IOException JavaDoc{
432         out.println("<tr>" );
433         out.print("<td class=\"bodyBlack\">");
434         out.print(col1);
435         out.println("</td>");
436         out.print("<td class=\"bodyBlack\">");
437         out.print(col2);
438         out.println("</td>");
439         out.println("</tr>");
440     }
441
442     /** prints a table row similar to this
443      *
444      * &lt;tr&gt;
445      * &lt;td&gt;some info&lt;/td&gt;
446      * &lt;td&gt;some more info&lt;/td&gt;
447      * &lt;td&gt;yet some more info&lt;/td&gt;
448      * &lt;/tr&gt;
449      * @param col1 the first column
450      * @param col2 the second column
451      * @param col3 the third column
452      * @param out the output to write to
453      * @throws IOException if an exception is thrown
454      */

455     protected void printRow(String JavaDoc col1, String JavaDoc col2, String JavaDoc col3, PrintWriter JavaDoc out) throws IOException JavaDoc{
456         out.println("<tr>");
457         out.print("<td class=\"bodyBlack\">");
458         out.print(col1);
459         out.println("</td>");
460         out.print("<td class=\"bodyBlack\">");
461         out.print(col2);
462         out.println("</td>");
463         out.print("<td class=\"bodyBlack\">");
464         out.print(col3);
465         out.println("</td>");
466         out.println("</tr>");
467     }
468
469     /*---------------------------------------------------------------*/
470     /* EJB API Callbacks */
471     /*---------------------------------------------------------------*/
472     /** called with the bean is created
473      * @throws CreateException if the bean cannot be created
474      */

475     public void ejbCreate() throws CreateException JavaDoc {}
476     
477     /** called on a stateful sessionbean after the bean is
478      * deserialized from storage and put back into use.
479      */

480     public void ejbActivate() {}
481
482     /** called on a stateful sessionbean before the bean is
483      * removed from memory and serialized to a temporary store.
484      * This method is never called on a stateless sessionbean
485      */

486     public void ejbPassivate() {}
487
488     /** called when the bean is about to be garbage collected
489      */

490     public void ejbRemove() {}
491
492     /** sets the session context
493      * @param sessionContext the session context
494      */

495     public void setSessionContext(SessionContext JavaDoc sessionContext) {
496         ejbContext = sessionContext;
497         if (sections == null) {
498             sections = initNavSections();
499         }
500     }
501 }
502
Popular Tags