KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > MBeanHandlers


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.handlers;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.view.ContainerView;
28 import com.iplanet.jato.view.View;
29 import com.iplanet.jato.view.ViewBase;
30
31 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
32 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
33 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
34 import com.sun.enterprise.tools.guiframework.view.ViewDescriptorManager;
35 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
36 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46
47 import javax.management.Attribute JavaDoc;
48 import javax.management.AttributeList JavaDoc;
49 import javax.management.MBeanServer JavaDoc;
50
51 import com.sun.enterprise.tools.admingui.util.Util;
52
53 /**
54  *
55  */

56 public class MBeanHandlers {
57
58     /**
59      * retrieves attribute values from mbean identified by "objectName"
60      * parameter.
61      *
62      * @param reqCtx The RequestContext
63      * @param handlerCtx The HandlerContext
64      *
65      * @return attributeList
66      */

67     public void getAttributeListFromMBean(RequestContext reqCtx, HandlerContext handlerCtx) {
68     ArrayList JavaDoc attributeNames =
69         (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
70     if (attributeNames == null) {
71         throw new IllegalArgumentException JavaDoc(
72         "The parameter map did not contain " + ATTRIBUTE_NAMES);
73     }
74     Object JavaDoc objectName = handlerCtx.getInputValue(OBJECT_NAME);
75     if (objectName == null) {
76         throw new IllegalArgumentException JavaDoc(
77         "The parameter map did not contain " + OBJECT_NAME);
78     }
79         AttributeList JavaDoc values = null;
80     try {
81             values = (AttributeList JavaDoc)MBeanUtil.getAttributes(
82             objectName.toString(),
83             (String JavaDoc[])attributeNames.toArray(new String JavaDoc[attributeNames.size()]));
84         // Set the output value
85
} catch (Exception JavaDoc ex) {
86             if (ex.getCause() instanceof javax.management.InstanceNotFoundException JavaDoc) {
87                 // ignore, just return null;
88
// important for the monitoring code; don't know if monitor MBean is there or not
89
if (Util.isLoggableFINE()) {
90                     Util.logFINE("caught: InstanceNotFoundException");
91                 }
92             } else {
93                 throw new FrameworkException(
94                     "Unable to get MBean attributes for object name: '"+
95                     objectName+"'.", ex,
96                     null, null);
97             }
98     }
99     handlerCtx.setOutputValue(VALUE, values);
100     }
101
102     /**
103      * retrieves attribute value from mbean identified by "objectName"
104      * parameter.
105      *
106      * @param reqCtx The RequestContext
107      * @param handlerCtx The HandlerContext
108      *
109      * @return attributeValue
110      */

111     public void getAttributeValueFromMBean(RequestContext reqCtx, HandlerContext handlerCtx) {
112     String JavaDoc attributeName =
113         (String JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAME);
114     if (attributeName == null) {
115         throw new IllegalArgumentException JavaDoc(
116         "The parameter map did not contain " + ATTRIBUTE_NAME);
117     }
118     Object JavaDoc objectName = handlerCtx.getInputValue(OBJECT_NAME);
119     if (objectName == null) {
120         throw new IllegalArgumentException JavaDoc(
121         "The parameter map did not contain " + OBJECT_NAME);
122     }
123     try {
124             handlerCtx.setOutputValue(VALUE, MBeanUtil.getAttribute(
125                         objectName.toString(), attributeName));
126         // Set the output value
127
} catch (Exception JavaDoc ex) {
128             if (ex.getCause() instanceof javax.management.InstanceNotFoundException JavaDoc) {
129                 // ignore, just return null;
130
// important for the monitoring code; don't know if monitor MBean is there or not
131
if (Util.isLoggableFINE()) {
132                     Util.logFINE("caught: InstanceNotFoundException");
133                 }
134             } else {
135                 throw new FrameworkException(
136                     "Unable to get MBean attribute for object name: '"+
137                     objectName+"'.", ex,
138                     null, null);
139             }
140     }
141     }
142
143     /**
144      * This method calls setAttributes on mbean specified by "objectName"
145      * parameter.
146      *
147      * @param reqCtx The RequestContext
148      * @param handlerCtx The HandlerContext
149      */

150     public void setAttributeListToMBean(RequestContext reqCtx, HandlerContext handlerCtx) {
151     AttributeList JavaDoc value = (AttributeList JavaDoc)handlerCtx.getInputValue(VALUE);
152     Object JavaDoc objectName = handlerCtx.getInputValue(OBJECT_NAME);
153     if (objectName == null) {
154         throw new IllegalArgumentException JavaDoc(
155         "The parameter map did not contain " + OBJECT_NAME);
156     }
157     try {
158         MBeanUtil.setAttributes(objectName.toString(), value);
159     } catch (Exception JavaDoc ex) {
160         throw new FrameworkException(
161         "Error while setting attributes on MBean '"+objectName+
162         "'!", ex, handlerCtx.getViewDescriptor(),
163         handlerCtx.getView());
164     }
165     }
166     
167     /**
168      * This method calls setAttribute on mbean specified by "objectName"
169      * parameter. KEY has the name of the MBean attribute, and value has
170      * the value to be saved. If th6e value is an array, it will be converted to a
171      * comma seperated list before saving.
172      *
173      * @param reqCtx The RequestContext
174      * @param handlerCtx The HandlerContext
175      */

176     public void setAttributeToMBean(RequestContext reqCtx, HandlerContext handlerCtx) {
177     Object JavaDoc value = handlerCtx.getInputValue(VALUE);
178     String JavaDoc name = (String JavaDoc)handlerCtx.getInputValue(KEY);
179         String JavaDoc val = null;
180         
181         if (value instanceof Object JavaDoc[]){
182             val = convertToCommaSeperatedList((Object JavaDoc[] ) value);
183         }else{
184             val = (String JavaDoc) value;
185         }
186         
187     Attribute JavaDoc attr = new Attribute JavaDoc(name, val);
188         
189     Object JavaDoc objectName = handlerCtx.getInputValue(OBJECT_NAME);
190     if (objectName == null) {
191         throw new IllegalArgumentException JavaDoc(
192         "The parameter map did not contain " + OBJECT_NAME);
193     }
194     if (name == null) {
195         throw new IllegalArgumentException JavaDoc(
196         "The parameter map did not contain AttributeName");
197     }
198     try {
199         MBeanUtil.setAttribute(objectName.toString(), attr);
200     } catch (Exception JavaDoc ex) {
201         throw new FrameworkException(
202         "Error while setting attributes on MBean '"+objectName+
203         "'!", ex, handlerCtx.getViewDescriptor(),
204         handlerCtx.getView());
205     }
206     }
207
208     private String JavaDoc convertToCommaSeperatedList(Object JavaDoc[] values){
209         String JavaDoc allValues = null;
210          if (values != null) {
211         int len = ((Object JavaDoc[])values).length;
212         for (int count=0; count<len; count++) {
213         String JavaDoc val = (String JavaDoc) (((Object JavaDoc[])values)[count]);
214         if ((val == null) || (val.toString().trim().length() == 0)) {
215             continue;
216         }
217         allValues = (allValues == null) ? val : allValues+ ","+ val;
218         }
219      }
220         return allValues;
221     }
222     
223     /**
224      * retrieves attribute values from mbean identified by "objectName" parameter.
225      *
226      * @param reqCtx The RequestContext
227      * @param handlerCtx The HandlerContext
228      *
229      * @return attributeList
230      *
231      */

232     private Attribute JavaDoc split(Attribute JavaDoc attr, String JavaDoc splitChar) {
233     if (attr == null || attr.getValue() == null) {
234         return attr;
235     }
236     String JavaDoc newline = System.getProperty("line.separator");
237     String JavaDoc strValue = attr.getValue().toString();
238         boolean changed = false;
239     if (strValue.indexOf(splitChar) > -1) {
240         strValue = strValue.replaceAll(splitChar, newline);
241             changed = true;
242         }
243     if (strValue.indexOf("${path.separator}") > -1) {
244         strValue = strValue.replaceAll("\\$\\{path.separator\\}", newline);
245             changed = true;
246     }
247         return (changed) ? new Attribute JavaDoc(attr.getName(), strValue) : attr;
248     }
249
250
251     /**
252      *
253      */

254     private Attribute JavaDoc unsplit(Attribute JavaDoc attr, String JavaDoc splitChar) {
255     if (attr == null || attr.getValue() == null) {
256         return attr;
257     }
258     String JavaDoc strValue = attr.getValue().toString();
259
260     String JavaDoc result = "";
261     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(strValue, "\n\r");
262     while (st.hasMoreTokens()) {
263         if (result.equals("") == false) {
264         result += "${path.separator}"; //splitChar;
265
}
266         result += st.nextToken();
267     }
268     return new Attribute JavaDoc(attr.getName(), result);
269     }
270
271
272     /**
273      * @param reqCtx The RequestContext
274      * @param handlerCtx The HandlerContext
275      */

276     public void split(RequestContext reqCtx, HandlerContext handlerCtx) {
277     Object JavaDoc value = null;
278         
279         Object JavaDoc inputList = handlerCtx.getInputValue(IN);
280
281     String JavaDoc splitChar = System.getProperty("path.separator");
282     if (!splitChar.equals("") && (inputList instanceof AttributeList JavaDoc)) {
283         AttributeList JavaDoc list = (AttributeList JavaDoc)inputList;
284         AttributeList JavaDoc newList = new AttributeList JavaDoc();
285         for (int i=0; i<list.size(); i++) {
286         newList.add(i, split((Attribute JavaDoc)list.get(i), splitChar));
287         }
288         value = newList;
289     }
290
291     // Set the output value
292
handlerCtx.setOutputValue(OUT, value);
293     }
294
295
296     /**
297      * This method calls setAttributes on mbean specified by "objectName" parameter
298      *
299      * @param reqCtx The RequestContext
300      * @param handlerCtx The HandlerContext
301      */

302     public void unsplit(RequestContext reqCtx, HandlerContext handlerCtx) {
303     Object JavaDoc value = handlerCtx.getInputValue(IN);
304     String JavaDoc splitChar = System.getProperty("path.separator");
305     if (!splitChar.equals("") && (value instanceof AttributeList JavaDoc)) {
306         AttributeList JavaDoc list = (AttributeList JavaDoc)value;
307         AttributeList JavaDoc newList = new AttributeList JavaDoc();
308         for (int i=0; i<list.size(); i++) {
309         newList.add(i, unsplit((Attribute JavaDoc)list.get(i), splitChar));
310         }
311         value = newList;
312     }
313     handlerCtx.setOutputValue(OUT, value);
314     }
315
316     /**
317      *
318      * @param reqCtx The RequestContext
319      * @param handlerCtx The HandlerContext
320      *
321      * @return attributeList
322      * returns "AttributeList" for values of all displayFields, identified by "displayNames".
323      * ignores displayFields that have null value.
324      */

325     public void getAttributeListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) {
326     ArrayList JavaDoc attributeNames = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
327     if (attributeNames == null) {
328         throw new IllegalArgumentException JavaDoc(
329         "The parameter map did not contain " + ATTRIBUTE_NAMES);
330     }
331
332     ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
333     if (displayNames == null) {
334         throw new IllegalArgumentException JavaDoc(
335         "The parameter map did not contain " + DISPLAY_NAMES);
336     }
337
338     if (displayNames.size() != attributeNames.size()) {
339         throw new IllegalArgumentException JavaDoc(
340         "displayNames and attributeNames should have same number of entries!");
341     }
342     ContainerView view = getView(reqCtx, handlerCtx);
343
344     AttributeList JavaDoc attrList = new AttributeList JavaDoc();
345     for (int i = 0; i < attributeNames.size(); i++) {
346         Object JavaDoc fieldValue = view.getDisplayFieldValue((String JavaDoc)displayNames.get(i));
347         if (fieldValue != null) {
348         attrList.add(new Attribute JavaDoc((String JavaDoc)attributeNames.get(i), fieldValue));
349         }
350     }
351     // Set the output value
352
handlerCtx.setOutputValue(VALUE, attrList);
353     }
354     
355      /**
356      *
357      * @param reqCtx The RequestContext
358      * @param handlerCtx The HandlerContext
359      *
360      * @return attributeList
361      * returns "AttributeList" for values of all displayFields, identified by "displayNames".
362      * ignores displayFields that have null value.
363      */

364     public void getPropertiesListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) {
365     ArrayList JavaDoc propertyNames = (ArrayList JavaDoc)handlerCtx.getInputValue(PROPERTY_NAMES);
366     if (propertyNames == null) {
367         throw new IllegalArgumentException JavaDoc(
368         "The parameter map did not contain " + PROPERTY_NAMES);
369     }
370
371     ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
372     if (displayNames == null) {
373         throw new IllegalArgumentException JavaDoc(
374         "The parameter map did not contain " + DISPLAY_NAMES);
375     }
376
377     if (displayNames.size() != propertyNames.size()) {
378         throw new IllegalArgumentException JavaDoc(
379         "displayNames and attributeNames should have same number of entries!");
380     }
381     ContainerView view = getView(reqCtx, handlerCtx);
382
383     Properties JavaDoc props = new Properties JavaDoc();
384     for (int i = 0; i < propertyNames.size(); i++) {
385         String JavaDoc fieldValue = (String JavaDoc)view.getDisplayFieldValue((String JavaDoc)displayNames.get(i));
386         if (fieldValue != null && fieldValue != "") {
387         props.put((String JavaDoc)propertyNames.get(i), fieldValue);
388         }
389     }
390     // Set the output value
391
handlerCtx.setOutputValue(VALUE, props);
392     }
393
394
395     // makes and attribute list out of name value pairs. names and values should be ArrayList
396
public void getAttributeListFromNameValues(RequestContext reqCtx, HandlerContext handlerCtx) {
397     ArrayList JavaDoc attributeNames = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
398     ArrayList JavaDoc attributeValues = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_VALUES);
399     if (attributeNames == null || attributeValues == null) {
400         throw new IllegalArgumentException JavaDoc(
401         "The parameter map must contain " + ATTRIBUTE_NAMES + " and " + ATTRIBUTE_VALUES);
402     }
403
404     if (attributeValues.size() != attributeNames.size()) {
405         throw new IllegalArgumentException JavaDoc(
406                 ATTRIBUTE_NAMES + "and " + ATTRIBUTE_VALUES + " should have same number of entries!");
407     }
408         
409     AttributeList JavaDoc attrList = new AttributeList JavaDoc();
410     for (int i = 0; i < attributeNames.size(); i++) {
411             attrList.add(new Attribute JavaDoc((String JavaDoc)attributeNames.get(i), attributeValues.get(i)));
412     }
413     // Set the output value
414
handlerCtx.setOutputValue(VALUE, attrList);
415     }
416     
417     /**
418      *
419      * @param reqCtx The RequestContext
420      * @param handlerCtx The HandlerContext
421      *
422      * @return arrayList
423      * returns "ArrayList" for values of all displayFields, identified by "displayNames".
424      * ignores displayFields that have null value.
425      */

426     public void getArrayListFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) {
427     ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
428     if (displayNames == null) {
429         throw new IllegalArgumentException JavaDoc(
430         "The parameter map did not contain " + DISPLAY_NAMES);
431     }
432
433     ContainerView view = getView(reqCtx, handlerCtx);
434
435     ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
436     for (int i = 0; i < displayNames.size(); i++) {
437         Object JavaDoc fieldValue = view.getDisplayFieldValue((String JavaDoc)displayNames.get(i));
438             if (fieldValue instanceof String JavaDoc && ((String JavaDoc)(fieldValue)).length() == 0)
439                 fieldValue = null;
440             arrayList.add(fieldValue);
441     }
442     // Set the output value
443
handlerCtx.setOutputValue(VALUE, arrayList);
444     }
445     
446     /**
447      *
448      * @param reqCtx The RequestContext
449      * @param handlerCtx The HandlerContext
450      *
451      * @return map
452      * returns "Map" of all displayFields, identified by "displayNames".
453      * ignores displayFields that have null value. Use the "attributeNames" as keys.
454      */

455     public void getMapFromDisplayFields(RequestContext reqCtx, HandlerContext handlerCtx) {
456     ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
457         ArrayList JavaDoc keyNames = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
458     if (displayNames == null) {
459         throw new IllegalArgumentException JavaDoc(
460         "The parameter map did not contain " + DISPLAY_NAMES);
461     }
462     ContainerView view = getView(reqCtx, handlerCtx);
463         Map JavaDoc map = new HashMap JavaDoc();
464     for (int i = 0; i < displayNames.size(); i++) {
465         Object JavaDoc fieldValue = view.getDisplayFieldValue((String JavaDoc)displayNames.get(i));
466             if (fieldValue instanceof String JavaDoc && ((String JavaDoc)(fieldValue)).length() == 0)
467                 fieldValue = null;
468             map.put((String JavaDoc)keyNames.get(i),fieldValue);
469     }
470     handlerCtx.setOutputValue(VALUE, map);
471     }
472
473
474
475     /**
476      * This method calls setAttributes on mbean specified by "objectName"
477      * parameter.
478      *
479      * @param reqCtx The RequestContext
480      * @param handlerCtx The HandlerContext
481      */

482     public void setDisplayFieldsFromAttributeList(RequestContext reqCtx, HandlerContext handlerCtx) {
483     Object JavaDoc value = handlerCtx.getInputValue(VALUE);
484     if (value == null) {
485         return;
486     }
487
488     // Get the attribute names
489
ArrayList JavaDoc attributeNames = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
490     if (attributeNames == null) {
491         throw new IllegalArgumentException JavaDoc(
492         "The parameter map did not contain " + ATTRIBUTE_NAMES);
493     }
494
495     // Get the display field names
496
ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
497     if (displayNames == null) {
498         throw new IllegalArgumentException JavaDoc(
499         "The parameter map did not contain " + DISPLAY_NAMES);
500     }
501
502     // Make sure they match in size
503
if (displayNames.size() != attributeNames.size()) {
504         throw new IllegalArgumentException JavaDoc(
505         "displayNames and attributeNames should have same number of entries!");
506     }
507
508     // Get the view which holds the display fields to set
509
ContainerView view = getView(reqCtx, handlerCtx);
510     AttributeList JavaDoc attrList = (AttributeList JavaDoc)value;
511         for (int i=0; i<displayNames.size(); i++) {
512             String JavaDoc attrName = (String JavaDoc)attributeNames.get(i);
513             int j=0;
514             while ((j<attrList.size()) &&
515                    !((Attribute JavaDoc)attrList.get(j)).getName().equals(attrName)) {
516                 j++;
517             }
518             if (j < attrList.size()) {
519                 Object JavaDoc attrValue = ((Attribute JavaDoc)attrList.get(j)).getValue();
520         view.setDisplayFieldValue((String JavaDoc)displayNames.get(i), attrValue);
521             }
522         }
523     }
524     
525     /**
526      * This method calls setDisplayFields from java.util.Properties Object"
527      * parameter.
528      *
529      * @param reqCtx The RequestContext
530      * @param handlerCtx The HandlerContext
531      */

532     public void setDisplayFieldsFromPropertiesList(RequestContext reqCtx, HandlerContext handlerCtx) {
533     Object JavaDoc value = handlerCtx.getInputValue(VALUE);
534     if (value == null) {
535         return;
536     }
537
538     // Get the attribute names
539
ArrayList JavaDoc propertyNames = (ArrayList JavaDoc)handlerCtx.getInputValue(PROPERTY_NAMES);
540     if (propertyNames == null) {
541         throw new IllegalArgumentException JavaDoc(
542         "The parameter map did not contain " + PROPERTY_NAMES);
543     }
544
545     // Get the display field names
546
ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
547     if (displayNames == null) {
548         throw new IllegalArgumentException JavaDoc(
549         "The parameter map did not contain " + DISPLAY_NAMES);
550     }
551
552     // Make sure they match in size
553
if (displayNames.size() != propertyNames.size()) {
554         throw new IllegalArgumentException JavaDoc(
555         "displayNames and attributeNames should have same number of entries!");
556     }
557
558     // Get the view which holds the display fields to set
559
ContainerView view = getView(reqCtx, handlerCtx);
560         Properties JavaDoc props = (Properties JavaDoc)value;
561         for (int i=0; i<displayNames.size(); i++) {
562             String JavaDoc propertyValue = props.getProperty(propertyNames.get(i).toString());
563             view.setDisplayFieldValue((String JavaDoc)displayNames.get(i), propertyValue);
564             
565         }
566     }
567     
568     
569     /**
570      * This method calls setDisplayFields from List "
571      * parameter.
572      *
573      * @param reqCtx The RequestContext
574      * @param handlerCtx The HandlerContext
575      */

576     public void setDisplayFieldsFromList(RequestContext reqCtx, HandlerContext handlerCtx) {
577     
578     ArrayList JavaDoc valueList = (ArrayList JavaDoc)handlerCtx.getInputValue("valueList");
579     if (valueList == null) {
580         throw new IllegalArgumentException JavaDoc(
581         "The parameter map did not contain valueList" );
582     }
583
584     // Get the display field names
585
ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
586     if (displayNames == null) {
587         throw new IllegalArgumentException JavaDoc(
588         "The parameter map did not contain " + DISPLAY_NAMES);
589     }
590
591     // Make sure they match in size
592
if (displayNames.size() != valueList.size()) {
593         throw new IllegalArgumentException JavaDoc(
594         "displayNames and valueList should have same number of entries!");
595     }
596
597     // Get the view which holds the display fields to set
598
ContainerView view = getView(reqCtx, handlerCtx);
599         for (int i=0; i < valueList.size(); i++) {
600             view.setDisplayFieldValue((String JavaDoc)displayNames.get(i), (String JavaDoc)valueList.get(i));
601         }
602     }
603     
604
605 // for (int i = 0; i<attrList.size(); i++) {
606
// Attribute attr = (Attribute)attrList.get(i);
607
// int j = 0;
608
// while ((j < attributeNames.size()) &&
609
// !attr.getName().equals(attributeNames.get(j))) {
610
// j++;
611
// }
612
// if (j < attributeNames.size()) {
613
// Object attrValue = attr.getValue();
614
//// if (attrValue != null) {
615
//// attrValue = attrValue.toString();
616
//// }
617
// view.setDisplayFieldValue((String)displayNames.get(j), attrValue);
618
// }
619
// }
620
// }
621

622     /**
623      * @param reqCtx The RequestContext
624      * @param handlerCtx The HandlerContext
625      */

626     public static ContainerView getView(RequestContext reqCtx, HandlerContext handlerCtx) {
627     ViewDescriptor viewDesc = null;
628     Object JavaDoc value = handlerCtx.getInputValue(VIEW_DESCRIPTOR_NAME);
629     if (value == null) {
630         viewDesc = handlerCtx.getViewDescriptor();
631     } else {
632         // Get the ViewDescriptorManager
633
ViewDescriptorManager mgr = ViewDescriptorManager.getInstance();
634
635         // Get the ViewDescriptor using a List or a dotted name.
636
if (value instanceof List JavaDoc) {
637         // Iterate through the keys to find the child ViewDescriptor
638
Iterator JavaDoc it = ((List JavaDoc)value).iterator();
639         String JavaDoc key = it.next().toString();
640         viewDesc = mgr.getViewDescriptor(key);
641         if (viewDesc == null) {
642             throw new IllegalArgumentException JavaDoc(
643             "Top-level ViewDescriptor '"+key+"' not found!");
644         }
645         while (it.hasNext()) {
646             key = it.next().toString();
647             viewDesc = viewDesc.getChildDescriptor(key);
648             if (viewDesc == null) {
649             throw new IllegalArgumentException JavaDoc(
650                 "Child ViewDescriptor '"+key+"' not found!");
651             }
652         }
653         } else if (value instanceof String JavaDoc) {
654                 View view = handlerCtx.getView();
655                 DescriptorContainerView descView = (DescriptorContainerView)
656                     (((ViewBase)view).getParentViewBean());
657                 return (ContainerView)descView.getChild((String JavaDoc)value);
658
659 // String viewName = (String)value;
660
//
661
// StringTokenizer st = new StringTokenizer(viewName, ".");
662
// if (st.hasMoreTokens()) {
663
// String key = st.nextToken();
664
// viewDesc = mgr.getViewDescriptor(key);
665
// // the name didn't start at the top level, look in current top.
666
// if (viewDesc == null) {
667
// View view = handlerCtx.getView();
668
// DescriptorContainerView descContainerView = (DescriptorContainerView)
669
// (((ViewBase)view).getParentViewBean());
670
// viewDesc = descContainerView.getViewDescriptor();
671
// if (viewDesc != null)
672
// viewDesc = viewDesc.getChildDescriptor(key);
673
// if (viewDesc == null) {
674
// throw new IllegalArgumentException(
675
// "Bad value for '"+VIEW_DESCRIPTOR_NAME+"'....("+value.toString()+")");
676
// }
677
// }
678
// }
679
// while (st.hasMoreTokens()) {
680
// viewDesc = viewDesc.getChildDescriptor(st.nextToken());
681
// if (viewDesc == null) {
682
// throw new IllegalArgumentException(
683
// "Bad value for '"+VIEW_DESCRIPTOR_NAME+"'. ("+value.toString()+")");
684
// }
685
// }
686
} else {
687                 throw new IllegalArgumentException JavaDoc(
688                     "Unknown type for "+VIEW_DESCRIPTOR_NAME+". ("+value.toString()+")");
689             }
690     }
691         View view = viewDesc.getView(reqCtx);
692         while ((view instanceof ContainerView) == false)
693             view = view.getParent();
694     return (ContainerView)view;
695     }
696     
697
698     /**
699      * Retrieves a JMX Attribute value. The Attribute should be stored in a
700      * request attribute with key identified by the "key" parameter.
701      *
702      * @param reqCtx The RequestContext
703      * @param handlerCtx The HandlerContext
704      *
705      * @return The attributes value (or null)
706      */

707     public void getJMXAttributeValue(RequestContext reqCtx, HandlerContext handlerCtx) {
708     // Get the Request attribute key that is holding the JMX Attribute
709
Object JavaDoc val = handlerCtx.getInputValue("JMXAttribute");
710     if (val == null) {
711         throw new FrameworkException(
712         "The input value for 'JMXAttribute' was not specified.",
713         null, null);
714     }
715     
716 // FIXME: This method should not pull the value from an attribute, it should
717
// FIXME: expect the value itself!
718
// Get the Attribute (or AttributeList)
719

720         Attribute JavaDoc att = null;
721     if (val instanceof AttributeList JavaDoc) {
722         AttributeList JavaDoc list = ((AttributeList JavaDoc)val);
723         // Only get the 1st element of an array
724
if (list.size() > 0) {
725         att = (Attribute JavaDoc)list.get(0);
726         }
727     } else {
728         // Expect the request parameter to be an attribute
729
att = (Attribute JavaDoc)val;
730     }
731
732     // Set the output value
733
handlerCtx.setOutputValue(VALUE, (att != null) ? att.getValue() : null);
734     }
735
736     public void restoreDefaultValues(RequestContext reqCtx, HandlerContext handlerCtx) {
737     ContainerView view = getView(reqCtx, handlerCtx);
738         
739         ArrayList JavaDoc displayNames = (ArrayList JavaDoc)handlerCtx.getInputValue(DISPLAY_NAMES);
740         ArrayList JavaDoc modelNames = (ArrayList JavaDoc)handlerCtx.getInputValue(ATTRIBUTE_NAMES);
741         String JavaDoc objectName = (String JavaDoc)handlerCtx.getInputValue(OBJECT_NAME);
742         String JavaDoc[] params = new String JavaDoc[1];
743         String JavaDoc[] types = {"java.lang.String"};
744         for (int i = 0; i < modelNames.size(); i++) {
745             params[0] = (String JavaDoc)modelNames.get(i);
746             String JavaDoc displayName = (String JavaDoc)displayNames.get(i);
747             if (view.getDisplayFieldValue(displayName) == null) {
748                 view.setDisplayFieldValue(displayName,
749                     MBeanUtil.getAttribute(objectName, params[0]));
750             } else {
751                 view.setDisplayFieldValue(displayName,
752                     MBeanUtil.invoke(objectName, "getDefaultAttributeValue", params, types));
753             }
754         }
755     }
756         
757
758     public static final String JavaDoc IN = "in";
759     public static final String JavaDoc OUT = "out";
760     public static final String JavaDoc KEY = "key";
761     public static final String JavaDoc VALUE = "value";
762     public static final String JavaDoc OBJECT_NAME = "objectName";
763     public static final String JavaDoc DISPLAY_NAMES = "displayNames";
764     public static final String JavaDoc ATTRIBUTE_NAMES = "attributeNames";
765     public static final String JavaDoc PROPERTY_NAMES = "propertyNames";
766     public static final String JavaDoc ATTRIBUTE_NAME = "attributeName";
767     public static final String JavaDoc ATTRIBUTE_VALUES = "attributeValues";
768     public static final String JavaDoc DATA_CONVERSION_TYPE = "dataConversionType";
769     public static final String JavaDoc VIEW_DESCRIPTOR_NAME = "viewDescriptorName";
770 }
771
Popular Tags