You are currently browsing the tag archive for the ‘gae’ tag.

Google App Engine recently added support for Java. As a beautiful side-effect of this, Scala is supported. Unfortunately the delicious Scala-based web framework Lift does not work there, due to sandbox constraints in GAE around thread creation and JDBC access. (Though there is hope that David Pollak will get core Lift working with GAE as early as mid April*).

I’ve just uploaded a hello world app that is written with a JSP delegating to Scala. (It’s not worth clicking through to, but it is here if you need proof). Here is what I needed to do, once I had signed up and downloaded the SDK.

First you need to create the right project structure. You can get it automagically if you are using the Eclipse plugin, but I find that particular IDE to be a choking hazard. Here’s the structure I created manually.

  • project_root
    • src
    • web
      • WEB-INF
        • classes
        • lib

The one-and-only page is the JSP war/index.jsp. It delegates to the Scala class.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="au.com.loftinspace.scalachess.LoginSnippet" %>
<html>
  <body>
    <h1>Just a test</h1>
    <%= LoginSnippet.render(request) %>
  </body>
</html>

The custom Scala object providing the XHTML snippet to display is defined in LoginSnippet.scala

package au.com.loftinspace.scalachess

object LoginSnippet {
    def render(request: _root_.javax.servlet.http.HttpServletRequest) = {
        import _root_.com.google.appengine.api.users._
        val userService = UserServiceFactory.getUserService
        val user = userService.getCurrentUser

        if (user == null) {
            <p>Hello! <a href={userService.createLoginURL
                (request.getRequestURI)}>Sign in</a>
                just for kicks.</p>
        } else {
            <p>Hello, {user.getNickname}! Now you can
                <a href={userService.createLogoutURL(
                request.getRequestURI)}>sign out</a> again.</p>
        }
    }
}

Scala classes need to be compiled to web/WEB-INF/classes (just as Java classes would). When compiling the class I required the following on the classpath:

  • appengine-java-sdk/lib/shared/geronimo-servlet_2.5_spec-1.2.jar (for HttpServletRequest); and
  • appengine-java-sdk/lib/user/appengine-api-1.0-sdk-1.2.0.jar (for the User functionality)

Some jars need to be deployed with your application. This simply means placing them in web/WEB-INF/lib. The jars I required were:

  • scala-library.jar (for the scala -> java bridging objects you get when scala is compiled, and several classes from scala.xml._ which I have used implicitly by defining the inline XML in the render function).
  • appengine-api-1.0-sdk-1.2.0.jar (which surprises me a little, as I thought GAE could provide this itself)

This is a reworked version of login as seen in the SDK’s demo application GuestBook. It is worth noting that the User functionality allows any username when running under the SDK’s development server, but integrates with Google account authentication once published to GAE.

Update: David Pollak – In case you missed it, Lift *does* run on the Google App Engine http://lift-example.appspot.com/index