Skip to content

Instantly share code, notes, and snippets.

@janih
Created November 26, 2013 12:41
Show Gist options
  • Save janih/7657713 to your computer and use it in GitHub Desktop.
Save janih/7657713 to your computer and use it in GitHub Desktop.
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.webapp.WebAppContext;
/**
* "Better than using the WTP adapters I prefer to use an embedded jetty. I just create a
* regular java project, let's call "embedded-jetty". I make the original webapp project a
* requirement to this project in the Projects section of the Java Build Path of the
* project properties. Than I create a class that start a jetty instance like this:
*
* <code>JettyServer</code>
*
* On the embedded-jetty project I create a "lib" folder and copy all the libs from the
* jetty/lib folder, then I add the libs on the Libraries of the project properties.
*
* Running and debugging the jetty embedded works great for me, the jsp and class reloading works like a charm"
*
* source: http://stackoverflow.com/questions/2415803/running-jetty-7-server-in-eclipse
*/
public class JettyServer {
public static String BASE_DIR = "/project/base/dir";
public static String CLASSPATH = "/class/path:/some/other/path/maybe";
public static int SERVER_PORT = 7001;
private static final List<Handler> handlers = new ArrayList<Handler>();
public static void main(String[] args) {
if (isRunning()) {
System.exit(0);
}
Server server = new Server(SERVER_PORT);
// webapp1 - http://localhost:SERVER_PORT/webapp1/
WebAppContext appwsContext = new WebAppContext("webapp1", "/webapp1");
appwsContext.setResourceBase(BASE_DIR + "\\webapp1\\target\\webapp1");
appwsContext.setDescriptor(BASE_DIR + "\\webapp1\\target\\webapp1\\WEB-INF\\web.xml");
appwsContext.setExtraClasspath(CLASSPATH);
appwsContext.setParentLoaderPriority(true);
handlers.add(appwsContext);
// webapp2 - http://localhost:SERVER_PORT/webapp2/
WebAppContext appwsContext = new WebAppContext("webapp2", "/webapp2");
appwsContext.setResourceBase(BASE_DIR + "\\webapp2\\target\\webapp2");
appwsContext.setDescriptor(BASE_DIR + "\\webapp2\\target\\webapp2\\WEB-INF\\web.xml");
appwsContext.setExtraClasspath(CLASSPATH);
appwsContext.setParentLoaderPriority(true);
handlers.add(appwsContext);
// http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Configuring_a_Context_Handler_Collection
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(handlers.toArray(new Handler[0]));
server.setHandler(contexts);
server.addLifeCycleListener(getServerStartupListener(BASE_DIR));
try {
server.start();
server.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
private static LifeCycle.Listener getServerStartupListener(final String baseDir) {
return new LifeCycle.Listener() {
final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
@Override
public void lifeCycleStopping(LifeCycle arg0) { }
@Override
public void lifeCycleStopped(LifeCycle arg0) { }
@Override
public void lifeCycleStarting(LifeCycle arg0) {
if (arg0 instanceof Server) {
System.out.println("Server is starting..");
startTime.set(System.currentTimeMillis());
}
}
@Override
public void lifeCycleStarted(LifeCycle arg0) {
if (arg0 instanceof Server) {
StringBuilder started = new StringBuilder("Started service(s): ");
StringBuilder notStarted = new StringBuilder();
for (Handler hand : handlers) {
if (hand instanceof WebAppContext) {
WebAppContext webAppCtx = (WebAppContext)hand;
if (webAppCtx.isAvailable()) {
started.append(" ").append(webAppCtx.getContextPath());
}
else {
notStarted.append(" ").append(webAppCtx.getContextPath());
}
}
}
System.out.println(started.toString());
if (!notStarted.toString().isEmpty()) {
System.out.println("Service(s) FAILED to start: " + notStarted.toString() + " !");
}
System.out.println(String.format("Server basedir: %s ", baseDir));
System.out.println(String.format("Server startup in %s ms.", (System.currentTimeMillis() - startTime.get())));
}
}
@Override
public void lifeCycleFailure(LifeCycle arg0, Throwable arg1) { }
};
}
private static boolean isRunning() {
Socket sock = null;
try {
sock = new Socket("localhost", SERVER_PORT);
return true;
}
catch (Exception e) {
return false;
}
finally {
if (sock != null) {
try { sock.close(); } catch (Exception e) {}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment