Skip to content

Instantly share code, notes, and snippets.

@msakamoto-sf
Last active January 5, 2021 19:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save msakamoto-sf/5329207 to your computer and use it in GitHub Desktop.
Save msakamoto-sf/5329207 to your computer and use it in GitHub Desktop.
Jetty + GroovyServlet + Groovy Script = Start Jetty Anywhere !! You only need Groovy :)
@Grapes([
@Grab('org.eclipse.jetty.aggregate:jetty-all:8.1.10.v20130312'),
@Grab('com.h2database:h2:1.3.171'),
@Grab('javax.servlet:servlet-api:2.5'),
])
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.*
import org.eclipse.jetty.webapp.*
import javax.servlet.*
import javax.servlet.http.*
import java.net.*
import groovy.servlet.*
def cli = new CliBuilder(usage: 'start_jetty [-r] [-p] [--base]')
cli.with {
h longOpt: 'help', 'Show usage information'
r longOpt: 'root', args: 1, argName: 'context path', 'ContextPath, default is "/"'
p longOpt: 'port', args: 1, argName: 'port', 'Listening Port, default is 8090'
_ longOpt: 'base', args: 1, argName: 'base', 'Base Directory, default is "."(current working directory)'
}
def options = cli.parse(args)
if (options.h) {
cli.usage()
return
}
int argPort = 8090
if (options.p) { argPort = options.p.toInteger() }
String argContextPath = options.r ?: '/'
String argBaseDir = options.base ?: '.'
printf(
'starting... ContextPath=[%s], ListeningPort=[%d], BaseDir=[%s]%n',
argContextPath, argPort, argBaseDir)
def shutdownSignal = new Object()
class ShutdownServlet extends HttpServlet {
def notifier
def ShutdownServlet(Object theNotifier) {
this.notifier = theNotifier
}
void doGet(HttpServletRequest req, HttpServletResponse res) {
res.writer << "start shutdown."
res.writer.close()
synchronized(notifier) {
notifier.notifyAll()
}
}
}
class SampleContextListener implements ServletContextListener {
def SampleContextListener() {
println 'Customize SampleContextListener.'
}
void contextInitialized(ServletContextEvent sce) {
ServletContext ctx = sce.getServletContext()
ctx.setAttribute('sample.attr', 'Hello, ServletContext')
ctx.log('Customize Context Initialize Event Here.')
}
void contextDestroyed(ServletContextEvent sce) {
ServletContext ctx = sce.getServletContext()
ctx.log('Customize Context Destory Event Here.')
}
}
def server = new Server(argPort)
def context = new WebAppContext(server, argBaseDir, argContextPath)
context.setWelcomeFiles(['index.groovy', 'index.html'] as String[])
context.addServlet(GroovyServlet, "*.groovy")
context.addServlet(new ServletHolder(new ShutdownServlet(shutdownSignal)), '/__stop__')
context.addServlet(TemplateServlet, "*.gsp")
context.addServlet(org.h2.server.web.WebServlet, '/console/*')
/* avoid file locking on windows platform.
* see:
* http://stackoverflow.com/questions/184312/how-to-make-jetty-dynamically-load-static-pages
* https://code.google.com/p/run-jetty-run/issues/detail?id=7
* http://kaanmutlu.wordpress.com/2012/01/10/avoiding-jetty-locking-static-files/
* https://www.assembla.com/wiki/show/liftweb/Fix_file_locking_problem_with_jettyrun
*/
DefaultServlet defaultServlet = new DefaultServlet()
ServletHolder holder = new ServletHolder(defaultServlet)
holder.setInitParameter("useFileMappedBuffer", "false")
context.addServlet(holder, '*.html')
context.addServlet(holder, '*.js')
context.addServlet(holder, '*.css')
context.addEventListener(new SampleContextListener())
server.start()
println '----------------------'
println '"(context path)/__stop__" is ready for SHUTDOWN'
println '----------------------'
Thread.startDaemon {
synchronized(shutdownSignal) {
shutdownSignal.wait()
}
server.stop()
}
server.join()
@pedemonte
Copy link

cool! is it possibile to add the support for jsp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment