Deploying Scala resource classes

In my last blog entry i neglected to mention how i was deploying and testing Scala classes.

I used the light weight HTTP server with the following Scala code: 

 object Server {
import com.sun.ws.rest.api.container.ContainerFactory
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import java.net.InetSocketAddress
import java.util.HashSet

def main(args: Array[String]) {
     val resources = new HashSet()
     resources.add(classOf[MyResource])
        val handler =
            ContainerFactory.createContainer(classOf[HttpHandler], resources).
        asInstanceOf[HttpHandler];
        val server = HttpServer.create(
            new InetSocketAddress(9998), 0);
        server.createContext("/service", handler);
        server.setExecutor(null);
        server.start();
        println(server);
        Thread sleep 100000
    }
}

Eduardo wanted to know if it is possible to deploy Scala resource classes in Glassfish. Here is how.

1) Create a new Web application in NetBeans called "ScalaWebApplication".

2) Create a new package called "com.sun.ws.rest.samples.myscala.resources".

3) Create a file ScalaResource.scala in the new package with the following code:

import java.util.List
import javax.ws.rs.{UriTemplate, HttpMethod, ProduceMime, UriParam}
import javax.ws.rs.core.{HttpContext, UriInfo, HttpHeaders}
import scala.collection.mutable.JavaMapAdaptor

package com.sun.ws.rest.samples.myscala.resources {

@UriTemplate("/{id}")
class ScalaResource(@UriParam("id") id: String,
                 @HttpContext var uriInfo: UriInfo,
                 @HttpContext var headers: HttpHeaders) {

    var hmap = new JavaMapAdaptor[String, List](
    headers.getRequestHeaders())

    @ProduceMime(Array("text/html"))
    @HttpMethod
    def getMe() = <html>
    <body>
    <p>ID: { id }</p>
    <p>URI : { uriInfo.getAbsolute() }</p>
    <table>
        <tr>
        <th>Header</th>
        <th>Value</th>
        </tr>
        { for (header <- hmap) yield
        <tr>
        <td>{ header._1 }</td>
        <td>{ header._2 }</td>
        </tr>
        }
    </table>
    </body>
</html>.toString()
}
}

4) Create a file WebResources.scala in the new package with the following code: 

package com.sun.ws.rest.samples.myscala.resources;

import com.sun.ws.rest.api.core.DefaultResourceConfig;

class WebResources extends DefaultResourceConfig {
    getResourceClasses().add(classOf[ScalaResource]);
}

5) Modify the web.xml to be as follows:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
  <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.ws.rest.impl.container.servlet.ServletAdaptor</servlet-class>
    <init-param>
      <param-name>webresourceclass</param-name>
        <param-value>com.sun.ws.rest.samples.myscala.resources.WebResources</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>Jersey Web Application</servlet-name>
      <url-pattern>/\*</url-pattern>
    </servlet-mapping>
</web-app>

6) Add the following to the project build.xml

    <property
        name="scala.home"
        value="${scala-2.6.0-final}/share/scala"
    />
  
    <target name="-pre-compile">
        <taskdef resource="scala/tools/ant/antlib.xml">
            <classpath>
                <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
                <pathelement location="${scala.home}/lib/scala-library.jar"/>
            </classpath>
        </taskdef>
              
        <path id="build.classpath">
            <path path="${javac.classpath}"/>
            <pathelement location="${build.classes.dir}"/>
        </path>
      
        <scalac srcdir="${src.dir}"
                destdir="${build.classes.dir}"
                classpathref="build.classpath"
                target="jvm-1.5"
                force="changed">
            <include name="\*\*/\*.scala"/>
        </scalac>
    </target>

7) Add the directory location of the Scala 2.6.0 final distribution to the nbproject/project.properties file using the property name "scala-2.6.0-final". (This is the directory location that contains the directories "bin" and "share".)

8) Add the scala library jar "${scala-2.6.0-final}/share/scala/lib/scala-library.jar" to the list of compile libraries.

9) Add the following jars in the Jersey 0.3 distribution to the list of compile libraries: lib/jersey.jar, lib/persistence-api-1.0.jar, lib/jsr311-api.jar.

10) Deploy.

11) Goto the URL:

http://localhost:8080/ScalaWebApplication/1

... and hopefully you will see some HTML.

Notice that there is no special configuration of Scala and this should work with Glassfish V2 and V3. All that is required is correct configuration of the web.xml with the Servlet Jersey uses to dispatch HTTP requests and the initialization parameter "webresourceclass", which points to the compiled Java class of WebResources.scala. 

Obviously this whole process could be better improved with some built in support for compiling Scala source files in NetBeans.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today