Index: varia/src/main/java/org/jboss/ant/JMX.java =================================================================== --- varia/src/main/java/org/jboss/ant/JMX.java (revision 103976) +++ varia/src/main/java/org/jboss/ant/JMX.java (working copy) @@ -24,14 +24,15 @@ import java.beans.PropertyEditor; import java.beans.PropertyEditorManager; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.Properties; import javax.management.Attribute; import javax.management.MBeanServerConnection; import javax.management.ObjectName; -import javax.naming.Context; -import javax.naming.InitialContext; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; @@ -63,7 +64,21 @@ * * * + * Another example using security and setting the verbose flag: * + * + * + * test jmx task + * + * + * + * + * + * + * + * + * * Created: Tue Jun 11 20:17:44 2002 * * @author David Jencks @@ -72,9 +87,8 @@ */ public class JMX extends Task { - private String serverURL; + private String serverURL = "service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi"; - private String adapterName = "jmx/invoker/RMIAdaptor"; private String username; @@ -84,6 +98,8 @@ private List editors = new ArrayList(); + private boolean verbose; + /** * Creates a new JMX instance. * Provides a default adapterName for the current server, so you only need to set it to @@ -95,6 +111,14 @@ { } + public boolean isVerbose() { + return verbose; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + /** * Use the setServerURL method to set the URL of the server * you wish to connect to. @@ -103,6 +127,10 @@ */ public void setServerURL(String serverURL) { + if(verbose) + { + log("changing server url from " + this.serverURL + " to " + serverURL); + } this.serverURL = serverURL; } @@ -111,10 +139,11 @@ * adapter mbean is bound under in jndi. * * @param adapterName a String value + * @deprecated and now ignored */ public void setAdapterName(String adapterName) { - this.adapterName = adapterName; + log("ignoring adapter name (" + adapterName+") as its no longer used."); } /** @@ -125,6 +154,11 @@ */ public void setUsername(String username) { + if(verbose) + { + log("will perform operation as user " + username); + } + this.username = username; } @@ -136,6 +170,11 @@ */ public void setPassword(String password) { + if(verbose) + { + log("password has been set"); + } + this.password = password; } @@ -148,6 +187,21 @@ */ public void addInvoke(Invoke invoke) { + if(verbose) + { + String operation = null; + String name = null; + if(invoke != null && invoke.target != null) + { + name = invoke.target.getCanonicalName(); + } + if(invoke != null && invoke.operation != null) + { + operation = invoke.operation; + } + log("invoke added for "+ operation+" on " + name); + } + ops.add(invoke); } @@ -161,6 +215,15 @@ */ public void addSetAttribute(Setter setter) { + if(verbose) + { + String name = null; + if(setter != null && setter.target != null) + { + name = setter.target.getCanonicalName(); + } + log("set attribute " + setter.attribute + " on " + name); + } ops.add(setter); } @@ -174,6 +237,17 @@ */ public void addGetAttribute(Getter getter) { + if(verbose) + { + String name = null; + if(getter != null && getter.target != null) + { + name = getter.target.getCanonicalName(); + } + + log("get attribute " + getter.attribute + " on " + name); + } + ops.add(getter); } @@ -186,6 +260,10 @@ */ public void addPropertyEditor(PropertyEditorHolder peh) { + if(verbose) + { + log("use property editor "+peh.getEditor()); + } editors.add(peh); } @@ -196,6 +274,11 @@ */ public void execute() throws BuildException { + if(verbose) + { + log("started execute"); + } + final ClassLoader origCL = Thread.currentThread().getContextClassLoader(); try { @@ -204,6 +287,11 @@ { for (int i = 0; i < editors.size(); i++) { + if(verbose) + { + log("execute editor " + editors.get(i).getEditor()); + } + editors.get(i).execute(); } // end of for () @@ -216,48 +304,45 @@ try { - Properties props = new Properties(); - String factory = "org.jnp.interfaces.NamingContextFactory"; + HashMap env = new HashMap(); if (username != null && password != null) { - factory = "org.jboss.security.jndi.JndiLoginInitialContextFactory"; - props.put(Context.SECURITY_PRINCIPAL, username); - props.put(Context.SECURITY_CREDENTIALS, password); + if (verbose ) + { + log("will connect with username=" + username); + } + String[] creds = new String[2]; + creds[0] = username; + creds[1] = password; + env.put(JMXConnector.CREDENTIALS, creds); } - props.put(Context.INITIAL_CONTEXT_FACTORY, factory); - props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); + + JMXServiceURL url = new JMXServiceURL(serverURL); - if (serverURL == null || "".equals(serverURL)) + if (verbose ) { - props.put(Context.PROVIDER_URL, "jnp://localhost:1099"); + log("will connect with JMXServiceURL = "+url); } - else - { - props.put(Context.PROVIDER_URL, serverURL); - } - InitialContext ctx = new InitialContext(props); - // if adapter is null, the use the default - if (adapterName == null) + JMXConnector jmxc = JMXConnectorFactory.connect(url, env); + MBeanServerConnection server = jmxc.getMBeanServerConnection(); + if(verbose) { - adapterName = "jmx/rmi/RMIAdaptor";//org.jboss.jmx.adaptor.rmi.RMIAdaptorService.DEFAULT_JNDI_NAME; + log("connected to server"); } - Object obj = ctx.lookup(adapterName); - ctx.close(); - - if (!(obj instanceof MBeanServerConnection)) - { - throw new ClassCastException("Object not of type: MBeanServerConnection, but: " - + (obj == null ? "not found" : obj.getClass().getName())); - } - - MBeanServerConnection server = (MBeanServerConnection) obj; - for (int i = 0; i < ops.size(); i++) { Operation op = ops.get(i); - op.execute(server, this); + if(verbose) + { + log("execute operation " + op.getLogInformation()); + } + Object result = op.execute(server, this); + if( verbose && result != null) + { + log(result.toString()); + } } // end of for () } @@ -271,7 +356,10 @@ { Thread.currentThread().setContextClassLoader(origCL); } - + if(verbose) + { + log("stopped execute"); + } } /** @@ -281,7 +369,8 @@ */ public static interface Operation { - void execute(MBeanServerConnection server, Task parent) throws Exception; + Object execute(MBeanServerConnection server, Task parent) throws Exception; + String getLogInformation(); } /** @@ -343,8 +432,13 @@ params.add(param); } - public void execute(MBeanServerConnection server, Task parent) throws Exception + public String getLogInformation() { + return "invoking " + operation; + } + + public Object execute(MBeanServerConnection server, Task parent) throws Exception + { int paramCount = params.size(); Object[] args = new Object[paramCount]; String[] types = new String[paramCount]; @@ -361,6 +455,7 @@ { parent.getProject().setProperty(property, result.toString()); } + return result; } } @@ -409,10 +504,16 @@ this.value = value; } - public void execute(MBeanServerConnection server, Task parent) throws Exception + public String getLogInformation() { + return "setting " + target.getCanonicalName() + ":" + attribute + " to " + value.getArg(); + } + + public Object execute(MBeanServerConnection server, Task parent) throws Exception + { Attribute att = new Attribute(attribute, value.getValue()); server.setAttribute(target, att); + return null; } } @@ -462,13 +563,19 @@ this.property = property; } - public void execute(MBeanServerConnection server, Task parent) throws Exception + public String getLogInformation() { + return "getting " + target.getCanonicalName() + ":" + attribute; + } + + public Object execute(MBeanServerConnection server, Task parent) throws Exception + { Object result = server.getAttribute(target, attribute); if ((property != null) && (result != null)) { parent.getProject().setProperty(property, result.toString()); } + return result; } }