1 package net.sf.panoptes.component.jmx.model; 2 3 import java.io.IOException ; 4 import java.rmi.ConnectException ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 import java.util.Set ; 14 15 import javax.management.InstanceNotFoundException ; 16 import javax.management.IntrospectionException ; 17 import javax.management.MBeanServerConnection ; 18 import javax.management.Notification ; 19 import javax.management.NotificationListener ; 20 import javax.management.ObjectName ; 21 import javax.management.QueryExp ; 22 import javax.management.ReflectionException ; 23 24 import net.sf.panoptes.component.jmx.connector.MBeanServerConnector; 25 import net.sf.panoptes.model.node.GroupNode; 26 import net.sf.panoptes.model.node.Node; 27 import net.sf.panoptes.model.node.NodeDescriptor; 28 import net.sf.panoptes.model.node.NodeSupport; 29 import net.sf.panoptes.swing.Operation; 30 import net.sf.panoptes.swing.OperationFactory; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 55 public class MBeanServerNode 56 extends GroupNode 57 implements NotificationListener , MBeanServerNodeMBean { 58 59 private String id; 60 private String hostName; 61 private MBeanServerConnection serverConnection = null; 62 private HashMap mBeans = new HashMap (); 63 private Map domains = new HashMap (); 64 private MBeanServerConnector connector; 65 private Log log = LogFactory.getLog(getClass()); 66 private GroupNode viewGroup; 67 68 public static final String MBEANSERVERNODE_KEY = "serverNode"; 69 private Properties connectionProperties; 70 71 74 public MBeanServerNode(NodeSupport parent, MBeanServerConnector connector) { 75 super(parent); 76 this.connector = connector; 77 getConfigDescriptor().setIconName(Node.ICON_SERVER); 78 getContext().put(MBEANSERVERNODE_KEY, this); 79 viewGroup = new GroupNode(this, "Views", "Attached views"); 80 } 81 82 86 public MBeanServerConnector getConnector() { 87 return connector; 88 } 89 90 public void setProperties(Properties properties) { 91 this.connectionProperties = properties; 92 } 93 94 95 99 public void startListening() { 100 try { 101 serverConnection.addNotificationListener( 102 new ObjectName ("JMImplementation:type=MBeanServerDelegate"), 103 this, 104 null, 105 new Object ()); 106 } catch (Exception e) { 107 log.error("Unable add listener. Notifications will not be received.", e); 108 } 109 } 110 111 115 public void connect() throws Exception { 116 Operation connectOp = 117 OperationFactory.getFactory().createOperation("Connecting to JMX Server"); 118 connectOp.setIndefinite(true); 119 try { 120 connectOp.newTask("Establishing connection"); 121 setServer(connector.createConnection(connectionProperties)); 122 connectOp.newTask("Registeringing with MBeanDelegate"); 123 startListening(); 124 connectOp.newTask("Loading MBeans and Domains"); 125 initDomains(); 126 OperationFactory.getFactory().createStatusMessage("Connected to JMX Server"); 127 } finally { 128 connectOp.operationFinished(); 129 } 130 } 131 132 protected void setServer(MBeanServerConnection connection) { 133 this.serverConnection = connection; 134 } 135 136 141 public String getHostName() { 142 return hostName; 143 } 144 145 150 public String getId() { 151 return id; 152 } 153 154 160 public void setHostName(String host) { 161 this.hostName = host; 162 } 163 164 170 public void setId(String name) { 171 this.id = name; 172 } 173 174 179 public MBeanServerConnection getServerConnection() { 180 return serverConnection; 181 } 182 183 private void addPossibleDomain(ObjectName objectName) throws Exception { 184 DomainNode domain; 185 String domainName = objectName.getDomain(); 186 if (!domains.containsKey(domainName)) { 187 188 domain = new DomainNode(this, domainName, this); 190 191 domains.put(domainName, domain); 193 } 194 } 195 196 public void refresh() { 197 } 198 199 203 public NodeDescriptor getConfigDescriptor() { 204 return new NodeDescriptor( 205 id, 206 "Connection to " + getHostName() + " on host " + getHostName(), 207 Node.ICON_SERVER); 208 } 209 210 public void handleNotification(Notification n, Object handback) { 211 log.info("Notification received: " + n.toString()); 212 } 213 214 private void removeMBean(ObjectName objectName) { 215 mBeans.remove(objectName); 216 } 217 218 223 public List getChildren() { 224 List l = new ArrayList (); 225 l.addAll(super.getChildren()); 226 l.addAll(getDomains()); 227 l.add(viewGroup); 228 return l; 229 } 230 231 234 private void initDomains() { 235 try { 236 Set s = serverConnection.queryNames(new ObjectName ("*:*"), null); 237 Iterator it = s.iterator(); 238 int i = 0; 239 while (it.hasNext()) { 240 ObjectName objectName = (ObjectName ) it.next(); 241 addPossibleDomain(objectName); 242 } 243 } catch (Exception e) { 244 e.printStackTrace(); 245 } 246 } 247 248 public Collection getDomains() { 249 return domains.values(); 250 } 251 252 public int hashCode() { 253 return getId().hashCode(); 254 } 255 256 public String toString() { 257 return "(MBeanServerNode) host=" + getHostName() + ", id=" + getId(); 258 } 259 260 264 public void disconnect() throws Exception { 265 for (Iterator i = mBeans.keySet().iterator(); i.hasNext();) { 266 removeMBean((ObjectName ) i.next()); 267 } 268 getConnector().disconnect(serverConnection); 269 } 270 271 public Collection queryMBeans( 272 ObjectName name, 273 QueryExp query, 274 Node parent, 275 NodeFactory linkFactory) 276 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException { 277 try { 278 if (!name.isPattern() && query == null) { 279 ArrayList a = new ArrayList (); 280 a.add(linkFactory.createNode(parent, name)); 281 } 282 283 MBeanServerConnection conn = getServerConnection(); 284 Set s = conn.queryNames(name, query); 285 286 Set mbeanNodes = new HashSet (s.size()); 287 288 for (Iterator i2 = s.iterator(); i2.hasNext();) { 289 ObjectName on = (ObjectName ) i2.next(); 290 mbeanNodes.add(linkFactory.createNode(parent, on)); 291 } 292 Collection c; 293 return mbeanNodes; 294 } catch (ConnectException e) { 295 log.info("Server disconnected"); 296 try { 297 disconnect(); 298 } catch (Exception e1) { 299 } 300 throw e; 301 } 302 303 } 304 305 310 public Collection queryMBeans(ObjectName name, QueryExp query) 311 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException { 312 313 if (!name.isPattern() && query == null) { 314 ArrayList a = new ArrayList (); 315 a.add(getCachedMBean(name)); 316 } 317 318 MBeanServerConnection conn = getServerConnection(); 319 323 Set s = conn.queryNames(name, query); 324 325 Set mbeanNodes = new HashSet (s.size()); 326 327 for (Iterator i2 = s.iterator(); i2.hasNext();) { 328 ObjectName on = (ObjectName ) i2.next(); 329 mbeanNodes.add(getCachedMBean(on)); 330 } 331 Collection c; 332 return mbeanNodes; 333 } 334 335 public Set queryNames(ObjectName name, QueryExp query) throws IOException { 336 return serverConnection.queryNames(name, query); 337 } 338 339 344 public Collection queryMBeans(ObjectName name) 345 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException { 346 return queryMBeans(name, null); 347 } 348 349 353 public MBeanWrapper getMBean(ObjectName name) 354 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException { 355 return getCachedMBean(name); 356 } 357 358 private synchronized MBeanWrapper getCachedMBean(ObjectName name) 359 throws InstanceNotFoundException , IntrospectionException , ReflectionException , IOException { 360 if (!mBeans.containsKey(name)) { 361 mBeans.put(name, new MBeanWrapper(this, name)); 362 } 363 return (MBeanWrapper) mBeans.get(name); 364 } 365 366 } 367 | Popular Tags |