Commit 72a2c8fa authored by Philippe Merle's avatar Philippe Merle

Corrections for issue #105

* Added a new shutdown method to interface ServletManager.
  The implementation destroys all created Apache CXF Jetty server engines.
* Exposed the service 'servlet-manager' by the FraSCAti composite.
* Shutdown the servlet manager before destroying Apache CXF.
parent 9d8b30b2
/**
* OW2 FraSCAti Servlet CXF
* Copyright (C) 2009-2011 INRIA, University of Lille 1
* Copyright (C) 2009-2013 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
......@@ -162,10 +162,28 @@ public class FraSCAtiServlet
@Override
public void destroy()
{
// Stop all started SCA composites.
for (Launcher launcher : launchers) {
launcher.close();
}
launchers.clear();
// Added for issue http://jira.ow2.org/browse/FRASCATI-105
// Stop all started Apache CXF Jetty server engines.
try {
ServletManager servletManager = frascati.getService(frascati.getFrascatiComposite(), "servlet-manager", ServletManager.class);
servletManager.shutdown();
} catch(Exception exc) {
exc.printStackTrace();
}
// Stop FraSCAti.
try {
frascati.close();
} catch(Exception exc) {
exc.printStackTrace();
}
super.destroy();
}
......@@ -222,6 +240,16 @@ public class FraSCAtiServlet
log.info("UnregisterServlet(uri=" + uri + ") done.");
}
/**
* @see ServletManager#shutdown()
*
* Added for issue http://jira.ow2.org/browse/FRASCATI-105
*/
public void shutdown()
{
// Nothing to do.
}
}
class MyHttpServletRequestWrapper extends HttpServletRequestWrapper
......
/**
* OW2 FraSCAti: Servlet Manager
* Copyright (C) 2010-2011 INRIA, University of Lille 1
* Copyright (C) 2010-2013 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
......@@ -46,4 +46,10 @@ public interface ServletManager
*/
void unregisterServlet(final String uri);
/**
* Shutdown the servlet manager.
*
* Added for issue http://jira.ow2.org/browse/FRASCATI-105
*/
void shutdown();
}
/**
* OW2 FraSCAti: Servlet Manager
* Copyright (C) 2010-2012 Inria, University of Lille 1
* Copyright (C) 2010-2013 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
......@@ -31,6 +31,7 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.HashSet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
......@@ -64,6 +65,11 @@ public class JettyServletManager
extends AbstractLoggeable
implements ServletManager
{
/**
* Set of Apache Jetty server engine ports.
*/
protected HashSet<Integer> ports = new HashSet<Integer>();
/**
* @see ServletManager#registerServlet(String, Servlet)
*/
......@@ -94,6 +100,9 @@ public class JettyServletManager
JettyHTTPServerEngine jettyServer;
try {
jettyServer = jettyFactory.createJettyHTTPServerEngine(url.getHost(), url.getPort(), "http");
// keep the port of this Apache CXF Jetty server engine
// in order to be able to destroy it when shutdowning.
this.ports.add(url.getPort());
} catch(Exception exc) {
throw new RuntimeException(exc);
}
......@@ -145,6 +154,24 @@ public class JettyServletManager
log.info("UnregisterServlet(uri=" + uri + ") done.");
}
/**
* @see ServletManager#shutdown()
*
* Added for issue http://jira.ow2.org/browse/FRASCATI-105
*/
public void shutdown()
{
// Obtain the Apache CXF Jetty server factory.
JettyHTTPServerEngineFactory jettyFactory =
BusFactory.getDefaultBus().getExtension(JettyHTTPServerEngineFactory.class);
// Shutdown all started Apache CXF Jetty server engines.
log.info("Shutdown all started Apache CXF Jetty server engines.");
for(int port : this.ports) {
log.info("Shutdown Apache CXF Jetty server engine on port " + port + ".");
jettyFactory.destroyForPort(port);
}
}
}
class ServletHandler extends JettyHTTPHandler
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- OW2 FraSCAti: Servlet Manager -->
<!-- Copyright (C) 2013 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): -->
<!-- -->
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
name="org.ow2.frascati.FraSCAti">
<service name="servlet-manager" promote="assembly-factory/servlet-manager">
<interface.java interface="org.ow2.frascati.servlet.api.ServletManager"/>
</service>
</composite>
<?xml version="1.0" encoding="UTF-8"?>
<!-- OW2 FraSCAti: Servlet Manager -->
<!-- Copyright (C) 2011 INRIA, University of Lille 1 -->
<!-- Copyright (C) 2011-2013 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 -->
......@@ -26,6 +26,10 @@
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
name="org.ow2.frascati.assembly.factory.AssemblyFactory">
<service name="servlet-manager" promote="servlet-manager/servlet-manager">
<interface.java interface="org.ow2.frascati.servlet.api.ServletManager"/>
</service>
<component name="servlet-manager">
<implementation.java class="org.ow2.frascati.servlet.manager.JettyServletManager"/>
<service name="servlet-manager">
......
/**
* OW2 FraSCAti : Test Servlet with Apache CXF
* Copyright (C) 2010 - 2012 Inria, USTL
* OW2 FraSCAti: Test Servlet with Apache CXF
* Copyright (C) 2010 - 2013 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
......@@ -14,7 +14,8 @@
*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
......@@ -43,8 +44,6 @@ import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.control.ContentController;
import org.objectweb.fractal.api.control.NameController;
import org.ow2.frascati.FraSCAti;
import org.ow2.frascati.servlet.api.ServletManager;
......@@ -67,10 +66,9 @@ public class FraSCAtiTest {
public void newFraSCAti() throws Exception {
//init FraSCAti
frascati = FraSCAti.newFraSCAti();
//Found the ServletManager Component
Component c = getComponent("assembly-factory/servlet-manager");
manager = (ServletManager) frascati.getService(c, "servlet-manager", ServletManager.class);
//Found the ServletManager service
manager = frascati.getService(frascati.getFrascatiComposite(), "servlet-manager", ServletManager.class);
//Create a new Servlet
Servlet s1 = new ServletTest(message1);
Servlet s2 = new ServletTest(message2);
......@@ -98,89 +96,39 @@ public class FraSCAtiTest {
manager.registerServlet(URL_PREFIX + "myServlet", s1);
assertEquals(message1,callServlet(u1));
assertEquals(message2,callServlet(u2));
}
private String callServlet(URL u) throws IOException {
manager.shutdown();
frascati.close();
}
private String callServlet(URL u) throws IOException
{
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
u.openConnection().getInputStream()));
String message = in.readLine();
System.out.println(message);
return message;
}
/**
* Return a component in FraSCAti from its path
*
* @param path
* the path of the Component
* @return
* the found Component
* @throws Exception
*/
private Component getComponent(String path) throws Exception {
String[] pathElements = path.split("/");
Component c = frascati.getCompositeManager().getTopLevelDomainComposite();
ContentController controller = (ContentController) c.getFcInterface("content-controller");
c = controller.getFcSubComponents()[0];
for(String pathElement : pathElements){
c = getSubComponent(c,pathElement);
}
return c;
}
/**
* Return a sub-component from its name in the Component passed on as parameter
* @param c
* The Component where to look in
* @param name
* The name of the Component
* @return
* The found Component
* @throws Exception
*/
private Component getSubComponent(Component c,String name) throws Exception {
System.out.println("search " + name + " in " + c);
ContentController controller = (ContentController) c.getFcInterface("content-controller");
for(Component comp : controller.getFcSubComponents()){
NameController nameController = (NameController) comp.getFcInterface("name-controller");
String n = nameController.getFcName();
System.out.println("\t\tfound " + n + " in " + c);
if(name.equals(n)){
return comp;
}
}
return null;
}
/**
* A simple Helloworld Servlet
*/
@SuppressWarnings("serial")
private class ServletTest extends HttpServlet {
private class ServletTest extends HttpServlet
{
String message;
public ServletTest(String message){
public ServletTest(String message)
{
this.message = message;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println(message);
}
}
}
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