[jboss-cvs] JBossAS SVN: r104386 - trunk/varia/src/main/java/org/jboss/ant.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sat May 1 07:18:54 EDT 2010
Author: smarlow at redhat.com
Date: 2010-05-01 07:18:53 -0400 (Sat, 01 May 2010)
New Revision: 104386
Modified:
trunk/varia/src/main/java/org/jboss/ant/JMX.java
Log:
JBAS-7826 Port the org.jboss.varia JMX ant task to act as a JSR-160 client
Modified: trunk/varia/src/main/java/org/jboss/ant/JMX.java
===================================================================
--- trunk/varia/src/main/java/org/jboss/ant/JMX.java 2010-05-01 11:00:15 UTC (rev 104385)
+++ trunk/varia/src/main/java/org/jboss/ant/JMX.java 2010-05-01 11:18:53 UTC (rev 104386)
@@ -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 @@
* </invoke>
* </jmx>
*
+ * Another example using security and setting the verbose flag:
*
+ * <project name="jmxtest" default="jmx" basedir=".">
+ * <description>
+ * test jmx task
+ * </description>
+ * <target name="jmx">
+ * <taskdef name="jmx" classname="org.jboss.ant.JMX"/>
+ * <jmx verbose="true" username="sysadmin" password="password">
+ * <invoke target="jboss.system:type=ServerInfo"
+ * operation="listThreadDump">
+ * </invoke>
+ * </jmx>
+ * </target>
+ *
* Created: Tue Jun 11 20:17:44 2002
*
* @author <a href="mailto:d_jencks at users.sourceforge.net">David Jencks</a>
@@ -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<PropertyEditorHolder> editors = new ArrayList<PropertyEditorHolder>();
+ private boolean verbose;
+
/**
* Creates a new <code>JMX</code> 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 <code>setServerURL</code> 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 <code>String</code> 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;
}
}
More information about the jboss-cvs-commits
mailing list