Commit 789a9cf9 authored by Munilla Christophe's avatar Munilla Christophe

no not use destroyForPort method, remove only the specified Servlet

update test
parent 38239c6b
......@@ -43,6 +43,7 @@ import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine;
import org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
......@@ -124,10 +125,23 @@ public class JettyServletManager
// Obtain the Apache CXF Jetty server factory.
JettyHTTPServerEngineFactory jettyFactory =
BusFactory.getDefaultBus().getExtension(JettyHTTPServerEngineFactory.class);
//Obtain the Jetty server engine for the specific port
JettyHTTPServerEngine engine = jettyFactory.retrieveJettyHTTPServerEngine(port);
//Remove the server engine to the port map and stop it
jettyFactory.destroyForPort(port);
try {
//Retrieve the ContextHandler associated to the URL of the Servlet
ContextHandler contextHandler = engine.getContextHandler(servletURI.toURL());
//If not stopped, do it
if(!contextHandler.getState().equals(ContextHandler.STOPPED)){
contextHandler.stop();
}
//Destroy the context attached to the Servlet
contextHandler.destroy();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
log.info("UnregisterServlet(uri=" + uri + ") done.");
}
......
......@@ -26,8 +26,14 @@
package org.ow2.frascati.servlet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
......@@ -36,14 +42,10 @@ import javax.servlet.http.HttpServletRequest;
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.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Scope;
import org.ow2.frascati.FraSCAti;
import org.ow2.frascati.assembly.factory.api.CompositeManager;
import org.ow2.frascati.servlet.api.ServletManager;
/**
......@@ -51,25 +53,64 @@ import org.ow2.frascati.servlet.api.ServletManager;
*/
public class FraSCAtiTest {
ServletManager manager;
private String message1 = "hello from s1";
private String message2 = "hello from s2";
private URL u1;
private URL u2;
private ServletManager manager;
private FraSCAti frascati;
@Test
@Test
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);
//Create a new Servlet
Servlet s = new ServletTest();
Servlet s1 = new ServletTest(message1);
Servlet s2 = new ServletTest(message2);
//Create new URLs
u1 = new URL("http://localhost:8090/myServlet");
u2 = new URL("http://localhost:8090/myOtherServlet");
//Register, Unregister and Register it again
manager.registerServlet("http://localhost:8090/myServlet", s);
manager.registerServlet("http://localhost:8090/myServlet", s1);
manager.registerServlet("http://localhost:8090/myOtherServlet", s2);
assertEquals(message1,callServlet(u1));
assertEquals(message2,callServlet(u2));
manager.unregisterServlet("http://localhost:8090/myServlet");
manager.registerServlet("http://localhost:8090/myServlet", s);
try{
assertEquals(message1,callServlet(u1));
fail();
} catch(IOException e){
//expected
}
assertEquals(message2,callServlet(u2));
manager.registerServlet("http://localhost:8090/myServlet", s1);
assertEquals(message1,callServlet(u1));
assertEquals(message2,callServlet(u2));
}
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
*
......@@ -126,12 +167,16 @@ public class FraSCAtiTest {
@SuppressWarnings("serial")
private class ServletTest extends HttpServlet {
public ServletTest(){}
String message;
public ServletTest(String message){
this.message = message;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
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