require 'java' # include java.lang.Object as JObject to # prevent it being confused with Ruby Object include_class "java.lang.Object" do |p,n| "J" + n end # include java.lang.Object as JString to # prevent it being confused with Ruby String include_class "java.lang.String" do |p,n| "J" + n end class MBeanServer include Enumerable def initialize(mbeanServer) @mbeanServer = mbeanServer end def each() mbeans = allMBeansInServer(@mbeanServer).toArray mbeans.each{|mbean| yield MBeanHandle.new(@mbeanServer, mbean.getObjectName()) } end def listMBeans() collect{|mbean| mbean.objectName.to_s + "\n" } end def mbeanAt(pos) mbeans = allMBeansInServer(@mbeanServer).toArray # TODO: add self not the mbeanServer objName = mbeans[pos].getObjectName MBeanHandle.new(@mbeanServer, objName ) end end class MBeanHandle def initialize(mbeanServer, objName) @objName = objName @mbeanServer = mbeanServer @mbeanInfo = @mbeanServer.getMBeanInfo(objName) end # TODO: maybe add operations and attributes as # methods to the current object def operations retVal = [] mbeanOps = @mbeanInfo.getOperations mbeanOps.each{|obj| retVal << obj} #retVal.each{|currOp| # define_method("x"+currOp.to_s){ # p "XX" # } #} retVal end def attrs retVal = [] mbeanAttrs = @mbeanInfo.getAttributes mbeanAttrs.each{|obj| retVal << obj} retVal end def listOperations ops = operations() ops.collect{|op| temp = op.getReturnType + " " + op.getName temp params = op.getSignature sig = "" params.each{|arg| sig += "XXX"} temp = temp + "("+sig + ")" + "\n" } end def listAttrs atts = attrs() atts.collect{|op| temp = op.getName + ":" + op.getType + "\n" } end def [](sel) @mbeanServer.getAttribute(@objName, sel.to_s) end def method_missing(*syms) if(syms.size > 0) @mbeanServer.invoke(@objName, syms[0].to_s, JObject[].new(0), JString[].new(0)) end end def objectName() @objName end def toMBean() @mbeanInfo end def to_s super.to_s + "[" + @objName.toString + "]" end end def allMBeansInCurrentVM() mbServer = platformMBeanServer() mbServer.queryMBeans(nil, nil) end def platformMBeanServer() include_class "java.lang.management.ManagementFactory" ManagementFactory.getPlatformMBeanServer() end def showAllMBeansInCurrentVm() txt = "" mb = allMBeansInCurrentVM() mb.each{|n| txt += n.getObjectName().toString() + "\n"} txt end def allMBeansInServer(server) server.queryMBeans(nil, nil) end def mbeansLs(*syms) if syms.length > 1 server = syms[1] mb = allMBeansInServer(server) else mb = allMBeansInCurrentVM() end retVal = [] mb.each{|n| retVal << n.getObjectName().toString() + "\n" } retVal end # Returns an MBeanServer object connected to the JMXServiceURL via JMXRemote and RMI def remote_connect(serviceUrlAsString) include_class "javax.management.remote.JMXServiceURL" include_class "javax.management.remote.JMXConnectorFactory" include_class "javax.management.remote.JMXConnector" # Sample for Java5 Remote connection "service:jmx:rmi:///jndi/rmi://localhost:8004/jmxrmi" url = JMXServiceURL.new(serviceUrlAsString) conn = JMXConnectorFactory.connect(url, nil) conn.getMBeanServerConnection end $lsHandlers__[:mbeans] = :mbeansLs unless $lsHandlers==nil