Saturday, November 17, 2012

Simple lift project

I have created a very small lift project that I will use to show some lift trick and how-to. The master branch shows the simplest project possible. It includes a basic build.sbt file:

 1:  name := "shiny-lana"
 2:  
 3:  oranization := "shiny.lana"
 4:  
 5:  version := "0.0.1-SNAPSHOT"
 6:  
 7:  scalaVersion := "2.9.1"
 8:  
 9:  seq(webSettings :_*)
10:  
11:  libraryDependencies ++= {
12:         val liftVersion = "2.4"
13:         Seq(
14:                 "net.liftweb" %% "lift-webkit" % liftVersion % "compile",
15:                 "org.mortbay.jetty" % "jetty" % "6.1.26" % "container",
16:                 "org.scalatest" %% "scalatest" % "1.8" % "test"          
17:         )
18:  }
19:  
20:  unmanagedResourceDirectories in Test <+= (baseDirectory) { _ / "src/main/webapp" }

It is a typical sbt build file. In line 9 , we configure the web plugin so we can use container:start to start a server with the running application. In line 20, we include the src/main/webapp directory in the classpath for test so we can test the snippets. The plugins.sbt is a single line to include the xsbt-web-plugin.

The Boot class include the most basic:

 1:  class Boot {
 2:  
 3:    def boot {
 4:  
 5:      LiftRules.addToPackages("shiny.lana")
 6:      LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
 7:      LiftRules.htmlProperties.default.set((r: Req) => new Html5Properties(r.userAgent))
 8:  
 9:    }
10:  }

We still include a small test to check that we're getting the Hello World message:

 1:  class WorldSpec extends FlatSpec with ShouldMatchers {
 2:  
 3:    "The index" should "say Hello World !" in  {
 4:  
 5:      val session = new LiftSession("/", "1", Empty)
 6:      S.initIfUninitted(session){
 7:        new Boot().boot
 8:        val page = S.session.get.findAndProcessTemplate(List("index")).openTheBox
 9:        page.text should include("Hello World !")
10:      }
11:    }
12:  }

The complete code is available on github https://github.com/arussel/shiny-lana

Sunday, September 30, 2012

Accessing static final variable of java inner class

I've seen this nice trivia in the scala mailing list. Zang was asking "how do I access a final static variable of a java class ?", such as:

public class Foo {
 public class Bar {
     public static final String MY_STRING = "myString";
 }
}

Som brought the answer quickly, using:

null.asInstanceOf[Foo].Bar.MY_STRING

Sunday, September 23, 2012

ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException

I've hit a OffsetTooBigException (java.lang.Error: ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException: offset too big to fit in 16 bits: 36580) when trying to compile a long list of case. It is easily reproducable with this code:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r
  test match {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }
}

The problem comes from the compiler creating for nested extractor huge sized bytecode. Apparently it is fixed for the 2.10 release. If you're not using it, the work around is easy, just cut the cases into pieces:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r

  val firstPatternSet : PartialFunction[String, String] = {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
  }

  val secondPatternSet: PartialFunction[String, String] = {
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }

 firstPatternSet(test) orElse secondPatternSet(test)
}

Monday, September 17, 2012

Using SBT in Debug mode

When running sbt, you can pass options using SBT_OPTS. The following line allows to run sbt in debug mode, you can bind your debugger to port 5004. It suspend the server as long as the debugger is not bound. Not the option I would normally use, but this allows to check that sbt did get the proper options.

SBT_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5004" sbt

The really nice thing about it, is, because sbt doesn't spawn a new process when starting the console, you can also debug the scala console started from sbt.

Sunday, September 2, 2012

Showing scala code with orgmode

To code in scala I use either Eclipse or Intellij. To write my blog and do many other things I use Emacs and orgmode. To have syntax highlighting in orgmode, you need to have a mode for the language you are using (thanks to Bastien and Thorsten for their help). So to get a nicely colored code snippet in scala. First download scala-mode:

svn co http://lampsvn.epfl.ch/svn-repos/scala/scala-tool-support/trunk/src/emacs/ scala-mode

Then load it into emacs:

(add-to-list 'load-path "/path/to/scala-mode")
(require 'scala-mode-auto)

and now to create a scala snippets nicely exported, in orgmode:

#+begin_src scala
/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }
#+end_src

will export like

/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }

To create a blog post, I just export the file to html and copy/paste to blogger. You can see the difference with my previous post that was using the java highlighting.

Wednesday, August 29, 2012

Processing *.csv file in lift

Lift dispatch uses a Req to match a request with a response, so if you want to process the request send to the path /foo/bar.html, you can have the request:

case Req("foo" :: "bar" ::  _, "html", GetRequest)       => returnTheResponse()

Now, it would seems normal, if you want to return a csv file (request /foo/bar.csv), you would do something like this:

case Req("foo" :: "bar" ::  _, "csv", GetRequest)       => returnTheCSVResponse()

I've banged my head a long time and went into the source to find out why my request was never processed. I finally found help in this Thread , lift only parse a set of suffix, and csv is not part of it. To add it, one just need to add in boot:

LiftRules.explicitlyParsedSuffixes += "csv"

Monday, August 27, 2012

Map#getOrElseUpdate

When manipulating data in a map, I often have to use this idiom, in java

Map map = new Map<>();
Value v = map.get(key);
if(v == null) {
  v = new DefaultValue();
  map.put(key, v)

}
// do something with v

In scala, I found this really nice function on the mutable.Map: getOrElseUpdate. The implementation is located on the MapLike trait and is pretty straightforward:

/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }

With this function, the previous code just become:

Value v = map.getOrElseUpdate(key, new DefaultValue())