Skip to content

Instantly share code, notes, and snippets.

@ReSTARTR
Created May 28, 2011 10:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ReSTARTR/996769 to your computer and use it in GitHub Desktop.
Save ReSTARTR/996769 to your computer and use it in GitHub Desktop.
Access from Scala to MongoDB on DotCloud.

This is as sbt project.

directory structure

  • mongodbsample/project/
    • build/MongodbSampleProject.scala
    • src/main
      • scala/MongoServlet.scala
      • webapp/WEB-INF/web.xml
package main.scala
import javax.servlet.http.{HttpServlet,HttpServletRequest,HttpServletResponse}
import org.eclipse.jetty.server.Server
import java.io.PrintWriter
import java.net.URLEncoder
class MongoServlet extends HttpServlet {
import com.mongodb.casbah.Imports._
val conn = MongoConnection("mongo.example.dotcloud.com",5907)
val db = conn("sampledb")
db.authenticate("sampleuser", "samplepass")
val collection = db("col1")
def html = {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<h1>mongo sample</h1>
<form action="/" method="POST">
<ul>
<li>id:<input type="test" name="id" value="" /></li>
<li>name:<input type="test" name="name" value="" /></li>
</ul>
<input type="submit" value="submit" />
</form>
</div>
<div>
<ul>{
collection.find().sort(MongoDBObject("_id" -> -1)).limit(20)
.map( (x:DBObject) => <li>{x.get("id")}:{x.get("name")}</li> )
}</ul>
</div>
</body>
</html>
}
override def doPost(req:HttpServletRequest, res:HttpServletResponse) = {
res.setContentType("text/html;charset=UTF-8")
val out = res.getWriter()
req.setCharacterEncoding("UTF-8")
val id = req.getParameter("id")
val name = req.getParameter("name")
(id.length, name.length) match {
case (0,0) => // do nothing
case _ => {
val user = MongoDBObject("id"->id, "name"->name)
collection += user
}
}
out.println(html)
}
override def doGet(req:HttpServletRequest, res:HttpServletResponse) = {
res.setContentType("text/html;charset=UTF-8")
val out = res.getWriter()
out.println(html)
}
}
import sbt._
class MongodbSampleProject(info: ProjectInfo) extends DefaultWebProject(info) {
/*
* Jetty7
*/
val JETTY7 = "7.3.1.v20110307"
val servletapi = "javax.servlet" % "servlet-api" % "2.5" % "compile"
val jetty7Server = "org.eclipse.jetty" % "jetty-server" % JETTY7 % "compile,test"
val jetty7Servlets = "org.eclipse.jetty" % "jetty-servlets" % JETTY7 % "compile,test"
val jetty7Webapp = "org.eclipse.jetty" % "jetty-webapp" % JETTY7 % "compile,test"
/**
* MongoDB
*/
val casbah = "com.mongodb.casbah" % "casbah_2.9.0-1" % "2.1.5.0"
/*
* DotCloud
*/
val copyDirectory = path("release")
val copyFileName = copyDirectory / "root.war"
val dotApplicationName = "example"
val dotServiceName = dotApplicationName + ".samplemongo"
import Process._
/*
* 'sbt dot_create'
*/
lazy val dot_create = task {
val lists:Stream[String] = "dotcloud list" lines_!;
def exists(x:String) = { lists.exists( _.indexOf(x)>=0) }
exists(dotApplicationName) match {
case false => "dotcloud create " + dotApplicationName ! log
case _ => log.warn("application is already exists.: " + dotApplicationName )
}
exists(dotServiceName) match {
case false => {
val res = "dotcloud deploy -t java " + dotServiceName !!;
res.indexOf("Created") >= 0 match {
case true => log.info("success to deproy")
case _ => log.info("fail to deproy")
}
}
case _ => log.warn("service is already exists.")
}
None
}
/*
* 'sbt dot_prepare'
*/
lazy val dot_prepare = task {
log.info("mkdir")
"mkdir " + copyDirectory !;
log.info( "copy war file to " + copyFileName )
val files:Stream[String] = "find ./target -name *.war -exec ls -t {} ;" lines_!;
if (files.length > 0 ) {
"cp " + files.head.toString + " " + copyFileName + "" !
}
None
}
/*
* 'sbt dot_push'
*/
lazy val dot_push = task {
import Process._
log.info("push to dotcloud")
"dotcloud push " + dotServiceName + " " + copyDirectory ! log; None
}.dependsOn(`package`, dot_prepare)
}
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>mongosample</display-name>
<servlet>
<servlet-name>mongo</servlet-name>
<servlet-class>main.scala.MongoServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mongo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment