Saturday, August 14, 2010

getting reacquainted with javascript

Ok, I'm admittedly a little late to the party here. Lots of people are doing really, really cool things with javascript these days.

So, I've been using rhino to whip together my java experiments lately. Setup is trivial, you just need java and js.jar.

  1. download rhino 
  2. extract js.jar (at least)
  3. for an interactive shell, run java -jar js.jar
  4. or run a script using java -jar js.jar some-javascript-file.js

Once you have rhino, all sorts of java-ish things can be whipped up as quick hacks.
A quick http server, for example, goes something like this:

function main() {
  var s = new java.net.ServerSocket(8080)
  while (true) {
    var client = s.accept()
    var sc = new java.util.Scanner(client.getInputStream())
    var method = sc.next()
    var path = '.' + sc.next()
    var out = new java.io.PrintWriter(
            new java.io.OutputStreamWriter(client.getOutputStream()))
    try {
      var f = new java.io.FileInputStream(path)
      out.println("HTTP/1.1 200 Success")
      out.println("Content-Type: text/html")
      out.println()
      for (var c = f.read(); c != -1; c = f.read())
        out.write(c)
    } catch (e if e.javaException
        instanceof java.io.FileNotFoundException) {
      out.println("HTTP/1.1 404 File not found")
      out.println("Content-Type: text/html")
      out.println()
      out.println("<html><body>")
      out.println("<h1>File not found</h1>")
      out.println("</body></html>")
    }
    out.flush()
    out.close()
  }
}

main()

Wait, what about a threaded server you say? Try this on for size.

function main() {
  var s = new java.net.ServerSocket(8080)
  while (true) {
    var client = s.accept()
    var t = java.lang.Thread(function() {
          var sc = new java.util.Scanner(client.getInputStream())
          var method = sc.next()
          var path = '.' + sc.next()
          var out = new java.io.PrintWriter(
                  new java.io.OutputStreamWriter(client.getOutputStream()))
          try {
            var f = new java.io.FileInputStream(path)
            out.println("HTTP/1.1 200 Success")
            out.println("Content-Type: text/html")
            out.println()
            for (var c = f.read(); c != -1; c = f.read())
              out.write(c)
          } catch (e if e.javaException
              instanceof java.io.FileNotFoundException) {
            out.println("HTTP/1.1 404 File not found")
            out.println("Content-Type: text/html")
            out.println()
            out.println("<html><body>")
            out.println("<h1>File not found</h1>")
            out.println("</body></html>")
          }
          out.flush()
          out.close()
        }
      )
    t.start()
  }
}

main()

For more reading about rhino and javascript including how to structure, organize, and manage your code for larger projects, try these great posts.

http://steve-yegge.blogspot.com/2008/06/rhinos-and-tigers.html
http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
http://peter.michaux.ca/articles/javascript-widgets-without-this
http://www.jspatterns.com/

No comments: