Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Rusin committed Jul 18, 2010
1 parent fa7d296 commit 3c114a3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
package org.apache.jetty.service.example;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jetty.service.api.JettyService;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.resource.FileResource;
import org.mortbay.resource.Resource;

public class HelloWorld {

private JettyService jettyService;
private ContextHandlerCollection handler;
private Handler reg;

public void setJettyService(JettyService jettyService) {
this.jettyService = jettyService;
}

public void init() {
public void init() throws Exception {
handler = new ContextHandlerCollection();
handler.addContext("app", "app").setHandler(
new AbstractHandler() {

handler.addContext("/app", "app").setHandler(new AbstractHandler() {
public void handle(String target, HttpServletRequest request,
HttpServletResponse response, int arg3) throws IOException,
ServletException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("Hello World Handler");
response.getWriter().println("<h1>Hello World Handler</h1>");

Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
}
});
handler.addContext("","static").setHandler(new ResourceHandler());
ResourceHandler r = new ResourceHandler();

Resource r2 = new FileResource(getClass().getResource("/static"));
r.setBaseResource(r2);
ContextHandler ch = handler.addContext("","");
ch.setHandler(r);
ch.setClassLoader(getClass().getClassLoader());

jettyService.registerApp("helloWorld", handler);
reg = jettyService.registerApp("helloWorld", handler);
}

public void destroy() {
jettyService.unregisterApp(handler);
public void destroy() throws Exception {
jettyService.unregisterApp(reg);
}
}
16 changes: 16 additions & 0 deletions example-helloworld/src/test/java/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.apache.jetty.service.JettyServiceImpl;
import org.apache.jetty.service.example.HelloWorld;

public class Test {
public static void main(String[] args) throws Exception {
JettyServiceImpl a = new JettyServiceImpl();
a.init();

HelloWorld h = new HelloWorld();
h.setJettyService(a);
h.init();
Thread.sleep(100000);
h.destroy();
a.destroy();
}
}
7 changes: 7 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ org.springframework.osgi.service.importer
</plugins>
</build>
<dependencies>
<!--
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jetty-bundle</artifactId>
<version>6.1.24_1</version>
</dependency>
-->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.24</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.jetty.service.api.JettyService;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;

public class JettyServiceImpl implements JettyService {
Expand All @@ -20,11 +21,17 @@ public void destroy() throws Exception {
server.stop();
}

public void registerApp(String name, Handler handler) {
rootContext.addContext("/" + name, name).setHandler(handler);
public Handler registerApp(String name, Handler handler) throws Exception {
server.stop();
ContextHandler h = rootContext.addContext("/" + name, name);
h.setHandler(handler);
server.start();
return h;
}

public void unregisterApp(Handler handler) {
public void unregisterApp(Handler handler) throws Exception {
server.stop();
rootContext.removeHandler(handler);
server.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import org.mortbay.jetty.Handler;

public interface JettyService {
public void registerApp(String name, Handler handler);
public void unregisterApp(Handler handler);
public Handler registerApp(String name, Handler handler) throws Exception;
public void unregisterApp(Handler handler) throws Exception;
}

0 comments on commit 3c114a3

Please sign in to comment.