cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r556933 - in /incubator/cxf/trunk/rt/transports/http-jetty/src: main/java/org/apache/cxf/transport/http_jetty/ test/java/org/apache/cxf/transport/http_jetty/
Date Tue, 17 Jul 2007 14:43:14 GMT
Author: ningjiang
Date: Tue Jul 17 07:43:08 2007
New Revision: 556933

URL: http://svn.apache.org/viewvc?view=rev&rev=556933
Log:
1. CXF-792 added the retrieve ContextHandler and Jetty server methods in JettyHTTPServerEngine

2. Added Better logger of the JettyHTTPServerEngine

Added:
    incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
  (with props)
Modified:
    incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
    incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/Messages.properties
    incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java

Modified: incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java?view=diff&rev=556933&r1=556932&r2=556933
==============================================================================
--- incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
(original)
+++ incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java
Tue Jul 17 07:43:08 2007
@@ -78,6 +78,8 @@
      */
     private String protocol;
     
+    
+    private Boolean isSessionSupport = true;
     private int servantCount;
     private Server server;
     private AbstractConnector connector;
@@ -146,6 +148,22 @@
     }
     
     /**
+     * get the jetty server instance
+     * @return
+     */
+    public Server getServer() {
+        return server;
+    }
+    
+    /**
+     * Set the jetty server instance 
+     * @param s 
+     */
+    public void setServer(Server s) {
+        server = s;
+    }
+    
+    /**
      * Register a servant.
      * 
      * @param url the URL associated with the servant
@@ -153,20 +171,11 @@
      */
     public synchronized void addServant(URL url, JettyHTTPHandler handler) {
         if (server == null) {
+            // create a new jetty server instance if there is no server there           

             server = new Server();
             
             connector = connectorFactory.createConnector(port);
-            //REVISITION for setup the connector's threadPool
-            /*
-            if (getListener().isSetMaxIdleTimeMs()) {
-                listener.setMaxIdleTimeMs(getListener().getMaxIdleTimeMs().intValue());
-            }
-            if (getListener().isSetLowResourcePersistTimeMs()) {
-                int lowResourcePersistTime = 
-                    getListener().getLowResourcePersistTimeMs().intValue();
-                listener.setLowResourcePersistTimeMs(lowResourcePersistTime);
-            }*/
-
+            
             server.addConnector(connector);
             contexts = new ContextHandlerCollection();
             server.addHandler(contexts);
@@ -183,40 +192,46 @@
                     }
                 }
             } catch (Exception e) {
-                LOG.log(Level.SEVERE, e.getMessage(), e);
+                LOG.log(Level.SEVERE, "START_UP_SERVER_FAILED_MSG", new Object[] {e.getMessage()});
                 //problem starting server
                 try {                    
                     server.stop();
                     server.destroy();
                 } catch (Exception ex) {
-                    LOG.log(Level.SEVERE, ex.getMessage(), ex);
+                    LOG.log(Level.SEVERE, "START_UP_SERVER_FAILED_MSG", new Object[] {e.getMessage()});
                 }    
             }
-        }
+        }        
         
-        String contextName = HttpUriMapper.getContextName(url.getPath());
-        final String smap = HttpUriMapper.getResourceBase(url.getPath());
+        String contextName = HttpUriMapper.getContextName(url.getPath());            
         ContextHandler context = new ContextHandler();
         context.setContextPath(contextName);
-        context.setHandler(handler);
-        // just add the session manager here by code
-        // TODO adding the configuration support for session manager
-        HashSessionManager sessionManager = new HashSessionManager();
-        SessionHandler sessionHandler = new SessionHandler(sessionManager);
-        HashSessionIdManager idManager = new HashSessionIdManager();
-        sessionManager.setIdManager(idManager);
-        context.addHandler(sessionHandler);
         
+        // bind the jetty http hanler with the context handler        
+        context.setHandler(handler);
+        if (isSessionSupport) {
+            // just add the session manager here by code
+            // TODO adding the configuration support for session manager
+            HashSessionManager sessionManager = new HashSessionManager();
+            SessionHandler sessionHandler = new SessionHandler(sessionManager);
+            HashSessionIdManager idManager = new HashSessionIdManager();
+            sessionManager.setIdManager(idManager);            
+            context.addHandler(sessionHandler);           
+        }
         contexts.addHandler(context);
+       
+        final String smap = HttpUriMapper.getResourceBase(url.getPath());
+        handler.setName(smap);
+        
         if (contexts.isStarted()) {           
             try {                
                 context.start();
             } catch (Exception ex) {
-                LOG.log(Level.WARNING, ex.getMessage(), ex);
+                LOG.log(Level.WARNING, "ADD_SERVANT_FAILED_MSG", new Object[] {ex.getMessage()});
             }
         }
         
-        handler.setName(smap);        
+            
         ++servantCount;
     }
     
@@ -228,12 +243,10 @@
     public synchronized void removeServant(URL url) {        
         
         String contextName = HttpUriMapper.getContextName(url.getPath());
-        //final String smap = HttpUriMapper.getResourceBase(url.getPath());
 
         boolean found = false;
         // REVISIT:After a stop(), the server is null, and therefore this 
-        // operation shouldn't find a handler, but then below, why do we
-        // print a message to Std Error?
+        // operation shouldn't find a handler
         if (server != null) {
             for (Handler handler : contexts.getChildHandlersByClass(ContextHandler.class))
{
                 ContextHandler contextHandler = null;
@@ -241,34 +254,21 @@
                     contextHandler = (ContextHandler) handler;
                     if (contextName.equals(contextHandler.getContextPath())) {
                         try {
-                            contexts.removeHandler(handler);
+                            contexts.removeHandler(handler);                            
                             handler.stop();
                             handler.destroy();
-                        } catch (Exception e) {
-                            // TODO Auto-generated catch block
-                            e.printStackTrace();
+                        } catch (Exception ex) {
+                            LOG.log(Level.WARNING, "REMOVE_SERVANT_FAILED_MSG", 
+                                    new Object[] {ex.getMessage()}); 
                         }
-                        /*for (Handler httpHandler
-                            : contextHandler.getChildHandlersByClass(JettyHTTPHandler.class))
{
-                            if (smap.equals(((JettyHTTPHandler)httpHandler).getName())) {
-                                contexts.removeHandler(httpHandler);
-                                try {
-                                    handler.stop();                               
-                                    handler.destroy();
-                                } catch (Exception e) {
-                                    // TODO Auto-generated catch block
-                                    e.printStackTrace();
-                                }    
-                            }
                         
-                        }*/
                     }
                     found = true;
                 }
             }
         }
         if (!found) {
-            LOG.warning("Not able to remove the handler");
+            LOG.log(Level.WARNING, "CAN_NOT_FIND_HANDLER_MSG", new Object[]{url});
         }
         
         --servantCount;
@@ -287,7 +287,7 @@
      * @return the HttpHandler if registered
      */
     public synchronized Handler getServant(URL url)  {
-        String contextName = HttpUriMapper.getContextName(url.getPath());
+        String contextName = HttpUriMapper.getContextName(url.getPath());       
         //final String smap = HttpUriMapper.getResourceBase(url.getPath());
         
         Handler ret = null;
@@ -307,6 +307,32 @@
         }
         return ret;
     }
+    
+    /**
+     * Get a registered context handler.
+     * 
+     * @param url the associated URL
+     * @return the HttpHandler if registered
+     */
+    public synchronized ContextHandler getContextHandler(URL url) {
+        String contextName = HttpUriMapper.getContextName(url.getPath());
+        ContextHandler ret = null;
+        // After a stop(), the server is null, and therefore this 
+        // operation should return null.
+        if (server != null) {           
+            for (Handler handler : server.getChildHandlersByClass(ContextHandler.class))
{
+                ContextHandler contextHandler = null;
+                if (handler instanceof ContextHandler) {
+                    contextHandler = (ContextHandler) handler;
+                    if (contextName.equals(contextHandler.getContextPath())) {          

+                        ret = contextHandler;
+                        break;
+                    }
+                }
+            }    
+        }
+        return ret;
+    }
 
     protected void retrieveListenerFactory() {
         if (tlsProgrammaticServerParameters != null) {
@@ -327,14 +353,14 @@
     protected JettyConnectorFactory getHTTPConnectorFactory() {
         return new JettyConnectorFactory() {                     
             public AbstractConnector createConnector(int porto) {
+                // now we just use the SelectChannelConnector as the default connector
                 SelectChannelConnector result = 
                     new SelectChannelConnector();
                 
                 // Regardless the port has to equal the one
                 // we are configured for.
-                assert porto == port;
+                assert porto == port;                
                 
-                //SocketConnector result = new SocketConnector();
                 result.setPort(porto);
                 return result;
             }

Modified: incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/Messages.properties
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/Messages.properties?view=diff&rev=556933&r1=556932&r2=556933
==============================================================================
--- incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/Messages.properties
(original)
+++ incubator/cxf/trunk/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/Messages.properties
Tue Jul 17 07:43:08 2007
@@ -21,3 +21,7 @@
 UNEXPECTED_RESPONSE_TYPE_MSG = Unexpected response type {0}
 NULL_RESPONSE_MSG = Response object is null
 DECOUPLED_RESPONSE_FAILED_MSG = Decouple response failed
+START_UP_SERVER_FAILED_MSG = Start up the jetty server failed: {0}
+ADD_SERVANT_FAILED_MSG = Add the servant to jetty server failed: {0}
+REMOVE_SERVANT_FAILED_MSG = Remove the servant from the jetty server failed: {0}
+CAN_NOT_FIND_HANDLER_MSG = Can not find the handler to remove , the context url is {0}

Modified: incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java?view=diff&rev=556933&r1=556932&r2=556933
==============================================================================
--- incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
(original)
+++ incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
Tue Jul 17 07:43:08 2007
@@ -20,15 +20,24 @@
 package org.apache.cxf.transport.http_jetty;
 
 
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+
 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.easymock.classextension.EasyMock;
 import org.easymock.classextension.IMocksControl;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mortbay.jetty.handler.ContextHandler;
 
 public class JettyHTTPServerEngineTest extends Assert {
 
@@ -80,6 +89,74 @@
         factory.removeTLSServerParametersForPort(1235);
         factory.destroyForPort(1234);
         factory.destroyForPort(1235);
+    }
+    
+    @Test 
+    public void testaddServants() throws Exception {
+        String urlStr = "http://localhost:1234/hello/test";
+        JettyHTTPServerEngine engine = 
+            factory.createJettyHTTPServerEngine(1234);
+        JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler("string1");
+        JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler("string2");        
+        engine.addServant(new URL(urlStr), handler1);
+        String response = null;
+        try {
+            response = getResponse(urlStr);
+        } catch (Exception ex) {
+            fail("Can't get the reponse from the server " + ex);
+        }
+        assertEquals("the jetty http handler did not take effect", response, "string1");
+        
+        engine.addServant(new URL(urlStr), handler2);
+        try {
+            response = getResponse(urlStr);
+        } catch (Exception ex) {
+            fail("Can't get the reponse from the server " + ex);
+        }
+        assertEquals("the jetty http handler did not take effect", response, "string1string2");
+        
+        
+        // set the get request
+        factory.destroyForPort(1234);       
+        
+    }
+    
+    @Test 
+    public void testGetContextHandler() throws Exception {
+        String urlStr = "http://localhost:1234/hello/test";
+        JettyHTTPServerEngine engine = 
+            factory.createJettyHTTPServerEngine(1234);
+        ContextHandler contextHandler = engine.getContextHandler(new URL(urlStr));
+        assertNull(contextHandler);
+        JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler("string1");
+        JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler("string2");
+        engine.addServant(new URL(urlStr), handler1);
+        
+        contextHandler = engine.getContextHandler(new URL(urlStr));
+        contextHandler.setHandler(handler2);
+        contextHandler.start();
+        
+        String response = null;
+        try {
+            response = getResponse(urlStr);
+        } catch (Exception ex) {
+            fail("Can't get the reponse from the server " + ex);
+        }
+        assertEquals("the jetty http handler did not take effect", response, "string2");
+        factory.destroyForPort(1234);
+    }
+    
+    private String getResponse(String target) throws Exception {
+        URL url = new URL(target);        
+        
+        URLConnection connection = url.openConnection();            
+        
+        assertTrue(connection instanceof HttpURLConnection);
+        connection.connect(); 
+        InputStream in = connection.getInputStream();
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        IOUtils.copy(in, buffer);
+        return buffer.toString();
     }
     
 }

Added: incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java?view=auto&rev=556933
==============================================================================
--- incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
(added)
+++ incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
Tue Jul 17 07:43:08 2007
@@ -0,0 +1,43 @@
+/**
+ * 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.cxf.transport.http_jetty;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class JettyHTTPTestHandler extends JettyHTTPHandler {
+    String string;
+
+    public JettyHTTPTestHandler(String s) {        
+        super(null, true); 
+        string = s;
+    }
+    
+    public void handle(String target, HttpServletRequest req,
+                       HttpServletResponse resp, int dispatch) throws IOException {     
  
+        
+        resp.getOutputStream().write(string.getBytes());
+        resp.flushBuffer();
+        
+    }
+
+}

Propchange: incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPTestHandler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message