00001
00026 package org.objectweb.jonas.webapp.jonasadmin;
00027
00028 import java.net.URLEncoder;
00029 import java.util.ArrayList;
00030 import java.util.Collections;
00031 import java.util.Iterator;
00032
00033 import javax.management.MBeanAttributeInfo;
00034 import javax.management.MBeanInfo;
00035 import javax.management.MBeanOperationInfo;
00036 import javax.management.ObjectName;
00037 import javax.servlet.http.HttpServletRequest;
00038
00039 import org.apache.struts.Globals;
00040 import org.apache.struts.action.ActionServlet;
00041 import org.apache.struts.util.MessageResources;
00042
00043 import org.objectweb.jonas.jmx.JonasManagementRepr;
00044 import org.objectweb.jonas.webapp.jonasadmin.common.BeanComparator;
00045 import org.objectweb.jonas.webapp.jonasadmin.mbean.J2eeMbeanItem;
00046 import org.objectweb.jonas.webapp.jonasadmin.mbean.MbeanItem;
00047 import org.objectweb.jonas.webapp.jonasadmin.mbean.OwnerMbeanItem;
00048 import org.objectweb.jonas.webapp.taglib.TreeBuilder;
00049 import org.objectweb.jonas.webapp.taglib.TreeControl;
00050 import org.objectweb.jonas.webapp.taglib.TreeControlNode;
00051
00060 public class MBeanTreeBuilder implements TreeBuilder {
00061
00062
00063
00064
00074 public void buildTree(TreeControl treeControl, ActionServlet servlet
00075 , HttpServletRequest request) {
00076 try {
00077 TreeControlNode rootNode = treeControl.getRoot();
00078 MessageResources resources = (MessageResources) servlet.getServletContext().
00079 getAttribute(Globals.MESSAGES_KEY);
00080 getMBeans(rootNode, resources);
00081 }
00082 catch (Throwable t) {
00083 t.printStackTrace(System.out);
00084 }
00085 }
00086
00087
00088
00097 public void getMBeans(TreeControlNode rootNode, MessageResources resources)
00098 throws Exception {
00099
00100 TreeControlNode nodeMBeans = new TreeControlNode("mbeans", "icon/mbeans.gif"
00101 , resources.getMessage("treenode.allmbeans"), "ListMBeans.do", "content", false);
00102 rootNode.addChild(nodeMBeans);
00103
00104 ArrayList[] als = JonasAdminJmx.getFamiliesMbeansLists();
00105 if (als[MbeanItem.FAMILY_J2EE].size() > 0) {
00106 getJ2eeMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_J2EE]);
00107 }
00108 if (als[MbeanItem.FAMILY_OWNER].size() > 0) {
00109 getOwnerMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_OWNER]);
00110 }
00111 if (als[MbeanItem.FAMILY_UNKNOWN].size() > 0) {
00112 getUnknownMBeans(nodeMBeans, resources, als[MbeanItem.FAMILY_UNKNOWN]);
00113 }
00114 }
00115
00125 public void getOwnerMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
00126 , ArrayList p_List)
00127 throws Exception {
00128
00129 TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00130 + "owner", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.owner")
00131 , "ListOwnerMBeans.do", "content", false);
00132 p_ParentNode.addChild(nodeMBeans);
00133
00134 Collections.sort(p_List, new BeanComparator(new String[] {
00135 "domain", "type", "objectName"}));
00136
00137
00138 String sLastDomain = "";
00139 String sLastType = "";
00140 TreeControlNode nodeMBean = null;
00141 TreeControlNode nodeDomain = null;
00142 TreeControlNode nodeType = null;
00143 TreeControlNode nodeParent = null;
00144 OwnerMbeanItem oItem;
00145
00146 Iterator it = p_List.iterator();
00147 while (it.hasNext()) {
00148 oItem = (OwnerMbeanItem) it.next();
00149
00150 nodeParent = nodeMBeans;
00151 if (oItem.getDomain() != null) {
00152
00153 if (oItem.getDomain().equals(sLastDomain) == false) {
00154 sLastDomain = oItem.getDomain();
00155 nodeDomain = new TreeControlNode(nodeParent.getName()
00156 + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
00157 , oItem.getDomain(), null, "content", false);
00158 nodeMBeans.addChild(nodeDomain);
00159 }
00160 nodeParent = nodeDomain;
00161
00162 if (oItem.getType() != null) {
00163
00164 if (oItem.getType().equals(sLastType) == false) {
00165 sLastType = oItem.getType();
00166 nodeType = new TreeControlNode(nodeParent.getName()
00167 + WhereAreYou.NODE_SEPARATOR + oItem.getType(), "icon/mbeantype.gif"
00168 , oItem.getType(), null, "content", false);
00169 nodeDomain.addChild(nodeType);
00170 }
00171 nodeParent = nodeType;
00172 }
00173 }
00174
00175
00176 nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00177 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
00178 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
00179 , "UTF-8"), "content", false);
00180 nodeParent.addChild(nodeMBean);
00181
00182 }
00183 }
00184
00194 public void getJ2eeMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
00195 , ArrayList p_List)
00196 throws Exception {
00197
00198 TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00199 + "j2ee", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.j2ee")
00200 , "ListJ2eeMBeans.do", "content", false);
00201 p_ParentNode.addChild(nodeMBeans);
00202
00203 Collections.sort(p_List, new BeanComparator(new String[] {
00204 "domain", "j2eeType", "name", "objectName"}));
00205
00206
00207 J2eeMbeanItem oItem;
00208 String sLastDomain = "";
00209 String sLastType = "";
00210 TreeControlNode nodeMBean = null;
00211 TreeControlNode nodeDomain = null;
00212 TreeControlNode nodeType = null;
00213 TreeControlNode nodeParent = null;
00214
00215 Iterator it = p_List.iterator();
00216 while (it.hasNext()) {
00217 oItem = (J2eeMbeanItem) it.next();
00218
00219 nodeParent = nodeMBeans;
00220 if (oItem.getDomain() != null) {
00221
00222 if (oItem.getDomain().equals(sLastDomain) == false) {
00223 sLastDomain = oItem.getDomain();
00224 nodeDomain = new TreeControlNode(nodeParent.getName()
00225 + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
00226 , oItem.getDomain(), null, "content", false);
00227 nodeMBeans.addChild(nodeDomain);
00228 }
00229 nodeParent = nodeDomain;
00230 }
00231 if (oItem.getJ2eeType() != null) {
00232
00233 if (oItem.getJ2eeType().equals(sLastType) == false) {
00234 sLastType = oItem.getJ2eeType();
00235 nodeType = new TreeControlNode(nodeParent.getName()
00236 + WhereAreYou.NODE_SEPARATOR + oItem.getJ2eeType(), "icon/mbeantype.gif"
00237 , oItem.getJ2eeType(), null, "content", false);
00238 nodeDomain.addChild(nodeType);
00239 }
00240 nodeParent = nodeType;
00241 }
00242
00243
00244 nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00245 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
00246 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
00247 , "UTF-8"), "content", false);
00248 nodeParent.addChild(nodeMBean);
00249
00250 }
00251 }
00252
00262 public void getUnknownMBeans(TreeControlNode p_ParentNode, MessageResources p_Resources
00263 , ArrayList p_List)
00264 throws Exception {
00265
00266 TreeControlNode nodeMBeans = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00267 + "unknown", "icon/mbeans.gif", p_Resources.getMessage("treenode.mbeans.unknown")
00268 , "ListUnknownMBeans.do", "content", false);
00269 p_ParentNode.addChild(nodeMBeans);
00270
00271 Collections.sort(p_List, new BeanComparator(new String[] {
00272 "domain", "objectName"}));
00273
00274
00275 MbeanItem oItem;
00276 String sLastDomain = "";
00277 TreeControlNode nodeMBean = null;
00278 TreeControlNode nodeDomain = null;
00279 TreeControlNode nodeType = null;
00280 TreeControlNode nodeParent = null;
00281
00282 Iterator it = p_List.iterator();
00283 while (it.hasNext()) {
00284 oItem = (MbeanItem) it.next();
00285
00286 nodeParent = nodeMBeans;
00287 if (oItem.getDomain() != null) {
00288
00289 if (oItem.getDomain().equals(sLastDomain) == false) {
00290 sLastDomain = oItem.getDomain();
00291 nodeDomain = new TreeControlNode(nodeParent.getName()
00292 + WhereAreYou.NODE_SEPARATOR + oItem.getDomain(), "icon/mbeandomain.gif"
00293 , oItem.getDomain(), null, "content", false);
00294 nodeMBeans.addChild(nodeDomain);
00295 }
00296 nodeParent = nodeDomain;
00297
00298 }
00299
00300
00301 nodeMBean = new TreeControlNode("mbeans" + WhereAreYou.NODE_SEPARATOR
00302 + oItem.getObjectName(), "icon/mbean.gif", oItem.getObjectName()
00303 , "ListMBeanDetails.do?select=" + URLEncoder.encode(oItem.getObjectName()
00304 , "UTF-8"), "content", false);
00305 nodeParent.addChild(nodeMBean);
00306
00307 }
00308 }
00309
00318 protected void getMBeanInfo(TreeControlNode nodeMBean, ObjectName onMBean
00319 , MessageResources resources)
00320 throws Exception {
00321
00322 MBeanInfo oMBeanInfo = JonasManagementRepr.getMBeanInfo(onMBean);
00323
00324 MBeanAttributeInfo[] aoMBeanAttributeInfo = oMBeanInfo.getAttributes();
00325 if (aoMBeanAttributeInfo.length > 0) {
00326
00327 TreeControlNode nodeAttributes = new TreeControlNode("Attributes"
00328 + WhereAreYou.NODE_SEPARATOR + onMBean.toString(), "icon/JonasQuestion.gif"
00329 , resources.getMessage("treenode.allmbeans.attributes"), null, "content", false);
00330 nodeMBean.addChild(nodeAttributes);
00331
00332 for (int i = 0; i < aoMBeanAttributeInfo.length; i++) {
00333 TreeControlNode nodeAttr = new TreeControlNode(String.valueOf(i)
00334 + WhereAreYou.NODE_SEPARATOR + onMBean.toString() + WhereAreYou.NODE_SEPARATOR
00335 + aoMBeanAttributeInfo[i].getName(), "icon/property.gif"
00336 , aoMBeanAttributeInfo[i].getName(), null, "content", false);
00337 nodeAttributes.addChild(nodeAttr);
00338 }
00339 }
00340
00341 MBeanOperationInfo[] aoMBeanOperationInfo = oMBeanInfo.getOperations();
00342 if (aoMBeanOperationInfo.length > 0) {
00343
00344 TreeControlNode nodeOperations = new TreeControlNode("Operations"
00345 + WhereAreYou.NODE_SEPARATOR + onMBean.toString(), "icon/JonasQuestion.gif"
00346 , resources.getMessage("treenode.allmbeans.operations"), null, "content", false);
00347 nodeMBean.addChild(nodeOperations);
00348
00349 for (int i = 0; i < aoMBeanOperationInfo.length; i++) {
00350 TreeControlNode nodeOpe = new TreeControlNode(String.valueOf(i)
00351 + WhereAreYou.NODE_SEPARATOR + onMBean.toString() + WhereAreYou.NODE_SEPARATOR
00352 + aoMBeanOperationInfo[i].getName(), "icon/action.gif"
00353 , aoMBeanOperationInfo[i].getName(), null, "content", false);
00354 nodeOperations.addChild(nodeOpe);
00355 }
00356 }
00357 }
00358 }