Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created August 7, 2013 07:15
Show Gist options
  • Save hamnis/6171880 to your computer and use it in GitHub Desktop.
Save hamnis/6171880 to your computer and use it in GitHub Desktop.
Boot jax-ws inside jetty
package antifusion;
import mock.itsm.CreateIncident;
import mock.netcool.UpdateIncident;
import org.eclipse.jetty.http.spi.JettyHttpServerProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import javax.xml.ws.Endpoint;
import java.io.File;
import java.net.URL;
public class JettyBooter {
private final int port;
private Server server;
public JettyBooter(int port) {
this.port = port;
server = new Server(port);
HandlerCollection handler = new HandlerCollection();
WebAppContext ctx = new WebAppContext();
ctx.setWar(new File(getRoot(), "target/antifusion.war").getAbsolutePath());
ctx.setExtractWAR(true);
ctx.setLogUrlOnStart(true);
ctx.setContextPath("/");
handler.addHandler(new ContextHandlerCollection());
handler.addHandler(ctx);
server.setHandler(handler);
JettyHttpServerProvider.setServer(server);
System.setProperty("com.sun.net.httpserver.HttpServerProvider", JettyHttpServerProvider.class.getName());
}
public void start() {
Endpoint createIncidentEndpoint = Endpoint.create(new CreateIncident());
createIncidentEndpoint.publish("http://localhost:" + port + "/mock/incident/Create/1.0");
Endpoint updateIncidentEndpoint = Endpoint.create(new UpdateIncident());
updateIncidentEndpoint.publish("http://localhost:" + port + "/mock/incident/Update/1.0");
try {
server.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void stop() {
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static File getRoot() {
URL location = JettyBooter.class.getProtectionDomain().getCodeSource().getLocation();
File parent = new File(location.getFile()).getAbsoluteFile();
while(!new File(parent, "pom.xml").exists()) {
parent = parent.getParentFile();
}
return parent;
}
public int getPort() {
return port;
}
public static void main(String[] args) throws Exception {
JettyBooter booter = new JettyBooter(1337);
booter.start();
booter.server.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment