1 7 package net.sf.panoptes.component.jmx.model; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.List ; 12 13 import javax.management.ObjectName ; 14 import javax.management.QueryExp ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import net.sf.panoptes.model.node.DefaultNodeDescriber; 20 import net.sf.panoptes.model.node.Node; 21 import net.sf.panoptes.model.node.NodeDescriber; 22 import net.sf.panoptes.model.node.NodeDescriptor; 23 import net.sf.panoptes.model.node.NodeSupport; 24 25 35 public class MBeanQuery extends NodeSupport { 36 37 40 private static final long REFRESH_RATE = 1000; 41 private long cacheExpire = 0; 42 43 private ObjectName objectName = null; 44 private QueryExp queryExp = null; 45 private Log log = LogFactory.getLog(getClass()); 46 private List mbeanCache = null; 47 48 51 private static int nextId = 1; 52 53 private NodeDescriber nodeDescriber = new DefaultNodeDescriber(); 54 private NodeFactory childFactory = new DefaultSymLinkFactory(); 55 56 public MBeanQuery(NodeSupport parent) { 57 super(parent); 58 } 59 60 public void setQuery(ObjectName objectName, QueryExp queryExp) { 61 this.objectName = objectName; 62 this.queryExp = queryExp; 63 setConfigDescriptor( 64 new NodeDescriptor( 65 objectName.toString(), 66 "Group of components in query " + objectName.toString(), 67 Node.ICON_QUERY_FOLDER)); 68 } 69 70 73 public void setChildFactoryClass(Class factoryClass) 74 throws InstantiationException , IllegalAccessException { 75 Object temp = factoryClass.newInstance(); 76 if (!(temp instanceof NodeFactory)) 77 throw new UnsupportedOperationException ( 78 "Given class " + factoryClass + " is not a SymLinkFactory"); 79 childFactory = (NodeFactory) temp; 80 } 81 82 public void setChildDescriber(NodeDescriber describer) { 83 this.nodeDescriber = describer; 84 } 85 86 public void init() { 87 88 } 89 90 public List getChildren() { 91 return getMBeans(); 92 } 93 94 public List getMBeans() { 95 if (System.currentTimeMillis() > cacheExpire) { 96 mbeanCache = null; 97 } 98 if (mbeanCache == null) { 99 mbeanCache = new ArrayList (); 100 synchronized (this) { 101 try { 102 Collection mbeansFromServer; 103 if (getContext().get(MBeanServerNode.MBEANSERVERNODE_KEY) == null) 104 mbeansFromServer = 105 ServerRegistry.getInstance().queryMBeans( 106 objectName, 107 queryExp, 108 this, 109 childFactory); 110 else { 111 mbeansFromServer = 112 ( 113 (MBeanServerNode) getContext().get( 114 MBeanServerNode.MBEANSERVERNODE_KEY)).queryMBeans( 115 objectName, 116 null, 117 this, 118 childFactory); 119 } 120 mbeanCache.addAll(mbeansFromServer); 121 122 } catch (Exception e) { 123 log.error("Unable to execute query", e); 124 mbeanCache = new ArrayList (); 125 } 126 } 127 cacheExpire = System.currentTimeMillis() + REFRESH_RATE; 128 } 129 return mbeanCache; 130 } 131 132 public int getChildCount() { 133 return getMBeans().size(); 134 } 135 136 139 public ObjectName getObjectName() { 140 return objectName; 141 } 142 143 public QueryExp getQueryExp() { 144 return queryExp; 145 } 146 147 public boolean mightHaveChildren() { 148 return true; 149 } 150 151 156 public void refresh() { 157 mbeanCache = null; 158 getMBeans(); 159 } 160 161 public Object getBean() { 162 return this; 163 } 164 165 } 166 | Popular Tags |