blog archives

june 2009
july 2009
august 2009

rss feeds

my blog

Part 1: Lifting Off With Scala Aug 22, 2009

Static Scala Website in under 10 Commands

The Purpose

The purpose of this tutorial is to build a Scala web app from scratch. This app will not do much at first but you will be ready to get working with the Lift Framework. I feel the biggest hurdle that JVM based applications face is getting a new project off the ground. In this tutorial I will go through all the pieces needed to get you ready to code some Scala on the web!

On a personal note I oft wonder why Web Applications in Java are so hard to get started. I personally think this leads to Java programmers spending their free time working with other languages. In the 5 years I have been doing Java Development I have not created a single Java Web App from scratch. Even then most people just copy another applications structure and build.xml ( cringe @ ant ), hallow it out, then start to code.

The Language

Scala is a functional / object oriented programming language that runs on the Java Virtual Machine or JVM. As far as speed goes it can keep up with Java in most cases, and will blow away most dynamic languages in most benchmarks. ( ie. Ruby, Python, Groovy )

The Web Framework

My web framework of choice for Scala is the Lift Framework. From their site "Lift stresses the importance of security, maintainability, scalability and performance, while allowing for high levels of developer productivity. Lift open source software licensed under an Apache 2.0 license." Yes, this means it is free.

The Server

For my server I chose Ubuntu Linux 9.04 since I love apt-get and I am a fan of the work being done at Canonical. I also Use Ubuntu 8.10 at work on my desktop. Unfortunately server distributions are a bit harder to work with but you will be happy when your site doesn't get hacked. Unless of course your root password is well... password.

I've recently bought an account over at linode.com to have a computer to "hack" on. For a low monthly fee you basically get your own virtual machine to play with. They just ask you what Operating System Distro you would like to install and then give you root ssh access. Light years ahead of the competitors like godaddy. When setting up your hosting you can choose Ubuntu 9.04 as your OS.

Here are the commands I used to get my server up and serving a Scala Website.

First we need to update the aptitude program.

rhigdon@vanilla-bear:~$ sudo apt-get update

Now we need to download our http server. I choose Apache Http to server traffic on port 80 normally. There is some discussion whether or not to use iptables for port redirection. I personally like using Http and all its modules. It is easily extensible. The package name we are going to look for is apache2.

rhigdon@vanilla-bear:~$ sudo apt-get install apache2

Now traffic on our domain will be hitting our Apache webserver! Currently my site minus9.org is serving up the default index.html of /var/www/index.html. This is the default for Apache Http Server.

Now we need the Java Virtual Machine! Scala runs on the JVM so we need to install a flavor of the JVM. The default-jdk in Ubuntu 9.04 is the IcedTea JDK. This is an open source version of the GPL'd version of the Sun JDK. Certain libraries could not be exported to GPL due to Legal Obligations so RedHat created the IcedTea project to write what was missing.

rhigdon@vanilla-bear:~$ sudo apt-get install default-jdk

rhigdon@vanilla-bear:~$ java -version

java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu11) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode)

Now we need Maven2. This will not be in aptitude's packages. I choose the route to download it directly from the Apache Site. For this I needed wget.

rhigdon@vanilla-bear:~$ sudo apt-get install wget

Then from your home directory grab the binaries

rhigdon@vanilla-bear:~$ wget http://www.ip97.com/apache.org/maven/binaries/apache-maven-2.2.1-bin.tar.bz2

This file is now in bzip2 format. We will need to un-archive it using tar:

rhigdon@vanilla-bear:~$ tar -xvpjf apache-maven-2.2.1-bin.tar.bz2 /usr/local/share/

Now you need to add the environment variables for the JDK and Maven2.

rhigdon@vanilla-bear:~$ export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
rhigdon@vanilla-bear:~$ export M2_HOME=/usr/local/share/apache-maven-2.2.1
rhigdon@vanilla-bear:~$ export M2=$M2_HOME/bin
rhigdon@vanilla-bear:~$ export PATH=$M2:$JAVA_HOME:$PATH

Note -- This can be added to your profile with "vim ~/.bashrc"

Now Maven and the JDK should be ready to go.

Now we will need to create the maven from the lift archtype. For those of you not very familiar with Maven, an arch-type is basically a project template.

rhigdon@vanilla-bear:~$ mvn archetype:generate -U \ -DarchetypeGroupId=net.liftweb \ -DarchetypeArtifactId=lift-archetype-blank \ -DarchetypeVersion=1.0 -DremoteRepositories=http://scala-tools.org/repo-releases \ -DgroupId=org.minus9 \ -DartifactId=minus9Site \ -Dversion=1.0-SNAPSHOT

Note that "-DgroupId=org.minus9" needs to be replaced with your package structure and "-DartifactId=minus9Site" is your project name.

Now lets get started! You need to enter the top level directory of your project:

rhigdon@vanilla-bear:~$ cd minus9Site/

rhigdon@vanilla-bear:~$ mvn jetty:run

You now have your very first Scala Web Application! It isn't much but it is a start.

For Form LLC-12: "California Office where records are Maintained" Jul 28, 2009

As of Tuesday July 28th, 2009 the address is:

1500 11th Street
Sacramento, CA 94244-2300

Since I don't think the State of California is in any sort of financial position to be applying SEO to their information I wanted to provide something I had to google for earlier today.

Why we are on other confusing parts of LLC-12 I guess I can mention that #2 ( File Number ) can be found on your LLC-1 Form Article of Organization.

This was kind of off topic from Software but lets face it. Business is a big part of programming.

ID3v2 -- Now there is an idea! Jul 15, 2009

In my professional life I've recently been dealing with ID3 tags in some weird ways. It really is an interesting specification. What's even more interesting is where it could be headed.

Friends, think back to the days of Napster. This is around the time that ID3v1 tags were becoming a standard. Since then, the official specification has evolved significantly, but it is still the most commonly used technique for tagging music files. MP4 is expected to be the eventual successor to those old MP3s, but interestingly, the iTunes Music Store settled on M4A. (a proprietary Apple-designed format) The closest comparison to iTunes that I've used has been rhythmbox. It is an excellent project... but not quite the experience of iTunes. Personally I love Genius -- it is Pandora for my own personal music. Especially now that Pandora is limiting the time users listen to their service.

Either way the steadily increasing grip of M4A has me worrying about the open-source communities ability to keep with up with Media Software. With the increased number of podcasts and videos being available on iTunes how long before I am Genius'ing Video's on iTunes? iTunes already forces videos on me when I am buying from the iTunes store. ( With Gift Cards -- not my own money -_- ) How long before they are forcing Video/Audio advertisements in that bundle? It has worked pretty well for the movie industry.

Just as great as MP4 is though, it has its proprietary restrictions. The open-source community's answer has been OGG, a free format that anyone can use! "Well," you may ask, "if it is free why isn't anyone using it?" I believe the main reason is that companies can't capitalize on open-source software.

Easy to Remember External IP to ping -- 4.2.2.2 Jul 13, 2009

If you are troubleshooting a home network ( or parent's network, girlfriend's network, etc ) and need to ping an external IP to test DNS you can use 4.2.2.2, which is a free DNS Server hosted by GTE / Verizon. Thanks guys!

Simple Java servlet getInputStream() Jul 01, 2009

If you need a simple proof of concept to display the body of a HTTP Post from a Java Servlet you can use the following code:

BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));
while (in.ready()) {
  System.out.println(in.readLine());
}

This will print out the body of a request to your standard output. Please note that this is _NOT_ the best way to do this. This is just a quick way to get at a HTTPRequest object's body tag.. Java can be complex but it doesn't need to be.

Django -- from Java to Python in hours. Jun 15, 2009

I assure you it is possible. I just wrote this blogging software and front end integration in just over a week's time. I decided as soon as I got home from work and all weekend I was going to learn, write, and deploy a django app. It is production ready with a web stack of python/postgres/django/apache/lighttpd. Unfortunately that doesn't make me a python professional but it does prove Django can really get you on your way.

Well the admin site is up! Jun 12, 2009

I got the site up and running late last night and needed to christen the code with a blog post.

rhigdon says...
Test Comment -- Feb 01, 2010 8:03 p.m.