提交 af832920 编写于 作者: W william.liangf

DUBBO-138 WebService支持调试通过

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@1980 1a56cb94-b969-4eaa-88fa-be21384802f2
上级 a744e396
......@@ -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();
......
......@@ -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<Integer, HttpHandler> handlers = new ConcurrentHashMap<Integer, HttpHandler>();
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)
......
......@@ -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
......@@ -34,9 +34,14 @@
<artifactId>dubbo-rpc-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-remoting-http</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<artifactId>cxf-rt-frontend-simple</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
......
......@@ -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<String, HttpServer> serverMap = new ConcurrentHashMap<String, HttpServer>();
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 <T> Runnable doExport(T impl, Class<T> 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> T doRefer(final Class<T> 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) {
......
......@@ -140,7 +140,7 @@
<exclusions>
<exclusion>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<artifactId>cxf-rt-frontend-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
......
......@@ -73,7 +73,7 @@
<curator_version>1.1.10</curator_version>
<jedis_version>2.0.0</jedis_version>
<xmemcached_version>1.3.6</xmemcached_version>
<cxf_version>2.6.0</cxf_version>
<cxf_version>2.6.1</cxf_version>
<thrift_version>0.8.0</thrift_version>
<jfreechart_version>1.0.13</jfreechart_version>
<hessian_version>4.0.7</hessian_version>
......@@ -183,7 +183,7 @@
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>${cxf_version}</version>
</dependency>
<dependency>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册