Skip to content
Jul 6 10

Scala and the Play Framework, Wow!

by marcus

As I continue down the path of testing out the Scala language and compatible web frameworks, I came across the Play Framework and all I can say is Wow! Play is a very nice polished framework, which just feels right to me. I had the app up running and doing everything I wanted in a fraction of the time on any other technology I tested. Everything just worked, I didn’t have to fuss with a bunch of settings, config files, library versions, or anything.

Play is a Java framework and with the 1.1 release (not final) they have added Scala support. They claim Scala is still experimental but it works better than most complete technologies I’ve tested recently. The console interface for installing, running and managing the app works great. One of the most impressive pieces is the testing framework which combines Selenium and Unit testing and managed through the browser.

I literally made more progress with Play in an hour than I did with everything else which took days or weeks. For example, after two weeks with StringTemplate and Scalasti I am still not able to loop through a result set within a template.

Structure and Features

The Play structure is very intuitive and works well with the way I think. I come from a heavy PHP background and have been developing on a large Zend Framework app for the past 4+ years. We built our app structure off the Rails structure and Play continues along the same project structure (app, controllers, models, views). I don’t come from a heavy Java background, so the Maven project hierarchy has never really quite gelled with me.

A few key features of Play:

  • Automatic Compiling and Reloading of Everything
  • Contextual and Relevant Error Codes
  • Testing Framework baked in
  • Fast getting started time
  • Good Documentation and Tutorials
  • I haven’t used the Play ORM yet, but I don’t really use Zend’s either. Most apps and queries tend to require a little more customization and optimizations than any ORM can give. I’m sure I’ll check it out, maybe they’ll surprise me there too.

    Quick Start

    Download the Play Framework 1.1 nightly build from: download.playframework.org

    I downloaded and installed to: /opt/play. Add the directory you extracted play to your PATH.

    Create New App:

    play new testapp --with scala

    oops.. you haven’t installed Scala Module yet, it’s as simple as

    play install scala

    Try to create your app again and it will generate the app structure and a working demo app.

    Run App:

    play run testapp

    or from within the testapp directory just run

    play run

    A great little bonus is Play has built-in documentation, just go to a running instance: http://localhost:9000/@documentation — which is so useful, since I don’t always have an internet connection, for example during a train ride from SF to Chicago.

    Kudos to the Play team, my search for a Scala Framework might be complete.

    Further Reading:


    Jun 30 10

    Data URI Scheme, a great replacement of sprite maps

    by marcus

    One of my colleagues recently forwarded this slide presentation about Mobile Web Performance that he saw at the Velocity Conference. I’m leading a large mobile web project at our company, which conveniently enough, we were just discussing the need for sprite maps to reduce the number of requests.

    So taking one of my engineer’s suggestion and the above presentation I learned about Data URI Scheme, which is a way to use encoded data sent to the browser as text instead of a separate file, such as an image file.

    Here’s an example of a simple red square:

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAMAAADzN3VRAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF/wAAAAAAQaMSAwAAABJJREFUeNpiYBgFo2AwAIAAAwACigABtnCV2AAAAABJRU5ErkJggg==
    

    You can display directly by putting that in an IMG tag:

    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAMAAADzN3VRAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRF/wAAAAAAQaMSAwAAABJJREFUeNpiYBgFo2AwAIAAAwACigABtnCV2AAAAABJRU5ErkJggg=="/>

    In a browser, it shows like so (no external file needed):

    You should see a “red square” image above, assuming you are using Firefox, Safari or Chrome. Unfortunately, IE7 does not work; IE8 claims some support, but I did not have any luck with my testing.

    So once again our cool new fancy technique can’t be used because of browser support, right? Wrong, IE is a fraction of the mobile web market. It is dominated by webkit browsers, which do support it. Particularly nice since mobile is an area where every requests makes a difference, considering the spotty networks and under powered phones (compared to a desktop browser)

    Using with CSS

    Now, you don’t necessarily want to include the data in an HTML file, because you won’t get the benefit of caching. Here’s an example of the Data URI Scheme used within CSS:

    .red_square {
      background url('data:image/png;base64,[encoding]');
      width: 25; height: 25;
    }
    

    Replacing [encoding] with the large string, no line breaks or spaces, but keep the quotes. Than any where you want to use that image, just reference the CSS;

    <div class="red_square"/></div>

    You can collect up similar and common items that you might of previously put in a sprite map in a single css file. If you use a CDN, such as Akamai, or even just using the browser cache, you’ll end of delivering multiple images in a single request which can be cached.

    Using the Data URI Scheme it is easy to update a single image, which in a sprite map could be tricky. Also it simplifies the reference of the images without requiring a more complex coordinate selection of images which is typically done in CSS sprites.

    How to Create Base64 Encoding

    There are a few ways to encode a file to base64, there is a unix program aptly called base64 which is what I use; I installed using MacPorts, on Ubuntu you can install with a simple apt-get. If those don’t work for you, you can try an online base64 generator.

    To encode:

    $ base64 red_square.png
    

    It will spit out the encoded string, you’ll have to remove any line breaks that get included. If you are on a Mac, there a couple of nice clipboard unix tools that make it even easier. You can pipe output into pbcopy which will place the output in your clipboard buffer. Ready to be pasted in.

    $ base64 red_square.png | pbcopy
    

    The base64 utility can also decode a string back to the original file. To do that save the encoded string to a file such as encoded.txt and run base64 with the –decode parameter, like so

    $ base64 --decode encoded.txt >red_square_decoded.png
    

    This makes it easy to “recover” the original images if all you had was the CSS files.

    Summary

    I think this is a great technique to reduce complexity for accessing images and provide greater performance to our mobile users. We’ve already started implementing across our mobile sites.

    Further Reading:


    Jun 26 10

    Using Scala with JDBC to connect to MySQL

    by marcus

    A quick howto on connecting Scala to a MySQL database using JDBC. There are a number of database libraries for Scala, but I ran into a problem getting most of them to work. I have the highest hopes for Querulous, a library created by Twitter. However, it currently only supports Scala 2.7.7 and I’m running a 2.8 candidate which is the only compatible version with the template engine I’m using. Hopefully things stabilize once 2.8 is final.

    I attempted to use scala.dbc, scala.dbc2 and Scala Query but either they aren’t supported, have a very limited featured set or abstracts SQL to a weird pseudo language. So I ended up with basic Java JDBC and it turned out to be the easiest solution.

    Here is the code for the a database query example using JDBC. You need to change the connection string parameters and modify the query for your database.

    import java.sql.{Connection, DriverManager, ResultSet};
    
    // Change to Your Database Config
    val conn_str = "jdbc:mysql:/localhost:3306/DBNAME?user=DBUSER&password=DBPWD"
    
    // Load the driver
    classOf[com.mysql.jdbc.Driver]
    
    // Setup the connection
    val conn = DriverManager.getConnection(conn_str)
    try {
        // Configure to be Read Only
        val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
    
        // Execute Query
        val rs = statement.executeQuery("SELECT quote FROM quotes LIMIT 5")
    
        // Iterate Over ResultSet
        while (rs.next) {
            println(rs.getString("quote"))
        }
    }
    finally {
        conn.close
    }
    

    You will need to download the mysql-connector jar. Download the mysql connector jar from here.

    Or if you are using maven, the pom snippets to load the mysql connector

     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.12</version>
     </dependency>
    

    To run the example, save the following to a file (query_test.scala) a run using, the following specifying the classpath to the connector jar:

    scala -classpath mysql-connector-java-5.1.12.jar:. query_test.scala

    Hope it helps.




    Jun 25 10

    Holla if you like Scala

    by marcus

    I started playing with the Scala programming language recently. For most new languages I want to learn, I try them out solving problems from Project Euler — a fun site with various challenging math problems that typically require programming to solve. For example: What is the largest prime factor of the number 600851475143 ?

    Typically after solving a few problems, I find the language is not for me and I shelve it. This happened several months ago the first time I played with Scala. However, after reading a few big sites are adopting the language and more about its core features and structure; it peeked my interest again. After solving fifteen Project Euler problems using Scala, I got hooked on the language.

    So the past month or so I’ve been investigating and learning the language more and seeing what role it can play in my world. I’ve already started using it instead of Python as a general scripting language. One of the great abilities of Scala is its flexibility to use scripting, interactive or compiled.

    What I like about Scala…

    • Blend of Functional and Pure Object Oriented
    • Gives flexibility of passing functions and using closures, like Javascript
    • Plus real Object Oriented constructs allowing larger more structured systems
    • Legible and Concise Style, not nearly as verbose as Java
    • Statically Typed and Compiled (Easier Refactoring, contributes to consistent programmer’s behavior)
    • Compiles to JVM Byte Code, can run in tried and true server engines, plus extendable to use any Java Libraries within Scala

    Developer Ecosystem is Young

    I think overall the developer ecosystem is still very young, I keep running into various stumbling blocks when trying to do some fairly basic items. A few areas that need growth is documentation, working examples, and deeper explanations for all programming levels. My best guess is its just full of too many experts and a very high barrier to entry.

    Ruby and Rails showed how some great documentation, good examples and tutorials can help a platform develop and greatly improve adoption. I’m not sure if Scala is right for the same level of main stream adoption, but getting developers excited and a few quick wins starting off can only help generate enthusiasm.

    I don’t want to quibble about the various issues I found, but plan to start documenting some of my experiences and learnings to help contribute to a healthier ecosystem.

    Further Reading

    • Book: Programming in Scala — This is a great introduction to Scala, lots of good examples and comparisons to Java, highly recommended.

    • Scala in the Enterprise — A list of companies and case studies currently using Scala in their Enterprise, including Twitter, Siemens, Sony and Xerox.

    • Getting Started with Scala — a quick introduction showing the various ways you can write and run Scala
    May 17 10

    You should see him run the bases

    by marcus

    May 2 10

    AT&T Park Panorama

    by marcus

    Apr 30 10

    Happy Honey!

    by marcus

    Apr 30 10

    Commute In II

    by marcus

    Apr 27 10

    Commute in

    by marcus

    Apr 23 10

    Happy Earth Day!

    by marcus

    Happy Earth Day! from Marcus Kazmierczak on Vimeo.