Saturday, July 11, 2009

Scala on Google App Engine playing it nice with Java GWT and JDO

Few days ago I gave a short review on the SF Bay Area Google App Engine Developers meetup about Scala on the Google App Engine. If you would like to dive into it, here are some of the details with examples taken from the code of the newspipes app available at GitHub.
Scala playes out very nicely with GAE with the same pros and cons a Java application would have. There is one thing Scala can't do which is to take part in any GWT related code. It has nothing to do with GAE since GWT is designed to work only with Java code. If you wish to use GWT for the front end and still use Scala for the back end - no problems. You write your GWT code with Java, write the GWT service interface with Java so the GWT compiler will be happy, and implement the service with Scala. The service implementation is hooked up with GWT in the web.xml as the http end point.

Next step will be using JDO. If your persistent class is used by GWT then you must use Java. Else you may use Scala which, typical to Scala, makes the code much smaller. For example:

import javax.jdo.annotations.{Extension, Persistent, IdGeneratorStrategy, PrimaryKey, PersistenceCapable, IdentityType}

@PersistenceCapable{val identityType = IdentityType.APPLICATION}
class SearchKeyword(
@Persistent{val valueStrategy = IdGeneratorStrategy.IDENTITY}
@Extension{val vendorName="datanucleus", val key="gae.encoded-pk", val value="true"}
var key: String,
@Persistent var value: String,
@Persistent var count: Int)
Now we need to compile it all. Using the Eclipse plugins might be nice but you should have a proper build file if you want to do more then "Hello World". This build will run test, deploy in production and test environment and handle Java, Scala, GWT and JDO compilations. On top of it, I use IntelliJ IDEA which has the best Scala support at the moment (competition is great!) and you really don't want to be strained into an IDE.
To do the Scala part first run the Scalac compiler to the same place you'll compile the Java code a step after:
  <target name="compile" depends="copyjars" description="Compiles Java source and copies other source files to the WAR.">
<mkdir dir="${dstdir}" />
<copy todir="${dstdir}">
<fileset dir="${srcdir}">
<exclude name="${javafiles}" />
<exclude name="${scalafiles}" />
<src path="${srcdir}"/>
<classpath refid="project.classpath"/>
<javac srcdir="${srcdir}"
debug="on" />
Then comes the JDO enhancement part which takes the compiled source code and does its stuff. Note that it does not care at this point where the classes came from (Java or Scala) since it works on the *.class files the compilers placed in ${dstdir} (war/WEB-INF/classes).
  <target name="datanucleusenhance" depends="compile"
description="Performs JDO enhancement on compiled data classes.">
<enhance_war war="war" />
That's about it.


Mark Essel October 22, 2009 at 2:40 PM  

Howdy. I'm a big fan of scala and recently got a nice template of lift/scala on the google app engine to build on. But I am working on a php project and would like to be able to have a maven build that works with quercus/dependent jars, the lift/scala stuff, and of course java. I'd also like to be able to use the data store.

Do you think having a nice set of batch files to create all this would be possible?

I have the separate pieces (an example of php/quercus/java gae, and a lift/scala/java gae but not both together). I do use an IDE to edit but build on the command line.

Any pointers would be appreciated.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.