📄 jmxwrapper.java
字号:
"setState". Right?- Kedar> Although the JMX spec refers to the property jmx.invoke.getters, this is> an editing mistake (see> <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4949203>). The> Reference Implementation used to allow getters and setters to be invoked> as operations always. As of version 1.2 (included in JDK 5.0), this is> no longer allowed. The jmx.invoke.getters property is intended for> pre-1.2 code that depended on the old behaviour. It should not be used> in new code.>> Having said that, the problem with your code is probably that you are> not specifying the signature array correctly. Your call to invoke> should look something like this:>> mbsc.invoke(mbeanName,> "setState",> new Object[] {"new state"},> new String[] {"java.lang.String"});>> A much better alternative than coding explicit calls to getAttribute and> invoke is to use MBeanServerInvocationHandler> <http://java.sun.com/j2se/1.5.0/docs/api/javax/management/MBeanServerInvocationHandler.html>>>> For example:>> SimpleStandardMBean proxy = (SimpleStandardMBean)> MBeanServerInvocationHandler.newProxyInstance(mbsc,> mbeanName,>> SimpleStandardMBean.class,> false);>> String state = proxy.getState();> proxy.setState("new state");> proxy.reset();>> This will correctly use the getAttribute, setAttribute, and invoke> methods of MBeanServerConnection, respectively.>> Regards,> �amonn McManus JMX Spec Lead*/ if (log.isLoggable(Level.FINER)) log.finer("invoke with: '" + args); if (this.mbeanServer == null) return null; if (useJmx == 0) return null; if (args == null || args.length() < 1) return null; String callString = args; if (args.startsWith("/InvokeAction")) { callString = args.substring("/InvokeAction//".length()); } // check for actionName (method) int j = callString.indexOf("/action=", 1); // get action, between '/action=' and '?' String action = getAction(callString.substring(j + 1));// for (Iterator mbeanIt = mbeanMap.entrySet().iterator() ; mbeanIt.hasNext();) {// Object key = ((Entry)mbeanIt.next()).getKey();// Object element = mbeanMap.get(key);// if (log.isLoggable(Level.FINE)) log.trace(ME, "key: " + key + " element: '" + element);// } // scan for arguments, starting with '&' (i don't know who has introduced the double noting of action=...) int k = callString.indexOf("&"); // '/action=myNiceMethodName?action=myNiceMethodName&p1+java.lang.String=arg1&p2+java.lang.String=arg1' if (k == -1) k = callString.indexOf("?"); // '...action=myNiceMethodName?p1+java.lang.String=arg1&p2+java.lang.String=arg1' ArrayList s = new ArrayList(); Object[] params = getParams(s, callString.substring(k+1)); String[] signature = (String[]) s.toArray(new String[s.size()]); ObjectName objectName = null; Object returnObject = null; try { if (log.isLoggable(Level.FINE)) log.fine("get object: '" + callString.substring(0, j)); objectName = new ObjectName(callString.substring(0, j)); Set set = mbeanServer.queryNames(objectName, null); if(set.size() <= 0) { log.severe("Instance Not Found"); return returnObject; } if (log.isLoggable(Level.FINE)) log.fine("invoke: '" + action + "@" + objectName);// params = new Object[]{"arg1"};// signature = new String[]{"java.lang.String"}; returnObject = mbeanServer.invoke(objectName, action, params, signature); } catch (javax.management.ReflectionException e) { try { // If invoke() fails with access to operations method, we try with getter/setter access if (action.startsWith("get")) { action = action.substring(3); if (log.isLoggable(Level.FINE)) log.fine("invoke: '" + action + "@" + objectName); returnObject = mbeanServer.getAttribute(objectName, action); } else if (action.startsWith("set")) { action = action.substring(3); Object value = (params.length>0) ? params[0] : ""; Attribute attribute = new Attribute(action, value); if (log.isLoggable(Level.FINE)) log.fine("invoke: '" + attribute.toString() + "@" + objectName); mbeanServer.setAttribute(objectName, attribute); returnObject = ""; } else { if (log.isLoggable(Level.FINE)) log.fine("invoke: '" + action + "@" + objectName); returnObject = mbeanServer.getAttribute(objectName, action); } } catch (Throwable e2) { log.warning("args: '" + args + "' invoke: '" + action + "@" + objectName + " failed: " + e2.toString()); } } catch (Throwable e) { log.warning("args: '" + args + "' invoke: '" + action + "@" + objectName + " failed: " + e.toString()); e.printStackTrace(); } return returnObject; } /** * Unregisters the specified mbean from the mbean server. * @param objectName The object you got from registerMBean() of type ObjectName, * if null nothing happens */ public synchronized void unregisterMBean(ObjectName objectName)// throws XmlBlasterException { if (objectName == null) return; if (this.mbeanServer == null) return; if (useJmx == 0) return; if (log.isLoggable(Level.FINER)) log.finer("Unregister MBean '" + objectName.toString() + "'"); try { Object removed = this.mbeanMap.remove(objectName.toString()); this.mbeanServer.unregisterMBean(objectName); if (removed == null) log.severe("No JMX MBean instance of " + objectName.toString() + " removed"); } catch (Exception e) { log.severe("JMX unregistration problems: " + e.toString()); e.printStackTrace(); } } public void unregisterMBean(JmxMBeanHandle jmxMBeanHandle) { unregisterMBean(jmxMBeanHandle.getObjectInstance().getObjectName()); } /** * Starts XmlBlasterConnector on mbeanServer-Instance * This is a small embedded xmlBlaster server instance which is started. * You can than access this adaptor using the SWING GUI "java org.xmlBlaster.jmxgui.Main" */ public void startXmlBlasterConnector(MBeanServer mbeanServer) throws XmlBlasterException { try { if (log.isLoggable(Level.FINER)) log.finer("Registering embedded xmlBlasterConnector for JMX..."); int port = glob.getProperty().get("xmlBlaster/jmx/XmlBlasterAdaptor/port", 3424); ObjectName xmlBlasterConnector_name = new ObjectName("Adaptor:transport=xmlBlaster,port="+port); mbeanServer.createMBean("org.xmlBlaster.util.admin.extern.XmlBlasterConnector", xmlBlasterConnector_name, null); log.info("Registered JMX xmlBlaster adaptor on port " + port + ", try to start swing GUI 'org.xmlBlaster.jmxgui.Main'"); // Start the adaptor: try { mbeanServer.invoke(xmlBlasterConnector_name, "startInternal", new Object[] {this.mbeanServer}, new String[]{MBeanServer.class.getName()}); } catch (Exception ex) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_CONFIGURATION, ME, " could not register connector into MBean Server.", ex); } } catch (Exception e) { e.printStackTrace(); log.severe("Error when registering new Connector >> " + e.toString() ); throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_UNAVAILABLE, ME, " could not start embedded xmlBlasterConnector", e); } } public void shutdown() { if (this.mbeanServer == null) return; if (this.jmxPropertiesHandle != null) { unregisterMBean(this.jmxPropertiesHandle); this.jmxPropertiesHandle = null; } if (this.jmxLogLevelHandle != null) { unregisterMBean(this.jmxLogLevelHandle); this.jmxLogLevelHandle = null; } // TODO: Implement complete shutdown //javax.management.MBeanServerFactory.releaseMBeanServer(this.mbeanServer); } private final String getAction(final String s) { int i = s.indexOf("action="); if (i < 0) return null; int j = s.indexOf("?", i); if (j < 0) return s.substring("action=".length()); else return s.substring(i + 7, j); } private Object[] getParams(ArrayList sigs, String callArgs) { if (log.isLoggable(Level.FINE)) log.fine("getParams from: '" + callArgs); Map param = StringPairTokenizer.parseToStringStringPairs(callArgs, "&", "+"); if (log.isLoggable(Level.FINE)) log.fine("params: '" + param); ArrayList parms = new ArrayList(); for (int p = 1; p <= param.size(); p++) { String key = "p" + p; if (param.containsKey(key)) { String argument = (String) param.get(key); String value = argument.substring(argument.indexOf('=') + 1, argument.length()); String signature = argument.substring(0, argument.indexOf('=')); parms.add(value); sigs.add(signature); } } Object[] ret = new Object[parms.size()]; for (int i=0; i<ret.length; i++) { String sig = (String)sigs.get(i); String par = (String)parms.get(i); if (sig.equals("java.lang.Integer") || sig.equals("int")) ret[i] = new Integer(par); else if (sig.equals("java.lang.Long") || sig.equals("long")) ret[i] = new Long(par); else if (sig.equals("java.lang.Short") || sig.equals("short")) ret[i] = new Short(par); else if (sig.equals("java.lang.Boolean") || sig.equals("boolean")) ret[i] = new Boolean(par); else ret[i] = par; } return ret; } public boolean isRegistered(ContextNode contextNode) throws XmlBlasterException { if (contextNode == null) { throw new XmlBlasterException(this.glob, ErrorCode.USER_ILLEGALARGUMENT, "JmxWrapper.isRegistered", "The context node was null"); } try { ObjectName objectName = new ObjectName( getObjectNameLiteral(this.glob, contextNode)); if (this.mbeanServer == null) return false; return this.mbeanServer.isRegistered(objectName); } catch (MalformedObjectNameException ex) { String txt = "The context node '" + contextNode.getAbsoluteName() + "' was malformed"; throw new XmlBlasterException(this.glob, ErrorCode.USER_ILLEGALARGUMENT, "JmxWrapper.isRegistered", txt); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -