[jboss-cvs] JBossAS SVN: r75311 - branches/Branch_4_2/varia/src/main/org/jboss/ant.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 2 13:25:52 EDT 2008


Author: mmoyses
Date: 2008-07-02 13:25:52 -0400 (Wed, 02 Jul 2008)
New Revision: 75311

Modified:
   branches/Branch_4_2/varia/src/main/org/jboss/ant/JMX.java
Log:
JBAS-5704: added username and password attributes to the task to use with secured JMX invokers

Modified: branches/Branch_4_2/varia/src/main/org/jboss/ant/JMX.java
===================================================================
--- branches/Branch_4_2/varia/src/main/org/jboss/ant/JMX.java	2008-07-02 17:07:53 UTC (rev 75310)
+++ branches/Branch_4_2/varia/src/main/org/jboss/ant/JMX.java	2008-07-02 17:25:52 UTC (rev 75311)
@@ -26,11 +26,13 @@
 import java.util.ArrayList;
 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 org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
 import org.jboss.util.propertyeditor.PropertyEditors;
@@ -41,6 +43,7 @@
  * To use this plugin with Ant, place the jbossjmx-ant.jar together with the
  * jboss jars jboss-j2ee.jar and jboss-common-client.jar, and the sun jnet.jar in the
  * ant/lib directory you wish to use.
+ * If the JMX invoker is secured, set the username and password attributes in the task.
  *
  * Here is an example from an ant build file.
  *
@@ -73,6 +76,10 @@
 
    private String adapterName = "jmx/invoker/RMIAdaptor";
 
+   private String username;
+
+   private String password;
+
    private List ops = new ArrayList();
 
    private List editors = new ArrayList();
@@ -111,6 +118,28 @@
    }
 
    /**
+    * Use the <code>setUsername</code> method to set the username for 
+    * the JMX invoker (if it's secured).
+    * 
+    * @param username a <code>String</code> value
+    */
+   public void setUsername(String username)
+   {
+      this.username = username;
+   }
+
+   /**
+    * Use the <code>setPassword</code> method to set the password for 
+    * the JMX invoker (if it's secured).
+    * 
+    * @param password a <code>String</code> value
+    */
+   public void setPassword(String password)
+   {
+      this.password = password;
+   }
+
+   /**
     * Use the <code>addInvoke</code> method to add an <invoke> operation.
     * Include as attributes the target ObjectName and operation name.
     * Include as sub-elements parameters: see addParameter in the Invoke class.
@@ -145,7 +174,7 @@
     */
    public void addGetAttribute(Getter getter)
    {
-       ops.add(getter);
+      ops.add(getter);
    }
 
    /**
@@ -168,30 +197,36 @@
    public void execute() throws BuildException
    {
       final ClassLoader origCL = Thread.currentThread().getContextClassLoader();
-      try 
+      try
       {
          Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
          try
          {
             for (int i = 0; i < editors.size(); i++)
             {
-               ((PropertyEditorHolder)editors.get(i)).execute();
+               ((PropertyEditorHolder) editors.get(i)).execute();
             } // end of for ()
-   
-   
+
          }
          catch (Exception e)
          {
             e.printStackTrace();
             throw new BuildException("Could not register property editors: " + e);
          } // end of try-catch
-   
+
          try
          {
             Properties props = new Properties();
-            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
+            String factory = "org.jnp.interfaces.NamingContextFactory";
+            if (username != null && password != null)
+            {
+               factory = "org.jboss.security.jndi.JndiLoginInitialContextFactory";
+               props.put(Context.SECURITY_PRINCIPAL, username);
+               props.put(Context.SECURITY_CREDENTIALS, password);
+            }
+            props.put(Context.INITIAL_CONTEXT_FACTORY, factory);
             props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
-   
+
             if (serverURL == null || "".equals(serverURL))
             {
                props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
@@ -200,39 +235,39 @@
             {
                props.put(Context.PROVIDER_URL, serverURL);
             }
-            InitialContext ctx = new InitialContext(props);;
-   
+            InitialContext ctx = new InitialContext(props);
+
             // if adapter is null, the use the default
-            if (adapterName == null) {
+            if (adapterName == null)
+            {
                adapterName = "jmx/rmi/RMIAdaptor";//org.jboss.jmx.adaptor.rmi.RMIAdaptorService.DEFAULT_JNDI_NAME;
             }
-   
+
             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()));
+
+            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 = (Operation)ops.get(i);
+               Operation op = (Operation) ops.get(i);
                op.execute(server, this);
             } // end of for ()
-   
-   
+
          }
          catch (Exception e)
          {
             e.printStackTrace();
             throw new BuildException("problem: " + e);
          } // end of try-catch
-      } 
-      finally 
+      }
+      finally
       {
          Thread.currentThread().setContextClassLoader(origCL);
       }
@@ -254,10 +289,10 @@
     * managed operation.
     *
     */
-   public static class Invoke
-      implements Operation
+   public static class Invoke implements Operation
    {
       private ObjectName target;
+
       private String property;
 
       private String operation;
@@ -272,7 +307,7 @@
        */
       public void setProperty(String property)
       {
-          this.property = property;
+         this.property = property;
       }
 
       /**
@@ -316,15 +351,15 @@
          int pos = 0;
          for (int i = 0; i < params.size(); i++)
          {
-            Param p = (Param)params.get(i);
+            Param p = (Param) params.get(i);
             args[pos] = p.getValue();
             types[pos] = p.getType();
             pos++;
          } // end of for ()
          Object result = server.invoke(target, operation, args, types);
-         if( (property != null) && (result != null) )
+         if ((property != null) && (result != null))
          {
-             parent.getProject().setProperty(property,result.toString());
+            parent.getProject().setProperty(property, result.toString());
          }
       }
    }
@@ -334,8 +369,7 @@
     * value on an mbean.
     *
     */
-   public static class Setter
-      implements Operation
+   public static class Setter implements Operation
    {
       private ObjectName target;
 
@@ -387,56 +421,55 @@
     * value of an mbean.
     *
     */
-   public static class Getter
-           implements Operation
+   public static class Getter implements Operation
    {
-       private ObjectName target;
+      private ObjectName target;
 
-       private String attribute;
+      private String attribute;
 
-       private String property;
+      private String property;
 
-       /**
-        * The <code>setTarget</code> method sets the ObjectName
-        * of the target mbean.
-        *
-        * @param target an <code>ObjectName</code> value
-        */
-       public void setTarget(ObjectName target)
-       {
-           this.target = target;
-       }
+      /**
+       * The <code>setTarget</code> method sets the ObjectName
+       * of the target mbean.
+       *
+       * @param target an <code>ObjectName</code> value
+       */
+      public void setTarget(ObjectName target)
+      {
+         this.target = target;
+      }
 
-       /**
-        * The <code>setAttribute</code> method specifies the attribute to be
-        * retrieved.
-        *
-        * @param attribute a <code>String</code> value
-        */
-       public void setAttribute(String attribute)
-       {
-           this.attribute = attribute;
-       }
+      /**
+       * The <code>setAttribute</code> method specifies the attribute to be
+       * retrieved.
+       *
+       * @param attribute a <code>String</code> value
+       */
+      public void setAttribute(String attribute)
+      {
+         this.attribute = attribute;
+      }
 
-       /**
-        * The <code>setProperty</code> method specifies the name of the property
-        * to be set with the attribute value.
-        *
-        * @param property a <code>String</code> value
-        */
-       public void setProperty(String property)
-       {
-           this.property = property;
-       }
+      /**
+       * The <code>setProperty</code> method specifies the name of the property
+       * to be set with the attribute value.
+       *
+       * @param property a <code>String</code> value
+       */
+      public void setProperty(String property)
+      {
+         this.property = property;
+      }
 
-       public void execute(MBeanServerConnection server, Task parent) throws Exception
-       {
-           Object result = server.getAttribute(target,attribute);
-           if( (property != null) && (result != null) )
-           {
-               parent.getProject().setProperty(property,result.toString());
-           }
-       }
+      public void execute(MBeanServerConnection server, Task parent) throws Exception
+      {
+         Object result = server.getAttribute(target, attribute);
+         if ((property != null) && (result != null))
+         {
+            parent.getProject().setProperty(property, result.toString());
+         }
+      }
    }
 
    /**
@@ -447,6 +480,7 @@
    public static class Param
    {
       private String arg;
+
       private String type;
 
       /**
@@ -505,6 +539,7 @@
    public static class PropertyEditorHolder
    {
       private String type;
+
       private String editor;
 
       /**
@@ -584,5 +619,5 @@
          PropertyEditorManager.registerEditor(getTypeClass(), getEditorClass());
       }
    }
-   
+
 }// JMX




More information about the jboss-cvs-commits mailing list