KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webjmx > tags > FormMBeanTag


1 /*
2  * Copyright (C) WebJMX.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the WebJMX License version 2.0.
6  * See the terms of the WebJMX License in the documentation provided with this software.
7  */

8 /*
9  * FormMBean.java
10  *
11  * Created on December 9, 2001, 3:44 PM
12  */

13
14 package org.webjmx.tags;
15
16 import java.io.*;
17 import java.util.*;
18 import javax.management.*;
19 import javax.servlet.http.*;
20 import javax.servlet.jsp.*;
21 import javax.servlet.jsp.tagext.*;
22
23 /** This tag writes an HTML form to the output stream. The submitted form is
24  * meant to be processed by the JMXAction Servlet. The tag generates the enclosing
25  * "form" tags, a "submit" button and several hidden input tags.
26  *
27  * @author John Aronson
28  */

29 public class FormMBeanTag extends TagSupport
30     implements JMXTaglibConstants, ILocatorTag
31 {
32     
33     /** Holds value of property action. Action should contain a URL which maps to a servlet of class JMXAction. */
34     private String JavaDoc action;
35     
36     /** Holds value of property name. Name of the HTML form to be generated. */
37     private String JavaDoc name;
38     
39     /** Holds value of property mbean. ObjectName of the MBean to be created/modified. */
40     private String JavaDoc mbean;
41     
42     /** Holds value of property method. HTML method type of this form. */
43     private String JavaDoc method = "POST";
44     
45     /** Holds value of property forward. URL of the page to load if the form processing succeeds. */
46     private String JavaDoc forward;
47     
48     /** Holds value of property error. URL of the page to be loaded if the form processing fails. */
49     private String JavaDoc error;
50     
51     /** Holds value of property invoke. MBean operation to be invoked. */
52     private String JavaDoc invoke;
53     
54     /** Holds value of property classname. Fully qualified Java classname to be passed to the createMBean method. */
55     private String JavaDoc classname;
56     
57     /** Holds value of property unregister. Set to 'true' if the named MBean should be unregistered from it's server. */
58     private String JavaDoc unregister;
59     
60     /** Holds value of property label. Override the label string of the Submit button for the form. */
61     private String JavaDoc label;
62     
63     /** Holds value of property serverLocator. Should be the locator string of an MBeanServer*/
64     private String JavaDoc locator;
65     
66     /** Holds value of property operationInfo. Name of an attribute of type MBeanOperationInfo. This is the MBean operation that will be invoked.*/
67     private String JavaDoc operationInfo;
68     
69     /** Holds value of property mbeanInfo. Name of an attribute of type MBeanInfo. This is the MBean that will be created.*/
70     private String JavaDoc mbeanInfo;
71     
72     /** Holds value of property onsubmit. Holds a script fragment that will be passed through to the HTML form tag onsubmit attribute*/
73     private String JavaDoc onsubmit;
74     
75     /** Process the start tag for this instance.
76      * @throws JspException
77      */

78     public int doStartTag()
79         throws JspException
80     {
81         HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
82         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<FORM");
83         results.append(" ACTION=\"");
84         if(action == null)
85             action = pageContext.getServletContext().getInitParameter(TAGLIB_ACTION_URL).toString();
86
87         results.append(response.encodeURL(action));
88         results.append("\"");
89         
90         if(operationInfo != null)
91         {
92             Object JavaDoc o = pageContext.getAttribute(operationInfo);
93             if(o == null || !(o instanceof MBeanOperationInfo))
94                 throw new JspException("tag attribute operationInfo must point to page attribute of type MBeanOperationInfo, found: " +o);
95             invoke = ((MBeanOperationInfo)o).getName();
96             if(label == null)
97                 label = ((MBeanOperationInfo)o).getName();
98         }else if(mbeanInfo != null)
99         {
100             Object JavaDoc o = pageContext.getAttribute(mbeanInfo);
101             if(o == null || !(o instanceof MBeanInfo))
102                 throw new JspException("tag attribute mbeanInfo must point to page attribute of type MBeanInfo, found: " +o);
103             classname = ((MBeanInfo)o).getClassName();
104             if(label == null)
105                 label = "Create";
106         }
107         
108         if(name != null)
109         {
110             results.append(" NAME=\"");
111             results.append(name);
112             results.append("\"");
113         }
114         if(method != null)
115         {
116             results.append(" METHOD=\"");
117             results.append(method);
118             results.append("\"");
119         }
120         if(onsubmit != null)
121         {
122             results.append(" ONSUBMIT=\"");
123             results.append(onsubmit);
124             results.append("\"");
125         }
126         if(invoke != null)
127         {
128             results.append("\">\n<INPUT TYPE=HIDDEN NAME=\"");
129             results.append(TAGLIB_INVOKE);
130             results.append("\" VALUE=\"");
131             results.append(invoke);
132         }
133         if(classname != null)
134         {
135             results.append("\">\n<INPUT TYPE=HIDDEN NAME=\"");
136             results.append(TAGLIB_CLASSNAME);
137             results.append("\" VALUE=\"");
138             results.append(classname);
139         }
140         if(/*isUnregister()*/ getUnregister() != null)
141         {
142             results.append("\">\n<INPUT TYPE=HIDDEN NAME=\"");
143             results.append(TAGLIB_UNREGISTER);
144             results.append("\" VALUE=\"true");
145         }
146
147         results.append("\">\n");
148         try{ pageContext.getOut().write(results.toString()); }
149         catch(IOException ex) { ex.printStackTrace(); }
150         
151         //put the forward URL in the session
152
if(Boolean.getBoolean(DEBUG_PROP)) pageContext.getServletContext().log("inserting forward path: " +forward);
153         if(forward == null)
154         {
155             HttpServletRequest httpReq = (HttpServletRequest)pageContext.getRequest();
156             forward = httpReq.getRequestURI().substring(httpReq.getContextPath().length() +1);
157             if(httpReq.getQueryString() != null)
158                 forward += '?' +httpReq.getQueryString();
159             if(Boolean.getBoolean(DEBUG_PROP)) pageContext.getServletContext().log("new forward URL: " +forward);
160         }
161         pageContext.getSession().setAttribute(TAGLIB_FORWARD, response.encodeURL(forward));
162         if(error != null)
163             pageContext.getSession().setAttribute(TAGLIB_ERROR, response.encodeURL(error));
164         
165     return (EVAL_BODY_INCLUDE);
166     }
167
168     /** Process the end tag for this instance.
169      * @throws JspException
170      */

171     public int doEndTag()
172         throws JspException
173     {
174         try
175         {
176             Writer out = pageContext.getOut();
177             if(locator != null)
178             {
179                 out.write("<INPUT TYPE=HIDDEN NAME=\"");
180                 out.write(TAGLIB_SERVER);
181                 out.write("\" VALUE=\"");
182                 if(locator != null)
183                     out.write(locator);
184                 else
185                     out.write(';');
186                 out.write("\"/>\n");
187             }
188             if(mbean != null)
189             {
190                 out.write("<INPUT TYPE=HIDDEN NAME=\"");
191                 out.write(TAGLIB_NAME);
192                 out.write("\" VALUE=\"");
193                 out.write(mbean);
194                 out.write("\"/>\n");
195             }
196             
197             /*if(onsubmit != null)
198             {
199                 out.write("<INPUT TYPE=BUTTON ONCLICK=\"");
200                 out.write(onsubmit);
201                 out.write("\" VALUE=\"");
202             }else*/

203                 out.write("<INPUT TYPE=\"SUBMIT\" VALUE=\"");
204             if(label != null)
205                 out.write(label);
206             else if(invoke != null)
207                 out.write(invoke);
208             else if(classname != null)
209                 out.write("Create");
210             else if(/*isUnregister()*/ getUnregister() != null)
211                 out.write("Unregister");
212             else
213                 out.write("Submit");
214             out.write("\"/></FORM>");
215         }catch(IOException ex) { ex.printStackTrace(); }
216     return (EVAL_PAGE);
217     }
218     
219     /** Getter for property action.
220      * @return Value of property action.
221      */

222     public String JavaDoc getAction()
223     {
224         return action;
225     }
226     
227     /** Setter for property action.
228      * @param action New value of property action.
229      */

230     public void setAction(String JavaDoc action)
231     {
232         this.action = action;
233     }
234     
235     /** Getter for property name.
236      * @return Value of property name.
237      */

238     public String JavaDoc getName()
239     {
240         return name;
241     }
242     
243     /** Setter for property name.
244      * @param name New value of property name.
245      */

246     public void setName(String JavaDoc name)
247     {
248         this.name = name;
249     }
250     
251     /** Getter for property mbean.
252      * @return Value of property mbean.
253      */

254     public String JavaDoc getMbean()
255     {
256         return mbean;
257     }
258     
259     /** Setter for property mbean.
260      * @param mbean New value of property mbean.
261      */

262     public void setMbean(String JavaDoc mbean)
263     {
264         this.mbean = mbean;
265     }
266     
267     /** Getter for property method.
268      * @return Value of property method.
269      */

270     public String JavaDoc getMethod()
271     {
272         return method;
273     }
274     
275     /** Setter for property method.
276      * @param method New value of property method.
277      */

278     public void setMethod(String JavaDoc method)
279     {
280         this.method = method;
281     }
282     
283     /** Getter for property forward.
284      * @return Value of property forward.
285      */

286     public String JavaDoc getForward()
287     {
288         return forward;
289     }
290     
291     /** Setter for property forward.
292      * @param forward New value of property forward.
293      */

294     public void setForward(String JavaDoc forward)
295     {
296         this.forward = forward;
297     }
298     
299     /** Getter for property error.
300      * @return Value of property error.
301      */

302     public String JavaDoc getError()
303     {
304         return error;
305     }
306     
307     /** Setter for property error.
308      * @param error New value of property error.
309      */

310     public void setError(String JavaDoc error)
311     {
312         this.error = error;
313     }
314     
315     /** Getter for property invoke.
316      * @return Value of property invoke.
317      */

318     public String JavaDoc getInvoke()
319     {
320         return invoke;
321     }
322     
323     /** Setter for property invoke.
324      * @param invoke New value of property invoke.
325      */

326     public void setInvoke(String JavaDoc invoke)
327     {
328         this.invoke = invoke;
329     }
330     
331     /** Getter for property classname.
332      * @return Value of property classname.
333      */

334     public String JavaDoc getClassname()
335     {
336         return classname;
337     }
338     
339     /** Setter for property classname.
340      * @param classname New value of property classname.
341      */

342     public void setClassname(String JavaDoc classname)
343     {
344         this.classname = classname;
345     }
346     
347     /** Getter for property unregister.
348      * @return Value of property unregister.
349      */

350     public String JavaDoc getUnregister()
351     {
352         return unregister;
353     }
354     
355     /** Boolean Getter for property unregister.
356      * @return Value of property unregister.
357      */

358     /*public boolean isUnregister()
359     {
360         if(unregister == null)
361             return false;
362         return (new Boolean(unregister)).booleanValue();
363     }*/

364     
365     /** Setter for property unregister.
366      * @param unregister New value of property unregister.
367      */

368     public void setUnregister(String JavaDoc unregister)
369     {
370         this.unregister = unregister;
371     }
372     
373     /** Getter for property label.
374      * @return Value of property label.
375      */

376     public String JavaDoc getLabel()
377     {
378         return label;
379     }
380     
381     /** Setter for property label.
382      * @param label New value of property label.
383      */

384     public void setLabel(String JavaDoc label)
385     {
386         this.label = label;
387     }
388     
389     /** Called on a Tag handler to release state
390      *
391      */

392     public void release()
393     {
394         action = classname = error = forward = invoke = label = mbean = method = name = locator = unregister = null;
395     }
396     
397     /** Getter for property locator.
398      * @return Value of property locator.
399      */

400     public String JavaDoc getLocator()
401     {
402         return locator;
403     }
404     
405     /** Setter for property serverLocator.
406      * @param serverLocator New value of property serverLocator.
407      */

408     public void setLocator(String JavaDoc locator)
409     {
410         this.locator = locator;
411     }
412     
413     /** Getter for property operationInfo.
414      * @return Value of property operationInfo.
415      */

416     public String JavaDoc getOperationInfo()
417     {
418         return operationInfo;
419     }
420     
421     /** Setter for property operationInfo.
422      * @param operationInfo New value of property operationInfo.
423      */

424     public void setOperationInfo(String JavaDoc operationInfo)
425     {
426         this.operationInfo = operationInfo;
427     }
428     
429     /** Getter for property mbeanInfo.
430      * @return Value of property mbeanInfo.
431      */

432     public String JavaDoc getMbeanInfo()
433     {
434         return mbeanInfo;
435     }
436     
437     /** Setter for property mbeanInfo.
438      * @param mbeanInfo New value of property mbeanInfo.
439      */

440     public void setMbeanInfo(String JavaDoc mbeanInfo)
441     {
442         this.mbeanInfo = mbeanInfo;
443     }
444     
445     /** Getter for property onsubmit.
446      * @return Value of property onsubmit.
447      */

448     public String JavaDoc getOnsubmit() {
449         return onsubmit;
450     }
451     
452     /** Setter for property onsubmit.
453      * @param onsubmit New value of property onsubmit.
454      */

455     public void setOnsubmit(String JavaDoc onsubmit) {
456         this.onsubmit = onsubmit;
457     }
458     
459 }
460
Popular Tags