Tuesday, September 25, 2007

Fast Web Development With Scala

I am currently experimenting with Scala. It seems quite convenient for web applications. Using Tomcat, it is possible to have a very productive developer environment.
Here is a sample Embedded Tomcat you can start in a Scala project:
import java.io._;
import org.apache.catalina._;
import org.apache.catalina.startup._;

object TomcatScalaServer {
  
  val CATALINAHOME : File = new File("../newsbeef.com");
  val WEBAPPS : File = new File(CATALINAHOME,"webapps");
  val ROOT : File = new File(CATALINAHOME,"web");
  val HOSTNAME : String = "localhost";
  val PORT : int 8080;
  
  def await() {
     whiletrue ) {
          try {
            System.out.println("sleeping 100s");
              Thread.sleep100000 );
          catch {
            case ie : InterruptedException =>;
          }
      }
  }
  
  def start() {
    val server = new Embedded();
    server.setCatalinaHome(CATALINAHOME.getAbsolutePath());

    val engine = server.createEngine();
    engine.setDefaultHost(HOSTNAME);

    val host = server.createHost(HOSTNAME, WEBAPPS.getAbsolutePath());
    engine.addChild(host);

    val context = server.createContext("", ROOT.getAbsolutePath());
     context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
     context.setReloadable(true);
    host.addChild(context);

    server.addEngine(engine);

    val http = server.createConnector(HOSTNAME, PORT, false);
    server.addConnector(http);

    server.start();
  }
  
  def main(args: Array[String]) {
    start();
    await();
  }

}

Here is a sample Scala Servlet outputing html directly. This is a simple example, but it shows something important. With Scala, the view layer can just be regular scala classes. There is no need for JSP or other templating languages as Scala already embbeds XML very nicely. By using the reloadable feature of Tomcat (there are also other pure Scala ways) and Eclipse autocompile, changes are instantanously taken in account.
import javax.servlet.http._;

class ScalaServlet extends HttpServlet {

  override def init() {
  }
  
  override def doGet(request : HttpServletRequest , response : HttpServletResponse{
    service(request, response)
  }
  
  override def service(req: HttpServletRequest,resp: HttpServletResponse) { 
    val pw = resp.getWriter();
    var output = <html>
    <head><title>Scala Servlet Test</title></head>
    <body>
      <h1>Hello World!</h1>
    </body>
    </html>
    pw.println(output);
    pw.flush();
  }
}

Now I am eagerly waiting for improvements in the Eclipse Scala plugin (Organise imports, class navigation).

Fast Web Development With Scala

I am currently experimenting with Scala. It seems quite convenient for web applications. Using Tomcat, it is possible to have a very productive developer environment.
Here is a sample Embedded Tomcat you can start in a Scala project:
import java.io._;
import org.apache.catalina._;
import org.apache.catalina.startup._;

object TomcatScalaServer {
  
  val CATALINAHOME : File = new File("../newsbeef.com");
  val WEBAPPS : File = new File(CATALINAHOME,"webapps");
  val ROOT : File = new File(CATALINAHOME,"web");
  val HOSTNAME : String = "localhost";
  val PORT : int 8080;
  
  def await() {
     whiletrue ) {
          try {
            System.out.println("sleeping 100s");
              Thread.sleep100000 );
          catch {
            case ie : InterruptedException =>;
          }
      }
  }
  
  def start() {
    val server = new Embedded();
    server.setCatalinaHome(CATALINAHOME.getAbsolutePath());

    val engine = server.createEngine();
    engine.setDefaultHost(HOSTNAME);

    val host = server.createHost(HOSTNAME, WEBAPPS.getAbsolutePath());
    engine.addChild(host);

    val context = server.createContext("", ROOT.getAbsolutePath());
     context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
     context.setReloadable(true);
    host.addChild(context);

    server.addEngine(engine);

    val http = server.createConnector(HOSTNAME, PORT, false);
    server.addConnector(http);

    server.start();
  }
  
  def main(args: Array[String]) {
    start();
    await();
  }

}

Here is a sample Scala Servlet outputing html directly. This is a simple example, but it shows something important. With Scala, the view layer can just be regular scala classes. There is no need for JSP or other templating languages as Scala already embbeds XML very nicely. By using the reloadable feature of Tomcat (there are also other pure Scala ways) and Eclipse autocompile, changes are instantanously taken in account.
import javax.servlet.http._;

class ScalaServlet extends HttpServlet {

  override def init() {
  }
  
  override def doGet(request : HttpServletRequest , response : HttpServletResponse{
    service(request, response)
  }
  
  override def service(req: HttpServletRequest,resp: HttpServletResponse) { 
    val pw = resp.getWriter();
    var output = <html>
    <head><title>Scala Servlet Test</title></head>
    <body>
      <h1>Hello World!</h1>
    </body>
    </html>
    pw.println(output);
    pw.flush();
  }
}

Now I am eagerly waiting for improvements in the Eclipse Scala plugin (Organise imports, class navigation).

Monday, August 27, 2007

2 Months of Ubuntu on Mac Mini

I am finally happy with my OS. I had previously some complaints about MacOs X and the Mac Mini. It is now over, with Ubuntu, I am very happy of my quiet system.

I use Quod Libet for Audio, it has similar interface as iTunes, with more features (ability to play most audio formats). I chose Quod Libet instead of the standard Rhythmbox because of its practical mp3 tags handling. This also means that unlike iTunes, when I reimport my full library with another player, or on another computer, I have it all organized the right way, because the right meta data is in the audio files and not in a xml file that sometimes gets corrupted.

I can use Open Office (not yet available in non alpha version for Mac Os X).

I can use Picasa or other more standard alternatives instead of iPhoto.

I can use free guitar tuners, plenty of esoteric software.

Remote control, fancy bluetooth apple keyboard, cd burning, dvd player, printer work flawlessly. And it's all free software (except Picasa which is only gratis).

I am happy with my Ubuntu system :).