KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > browser > web > action > CustomViewAction


1 /*
2
3  * EJTools, the Enterprise Java Tools
4
5  *
6
7  * Distributable under LGPL license.
8
9  * See terms of license at www.gnu.org.
10
11  */

12
13 package org.ejtools.jmx.browser.web.action;
14
15
16
17 import java.io.IOException JavaDoc;
18
19 import java.util.Iterator JavaDoc;
20
21 import java.util.Locale JavaDoc;
22
23
24
25 import javax.management.MBeanAttributeInfo JavaDoc;
26
27 import javax.management.MBeanInfo JavaDoc;
28
29 import javax.management.MBeanOperationInfo JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32
33 import javax.servlet.ServletContext JavaDoc;
34
35 import javax.servlet.ServletException JavaDoc;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41
42
43 import org.apache.log4j.Logger;
44
45 import org.apache.struts.action.Action;
46
47 import org.apache.struts.action.ActionError;
48
49 import org.apache.struts.action.ActionErrors;
50
51 import org.apache.struts.action.ActionForm;
52
53 import org.apache.struts.action.ActionForward;
54
55 import org.apache.struts.action.ActionMapping;
56
57 import org.apache.struts.util.MessageResources;
58
59 import org.ejtools.jmx.browser.mbean.View;
60
61 import org.ejtools.jmx.browser.mbean.ViewLine;
62
63 import org.ejtools.jmx.browser.model.Resource;
64
65 import org.ejtools.jmx.browser.web.Constants;
66
67 import org.ejtools.jmx.browser.web.JMXContainer;
68
69
70
71 /**
72
73  * Description of the Class
74
75  *
76
77  * @author letiemble
78
79  * @created 12 novembre 2001
80
81  * @version $Revision: 1.6 $
82
83  * @todo Javadoc to complete
84
85  */

86
87 public class CustomViewAction extends Action
88
89 {
90
91    /** Description of the Field */
92
93    private static Logger logger = Logger.getLogger(CustomViewAction.class);
94
95
96
97
98
99    /** Constructor for the CustomViewAction object */
100
101    public CustomViewAction() { }
102
103
104
105
106
107    /**
108
109     * Description of the Method
110
111     *
112
113     * @param mapping Description of the Parameter
114
115     * @param form Description of the Parameter
116
117     * @param request Description of the Parameter
118
119     * @param response Description of the Parameter
120
121     * @return Description of the Return Value
122
123     * @exception IOException Description of the Exception
124
125     * @exception ServletException Description of the Exception
126
127     */

128
129    public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
130
131       throws IOException JavaDoc, ServletException JavaDoc
132
133    {
134
135       JMXContainer tree = null;
136
137
138
139       // Extract attributes we will need
140

141       Locale JavaDoc locale = getLocale(request);
142
143       MessageResources messages = getResources();
144
145
146
147       // Validate the request parameters specified by the user
148

149       ActionErrors errors = new ActionErrors();
150
151
152
153       logger.debug("Connecting to JMX Server");
154
155
156
157       ServletContext JavaDoc context = this.getServlet().getServletContext();
158
159       tree = (JMXContainer) context.getAttribute(Constants.TREE);
160
161
162
163       if (tree != null)
164
165       {
166
167          logger.debug("Tree root found => " + tree);
168
169
170
171          try
172
173          {
174
175             // TODO : put in constants
176

177             int number = Integer.parseInt(request.getParameter("view"));
178
179
180
181             // To change
182

183             View view = tree.getView(number);
184
185             logger.debug("Get View " + view);
186
187
188
189             // TODO : put in constants
190

191             context.setAttribute(Constants.CUSTOM_VIEW, view);
192
193             context.setAttribute(Constants.CUSTOM_VIEW_INDEX, new String JavaDoc("" + number));
194
195
196
197             if (view != null)
198
199             {
200
201                Iterator JavaDoc iterator = null;
202
203                View resultView = new View();
204
205
206
207                // Process Attributes
208

209                iterator = view.getAttributeLines().iterator();
210
211                while (iterator.hasNext())
212
213                {
214
215                   ViewLine query = (ViewLine) iterator.next();
216
217
218
219                   String JavaDoc name = query.getName();
220
221                   Iterator JavaDoc it = tree.query(query.getObjectName(), null).iterator();
222
223
224
225                   while (it.hasNext())
226
227                   {
228
229                      ObjectName JavaDoc on = (ObjectName JavaDoc) it.next();
230
231                      Resource res = (Resource) tree.searchObjectName(on.getCanonicalName());
232
233
234
235                      MBeanInfo JavaDoc info = res.getMBeanInfo();
236
237                      MBeanAttributeInfo JavaDoc[] attrs = info.getAttributes();
238
239
240
241                      for (int i = 0; i < attrs.length; i++)
242
243                      {
244
245                         if (attrs[i].getName().equals(name))
246
247                         {
248
249                            resultView.addAttributeResult(res, attrs[i]);
250
251                            logger.debug("Add new attribute " + res + " => " + name);
252
253                            break;
254
255                         }
256
257                      }
258
259                   }
260
261                }
262
263
264
265                // Process Operations
266

267                iterator = view.getOperationLines().iterator();
268
269                while (iterator.hasNext())
270
271                {
272
273                   ViewLine query = (ViewLine) iterator.next();
274
275
276
277                   String JavaDoc name = query.getName();
278
279                   Iterator JavaDoc it = tree.query(query.getObjectName(), null).iterator();
280
281
282
283                   while (it.hasNext())
284
285                   {
286
287                      ObjectName JavaDoc on = (ObjectName JavaDoc) it.next();
288
289                      Resource res = (Resource) tree.searchObjectName(on.getCanonicalName());
290
291
292
293                      MBeanInfo JavaDoc info = res.getMBeanInfo();
294
295                      MBeanOperationInfo JavaDoc[] opers = info.getOperations();
296
297
298
299                      for (int i = 0; i < opers.length; i++)
300
301                      {
302
303                         if (opers[i].getName().equals(name))
304
305                         {
306
307                            resultView.addOperationResult(res, opers[i]);
308
309                            logger.debug("Add new attribute " + res + " => " + name);
310
311                            break;
312
313                         }
314
315                      }
316
317                   }
318
319                }
320
321
322
323                context.setAttribute(Constants.CUSTOM_VIEW_ATTRIBUTES, resultView.getAttributeResults());
324
325                context.setAttribute(Constants.CUSTOM_VIEW_OPERATIONS, resultView.getOperationResults());
326
327             }
328
329             else
330
331             {
332
333                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.process.view"));
334
335             }
336
337          }
338
339          catch (Exception JavaDoc e)
340
341          {
342
343             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.process.view"));
344
345          }
346
347       }
348
349       else
350
351       {
352
353          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect"));
354
355       }
356
357
358
359       // Report any errors we have discovered back to the original form
360

361       if (!errors.empty())
362
363       {
364
365          saveErrors(request, errors);
366
367          return (mapping.findForward("error"));
368
369       }
370
371
372
373       return (mapping.findForward("view"));
374
375    }
376
377 }
378
379
Popular Tags