Commit 4b792c23 authored by Philippe Merle's avatar Philippe Merle

Updated the support for <tuscany:binding.http> to use Apache CXF for creating Jetty servers.

This alllows WS, REST, and HTTP bindings to share same Jetty server instances.
parent 138b6074
......@@ -101,22 +101,25 @@
<version>${project.version}</version>
</dependency>
<!-- Support for <tuscany:binding.http>. -->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-metamodel-tuscany</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Support for Apache CXF and Jetty. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Servlet API. -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
......
......@@ -26,16 +26,22 @@
package org.ow2.frascati.binding.http;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.http_jetty.JettyHTTPHandler;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
import org.apache.cxf.transport.http_jetty.JettyHTTPTransportFactory;
import org.eclipse.jetty.server.Request;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
......@@ -55,50 +61,38 @@ public class JettyServletManager
extends AbstractLoggeable
implements ServletManager
{
/**
* Cache of already created Jetty servers and contexts.
*/
private Map<Integer, Server> servers = new HashMap<Integer, Server>();
private Map<Integer, Context> contexts = new HashMap<Integer, Context>();
/**
* @see ServletManager#registerServlet(String, Servlet)
*/
public final synchronized void registerServlet(final String aUri, final Servlet servlet)
{
log.info("RegisterServlet(uri= " + aUri + ") called.");
// Create a URI instance from the 'key' string.
URI uri;
/**
* @see ServletManager#registerServlet(String, Servlet)
*/
public final synchronized void registerServlet(final String aUri, final Servlet servlet)
{
log.info("RegisterServlet(uri=" + aUri + ") called.");
// Create a URL instance from the 'aUri' string.
URL url;
try {
uri = new URI(aUri);
} catch (URISyntaxException use) {
throw new RuntimeException(use);
}
String path = uri.getPath();
// Find the Jetty Context instance associated to the uri's port.
int port = uri.getPort();
Context context = contexts.get(port);
if(context == null) {
log.info("Creating a Jetty server on port " + port + "...");
Server server = new Server(port);
servers.put(port, server);
context = new Context(server, "/", Context.SESSIONS);
contexts.put(port, context);
try {
log.info("Starting the Jetty server on port " + port + "...");
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
url = new URL(aUri);
} catch (MalformedURLException mue) {
throw new RuntimeException(mue);
}
log.info("Adding a Servlet with path '" + path +
"' on the Jetty server on port " + port + " ...");
context.addServlet(new ServletHolder(servlet), path + "/*");
// Obtain the Apache CXF Jetty server factory.
Object o = BusFactory.getDefaultBus().getExtension(JettyHTTPTransportFactory.class);
JettyHTTPServerEngineFactory jettyFactory =
BusFactory.getDefaultBus().getExtension(JettyHTTPServerEngineFactory.class);
log.info("RegisterServlet(uri= " + uri + ") done.");
// Create an Apache CXF Jetty server.
JettyHTTPServerEngine jettyServer;
try {
jettyServer = jettyFactory.createJettyHTTPServerEngine(url.getHost(), url.getPort(), "http");
} catch(Exception exc) {
throw new RuntimeException(exc);
}
log.info("Adding a Servlet with path '" + url.getPath() +
"' on the Jetty server on port " + url.getPort() + " ...");
jettyServer.addServant(url, new ServletHandler(servlet));
log.info("RegisterServlet(uri=" + url + ") done.");
}
/**
......@@ -110,3 +104,38 @@ public class JettyServletManager
}
}
class ServletHandler extends JettyHTTPHandler
{
/**
* Servlet to delegate.
*/
private Servlet servlet;
/**
* Constructor.
*/
public ServletHandler(Servlet servlet)
{
super(null, false);
this.servlet = servlet;
}
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// Check if this request is for this handler.
if(target.startsWith(getName())) {
// Remove the path from the path info of this request.
String old_path_info = baseRequest.getPathInfo();
baseRequest.setPathInfo(old_path_info.substring(getName().length()));
// Dispatch the request to the servlet.
this.servlet.service(request, response);
// Restore the previous path info.
baseRequest.setPathInfo(old_path_info);
// This request was handled.
baseRequest.setHandled(true);
}
}
}
/**
* OW2 FraSCAti: SCA Binding HTTP
* Copyright (C) 2010 INRIA, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
*
*/
package org.ow2.frascati.binding.http;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
import org.ow2.frascati.binding.http.ServletManager;
import org.ow2.frascati.util.AbstractLoggeable;
/**
* Implementation of the {@link ServletManager} interface.
*
* @author Philippe Merle
*/
@Scope("COMPOSITE")
@Service(ServletManager.class)
public class JettyServletManager
extends AbstractLoggeable
implements ServletManager
{
/**
* Cache of already created Jetty servers and contexts.
*/
private Map<Integer, Server> servers = new HashMap<Integer, Server>();
private Map<Integer, ServletContextHandler> contexts = new HashMap<Integer, ServletContextHandler>();
/**
* @see ServletManager#registerServlet(String, Servlet)
*/
public final synchronized void registerServlet(final String aUri, final Servlet servlet)
{
log.info("RegisterServlet(uri=" + aUri + ") called.");
// Create a URI instance from the 'key' string.
URI uri;
try {
uri = new URI(aUri);
} catch (URISyntaxException use) {
throw new RuntimeException(use);
}
// Find the Jetty Context instance associated to the uri's port.
int port = uri.getPort();
ServletContextHandler context = contexts.get(port);
if(context == null) {
log.info("Creating a Jetty server on port " + port + "...");
Server server = new Server(port);
servers.put(port, server);
context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
contexts.put(port, context);
try {
log.info("Starting the Jetty server on port " + port + "...");
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
String path = uri.getPath();
log.info("Adding a Servlet with path '" + path +
"' on the Jetty server on port " + port + " ...");
context.addServlet(new ServletHolder(servlet), path + "/*");
log.info("RegisterServlet(uri=" + uri + ") done.");
}
/**
* @see ServletManager#unregisterServlet(String)
*/
public final void unregisterServlet(final String uri)
{
// TODO
}
}
......@@ -310,11 +310,7 @@
<fractal-bf.version>0.9-SNAPSHOT</fractal-bf.version>
<fscript.version>2.1.2</fscript.version>
<cxf.version>2.3.0</cxf.version>
<!-- TODO: move to Jetty used by CXF 2.3.0, i.e., org.eclipse.jetty:jetty-server:jar:7.1.6.v20100715 -->
<!--
<jetty.version>7.1.6.v20100715</jetty.version>
-->
<jetty.version>6.1.21</jetty.version>
<spring.version>3.0.4.RELEASE</spring.version>
<xerces.version>2.9.1</xerces.version>
<jaxb.version>2.1.13</jaxb.version>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment