KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > MonitorGetCommand


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 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.monitor;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import javax.management.Attribute JavaDoc;
37 import javax.management.AttributeList JavaDoc;
38 import javax.management.AttributeNotFoundException JavaDoc;
39 import javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.MBeanAttributeInfo JavaDoc;
41 import javax.management.MBeanInfo JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43
44 /**
45  * Get command for monitoring.
46  */

47 public class MonitorGetCommand extends MonitorCommand {
48
49     /**
50      * Constant to denote - get specified attribute value from specified MBean
51      */

52     private static final int GET_ATTR = 2;
53
54     /**
55      * Constant to denote - get all attribute values from specified MBean
56      */

57     private static final int GET_ALL_ATTR = 4;
58
59     /**
60      * Constant to denote - get specified attribute value from all child MBeans
61      * of specified type.
62      */

63     private static final int GET_ATTR_FOR_TYPE = 6;
64
65     /**
66      * Constant to denote - get all attribute values from all child MBeans of
67      * specified type.
68      */

69     private static final int GET_ALL_ATTR_FOR_TYPE = 8;
70
71     /**
72      * Name of the attribute. This is either an attribute name or wildcard
73      * character (*) denoting all attributes. Partial wildcard is not supported,
74      * so num-invocation can be used but num* can not be.
75      */

76     private String JavaDoc attrName;
77
78     /**
79      * Create a new get command that gets specified attribute from specified
80      * MBean.
81      * @param mbeanName object name of the MBean
82      * @param attrName name of the attribute. This can be wildcard character
83      */

84     MonitorGetCommand(ObjectName JavaDoc mbeanName, String JavaDoc attrName) {
85         this.objectName = mbeanName;
86         this.attrName = attrName;
87         if (WILDCARD.equals(attrName)) {
88             actionCode = GET_ALL_ATTR;
89         } else {
90             actionCode = GET_ATTR;
91         }
92     }
93
94     /**
95      * Create a new get command that gets specified attribute from all child
96      * of specified type from the specified MBean.
97      * @param mbeanName object name of the MBean
98      * @param type the type of child MBeans to search
99      * @param attrName name of the attribute. This can be wildcard character
100      */

101     MonitorGetCommand(ObjectName JavaDoc mbeanName, MonitoredObjectType type,
102             String JavaDoc attrName) {
103         this.objectName = mbeanName;
104         this.monitoredObjectType = type.getTypeName();
105         this.attrName = attrName;
106         if (WILDCARD.equals(attrName)) {
107             actionCode = GET_ALL_ATTR_FOR_TYPE;
108         } else {
109             actionCode = GET_ATTR_FOR_TYPE;
110         }
111     }
112
113     /**
114      * Run get command. This method returns an instance of jmx AttributeList.
115      * @throws InstanceNotFoundException if the MBean is not found
116      * @throws AttributeNotFoundException if the attribute is not found
117      * @return list of attributes
118      */

119     Object JavaDoc runCommand() throws InstanceNotFoundException JavaDoc,
120             AttributeNotFoundException JavaDoc {
121         BaseMonitorMBean mbean = MonitoringHelper.getMonitorMBean(objectName);
122         MonitoredObjectType type = null;
123         if (monitoredObjectType != null) {
124             type = MonitoredObjectType.getMonitoredObjectType(monitoredObjectType);
125         }
126         ArrayList JavaDoc mbeanList = null;
127         if (actionCode == GET_ATTR_FOR_TYPE
128                 || actionCode == GET_ALL_ATTR_FOR_TYPE) {
129             mbeanList = mbean.getChildList(type);
130         } else {
131             mbeanList = new ArrayList JavaDoc();
132             mbeanList.add(mbean);
133         }
134         AttributeList JavaDoc result = new AttributeList JavaDoc();
135         Iterator JavaDoc iter = mbeanList.iterator();
136         while (iter.hasNext()) {
137             BaseMonitorMBean bmb = (BaseMonitorMBean)iter.next();
138             if (actionCode == GET_ATTR || actionCode == GET_ATTR_FOR_TYPE) {
139                 Object JavaDoc val = bmb.getAttribute(attrName);
140                 Attribute JavaDoc attr = null;
141                 if (actionCode == GET_ATTR_FOR_TYPE) {
142                     attr = new Attribute JavaDoc(getQualifiedName(bmb, type, attrName),
143                             val);
144                 } else {
145                     attr = new Attribute JavaDoc(attrName, val);
146                 }
147                 result.add(attr);
148             } else if (actionCode == GET_ALL_ATTR
149                     || actionCode == GET_ALL_ATTR_FOR_TYPE) {
150                 String JavaDoc[] attrNames = bmb.getAllAttributeNames();
151                 AttributeList JavaDoc attrList = bmb.getAttributes(attrNames);
152                 if (actionCode == GET_ALL_ATTR_FOR_TYPE) {
153                     AttributeList JavaDoc newAttrList = new AttributeList JavaDoc();
154                     int numAttr = attrList.size();
155                     for (int i = 0; i < numAttr; i++) {
156                         Attribute JavaDoc attr = (Attribute JavaDoc)attrList.get(i);
157                         attr = new Attribute JavaDoc(
158                                 getQualifiedName(bmb, type, attr.getName()),
159                                 attr.getValue());
160                         newAttrList.add(attr);
161                     }
162                     attrList = newAttrList;
163                 }
164                 result.addAll(attrList);
165             }
166         }
167         return result;
168     }
169
170     /**
171      * Get qualified name of an attribute. It is possible to get attributes
172      * from more than one MBean in same command invocation. In these cases,
173      * the attribute names are prefixed with child MBean type and optionally
174      * child MBean name to scope the attribute name properly.
175      * @param mbean the child mbean
176      * @param type type of the child mbean
177      * @param attrName name of the attribute in child mbean
178      */

179     private String JavaDoc getQualifiedName(BaseMonitorMBean mbean,
180             MonitoredObjectType type, String JavaDoc attrName) {
181         StringBuffer JavaDoc fullName = new StringBuffer JavaDoc();
182         fullName.append(mbean.getNodeType());
183         if (!type.isSingleton()) {
184             fullName.append("." + mbean.getNodeName());
185         }
186         fullName.append("." + attrName);
187         return fullName.toString();
188     }
189
190     /**
191      * Constant to denote wildcard character (addresses all attributes)
192      */

193     private static final String JavaDoc WILDCARD = "*";
194
195 }
196
Popular Tags