本文整理汇总了Scala中org.eclipse.jetty.servlet.ServletContextHandler类的典型用法代码示例。如果您正苦于以下问题:Scala ServletContextHandler类的具体用法?Scala ServletContextHandler怎么用?Scala ServletContextHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ServletContextHandler类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: probeForCycleClass
//设置package包名称以及导入依赖的类
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}
import javax.servlet.ServletContext
import nl.knaw.dans.lib.logging.DebugEnhancedLogging
import org.eclipse.jetty.ajp.Ajp13SocketConnector
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.ServletContextHandler
import org.scalatra._
import org.scalatra.servlet.ScalatraListener
import scala.util.Try
class ${javaName}Service(serverPort: Int, app: ${javaName}App) extends DebugEnhancedLogging {
import logger._
private val server = new Server(serverPort)
private val context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS)
context.addEventListener(new ScalatraListener() {
override def probeForCycleClass(classLoader: ClassLoader): (String, LifeCycle) = {
("anonymous", new LifeCycle {
override def init(context: ServletContext): Unit = {
context.mount(new ${javaName}Servlet(app), "/")
}
})
}
})
server.setHandler(context)
info(s"HTTP port is ${symbol_dollar}{serverPort}")
def start(): Try[Unit] = Try {
info("Starting service...")
server.start()
}
def stop(): Try[Unit] = Try {
info("Stopping service...")
server.stop()
}
def destroy(): Try[Unit] = Try {
server.destroy()
app.close()
}
}
示例2: Arguments
//设置package包名称以及导入依赖的类
package net.sayuan.kafka
import java.io.File
import java.util.concurrent.{Executors, TimeUnit}
import io.prometheus.client.exporter.MetricsServlet
import org.apache.log4j.{Level, Logger}
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
import org.json4s._
import org.json4s.jackson.JsonMethods._
case class Arguments(config: String = "config.json")
object KafkaOffsetExporter {
def main(args: Array[String]) {
implicit val formats = DefaultFormats
val parser = new scopt.OptionParser[Arguments]("kafka-offset-exporter") {
head("kafka-offset-exporter")
opt[String]("config") action { (x,c) => c.copy(config = x) } text("config file.")
help("help") text("prints this usage text")
}
parser.parse(args, Arguments()) match {
case Some(args) => {
val config = parse(new File(args.config)).extract[Config]
start(config)
}
case _ =>
}
}
def start(config: Config) {
val server = new Server(config.port)
val context = new ServletContextHandler()
context.setContextPath("/")
server.setHandler(context)
context.addServlet(new ServletHolder(
new MetricsServlet()), "/metrics")
val scheduler = Executors.newScheduledThreadPool(1)
scheduler.scheduleAtFixedRate(new KafkaOffsetUpdater(config), 0, config.interval, TimeUnit.SECONDS)
server.start()
server.join()
}
}
示例3: RackServer
//设置package包名称以及导入依赖的类
package coder36.rack
import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
import com.typesafe.scalalogging.LazyLogging
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
import scala.collection.mutable.Map
class RackServer extends LazyLogging {
val port : Int = 8080
var path : String = ""
val apps = scala.collection.mutable.Map[String,RackServlet]()
def map(path: String) : RackServer = {
this.path = path
this
}
def onto(rack: Rack) = {
apps(path) = new RackServlet(rack, path)
}
def start() : RackServer = {
val context = new ServletContextHandler(ServletContextHandler.SESSIONS)
context.setContextPath("/")
val server = new Server(port)
server.setHandler(context)
apps.foreach( app => context.addServlet(new ServletHolder(app._2), (app._1 + "/*").replaceAll("//", "/")) )
println(s"\u001B[34mRack started... listening for HTTP on /0.0.0.0:${port}")
server.start
this
}
}
class RackServlet(val rack: Rack, val path: String) extends HttpServlet {
override def service(request: HttpServletRequest, response: HttpServletResponse) {
val res = Middleware.BaseRack( rack, request, response ).call(Map('base_path -> path))
val status = res._1
val headers = res._2
val body = res._3
response.setStatus(status)
headers.foreach( (h) => response.addHeader(h._1, h._2))
if( body != null ) response.getWriter().write(body)
}
}
示例4: Server
//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders.main
import org.eclipse.jetty
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import org.glassfish.jersey.servlet.ServletContainer
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import scala.collection.JavaConverters._
import scala.util.control.NonFatal
import Server._
import org.eclipse.jetty.util.log.Slf4jLog
@Component
class Server(val jerseyApp : JerseyApp) {
def run() : Unit = {
jetty.util.log.Log.setLog(new Slf4jLog())
val port = System.getenv().asScala.getOrElse("PORT", "80").toInt
val context = new ServletContextHandler(ServletContextHandler.SESSIONS)
context.setContextPath("/")
val jettyServer = new jetty.server.Server(port)
jettyServer.setHandler(context)
val servletContainer = new ServletContainer(jerseyApp)
val servletHolder = new ServletHolder(servletContainer)
context.addServlet(servletHolder, "/*")
val jerseyServlet = servletHolder
jerseyServlet.setInitOrder(0)
try {
Log.info("Starting jetty")
jettyServer.start()
Log.info("Jetty started")
jettyServer.join()
Log.info("Jetty exited")
} finally {
Log.info("Destroying jetty")
try {
jettyServer.destroy()
} catch {
case NonFatal(e) => Log.error("Error during Jetty shutdown", e)
}
}
}
}
object Server {
val Log = LoggerFactory.getLogger(classOf[Server])
}