Wednesday, March 19, 2008

Groovy Meta Libraries

The more and more I write Groovy, the more I really like its meta-programming (Meta-Object Protocol or MOP) capabilities. I find myself frequently refactoring code into meta methods of existing Java classes that would have otherwise been methods in some utility classes. I am slowly building a libraries of these I would like to reuse. Unfortunately one thing that challenges me is what is the best practices for turning them into reusable meta libraries. I would like to be able to package them in a jar and just drop them in the classpath and they would be automatically available(bootstrapped). This would make them easier to carry between projects and share with the community. There would also have to be a good way of creating good GroovyDoc like we have with the GDK. If somebody has a recommendation or idea I would love to hear it. Otherwise, I wonder if there could be a standard way to use the Java jar service provider mechanism like JDBC driver do now to auto register.

Thursday, March 13, 2008

I might be getting this DSL stuff

I know I am a little slow. I usually have to hear and see something three times before I start to grasp it. I know it was that way with Aspects and it has definitely been that way with DSLs. At every conference I have gone to for the last 2 years I have made sure to attend DSL talks. I have probably seen Neal Ford speak on the topic at least five times alone. By this point, I have definitely understood the concepts. Hearing Neal Ford's Starbuck's language example gave me that aha moment. But I have not been able to take it to the next step. I have not been able to put it into practice. That may be because I have been writing code so long I don't see problem with curly braces and long lists of method chaining and ugly parameters. But tonight, I think I got it. I think I saw the value in a whole new way.

I took this simple example written in Groovy that only a programmer could love.

println this.class.getResourceAsStream('readme.txt').getText()

This example only reads a file found in the classpath and then prints it to standard out. It might look pretty standard to developers but to anybody else it looks like a cryptic foreign language. This example is even in Groovy which simplifies the code a lot compared to a Java equivalent. (I actually started writing a Java equivalent for this post but it was too painful after writing the Groovy solution so I stopped.)

So after looking at this example, I set out to write the same functionality in a human readable format. In all the DSL sessions I have attended, they always say start of with the end in mind and make it look like a sentence.

So, I came up with "write readme.txt contents". In code it looks like the following example:

write 'readme.txt'.contents()

Not to bad for my first DSL. I think the intent is pretty clear. Next is how I implemented it.

String.metaClass.contents = {
this.class.getResourceAsStream(delegate).getText()
}

def write = { file ->
println file
}

write 'readme.txt'.contents()


You can see I used a little bit of Groovy's MOP (Meta-Object Programming) to add a contents method to the String class that does the resource loading base on the delegate which is the object on which the message was passed which in this case is the 'readme.txt' String. Next I defined a write closure that does a println on the parameter passed. Note using Groovy's optional parentheses makes it flow much more like a natural sentence.

Now that I have reached this milestone, I am afraid all standard code is going to repulse me. I know I am stuggling to write Java code after having used Groovy.

Other advantages to Apple distribution model for iPhone apps

Another great advantages of distributing applications for the iPhones is not having to write installers or complicated installation instructions. That alone should save lots of development time and support headaches. Wouldn't it be nice if all software installation could be this easy.

Wednesday, March 12, 2008

Apple's iPhone application deployment model is fair

In talking to some of my fellow developer friends about the recent iPhone SDK launch, they are shocked and disappointed with Apple's for distribution model but especially with Apple's 70/30 split of the sale of the applications.

I happen to think this model is quite fair. Here is a list of things included with sharing 30% with Apple:
  • distribution
  • credit card transactions
  • marketing
  • platform
I think this makes the program a great bargain. For example other forms of distribution like retail often have a 40-50% markup. More importantly Apple is providing the opportunity by providing and opening the platform and SDK at no additional charge.

I also can't believe the Standard Developer Program is only $99. I have been developing J2ME application for several years now. Whenever you deploy you typically need to sign the J2ME app it can be $500 for a single deployment option because of certificates. If want to make your application available through several carriers you might have to have several certificates which cost additional money and add to the packaging and deployment complexity.

But the final benefit is the program includes free distribution for free apps. So if you don't want to share 30% with Apple, there is a missed revenue opportunity in Apple's plan. I have not heard or seen anything relate to participating in sharing for any other monetization options. So if you give away your application for free but sell ads, you can save it all :)

Finally, if you are looking for iPhone developers please contact me, I am very excited about the platform and would love the opportunity to work on this new frontier.

Monday, March 10, 2008

Free Groovy and Grails lunch and learn

Over the last year, I have become a huge fan of Groovy and Grails. I believe the speed of development combined with the short learning curve and the power of the Java platform will make them the future of the Java platform. If you want know why I believe this, I am offering a free hour and a half lunch and learn to companies in the Central Ohio area. This is a presentation I gave at CodeMash and to several companies in the area. The presentation defines Groovy and Grails and then demonstrates their capabilities with a live coding demo including some AJAX examples. If you are interested in learning more about scheduling me to speak at your next lunch and learn, please email me at groovy@juddsolutions.com.

Groovy Ant task is for Groovy Scripts

Groovy includes an Ant task for executing either file based or embedded Groovy. I recently discovered the file option denoted by the src attribute must be a Groovy script and not a Groovy class containing a main method. If you have used the Java task, you might expect the Ant arg set to be passed as a parameters to the main method since it is the common Ant convention for passing command line arguments. However, as you can see in the following snippet of code from org.codehaus.groovy.ant.Groovy, a GroovyShell is created and the args are passed as a property to the script.
final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
try {
final Script script = groovy.parse(txt, scriptName);
script.setProperty("ant", new AntBuilder(this));
// code removed for brevity
script.setProperty("args", cmdline.getCommandline());
script.run();
}
This behavior was unexpected since it does not follow the normal Ant convention and because the documentation states the src attribute is a File containing Groovy statements rather than stating it expects a Groovy Script.

Tuesday, March 4, 2008

Excellent cell phone browsing experience with Opera Mini

I have a MOTORAZR V3 from Sprint. I have been some what satisfied with my browsing experience and the embedded Obigo browser for websites designed for mobile content. However websites not designed for mobile content look awful. So when I hard the Java Posse talk about a J2ME based browser I had to give it a try. The browser is the free Opera Mini browser. It was easy to install, just use your embedded mobile browser and click on a link and it installs. Sites designed for mobile content look incredible while sites not designed for mobile also look good. For regular content, it renders a scaled down version and gives you the ability to zoom in and move about the page. The other great thing is unlike embedded browsers, it is easily upgrade able. If you are not pleased with your current mobile browsing experience you should give Opera Mini a try. It is not as nice as the iPhone browsing experience but if you have a standard phone for making phone calls this might make you look at your phone differently.

AWS EC2 Hibernate Java SDK v2 Example

I recently wanted to automate the creation of developer VMs in AWS using EC2 instances. To improve the developer experience (DX), I didn'...