servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r1137563 - in /servicemix/smx3/trunk: core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/ core/servicemix-core/src/test/java/org/apache/servi...
Date Mon, 20 Jun 2011 09:32:40 GMT
Author: gertv
Date: Mon Jun 20 09:32:39 2011
New Revision: 1137563

URL: http://svn.apache.org/viewvc?rev=1137563&view=rev
Log:
SM-2097: Ant tasks should allow interacting with ServiceMix running in WebSphere

Modified:
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ManagementContext.java
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/JbiTask.java
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/ManagementContextTest.java
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/task/JbiTaskTest.java
    servicemix/smx3/trunk/distributions/apache-servicemix/src/main/release/ant/servicemix-ant-task.xml

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ManagementContext.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ManagementContext.java?rev=1137563&r1=1137562&r2=1137563&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ManagementContext.java
(original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/ManagementContext.java
Mon Jun 20 09:32:39 2011
@@ -561,6 +561,24 @@ public class ManagementContext extends B
         return result;
     }
 
+    /**
+     * Retrieve an ObjectName instance that can be used for querying system service mbean
instances
+     *
+     * @param domainName
+     * @param containerName
+     * @param interfaceType
+     * @return
+     */
+    public static ObjectName getSystemObjectNameQuery(String domainName, String containerName,
Class interfaceType) {
+        ObjectName result = null;
+        try {
+            result = new ObjectName(String.format("%s,*", getSystemObjectName(domainName,
containerName, interfaceType)));
+        } catch (MalformedObjectNameException e) {
+            LOG.error("Failed to build object name query: " + e.getMessage(), e);
+        }
+        return result;
+    }
+
     public static String getSystemServiceName(Class interfaceType) {
         String name = interfaceType.getName();
         name = name.substring(name.lastIndexOf('.') + 1);

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/JbiTask.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/JbiTask.java?rev=1137563&r1=1137562&r2=1137563&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/JbiTask.java
(original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/management/task/JbiTask.java
Mon Jun 20 09:32:39 2011
@@ -18,10 +18,10 @@ package org.apache.servicemix.jbi.manage
 
 import java.io.IOException;
 import java.net.MalformedURLException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 import javax.management.MBeanServerInvocationHandler;
+import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 import javax.management.remote.JMXConnector;
 import javax.management.remote.JMXConnectorFactory;
@@ -57,6 +57,10 @@ public abstract class JbiTask extends Ta
 
     private String password;
 
+    private String environment;
+
+    private String serviceUrl;
+
     private boolean failOnError = true;
 
     private JMXConnector jmxConnector;
@@ -67,9 +71,13 @@ public abstract class JbiTask extends Ta
      * @return the url
      */
     public JMXServiceURL getServiceURL() throws MalformedURLException {
-        JMXServiceURL url = null;
-        url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + jndiPath);
-        return url;
+        if (serviceUrl == null || serviceUrl.trim().length() < 1) {
+            JMXServiceURL url = null;
+            url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port
+ jndiPath);
+            return url;
+        } else {
+            return new JMXServiceURL(serviceUrl);
+        }
     }
 
     /**
@@ -80,10 +88,26 @@ public abstract class JbiTask extends Ta
      * @throws IOException
      */
     public JMXConnector getJMXConnector(JMXServiceURL url) throws IOException {
+        log("Establishing connection to " + url, Project.MSG_DEBUG);
+        return JMXConnectorFactory.connect(url, getEnvironmentMap());
+    }
+
+    protected Map<String, Object> getEnvironmentMap() {
         String[] credentials = new String[] {getUsername(), getPassword() };
-        Map<String, Object> environment = new HashMap<String, Object>();
-        environment.put(JMXConnector.CREDENTIALS, credentials);
-        return JMXConnectorFactory.connect(url, environment);
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put(JMXConnector.CREDENTIALS, credentials);
+
+        if (environment != null && environment.trim().length() > 0) {
+            for (String entry : environment.split(",")) {
+                final String[] info = entry.trim().split("=");
+                final String value = info[1].trim();
+                final String key = info[0].trim();
+                log(String.format("Setting environment variable %s: %s", key, value), Project.MSG_DEBUG);
+                map.put(key, value);
+            }
+        }
+
+        return map;
     }
 
     /**
@@ -110,14 +134,21 @@ public abstract class JbiTask extends Ta
     }
 
     /**
-     * Get a servicemix internal system management instance, from it's class
-     * name
+     * Get a servicemix internal system management instance, from it's class name.
      * 
      * @param systemClass
      * @return the object name
      */
-    protected ObjectName getObjectName(Class systemClass) {
-        return ManagementContext.getSystemObjectName(jmxDomainName, containerName, systemClass);
+    protected ObjectName getObjectName(Class systemClass) throws IOException, MalformedObjectNameException
{
+        ObjectName query = ManagementContext.getSystemObjectNameQuery(jmxDomainName, containerName,
systemClass);
+
+        Set<ObjectName> names = jmxConnector.getMBeanServerConnection().queryNames(query,
null);
+
+        if (names.size() == 1) {
+            return names.iterator().next();
+        } else {
+            throw new BuildException(String.format("Expected one instance, but found %s instances
of %s", names.size(), systemClass));
+        }
     }
 
     /**
@@ -126,7 +157,7 @@ public abstract class JbiTask extends Ta
      * @return the main administration service MBean
      * @throws IOException
      */
-    public AdminCommandsServiceMBean getAdminCommandsService() throws IOException {
+    public AdminCommandsServiceMBean getAdminCommandsService() throws IOException, MalformedObjectNameException
{
         ObjectName objectName = getObjectName(AdminCommandsServiceMBean.class);
 
         return (AdminCommandsServiceMBean) MBeanServerInvocationHandler.newProxyInstance(jmxConnector.getMBeanServerConnection(),
@@ -253,6 +284,19 @@ public abstract class JbiTask extends Ta
         this.username = username;
     }
 
+    public String getServiceUrl() {
+        return serviceUrl;
+    }
+
+    /**
+     * Configure the JMX service URL - if this property is set, the host/port/path properties
are ignored.
+     *
+     * @param serviceUrl
+     */
+    public void setServiceUrl(String serviceUrl) {
+        this.serviceUrl = serviceUrl;
+    }
+
     /**
      * @return Returns the failOnError.
      */
@@ -268,6 +312,14 @@ public abstract class JbiTask extends Ta
         this.failOnError = failOnError;
     }
 
+    public String getEnvironment() {
+        return environment;
+    }
+
+    public void setEnvironment(String environment) {
+        this.environment = environment;
+    }
+
     /**
      * execute the task
      * 
@@ -301,5 +353,4 @@ public abstract class JbiTask extends Ta
     }
 
     protected abstract void doExecute(AdminCommandsServiceMBean acs) throws Exception;
-
 }
\ No newline at end of file

Modified: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/ManagementContextTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/ManagementContextTest.java?rev=1137563&r1=1137562&r2=1137563&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/ManagementContextTest.java
(original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/ManagementContextTest.java
Mon Jun 20 09:32:39 2011
@@ -16,10 +16,12 @@
  */
 package org.apache.servicemix.jbi.management;
 
+import javax.jbi.management.AdminServiceMBean;
 import javax.jbi.management.LifeCycleMBean;
 import javax.management.MBeanServerConnection;
 import javax.management.MBeanServerDelegateMBean;
 import javax.management.MBeanServerInvocationHandler;
+import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 import javax.management.remote.JMXConnector;
 import javax.management.remote.JMXConnectorFactory;
@@ -110,4 +112,9 @@ public class ManagementContextTest exten
         assertEquals(LifeCycleMBean.STOPPED, echo.getCurrentState());
     }
 
+    public void testGetSystemObjectNameQuery() throws MalformedObjectNameException {
+        assertEquals(new ObjectName("org.apache.servicemix:ContainerName=ServiceMix,Type=SystemService,Name=AdminService,*"),
+                     ManagementContext.getSystemObjectNameQuery("org.apache.servicemix",
"ServiceMix", AdminServiceMBean.class));
+    }
+
 }

Modified: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/task/JbiTaskTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/task/JbiTaskTest.java?rev=1137563&r1=1137562&r2=1137563&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/task/JbiTaskTest.java
(original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/management/task/JbiTaskTest.java
Mon Jun 20 09:32:39 2011
@@ -16,17 +16,22 @@
  */
 package org.apache.servicemix.jbi.management.task;
 
+import java.util.Map;
+import javax.management.remote.JMXConnector;
+
 import org.apache.servicemix.jbi.framework.AdminCommandsServiceMBean;
 import org.apache.tools.ant.Project;
 
-
 /**
  *
  * JbiTaskTest
  * @version $Revision$
  */
 public class JbiTaskTest extends JbiTaskSupport {
-   
+
+    private static final String USERNAME = "user";
+    private static final String PASSWORD = "passw0rd";
+
     private JbiTask jbiTask;
     
     /*
@@ -55,4 +60,47 @@ public class JbiTaskTest extends JbiTask
         AdminCommandsServiceMBean mbean = jbiTask.getAdminCommandsService();
         assertNotNull(mbean);
     }
-}
+
+    public void testGetEnvironmentMapOnlyCredentials() throws Exception {
+        jbiTask.setUsername(USERNAME);
+        jbiTask.setPassword(PASSWORD);
+
+        // support null ...
+        jbiTask.setEnvironment(null);
+        assertOnlyCredentialsInMap(jbiTask.getEnvironmentMap());
+
+        // ... as well as an empty string
+        jbiTask.setEnvironment("   ");
+        assertOnlyCredentialsInMap(jbiTask.getEnvironmentMap());
+    }
+
+    public void testGetEnvironmentMapExtraInformationAdded() throws Exception {
+        jbiTask.setUsername(USERNAME);
+        jbiTask.setPassword(PASSWORD);
+
+        // one extra value
+        jbiTask.setEnvironment("key=value");
+        Map<String, Object> map = jbiTask.getEnvironmentMap();
+        assertCredentialsInMap(map);
+        assertEquals("Extra value should be in map", "value", map.get("key"));
+
+        // two extra values
+        jbiTask.setEnvironment("key=value, another_key=another_value");
+        map = jbiTask.getEnvironmentMap();
+        assertCredentialsInMap(map);
+        assertEquals("Extra value should be in map", "value", map.get("key"));
+        assertEquals("Extra value should be in map", "another_value", map.get("another_key"));
+    }
+
+    private void assertOnlyCredentialsInMap(Map<String, Object> map) {
+        assertEquals("Map contains 1 entry", 1, map.size());
+        assertCredentialsInMap(map);
+    }
+
+    private void assertCredentialsInMap(Map<String, Object> map) {
+        String[] credentials = (String[]) map.get(JMXConnector.CREDENTIALS);
+        assertNotNull("Credentials should be in environment map", credentials);
+        assertEquals(USERNAME, credentials[0]);
+        assertEquals(PASSWORD, credentials[1]);
+    }
+}
\ No newline at end of file

Modified: servicemix/smx3/trunk/distributions/apache-servicemix/src/main/release/ant/servicemix-ant-task.xml
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/distributions/apache-servicemix/src/main/release/ant/servicemix-ant-task.xml?rev=1137563&r1=1137562&r2=1137563&view=diff
==============================================================================
--- servicemix/smx3/trunk/distributions/apache-servicemix/src/main/release/ant/servicemix-ant-task.xml
(original)
+++ servicemix/smx3/trunk/distributions/apache-servicemix/src/main/release/ant/servicemix-ant-task.xml
Mon Jun 20 09:32:39 2011
@@ -53,6 +53,12 @@
     
     <!-- default port. -->
     <property name="sm.port" value="1099" />
+
+    <!-- JMX connection url - when this value is provided, sm.host and sm.port will be
ignored -->
+    <property name="sm.url" value="" />
+
+    <!-- JMX connection environment settings -->
+    <property name="sm.environment" value="" />
     
     
     <!-- Command Line Argumants can override the following values. -->
@@ -81,6 +87,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              file="${sm.install.file}"/>  
       </target>
      
@@ -96,6 +104,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.component.name}"
          />
          
@@ -114,6 +124,8 @@
             password="${sm.password}"
             host="${sm.host}"
         	port="${sm.port}"
+            serviceUrl="${sm.url}"
+            environment="${sm.environment}"
             file="${sm.install.file}"
         />
      </target>
@@ -131,6 +143,8 @@
             password="${sm.password}"
             host="${sm.host}"
          	port="${sm.port}"
+            serviceUrl="${sm.url}"
+            environment="${sm.environment}"
             name="${sm.shared.library.name}"    
         />
          
@@ -149,6 +163,8 @@
             password="${sm.password}"
             host="${sm.host}"
          	port="${sm.port}"
+            serviceUrl="${sm.url}"
+            environment="${sm.environment}"
             name="${sm.component.name}"        
         />
      
@@ -167,6 +183,8 @@
             password="${sm.password}"
             host="${sm.host}"
          	port="${sm.port}"
+            serviceUrl="${sm.url}"
+            environment="${sm.environment}"
             name="${sm.component.name}"            
          />
           
@@ -185,6 +203,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.component.name}"                
          />
      </target>
@@ -202,6 +222,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              file="${sm.deploy.file}"                    
          />
      </target>
@@ -219,6 +241,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.service.assembly.name}"                        
          />
      
@@ -237,6 +261,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.service.assembly.name}"                            
          />
          
@@ -255,6 +281,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.service.assembly.name}"                                
          />     
      </target>
@@ -272,6 +300,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              name="${sm.service.assembly.name}"                                    
          />
      
@@ -292,6 +322,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              state="${sm.state}"
              serviceAssemblyName="${sm.service.assembly.name}"
              sharedLibraryName="${sm.shared.library.name}"
@@ -314,6 +346,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              state="${sm.state}"
              sharedLibraryName="${sm.shared.library.name}"
              serviceAssemblyName="${sm.service.assembly.name}"
@@ -335,6 +369,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              componentName="${sm.component.name}"
              sharedLibraryName="${sm.shared.library.name}"
       />
@@ -355,6 +391,8 @@
              password="${sm.password}"
              host="${sm.host}"
          	 port="${sm.port}"
+             serviceUrl="${sm.url}"
+             environment="${sm.environment}"
              state="${sm.state}"
              componentName="${sm.component.name}"
              serviceAssemblyName="${sm.service.assembly.name}"



Mime
View raw message