Wednesday, April 15, 2009

Moving into the cloud, loosing your freedom?

clouds.jpg I am a fan of the recent cloud computing hype. I have my own attempts to merge into the cloud.

I am also a fan of opensource software, of which the FSF and GPL are important proponents.

Google, Facebook, Twitter and friends, with all their free cloud applications lured me into thinking that the cloud and opensource are best frinds.


But recently two articles opened my eyes:
  • The JavaScript Trap by Richard Stallman (founder of GNU and FSF):
  • Silently loading and running non-free programs is one among several issues raised by "web applications".
  • GPL's cloudy future by Jeremy Allison (lead Samba developer):
    In such a world, service providers can use GPL-licensed code in proprietary back-end server farms with impunity. This seems contrary to the spirit of the authors of much of the GPL-licensed code used in this way, although it strictly complies with the license.
  • inside-of-a-prison-cell.jpg
    Well, this does not yet stop me from using cloud applications, but I think it is important to realize that there are not just Alphas in this brave new cloudy world...

    Monday, April 13, 2009

    Google App Engine: Guestbook with Groovy Groovlets

    google-app-engine-groovy.png The web is soaring, since Google announced Java support on Google App Engine!

    People are getting ahead of themselves to try out their favorite piece of the Java ecosystem on the Google platform.

    I figured that I also wanted to experience this pre-alpha-geek feeling :-)

    I followed the guidelines to run Groovy appliccations on Google App Engine from glaforge and reimplemented the guestbook example from the Google Appengine SDK with Groovy Groovlets.

    The result is here. Leave me a message ;-)


    The backend classes (Greeting.java, PMF.java) were not changed (however it seems to be possible to datanucleusenhance classes written in Groovy and compiled with groovyc, see here).

    I kicked out the two servlets and the jsp and replaced them with the following two Groovlets.

    hello.goovy
    import com.google.appengine.api.users.User
    import com.google.appengine.api.users.UserService
    import com.google.appengine.api.users.UserServiceFactory
    import javax.jdo.PersistenceManager
    import guestbook.PMF
    import guestbook.Greeting
    
    UserService userService = UserServiceFactory.getUserService()
    User u = userService.getCurrentUser()
    
    PersistenceManager pm = PMF.get().getPersistenceManager()
    String query = "select from " + Greeting.class.getName() + " order by date desc range 0,25"
    List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute()
    
    html.html {
     head {
      title "Hello"
      link(type:"text/css", rel:"stylesheet", href:"/stylesheets/main.css")
     }
     body {
      div(class: "main"){
       div ("Today is: ${new Date()}")
    
       if (u == null) {
        div(class:"login"){
         a (href: userService.createLoginURL(request.getRequestURI()) , "Log in with your Google Account.")
        }
       }
       else {
        div(class:"login"){
         span("Welcome ${u.nickname}. ")
         a (href: userService.createLogoutURL(request.getRequestURI()) , "Log out")
        }
       }
    
       p ("Leave me a message:")  
    
       form(method: "POST", action: "/post.groovy"){
        div(){
         textarea(name: "content", rows: "3", cols: "60", "")
        }
        div(){
         input(type: "submit", value: "Post")
        }
       }
    
       greetings.each{
        def user = "anonymous"
        if(it.author != null) user = it.author
        div(class:"entry-header", "On  ${it.date} ${user} wrote: ")
        div(class:"entry-body", "${it.content}")
       }
      }
     }
    }
    

    post.groovy:
    import com.google.appengine.api.users.User
    import com.google.appengine.api.users.UserService
    import com.google.appengine.api.users.UserServiceFactory
    import javax.jdo.PersistenceManager
    import guestbook.PMF
    import guestbook.Greeting
    
    UserService userService = UserServiceFactory.getUserService()
    User user = userService.getCurrentUser()
    String content = request.getParameter("content")
    Date date = new Date()
    Greeting greeting = new Greeting(user, content, date)
    
    PersistenceManager pm = PMF.get().getPersistenceManager()
    try {
        pm.makePersistent(greeting)
    } finally {
        pm.close()
    }
    
    response.sendRedirect("/hello.groovy")
    

    This was my first take at Groovlets, so it might well be that things are not state of the art...

    Groovlet quick tip: textarea()

    Using textarea with Markup Builder in a Groovlet can be tricky.

    My initial attempt was:
    div()
    {
    	textarea(name: "content", rows: "3", cols: "60")
    }
    

    This however results in invalid html:
    <div>
        <textarea name='content' rows='3' cols='60' />
    </div>
    


    The correct way is to include an empty string (content of the textarea) in the constructor:
    div()
    {
    	textarea(name: "content", rows: "3", cols: "60", "")
    }
    

    Which yields correct html:
    <div>
        <textarea name='content' rows='3' cols='60'></textarea>
    </div>
    

    I hope this helps, since Groovlet documentation is quite sparse.

    Saturday, April 11, 2009

    Quick Look for Groovy with Syntax Highlighting

    In my previous post OS X: Quick Look for Groovy I showed how to enable Quick Look for Groovy source files.
    The result was very cool, but syntax highlighting was missing.

    After playing around a bit I found out how to enable syntax highlighting for Groovy in Quick Look.

    Here is how it works:
  • Install the qlcolorcode-plugin.
    Unfortunately the plugin does not support Groovy out of the box, but we will change that. After installing the plugin you should have the package QLColorCode.qlgenerator in your ~/Library/QuickLook.

  • Go into the QLColorCode.qlgenerator package. Either by right-clicking in Finder and choosing "Show Package Contents" or by navigating into the directory in a shell.

  • Inside the QLColorCode.qlgenerator package edit the script Resources/colorize.sh.

    Extend the case-statement in the middle of the script the following way:
    ...
    case $target in
        *.graffle )
            # some omnigraffle files are XML and get passed to us.  Ignore them.
            exit 1
            ;;
        *.plist )
            lang=xml
            reader=(/usr/bin/plutil -convert xml1 -o - $target)
            ;;
        *.h )
            if grep -q "@interface" $target &> /dev/null; then
                lang=objc
            else
                lang=h
            fi
            ;;
        *.m )
            # look for a matlab-style comment in the first 10 lines, otherwise
            # assume objective-c.  If you never use matlab or never use objc,
            # you might want to hardwire this one way or the other
            if head -n 10 $target | grep -q "^ *%" &> /dev/null; then
                lang=m
            else
                lang=objc
            fi
            ;;
        *.groovy )
        	lang=java
            ;;
        * ) 
            lang=${target##*.}
        ;;
    esac
    ...
    

    This tells Highlight to treat Groovy files as Java source code.

  • Next edit Info.plist inside the QLColorCode.qlgenerator package.
    Add the follwing snippet at the end of Info.plist (just before the ending </array> </dict> </plist>)
    	
    		UTTypeConformsTo
    		
    			public.source-code
    		
    		UTTypeDescription
    		Groovy Source Code
    		UTTypeIdentifier
    		org.codehaus.groovy-source
    		UTTypeTagSpecification
    		
    			public.filename-extension
    			
    				groovy
    			
    		
    		
    

  • Now you need to nudge the system to tell it something has changed. Moving the whole plugin (QLColorCode.qlgenerator) to the desktop then back to its installed location should do the trick.

  • This should be it! The result is Quick Look for Groovy with syntax highlighting:

    Picture 2.png


    The syntax highlighting is still not perfect, since it is Java highlighting... but it is much better than no highlighting.

    Related Posts Plugin for WordPress, Blogger...