From af832920cf0fcd2ca507452a9db82c04c736ebea Mon Sep 17 00:00:00 2001 From: "william.liangf" Date: Wed, 13 Jun 2012 09:25:50 +0000 Subject: [PATCH] =?UTF-8?q?DUBBO-138=20WebService=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1980 1a56cb94-b969-4eaa-88fa-be21384802f2 --- .../remoting/http/jetty/JettyHttpServer.java | 26 +++-- .../http/servlet/DispatcherServlet.java | 16 +++- .../http/servlet/ServletHttpServer.java | 2 +- dubbo-rpc/dubbo-rpc-webservice/pom.xml | 7 +- .../webservice/WebServiceProtocol.java | 94 +++++++++++++++---- dubbo/pom.xml | 2 +- pom.xml | 4 +- 7 files changed, 112 insertions(+), 39 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java index cb30752fff..7a77c8b931 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/jetty/JettyHttpServer.java @@ -15,15 +15,10 @@ */ package com.alibaba.dubbo.remoting.http.jetty; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.mortbay.jetty.Server; -import org.mortbay.jetty.handler.AbstractHandler; import org.mortbay.jetty.nio.SelectChannelConnector; +import org.mortbay.jetty.servlet.ServletHandler; +import org.mortbay.jetty.servlet.ServletHolder; import org.mortbay.thread.QueuedThreadPool; import com.alibaba.dubbo.common.Constants; @@ -32,6 +27,7 @@ import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.common.utils.NetUtils; import com.alibaba.dubbo.remoting.http.HttpHandler; +import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet; import com.alibaba.dubbo.remoting.http.support.AbstractHttpServer; public class JettyHttpServer extends AbstractHttpServer { @@ -42,7 +38,8 @@ public class JettyHttpServer extends AbstractHttpServer { public JettyHttpServer(URL url, final HttpHandler handler){ super(url, handler); - + DispatcherServlet.addHttpHandler(url.getPort(), handler); + int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setDaemon(true); @@ -58,13 +55,12 @@ public class JettyHttpServer extends AbstractHttpServer { server = new Server(); server.setThreadPool(threadPool); server.addConnector(connector); - server.addHandler(new AbstractHandler() { - public void handle(String target, HttpServletRequest request, - HttpServletResponse response, int dispatch) throws IOException, - ServletException { - handler.handle(request, response); - } - }); + + ServletHandler servletHandler = new ServletHandler(); + ServletHolder servletHolder = servletHandler.addServletWithMapping(DispatcherServlet.class, "/*"); + servletHolder.setInitOrder(2); + + server.addHandler(servletHandler); try { server.start(); diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java index 29e7a3455a..ae3c7210bf 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/DispatcherServlet.java @@ -33,16 +33,26 @@ import com.alibaba.dubbo.remoting.http.HttpHandler; */ public class DispatcherServlet extends HttpServlet { - private static final long serialVersionUID = 5766349180380479888L; + private static final long serialVersionUID = 5766349180380479888L; + + private static DispatcherServlet INSTANCE; private static final Map handlers = new ConcurrentHashMap(); - static void addHttpInvoker(int port, HttpHandler processor) { + public static void addHttpHandler(int port, HttpHandler processor) { handlers.put(port, processor); } - static void removeHttpInvoker(int port) { + public static void removeHttpHandler(int port) { handlers.remove(port); + } + + public static DispatcherServlet getInstance() { + return INSTANCE; + } + + public DispatcherServlet() { + DispatcherServlet.INSTANCE = this; } protected void service(HttpServletRequest request, HttpServletResponse response) diff --git a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java index 0b9433a85e..fa913556c5 100644 --- a/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java +++ b/dubbo-remoting/dubbo-remoting-http/src/main/java/com/alibaba/dubbo/remoting/http/servlet/ServletHttpServer.java @@ -23,7 +23,7 @@ public class ServletHttpServer extends AbstractHttpServer { public ServletHttpServer(URL url, HttpHandler handler){ super(url, handler); - DispatcherServlet.addHttpInvoker(url.getPort(), handler); + DispatcherServlet.addHttpHandler(url.getPort(), handler); } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-webservice/pom.xml b/dubbo-rpc/dubbo-rpc-webservice/pom.xml index c3e8246757..dfd417abe6 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/pom.xml +++ b/dubbo-rpc/dubbo-rpc-webservice/pom.xml @@ -34,9 +34,14 @@ dubbo-rpc-api ${project.parent.version} + + com.alibaba + dubbo-remoting-http + ${project.parent.version} + org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple org.springframework diff --git a/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java b/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java index b1eb312595..2654b028ef 100644 --- a/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java +++ b/dubbo-rpc/dubbo-rpc-webservice/src/main/java/com/alibaba/dubbo/rpc/protocol/webservice/WebServiceProtocol.java @@ -17,13 +17,28 @@ package com.alibaba.dubbo.rpc.protocol.webservice; import java.io.IOException; import java.net.SocketTimeoutException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; -import org.apache.cxf.interceptor.LoggingInInterceptor; -import org.apache.cxf.interceptor.LoggingOutInterceptor; -import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; -import org.apache.cxf.jaxws.JaxWsServerFactoryBean; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.cxf.bus.extension.ExtensionManagerBus; +import org.apache.cxf.frontend.ClientProxyFactoryBean; +import org.apache.cxf.frontend.ServerFactoryBean; +import org.apache.cxf.transport.http.HTTPTransportFactory; +import org.apache.cxf.transport.http.HttpDestinationFactory; +import org.apache.cxf.transport.servlet.ServletController; +import org.apache.cxf.transport.servlet.ServletDestinationFactory; import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.remoting.http.HttpBinder; +import com.alibaba.dubbo.remoting.http.HttpHandler; +import com.alibaba.dubbo.remoting.http.HttpServer; +import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet; +import com.alibaba.dubbo.rpc.RpcContext; import com.alibaba.dubbo.rpc.RpcException; import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol; @@ -36,31 +51,78 @@ public class WebServiceProtocol extends AbstractProxyProtocol { public static final int DEFAULT_PORT = 80; + private final Map serverMap = new ConcurrentHashMap(); + + private final ExtensionManagerBus bus = new ExtensionManagerBus(); + + private final HTTPTransportFactory transportFactory = new HTTPTransportFactory(bus); + + private HttpBinder httpBinder; + + public WebServiceProtocol() { + super(IOException.class); + bus.setExtension(new ServletDestinationFactory(), HttpDestinationFactory.class); + } + + public void setHttpBinder(HttpBinder httpBinder) { + this.httpBinder = httpBinder; + } + public int getDefaultPort() { return DEFAULT_PORT; } + private class WebServiceHandler implements HttpHandler { + + private volatile ServletController servletController; + + public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + if (servletController == null) { + HttpServlet httpServlet = DispatcherServlet.getInstance(); + if (httpServlet == null) { + response.sendError(500, "No such DispatcherServlet instance."); + return; + } + synchronized (this) { + if (servletController == null) { + servletController = new ServletController(transportFactory.getRegistry(), httpServlet.getServletConfig(), httpServlet); + } + } + } + RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort()); + servletController.invoke(request, response); + } + + } + protected Runnable doExport(T impl, Class type, URL url) throws RpcException { - final JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); - jaxWsServerFactoryBean.setServiceClass(type); - jaxWsServerFactoryBean.setAddress(url.setProtocol("http").toString()); - jaxWsServerFactoryBean.setServiceBean(impl); - jaxWsServerFactoryBean.create(); + String addr = url.getIp() + ":" + url.getPort(); + HttpServer httpServer = serverMap.get(addr); + if (httpServer == null) { + httpServer = httpBinder.bind(url, new WebServiceHandler()); + serverMap.put(addr, httpServer); + } + final ServerFactoryBean serverFactoryBean = new ServerFactoryBean(); + serverFactoryBean.setServiceClass(type); + serverFactoryBean.setAddress(url.getAbsolutePath()); + serverFactoryBean.setServiceBean(impl); + serverFactoryBean.setDestinationFactory(transportFactory); + serverFactoryBean.setBus(bus); + serverFactoryBean.create(); return new Runnable() { public void run() { - jaxWsServerFactoryBean.destroy(); + serverFactoryBean.destroy(); } }; } @SuppressWarnings("unchecked") protected T doRefer(final Class serviceType, final URL url) throws RpcException { - JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); - jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); - jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); - jaxWsProxyFactoryBean.setServiceClass(serviceType); - jaxWsProxyFactoryBean.setAddress(url.setProtocol("http").toString()); - return (T) jaxWsProxyFactoryBean.create(); + ClientProxyFactoryBean proxyFactoryBean = new ClientProxyFactoryBean(); + proxyFactoryBean.setServiceClass(serviceType); + proxyFactoryBean.setAddress(url.setProtocol("http").toIdentityString()); + proxyFactoryBean.setBus(bus); + return (T) proxyFactoryBean.create(); } protected int getErrorCode(Throwable e) { diff --git a/dubbo/pom.xml b/dubbo/pom.xml index ca6c55a654..e3df1e1cea 100644 --- a/dubbo/pom.xml +++ b/dubbo/pom.xml @@ -140,7 +140,7 @@ org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple diff --git a/pom.xml b/pom.xml index fc1b1a6520..5ddc870202 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ 1.1.10 2.0.0 1.3.6 - 2.6.0 + 2.6.1 0.8.0 1.0.13 4.0.7 @@ -183,7 +183,7 @@ org.apache.cxf - cxf-rt-frontend-jaxws + cxf-rt-frontend-simple ${cxf_version}