Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Works, hey?
  • Loading branch information
codahale committed Jan 18, 2011
0 parents commit dd7a69f
Show file tree
Hide file tree
Showing 15 changed files with 366 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea
lib_managed
src_managed
project/boot
target
src/main/java/com/yammer/streamie/data/pb
streamie.conf
atlassian-ide-plugin.xml
project/plugins/project/build.properties
logs
8 changes: 8 additions & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#Project properties
#Mon Jan 17 19:25:38 PST 2011
project.organization=com.yammer
project.name=dropwizard
sbt.version=0.7.5.RC0
project.version=0.0.1-SNAPSHOT
build.scala.versions=2.8.1
project.initialize=false
77 changes: 77 additions & 0 deletions project/build/DropWizard.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import sbt._
import maven._

class DropWizard(info: ProjectInfo) extends DefaultProject(info)
with IdeaProject
with MavenDependencies {
/**
* Publish the source as well as the class files.
*/
override def packageSrcJar = defaultJarPath("-sources.jar")
val sourceArtifact = Artifact.sources(artifactID)
override def packageToPublishActions = super.packageToPublishActions ++ Seq(packageSrc)

/**
* Repositories
*/
val codasRepo = "Coda's Repo" at "http://repo.codahale.com"
val googleMaven = "Google Maven" at "http://google-maven-repository.googlecode.com/svn/repository/"
val sunRepo = "Sun Repo" at "http://download.java.net/maven/2/"

/**
* Test Dependencies
*/
val simplespec = "com.codahale" %% "simplespec" % "0.2.0" % "test"
val mockito = "org.mockito" % "mockito-all" % "1.8.4" % "test"

/**
* Jersey Dependencies
*/
val jerseyGuice = "com.sun.jersey.contribs" % "jersey-guice" % "1.5-ea08"
val jerseyScala = "com.codahale" %% "jersey-scala" % "0.1.1"

/**
* Misc Dependencies
*/
val fig = "com.codahale" %% "fig" % "1.1.1"
val jerkson = "com.codahale" %% "jerkson" % "0.1.4"
val jackson = "org.codehaus.jackson" % "jackson-core-asl" % "1.7.1"
val metrics = "com.yammer" %% "metrics" % "2.0.0-BETA8-SNAPSHOT"

/**
* Logging Dependencies
*/
val slf4jVersion = "1.6.1"
val slf4jBindings = "org.slf4j" % "slf4j-log4j12" % slf4jVersion
val jul2slf4j = "org.slf4j" % "jul-to-slf4j" % slf4jVersion
val logula = "com.codahale" %% "logula" % "2.1.0"

/**
* Guice Dependencies
*/
val guiceServlet = "com.google.inject.extensions" % "guice-servlet" % "2.0"

/**
* Jetty Dependencies
*/
val servletApi = "javax.servlet" % "servlet-api" % "2.5"
val jettyVersion = "7.2.2.v20101205"
val jettyServlet = "org.eclipse.jetty" % "jetty-servlet" % jettyVersion
val jettyServlets = "org.eclipse.jetty" % "jetty-servlets" % jettyVersion

override def fork = forkRun(List(
"-server", // make sure we're using the 64-bit server VM
"-d64",
"-XX:+UseParNewGC", // use parallel GC for the new generation
"-XX:+UseConcMarkSweepGC", // use concurrent mark-and-sweep for the old generation
"-XX:+CMSParallelRemarkEnabled", // use multiple threads for the remark phase
"-XX:+AggressiveOpts", // use the latest and greatest in JVM tech
"-XX:+UseFastAccessorMethods", // be sure to inline simple accessor methods
"-XX:+UseBiasedLocking", // speed up uncontended locks
"-Xss128k", // reduce the thread stack size, freeing up space for the heap
"-Xmx500M", // same with the max heap size
// "-XX:+PrintGCDetails", // log GC details to stdout
// "-XX:+PrintGCTimeStamps",
"-XX:+HeapDumpOnOutOfMemoryError" // dump the heap if we run out of memory
))
}
7 changes: 7 additions & 0 deletions project/plugins/Plugins.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Plugins(info: sbt.ProjectInfo) extends sbt.PluginDefinition(info) {
val sbtIdeaRepo = "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
val sbtIdea = "com.github.mpeltonen" % "sbt-idea-plugin" % "0.2.0"

val codaRepo = "Coda Hale's Repository" at "http://repo.codahale.com/"
val mavenSBT = "com.codahale" % "maven-sbt" % "0.1.1"
}
52 changes: 52 additions & 0 deletions src/main/scala/com/yammer/dropwizard/ConfigurationModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.yammer.dropwizard

import com.google.inject.AbstractModule
import com.codahale.fig.Configuration
import org.apache.log4j.Level
import com.codahale.logula.Logging
import java.util.logging.Logger
import org.slf4j.bridge.SLF4JBridgeHandler

/**
*
* @author coda
*/
class ConfigurationModule(filename: String) extends AbstractModule {
private val config = new Configuration(filename)

def configure = {
configureLogging()
bind(classOf[Configuration]).toInstance(config)
}

private def configureLogging() {
val rootLogger = Logger.getLogger("")
rootLogger.getHandlers.foreach(rootLogger.removeHandler)
rootLogger.addHandler(new SLF4JBridgeHandler)
Logging.configure{log =>
log.registerWithJMX = true

log.level = Level.toLevel(config("logging.level").or("info"), Level.INFO)

for ((name, level) <- config("logging.loggers").asMap[String]) {
log.loggers(name.toString) = Level.toLevel(level, Level.INFO)
}

log.console.enabled = config("logging.console.enabled").or(true)

config("logging.console.threshold").asOption[String].foreach{l =>
log.console.threshold = Level.toLevel(l, Level.ALL)
}

if (config("logging.file.enabled").or(false)) {
log.file.enabled = true
log.file.filename = config("logging.file.filename").asRequired[String]
log.file.maxSize = config("logging.file.max_log_size_kilobytes").or(10240)
log.file.retainedFiles = config("logging.file.retain_files").or(1)
config("logging.file.threshold").asOption[String].foreach{l =>
log.file.threshold = Level.toLevel(l, Level.ALL)
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/main/scala/com/yammer/dropwizard/ProviderModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yammer.dropwizard

import com.google.inject.AbstractModule

/**
*
* @author coda
*/
abstract class ProviderModule extends AbstractModule {
def configure = {}
}
34 changes: 34 additions & 0 deletions src/main/scala/com/yammer/dropwizard/RequestLogHandlerModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.yammer.dropwizard

import org.eclipse.jetty.server.handler.RequestLogHandler
import com.codahale.fig.Configuration
import com.google.inject.{AbstractModule, Provides, Singleton}
import org.eclipse.jetty.server.NCSARequestLog

/**
* Given a Configuration instance, provides a RequestLogHandler instance.
*
* @author coda
*/
class RequestLogHandlerModule extends AbstractModule {
def configure = {}

@Provides
@Singleton
def provideRequestLogHandler(config: Configuration): RequestLogHandler = {
val log = new NCSARequestLog
log.setIgnorePaths(config("request_log.ignore_paths").asList[String].toArray)
config("request_log.append").asOption[Boolean].foreach(log.setAppend)
config("request_log.filename").asOption[String].foreach(log.setFilename)
config("request_log.extended").asOption[Boolean].foreach(log.setExtended)
config("request_log.include_cookies").asOption[Boolean].foreach(log.setLogCookies)
config("request_log.include_latency").asOption[Boolean].foreach(log.setLogLatency)
config("request_log.include_server").asOption[Boolean].foreach(log.setLogServer)
config("request_log.timezone").asOption[String].foreach(log.setLogTimeZone)
config("request_log.retain_days").asOption[Int].foreach(log.setRetainDays)

val handler = new RequestLogHandler()
handler.setRequestLog(log)
handler
}
}
23 changes: 23 additions & 0 deletions src/main/scala/com/yammer/dropwizard/ScanningGuiceContainer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.yammer.dropwizard

import com.google.inject.{Inject, Injector}
import java.io.File
import com.sun.jersey.api.core.{ResourceConfig, ClasspathResourceConfig}
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer
import com.sun.jersey.spi.container.servlet.WebConfig

/**
* A Guice Jersey container which scans the entire classpath for @Path
* and @Provider annotated classes.
*
* @author coda
*/
class ScanningGuiceContainer @Inject() (injector: Injector) extends GuiceContainer(injector) {
override def getDefaultResourceConfig(props: java.util.Map[String, Object], webConfig: WebConfig) = {
val config = new ClasspathResourceConfig(classpath)
config.getFeatures.put(ResourceConfig.FEATURE_DISABLE_WADL, true)
config
}

private def classpath = System.getProperty("java.class.path").split(File.pathSeparator)
}
64 changes: 64 additions & 0 deletions src/main/scala/com/yammer/dropwizard/ServerModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.yammer.dropwizard

import com.codahale.fig.Configuration
import org.eclipse.jetty.util.thread.QueuedThreadPool
import org.eclipse.jetty.server.bio.SocketConnector
import com.google.inject.{Injector, Singleton, Provides, AbstractModule}
import org.eclipse.jetty.server.handler.{HandlerCollection, RequestLogHandler}
import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
import com.yammer.metrics.core.MetricsServlet
import com.yammer.metrics.jetty.InstrumentedHandler
import org.eclipse.jetty.{server => jetty}
import com.codahale.logula.Logging

/**
*
* @author coda
*/
class ServerModule extends AbstractModule with Logging {
def configure = {}

@Provides
@Singleton
def provideServer(config: Configuration, injector: Injector): jetty.Server = {
val port = config("http.port").or(8080)
log.debug("Creating main SocketConnector on port %d", port)
val connector = new SocketConnector
config("http.hostname").asOption[String].foreach(connector.setHost)
connector.setForwarded(config("http.forwarded").or(false))
connector.setPort(port)
connector.setName("main")

val internalConnector = new SocketConnector
internalConnector.setPort(config("metrics.http_port").or(8081))
internalConnector.setName("internal")

val server = new jetty.Server
server.addConnector(connector)
server.addConnector(internalConnector)
server.setSendServerVersion(false)
server.setThreadPool(new QueuedThreadPool(config("http.max_connections").or(50)))
server.setStopAtShutdown(true)
server.setGracefulShutdown(config("http.shutdown_milliseconds").or(2000))

val handlers = new HandlerCollection

val context = new ServletContextHandler()
context.addServlet(new ServletHolder(injector.getInstance(classOf[ScanningGuiceContainer])), "/*")
context.setConnectorNames(Array("main"))
handlers.addHandler(new InstrumentedHandler(context))

val internalContext = new ServletContextHandler()
internalContext.addServlet(new ServletHolder(new MetricsServlet), "/*")
internalContext.setConnectorNames(Array("internal"))
handlers.addHandler(internalContext)

if (config("request_log.enabled").or(false)) {
handlers.addHandler(injector.getInstance(classOf[RequestLogHandler]))
}

server.setHandler(handlers)

server
}
}
30 changes: 30 additions & 0 deletions src/main/scala/com/yammer/dropwizard/Service.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.yammer.dropwizard

import org.eclipse.jetty.{server => jetty}
import com.google.inject.{Stage, Guice, Module}
import com.codahale.logula.Logging

/**
*
*
* @author coda
*/
abstract class Service extends Logging {
def modules: Iterable[Module]

def name: String

def run(filename: String) {
val includedModules = Seq(new ConfigurationModule(filename),
new ServerModule,
new RequestLogHandlerModule)
val allModules = (includedModules ++ modules).toArray
val injector = Guice.createInjector(Stage.PRODUCTION, allModules:_*)
log.debug("Using modules: %s", allModules.mkString(", "))

log.info("Starting %s", name)
val server = injector.getInstance(classOf[jetty.Server])
server.start()
server.join()
}
}
6 changes: 6 additions & 0 deletions src/test/resources/example.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"saying": "Oh, hello!",
"logging": {
"level": "info"
}
}
13 changes: 13 additions & 0 deletions src/test/scala/com/yammer/dropwizard/examples/ExampleService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.yammer.dropwizard.examples

import com.yammer.dropwizard.Service

/**
*
* @author coda
*/
class ExampleService extends Service {
def name = "Example"

def modules = Seq(new SayingModule)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.yammer.dropwizard.examples

import com.google.inject.Inject
import javax.ws.rs.{Produces, GET, Path}
import javax.ws.rs.core.MediaType

@Path("/hello-world")
@Produces(Array(MediaType.APPLICATION_JSON))
class HelloWorldResource @Inject() (saying: String) {
@GET
def sayHello = Seq(saying)
}
8 changes: 8 additions & 0 deletions src/test/scala/com/yammer/dropwizard/examples/Runner.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.yammer.dropwizard.examples

object Runner {
def main(args: Array[String]) {
val service = new ExampleService
service.run(args.head)
}
}
11 changes: 11 additions & 0 deletions src/test/scala/com/yammer/dropwizard/examples/SayingModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yammer.dropwizard.examples

import com.yammer.dropwizard.ProviderModule
import com.google.inject.{Singleton, Provides}
import com.codahale.fig.Configuration

class SayingModule extends ProviderModule {
@Provides
@Singleton
def provideSaying(config: Configuration): String = config("saying").asRequired[String]
}

0 comments on commit dd7a69f

Please sign in to comment.