cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dval...@apache.org
Subject svn commit: r935945 - in /cxf/trunk/rt/transports/http-jetty: ./ src/main/java/org/apache/cxf/transport/http_jetty/ src/test/java/org/apache/cxf/transport/http_jetty/
Date Tue, 20 Apr 2010 14:49:19 GMT
Author: dvaleri
Date: Tue Apr 20 14:49:18 2010
New Revision: 935945

URL: http://svn.apache.org/viewvc?rev=935945&view=rev
Log:
[CXF-2771] Reworked management of Jetty MBeanContainer to avoid MBean name conflicts.

Modified:
    cxf/trunk/rt/transports/http-jetty/pom.xml
    cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
    cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineFactory.java
    cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java

Modified: cxf/trunk/rt/transports/http-jetty/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/pom.xml?rev=935945&r1=935944&r2=935945&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/pom.xml (original)
+++ cxf/trunk/rt/transports/http-jetty/pom.xml Tue Apr 20 14:49:18 2010
@@ -56,6 +56,12 @@
             <groupId>org.mortbay.jetty</groupId>
             <artifactId>jetty</artifactId>
             <version>${jetty.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.mortbay.jetty</groupId>
+            <artifactId>jetty-management</artifactId>
+            <version>${jetty.version}</version>
+            <optional>true</optional>
         </dependency>       
         <dependency>
             <groupId>org.slf4j</groupId>

Modified: cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java?rev=935945&r1=935944&r2=935945&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
(original)
+++ cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
Tue Apr 20 14:49:18 2010
@@ -29,16 +29,13 @@ import java.util.logging.Logger;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
-import javax.management.MBeanServer;
 import javax.servlet.ServletContext;
 
 import org.apache.cxf.Bus;
-import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.configuration.jsse.TLSServerParameters;
 import org.apache.cxf.interceptor.Fault;
-import org.apache.cxf.management.InstrumentationManager;
 import org.apache.cxf.transport.HttpUriMapper;
 import org.apache.cxf.transport.https_jetty.JettySslConnectorFactory;
 import org.mortbay.component.Container;
@@ -301,21 +298,12 @@ public class JettyHTTPServerEngine
             DefaultHandler defaultHandler = null;
             // create a new jetty server instance if there is no server there           

             server = new Server();
-            if (bus != null && bus.getExtension(InstrumentationManager.class) !=
null) {
-                MBeanServer mbs =  bus.getExtension(InstrumentationManager.class).getMBeanServer();
-                if (mbs != null) {
-                    try {
-                        Class<?> cls = ClassLoaderUtils.loadClass("org.mortbay.management.MBeanContainer",

-                                                              getClass());
-                        Container.Listener listener 
-                            = (Container.Listener)cls.getConstructor(MBeanServer.class).newInstance(mbs);
-                        server.getContainer().addEventListener(listener);
-                    } catch (Throwable ex) {
-                        //ignore - just won't instrument jetty.  Probably don't have the
-                        //jetty-management jar available
-                    }
-                }
+            
+            Container.Listener mBeanContainer = factory.getMBeanContainer();
+            if (mBeanContainer != null) {
+                server.getContainer().addEventListener(mBeanContainer);
             }
+            
             if (connector == null) {
                 connector = connectorFactory.createConnector(getHost(), getPort());
                 if (LOG.isLoggable(Level.FINER)) {

Modified: cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineFactory.java?rev=935945&r1=935944&r2=935945&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineFactory.java
(original)
+++ cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineFactory.java
Tue Apr 20 14:49:18 2010
@@ -28,14 +28,16 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.annotation.Resource;
+import javax.management.MBeanServer;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.buslifecycle.BusLifeCycleListener;
 import org.apache.cxf.buslifecycle.BusLifeCycleManager;
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.configuration.jsse.TLSServerParameters;
-
-
+import org.apache.cxf.management.InstrumentationManager;
+import org.mortbay.component.Container;
 
 
 /**
@@ -81,6 +83,11 @@ public class JettyHTTPServerEngineFactor
      */
     private Bus bus;
     
+    /**
+     * The Jetty {@link MBeanContainer} to use when enabling JMX in Jetty.
+     */
+    private Container.Listener mBeanContainer;
+    
     public JettyHTTPServerEngineFactory() {
         // Empty
     }    
@@ -273,13 +280,41 @@ public class JettyHTTPServerEngineFactor
             }            
         }
     }
+    
+    public synchronized Container.Listener getMBeanContainer() {
+        if (this.mBeanContainer != null) {
+            return mBeanContainer;
+        }
+        
+        if (bus != null && bus.getExtension(InstrumentationManager.class) != null)
{
+            MBeanServer mbs =  bus.getExtension(InstrumentationManager.class).getMBeanServer();
+            if (mbs != null) {
+                try {
+                    Class<?> cls = ClassLoaderUtils.loadClass("org.mortbay.management.MBeanContainer",

+                                                          getClass());
+                    
+                    mBeanContainer = (Container.Listener) cls.
+                        getConstructor(MBeanServer.class).newInstance(mbs);
+                    
+                    cls.getMethod("start", (Class<?>[]) null).invoke(mBeanContainer,
(Object[]) null);
+                } catch (Throwable ex) {
+                    //ignore - just won't instrument jetty.  Probably don't have the
+                    //jetty-management jar available
+                    LOG.info("Could not load or start org.mortbay.management.MBeanContainer.
 "
+                             + "Jetty JMX support will not be enabled: " + ex.getMessage());
+                }
+            }
+        }
+        
+        return mBeanContainer;
+    }
 
     public void initComplete() {
         // do nothing here
     }
 
     public void postShutdown() {
-        //shut down the jetty server in the portMap
+        // shut down the jetty server in the portMap
         // To avoid the CurrentModificationException, 
         // do not use portMap.vaules directly       
         JettyHTTPServerEngine[] engines = portMap.values().toArray(new JettyHTTPServerEngine[0]);
@@ -289,6 +324,7 @@ public class JettyHTTPServerEngineFactor
         // clean up the collections
         threadingParametersMap.clear();
         tlsParametersMap.clear();
+        mBeanContainer = null;
     }
 
     public void preShutdown() {

Modified: cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java?rev=935945&r1=935944&r2=935945&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
(original)
+++ cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
Tue Apr 20 14:49:18 2010
@@ -23,17 +23,22 @@ package org.apache.cxf.transport.http_je
 
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.lang.management.ManagementFactory;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+
+import javax.management.ObjectName;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.configuration.Configurer;
 import org.apache.cxf.configuration.jsse.TLSServerParameters;
 import org.apache.cxf.configuration.spring.ConfigurerImpl;
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.management.InstrumentationManager;
 import org.easymock.classextension.EasyMock;
 import org.easymock.classextension.IMocksControl;
 import org.junit.Assert;
@@ -59,7 +64,14 @@ public class JettyHTTPServerEngineTest e
         Configurer configurer = new ConfigurerImpl();
         bus.getExtension(Configurer.class);
         EasyMock.expectLastCall().andReturn(configurer).anyTimes();
-
+        
+        InstrumentationManager iManager = control.createMock(InstrumentationManager.class);
+        iManager.getMBeanServer();
+        EasyMock.expectLastCall().andReturn(ManagementFactory.getPlatformMBeanServer()).anyTimes();
+        
+        bus.getExtension(InstrumentationManager.class);
+        EasyMock.expectLastCall().andReturn(iManager).anyTimes();
+        
         control.replay();
 
         factory = new JettyHTTPServerEngineFactory();
@@ -168,6 +180,11 @@ public class JettyHTTPServerEngineTest e
         response = getResponse(urlStr);
         assertEquals("The jetty http handler did not take effect", response, "string1string2");
         engine.addServant(new URL(urlStr2), handler2);
+        
+        Set<ObjectName>  s = ManagementFactory.getPlatformMBeanServer().
+            queryNames(new ObjectName("org.mortbay.jetty:type=server,*"), null);
+        assertEquals("Could not find 1 Jetty Server: " + s, 1, s.size());
+        
         engine.removeServant(new URL(urlStr));
         engine.shutdown();
         response = getResponse(urlStr2);
@@ -176,6 +193,53 @@ public class JettyHTTPServerEngineTest e
         factory.destroyForPort(9234);
 
     }
+    
+    /**
+     * Test that multiple JettyHTTPServerEngine instances can be used simultaneously
+     * without having name collisions.
+     */
+    @Test
+    public void testJmxSupport() throws Exception {
+        String urlStr = "http://localhost:9234/hello/test";
+        String urlStr2 = "http://localhost:9235/hello/test";
+        JettyHTTPServerEngine engine =
+            factory.createJettyHTTPServerEngine(9234, "http");
+        JettyHTTPServerEngine engine2 =
+            factory.createJettyHTTPServerEngine(9235, "http");
+        JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler("string1", true);
+        JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler("string2", true);
+        
+        engine.addServant(new URL(urlStr), handler1);
+        
+        Set<ObjectName>  s = ManagementFactory.getPlatformMBeanServer().
+            queryNames(new ObjectName("org.mortbay.jetty:type=server,*"), null);
+        assertEquals("Could not find 1 Jetty Server: " + s, 1, s.size());
+        
+        engine2.addServant(new URL(urlStr2), handler2);
+        
+        s = ManagementFactory.getPlatformMBeanServer().
+            queryNames(new ObjectName("org.mortbay.jetty:type=server,*"), null);
+        assertEquals("Could not find 2 Jetty Server: " + s, 2, s.size());
+        
+        engine.removeServant(new URL(urlStr));
+        engine2.removeServant(new URL(urlStr2));
+        
+        
+        engine.shutdown();
+        
+        s = ManagementFactory.getPlatformMBeanServer().
+            queryNames(new ObjectName("org.mortbay.jetty:type=server,*"), null);
+        assertEquals("Could not find 2 Jetty Server: " + s, 1, s.size());
+        
+        engine2.shutdown();
+        
+        s = ManagementFactory.getPlatformMBeanServer().
+            queryNames(new ObjectName("org.mortbay.jetty:type=server,*"), null);
+        assertEquals("Could not find 0 Jetty Server: " + s, 0, s.size());
+        
+        factory.destroyForPort(9234);
+        factory.destroyForPort(9235);
+    }
 
     @Test
     public void testSetHandlers() throws Exception {
@@ -185,6 +249,7 @@ public class JettyHTTPServerEngineTest e
 
         JettyHTTPServerEngine engine = new JettyHTTPServerEngine();
         engine.setPort(9235);
+        engine.setJettyHTTPServerEngineFactory(factory);
 
         List<Handler> handlers = new ArrayList<Handler>();
         handlers.add(handler1);
@@ -214,6 +279,12 @@ public class JettyHTTPServerEngineTest e
         JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler("string2", true);
         engine.addServant(new URL(urlStr), handler1);
 
+        // Note: There appears to be an internal issue in Jetty that does not
+        // unregister the MBean for handler1 during this setHandler operation.
+        // This scenario may create a warning message in the logs 
+        //     (javax.management.InstanceAlreadyExistsException: org.apache.cxf.
+        //         transport.http_jetty:type=jettyhttptesthandler,id=0)
+        // when running subsequent tests.
         contextHandler = engine.getContextHandler(new URL(urlStr));
         contextHandler.setHandler(handler2);
         contextHandler.start();



Mime
View raw message