SM-2096: MBean lookup failures in web console (plus a few more fixes to the console)

git-svn-id: https://svn.apache.org/repos/asf/servicemix/smx3/trunk@1137562 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/distributions/apache-servicemix-web/src/main/webapp/WEB-INF/web.xml b/distributions/apache-servicemix-web/src/main/webapp/WEB-INF/web.xml
index f85a219..650a41f 100644
--- a/distributions/apache-servicemix-web/src/main/webapp/WEB-INF/web.xml
+++ b/distributions/apache-servicemix-web/src/main/webapp/WEB-INF/web.xml
@@ -105,9 +105,9 @@
     </servlet-mapping>

 

     <servlet>

-      <servlet-name>cxf</servlet-name>

-      <display-name>cxf</display-name>

       <description>Apache CXF Endpoint</description>

+      <display-name>cxf</display-name> 

+      <servlet-name>cxf</servlet-name>

       <servlet-class>org.apache.cxf.transport.servlet.CXFNonSpringServlet</servlet-class>

       <load-on-startup>1</load-on-startup>

   </servlet>

diff --git a/web/servicemix-web-console/pom.xml b/web/servicemix-web-console/pom.xml
index 4dc1dc8..8784392 100644
--- a/web/servicemix-web-console/pom.xml
+++ b/web/servicemix-web-console/pom.xml
@@ -158,6 +158,10 @@
         <artifactId>spring-aop</artifactId>
         <version>${spring-version}</version>
     </dependency>
+    <dependency>
+        <groupId>commons-logging</groupId>
+        <artifactId>commons-logging</artifactId>
+    </dependency>
     
     <dependency>
       <groupId>org.mortbay.jetty</groupId>
diff --git a/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBean.java b/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBean.java
new file mode 100644
index 0000000..b272f14
--- /dev/null
+++ b/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBean.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.web.jmx;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.jmx.MBeanServerNotFoundException;
+import org.springframework.jmx.access.MBeanInfoRetrievalException;
+import org.springframework.jmx.access.MBeanProxyFactoryBean;
+
+import javax.management.MBeanServerConnection;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import java.io.IOException;
+
+/**
+ * Variant of Spring's {@link MBeanProxyFactoryBean} that improves support for WebSphere by automatically
+ * determining the cell/node/process attributes of the MBean ObjectName
+ */
+public class EnhancedMBeanProxyFactoryBean extends MBeanProxyFactoryBean {
+
+    private static final Log LOG = LogFactory.getLog(EnhancedMBeanProxyFactoryBean.class);
+    protected static final String WEBSPHERE = "WebSphere";
+	
+	private MBeanServerConnection connection;
+	private String originalName;
+
+	@Override
+	public void afterPropertiesSet() throws MBeanServerNotFoundException, MBeanInfoRetrievalException {
+		try {
+			if (WEBSPHERE.equals(connection.getDefaultDomain())) {
+				doWebSphereConfiguration();
+			}
+		} catch (IOException e) {
+			if (LOG.isDebugEnabled()) {
+                LOG.debug("Unable to determine default domain name - assuming it's not WebSphere", e);
+            }
+		}
+		super.afterPropertiesSet();
+	}
+
+    /*
+     * When running in WebSphere, try determining the cell/node/process values and append those to the
+     * JMX MBean object name.
+     */
+	private void doWebSphereConfiguration() {
+		LOG.info("Running on WebSphere - adding cell/node/process information to " + originalName);
+
+		try {
+            String cell = null;
+            String node = null;
+            String process = null;
+
+			for (Object value : connection.queryNames(new ObjectName("WebSphere:name=JVM,*"), null)) {
+				ObjectName objectName = (ObjectName) value;
+				cell = objectName.getKeyProperty("cell");
+				node = objectName.getKeyProperty("node");
+				process = objectName.getKeyProperty("process");
+			}
+
+            String name = String.format("%s,cell=%s,node=%s,process=%s", originalName, cell, node, process);
+            super.setObjectName(name);
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Object name has been changed to " + name);
+            }
+		} catch (MalformedObjectNameException e) {
+            throw new MBeanInfoRetrievalException("Unable to determine cell/node/process information while running in WebSphere", e);
+		} catch (IOException e) {
+            throw new MBeanInfoRetrievalException("Unable to determine cell/node/process information while running in WebSphere", e);
+		}
+	}
+
+    // override to capture the MBean server connection
+	@Override
+	public void setServer(MBeanServerConnection connection) {
+		this.connection = connection;
+		super.setServer(connection);
+	}
+
+    // override to capture the original object name
+    @Override
+    public void setObjectName(Object name) throws MalformedObjectNameException {
+        this.originalName = name != null ? name.toString() : null;
+        super.setObjectName(name);
+    }
+}
diff --git a/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/model/ProxyManager.java b/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/model/ProxyManager.java
index aa42a06..9596d17 100644
--- a/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/model/ProxyManager.java
+++ b/web/servicemix-web-console/src/main/java/org/apache/servicemix/web/model/ProxyManager.java
@@ -16,16 +16,16 @@
  */

 package org.apache.servicemix.web.model;

 

-import java.lang.ref.Reference;

-import java.lang.ref.SoftReference;

-import java.util.Map;

-import java.util.concurrent.ConcurrentHashMap;

+import org.apache.servicemix.web.jmx.EnhancedMBeanProxyFactoryBean;

+import org.springframework.jmx.access.MBeanProxyFactoryBean;

 

 import javax.management.MBeanServerConnection;

 import javax.management.MalformedObjectNameException;

 import javax.management.ObjectName;

-

-import org.springframework.jmx.access.MBeanProxyFactoryBean;

+import java.lang.ref.Reference;

+import java.lang.ref.SoftReference;

+import java.util.Map;

+import java.util.concurrent.ConcurrentHashMap;

 

 public class ProxyManager {

 

@@ -41,7 +41,7 @@
         Reference r = proxies.get(name);

         T proxy = (r != null) ? (T) r.get() : null;

         if (proxy == null) {

-            MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();

+            MBeanProxyFactoryBean factory = new EnhancedMBeanProxyFactoryBean();

             factory.setServer(server);

             try {

                 factory.setObjectName(name);

diff --git a/web/servicemix-web-console/src/main/webapp/WEB-INF/applicationContext.xml b/web/servicemix-web-console/src/main/webapp/WEB-INF/applicationContext.xml
index 6f78dad..118734a 100644
--- a/web/servicemix-web-console/src/main/webapp/WEB-INF/applicationContext.xml
+++ b/web/servicemix-web-console/src/main/webapp/WEB-INF/applicationContext.xml
@@ -23,7 +23,7 @@
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

   </bean>

   

-  <bean id="jmxProxy" abstract="true" class="org.springframework.jmx.access.MBeanProxyFactoryBean">

+  <bean id="jmxProxy" abstract="true" class="org.apache.servicemix.web.jmx.EnhancedMBeanProxyFactoryBean">

     <property name="server" ref="server"/>

     <property name="useStrictCasing" value="false" />

     <property name="connectOnStartup" value="false" />

diff --git a/web/servicemix-web-console/src/main/webapp/WEB-INF/jspf/headertags.jspf b/web/servicemix-web-console/src/main/webapp/WEB-INF/jspf/headertags.jspf
index 7121a70..eb7c888 100644
--- a/web/servicemix-web-console/src/main/webapp/WEB-INF/jspf/headertags.jspf
+++ b/web/servicemix-web-console/src/main/webapp/WEB-INF/jspf/headertags.jspf
@@ -17,3 +17,4 @@
 <%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
diff --git a/web/servicemix-web-console/src/test/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBeanTest.java b/web/servicemix-web-console/src/test/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBeanTest.java
new file mode 100644
index 0000000..0c43012
--- /dev/null
+++ b/web/servicemix-web-console/src/test/java/org/apache/servicemix/web/jmx/EnhancedMBeanProxyFactoryBeanTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.web.jmx;
+
+import junit.framework.TestCase;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
+/**
+ * Test cases for {@link EnhancedMBeanProxyFactoryBean}
+ */
+public class EnhancedMBeanProxyFactoryBeanTest extends TestCase {
+
+    public void testWebSphereAddsCellNodeAndProcess() throws Exception {
+        MBeanServer server = MBeanServerFactory.createMBeanServer(EnhancedMBeanProxyFactoryBean.WEBSPHERE);
+        server.registerMBean(new Mock(), new ObjectName("WebSphere:name=JVM,cell=myCell,process=myProcess,node=myNode"));
+
+        Mock mbean = new Mock();
+        server.registerMBean(mbean, new ObjectName("test:name=MyMockBean,cell=myCell,process=myProcess,node=myNode"));
+
+        EnhancedMBeanProxyFactoryBean bean = new EnhancedMBeanProxyFactoryBean();
+        bean.setServer(server);
+        bean.setObjectName("test:name=MyMockBean");
+        bean.setProxyInterface(MockMBean.class);
+        bean.afterPropertiesSet();
+
+        MockMBean proxy = (MockMBean) bean.getObject();
+        proxy.doSomethingUseful();
+        assertEquals("We should have use the MBean using the object name without the cell/process/node inforamtion",
+                     1, mbean.usefulness);
+    }
+
+    public static interface MockMBean {
+
+        public void doSomethingUseful();
+
+    }
+
+    public static final class Mock implements MockMBean {
+
+        private int usefulness;
+
+        public void doSomethingUseful() {
+            usefulness++;
+        }
+
+    }
+}
diff --git a/web/servicemix-web-console/src/test/resources/log4j.properties b/web/servicemix-web-console/src/test/resources/log4j.properties
new file mode 100644
index 0000000..89703d2
--- /dev/null
+++ b/web/servicemix-web-console/src/test/resources/log4j.properties
@@ -0,0 +1,42 @@
+# 
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed  under the  License is distributed on an "AS IS" BASIS,
+# WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+# implied.
+#  
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+
+#
+# The logging properties used during tests..
+#
+log4j.rootLogger=DEBUG, stdout
+
+log4j.logger.org.springframework=INFO
+log4j.logger.org.apache.activemq=INFO
+log4j.logger.org.apache.activemq.spring=WARN
+log4j.logger.org.apache.activemq.store.journal=INFO
+log4j.logger.org.activeio.journal=INFO
+
+# CONSOLE appender not used by default
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n
+
+# File appender
+log4j.appender.out=org.apache.log4j.FileAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.out.file=target/servicemix-test.log
+log4j.appender.out.append=true