Archive for the ‘groovy’ Category

Groovy as a script language for Open Office

Wednesday, January 13th, 2010

I post this only because I am impressed with the extent to which the JVM world is now able to fight back against Microsoft. Record macros in OpenOffice with Groovy

Windows/Mac/Linux (OpenOffice): Free OpenOffice extension Groovy makes it possible to record and run Macros in OpenOffice. Don’t confuse Groovy for a cheap Visual Basic knockoff. Groovy has its own syntax similar to bash mixed with Java. If you were sticking to Microsoft Office solely for its macro capabilities, you may be able to break away with Groovy. Unfortunately, Groovy is not nearly as beginner friendly as VB/VBA. However, beginners will have no problem getting started with simple macros. Groovy is a free extension for all platforms with OpenOffice. Here is an ODT with several sample macros to help you get your feet wet (remember, you need to install Groovy before you can run the macros).

Groovy is an easy step into the JVM world for PHP developers

Saturday, January 2nd, 2010

A script written in Groovy that finds and deletes files that match some given pattern. The syntax of Groovy is wonderfully clean, and not especially foreign to PHP developers. I’d think that if a PHP developer wanted to dive into the world of the JVM, Groovy would make an easy first stop.

Closures are probably the best, and least appreciated, features of Javascript

Saturday, October 10th, 2009

Closures are one of the features that I like most about languages like Javascript and Groovy (Ruby, too, of course, though I don’t use Ruby much). This article on Advanced Javascript techniques highlights closures, and is worth a read. Closures are the only way to introduce private class variables into Javascript. Closures often seem to me to be the way you can introduce into a language anything you think the language is missing. Closures are important to meta-programmng – a common practice in Ruby and Groovy. Closures have finally been introduced in PHP , as of version 5.3, but I have not yet used them in any of my PHP projects. Will be interesting to do so.

I first learned about closures from this old article on Jibbering. I think that is a great article and, even though it focuses on Javascript, I believe you can learn all of the basic concepts about closures by reading that article.

Java and Groovy compared

Tuesday, July 28th, 2009

Java and Groovy compared:

Groovy provides an excellent playground for trying out features that may be coming to future versions of the Java programming language. Its close relationship to Java makes it even more attractive for this than other less Java-like languages with the same features. In this blog posting, I have attempted to demonstrate Groovy features that may end up in a future version of Java. These proposed new Java features include property support, BigDecimal operators, switch on String, multiline Strings, and the Elvis operator. Other than the brief mention of BigDecimal operator overloading, I did not focus on Groovy’s operator overloading here for the same reason I did not mention Groovy’s closure support here: they both deserve blog posts of their own. I instead focused on smaller new features here that might someday be added to traditional Java.

The explosion of Java frameworks

Tuesday, July 28th, 2009

Having read a little about Apache Wicket, I wanted to learn more, so I went to the website. I was surprised by their list of Java frameworks:

Echo
Cocoon
Millstone
OXF
Struts
SOFIA
Tapestry
WebWork
RIFE
Spring MVC
Canyamo
Maverick
JPublish
JATO
Folium
Jucas
Verge
Niggle
Bishop
Barracuda
Action Framework
Shocks
TeaServlet
wingS
Expresso
Bento
jStatemachine
jZonic
OpenEmcee
Turbine
Scope
Warfare
JWAA
Jaffa
Jacquard
Macaw
Smile
MyFaces
Chiba
JBanana
Jeenius
JWarp
Genie
Melati
Dovetail
Cameleon
JFormular
Xoplon
Japple
Helma
Dinamica
WebOnSwing
Nacho
Cassandra
Baritus
Stripes
Click
GWT

Damn. How can anyone keep up? And that’s just plain Java frameworks. They are, apparently, not counting web frameworks like Grails, written in Groovy, which runs on the JVM.

There are so many languages, so many frameworks, so much new stuff. It has become impossible to make any pretense of choosing the “best” technology for a job. Rather, we are all subject to circumstance now, the accidents of our lives that bring us into contact with a particular technology at a particular time. This has always been true, but it beomces more obvious to me when I see a list like this.

This part of what Wicket says about itself has some appeal to me:

Most existing frameworks require special HTML code.

JSP is by far the worst offender, allowing the embedding of Java code directly in web pages, but to some degree almost all of the frameworks from the list (except Tapestry) above introduce some kind of special syntax to your HTML code.

Special syntax is highly undesirable because it changes the nature of HTML from the kind of pure-and-simple HTML markup that web designers are familiar with, to some kind of special HTML. This special HTML can be more difficult to preview, edit and understand.

Wicket does not introduce any special syntax to HTML. Instead, it extends HTML in a standards-compliant way via a Wicket namespace that is fully compliant with the XHTML standard. This means that you can use Macromedia Dreamweaver, Microsoft Front Page, Word, Adobe Go Live, or any other existing HTML editor to work on your web pages and Wicket components. To accomplish this, Wicket consistently uses a single id attribute in the Wicket namespace (”wicket:id”) to mark HTML tags that should receive special treatment by the toolkit. If you prefer not to render Wicket namespaced tags and attributes to your end-users, Wicket has a simple setting to strip them all out, resulting in ordinary, standards-compliant HTML.

No “special sauce” in your HTML means designers can mock up pages that you can use directly in development. Adding Java components to the HTML is as simple as setting the component name attribute. And you can then give the HTML back to your web designers knowing that they can change it with confidence.

Wicket, more than any other framework gives you a separation of concerns. Web designers can work on the HTML with very little knowledge of the application code (they cannot remove the component name tags and they cannot arbitrarily change the nesting of components, but anything else goes). Likewise, coders can work on the Java components that attach to the HTML without concerning themselves with what a given page looks like. By not stepping on each other’s toes, everyone can get more work done.
Existing frameworks are not easy.

Most of the existing toolkits have poorly defined or non-existent object models. In some cases, the model is defined using special XML syntaxes. The syntaxes may be so cumbersome that special tools are required to manipulate all the configuration information. Since these toolkits are not simple Java libraries you may or may not be able to use your favorite IDE tools such as editors, debuggers and compilers.

Wicket is all about simplicity. There are no configuration files to learn in Wicket. Wicket is a simple class library with a consistent approach to component structure. In Wicket, your web applications will more closely resemble a Swing application than a JSP application. If you know Java (and especially if you know Swing), you already know a lot about Wicket.

Why do we need JavaFX when we have Groovy?

Friday, June 5th, 2009

The more I learn about Groovy, the less I understand why the world needs JavaFX.

Back around 2002 or 2003 I studied Java, but it seemed heavy to me, verbose, and slow to program with. My attitude has mostly been, life is short, so I’d rather program in light-weight, high level programming languages. I want to be able to go as fast possible. For the last few years I’ve mostly been doing web development, mostly with PHP, though I did one project with Ruby On Rails.

But then the iPhone came out. And then the iPhone app store. So I became intrigued with the idea of programming for cell phones. But I didn’t want to especially learn Objective C just to be able to work with the iPhone. I was relieved to hear that Google was coming out with its Android operating system for cell phones, and programming for Android would be done in Java. That opened the door to using the light-weight languages (derived from Java) for cell phone programming. Very cool.

So I began to delve into the world of Java, something I hadn’t done much in the last few years. At first I was attracted to JavaFX, which Sun was hyping. But then I began to wonder why JavaFX was suffering such a slow uptake, and why programmers seemed less thrilled about it than I.

Now that I’ve started to study Groovy, I realize why JavaFX has so little traction: it isn’t needed. Much of what I liked about JavaFX is already present in Groovy. Lexecorp expresses some of the same hesitation that I feel:

I wonder whether I will be able to like JavaFX! Have I been spoilt already by Groovy (it does, virtually, the same thing)? Maybe, but I managed to accept the long winded Java language well after Perl’s expressive power had spoilt me. I learned to love Java for its platforms and immense libraries, so what can I love about JavaFX?

Consider this tutorial, which showed how to draw a coffee cup, and which showed off the clean, declarative style of JavaFX:

def plateGroup = Group {

translateX: 250
translateY: 300

content: [

// The gray ellipse under the plate
Ellipse{centerX:0 centerY:10 radiusX:160 radiusY:50 fill:Color.DIMGRAY},

// The thin "lip" of the plate (provides a sense of 3D)
Ellipse{centerX:0 centerY:3 radiusX:170 radiusY:50 fill:Color.LAVENDER},

// The large plate ellipse
Ellipse{centerX:0 centerY:0 radiusX:170 radiusY:50
fill:RadialGradient{
centerX:0.5 centerY:0.75
stops:[Stop {offset: 0.0 color: Color.WHITESMOKE},
Stop {offset: 0.5 color: Color.LIGHTGRAY},
Stop {offset: 1.0 color: Color.DARKGRAY}]
}
},

// Recessed plate center
Ellipse{
centerX:0 centerY:5 radiusX:90 radiusY:22
fill:RadialGradient{
centerX:0.5 centerY:0.75
stops:[Stop {offset: 0.0 color: Color.BLACK},
Stop {offset: 0.4 color: Color.LIGHTGRAY},
Stop {offset: 1.0 color: Color.GHOSTWHITE}]
}
}
]
}

def cupGroup = Group {

translateX: 152
translateY: 20
scaleX: 3.0
scaleY: 4.0

content: [
// Cup body
Circle {centerX: 100 centerY: 100 radius: 50
fill: RadialGradient {
centerX: 0.4 centerY: 0.0 focusX: 0.5 focusY:.65, proportional:true
stops: [Stop {offset: 0.0 color: Color.GHOSTWHITE},
Stop {offset: 1.0 color: Color.SILVER}]
}
},

// Cut top of cup
Rectangle{stroke: BGCOLOR fill: BGCOLOR
x:25 y:50 width:150 height:50},

// Outer rim
Ellipse{fill: Color.WHITE centerX:100 centerY:100 radiusX:50 radiusY:8},

// Inner rim
Ellipse{centerX:100 centerY:100 radiusX:48 radiusY:7 //inner rim
fill: RadialGradient {
centerX: 0.4 centerY: 0.0 focusX: 0.5 focusY:.4, proportional:true
stops: [Stop {offset: 0.0 color: Color.GHOSTWHITE},
Stop {offset: 1.0 color: Color.SILVER}]
}
},

// Create “coffee in cup” by intersecting coffee ellipse with inner rim ellipse
ShapeIntersect {

fill:Color.SADDLEBROWN

//Inner Rim
a: Ellipse{centerX:100 centerY:100 radiusX:48 radiusY:7} // inner rim dimensions

//Coffee
b: Ellipse{centerX:100 centerY:102 radiusX:46 radiusY:6} // coffee dimensions
}
]
}

Stage {
title: “Coffee Cup”
width: 500
height: 500
visible: true
scene: Scene {
fill: BGCOLOR
content: [plateGroup,cupGroup]
}
}

That’s impressive! Look how clean the code is! So much simpler than Swing! But then Andres Almiray turned around and demonstrated how to draw a coffee cup with Groovy:

group( id: ‘plategroup’, bc: ‘none’ ) {
// gray ellipse under the plate
ellipse( cx: 0, cy: 10, rx: 160, ry: 50, f: color(’darkGray’).brighter() )

// thin ‘lip’ of the plate (provides a sense of 3D)
ellipse( cx: 0, cy: 3, rx: 170, ry: 50, f: ‘lavender’ )

// large plate
ellipse( cx: 0, cy: 0, rx: 170, ry: 50 ) {
radialGradient( cx: 1, cy: 1, fy: 100, r: 270 ) {
stop( c: ‘whiteSmoke’, s: 0 )
stop( c: ‘lightGray’, s: 0.5 )
stop( c: ‘darkGray’, s: 1 )
}
}

// recessed plate center
ellipse( cx: 0, cy: 5, rx: 90, ry: 22 ) {
radialGradient( cx: 1, cy: 1, fy: 20, r: 180 ) {
stop( c: ‘black’, s: 0 )
stop( c: ‘lightGray’, s: 0.4 )
stop( c: color(’white’).darker(), s: 1 )
}
}

transformations {
translate( x: 250, y: 300 )
}
}

group( id: ‘cupgroup’, bc: ‘none’ ) {
subtract( id: ‘cup’, asShape: yes ) {
circle( cx: 100, cy: 100, r: 50 )
rect( x: 25, y:50, w: 150, h: 50 )
}
draw( cup, keepTrans: yes ) {
radialGradient( cx: 0.4, cy: 0, r: 100 ) {
stop( c: ‘whiteSmoke’, s: 0 )
stop( c: color(’whiteSmoke’).darker(), s: 1 )
}
}

// outer rim
ellipse( cx: 100, cy: 100, rx: 50, ry: 8, f: ‘white’ )

// inner rim
ellipse( cx: 100, cy: 100, rx: 48, ry: 7, id: ‘innerRim’ ) {
linearGradient( y2: 1 ){
stop( c: ‘whiteSmoke’, s: 0 )
stop( c: color(’whiteSmoke’).darker(), s: 1 )
}
}

intersect( f: color(’darkOrange’).darker().darker() ) {
shape( innerRim )
ellipse( cx: 100, cy: 102, rx: 46, ry: 6 )
}

transformations {
scale( x: 3, y: 4 )
translate( x: -16, y: -70 )
}
}

transformations {
translate( x: -50, y: -50 )
}

Damn. Doesn’t that look similar? There are some syntax differences, but both use a declarative style, and both require less work than using Swing.

Groovy offers some solid MVC frameworks. For web development, there is Grails, which is an imitation of Ruby On Rails. For the desktop, there is Griffon. Consider this post on Transentia:

Although it is early days yet for the project, Griffon aims to bring the same “Configuration by Convention” goodness to desktop GUI development that Grails has brought to Web development. In fact, Griffon ‘borrows’ heavily from Grails: many of the build scripts, etc. are straight carry-overs, meaning that anybody familiar with Grails can get started easily.

This is all that is needed to get started:

griffon create-app GT
cd GT
griffon run-app

These three shell commands give a simple immediately runnable application.

As with Grails, Griffon imposes a clear MVC structure for the code and also creates a standard project filesystem heirarchy.

I haven’t yet worked with Griffon, but the setup described on Transentia looks very clean. Certainly, everything I’ve learned so far about Groovy has left me impressed. And since Groovy integrates with regular Java far more smoothly than JavaFX, why do we need JavaFX? Can anyone name one thing that JavaFX gives us that Groovy hasn’t already given us?

The new programming style: high-level and low-level languages mixed together

Monday, May 18th, 2009

As I recall, when I was getting into programming in the late 90s, and especially when I was getting into PHP, there was a debate about the value of light-weight scripting languages, such as Ruby, Python, Perl and PHP. Some old-school programmers insisted that working with those languages didn’t count as “real” programming – those languages were too easy. And many old-school programmers were critical of the performance hit one takes with the light-weight scripting languages. After all, for some tasks, a routine written in C will run more than 100 times faster than the same routine written in PHP. A two-order-of-magnitude difference in execution speed needs to be taken seriously. Of course, on the flip side, a programmer can rapidly prototype new software with a light-weight scripting language, whereas using C would take much, much longer, and then it would be harder to change once it was done.

Back then, the decision was seen as either/or: either use a light-weight scripting language, or use a lower-level language that is more efficient.

In recent years, the new style has been “both”: mix the high-level and low-level languages. Use light-weight scripting languages to prototype some software, then look at the bottlenecks and drop into a lower level language for those parts of your code that need the speed. For instance, consider what David Heinemeier Hansson says about Campfire, the chat software he helped developed. First written in Ruby On Rails, it soon became clear that the code that polls to see who is in the chat room needed to be as fast as possible:

We rewrote the 100 lines of Ruby that handled the poll action in 300 lines of C. Jamis Buck did that in a couple of hours. Now each poll just does two super cheap db calls and polling is no longer a bottleneck.

Campfire and a shared todo list is different because they’re not working on a shared resource. There’s no concept of locking. Or two people dragging the same item. So a 3 second delay between posting and showing up doesn’t matter. It does when you’re working on a shared resource.

In between C and the light-weight script languages are those languages that added some extras, such as Java, C# and Erlang. Java and C# offer object orientation and automatic garbage collection, whereas Erlang offers automatic concurrency. The crew at 37 Signals has decided to re-write the polling in Campfire, using Erlang:

Last Friday we rolled out the Erlang based poller service into production. There are three virtual instances running a total of three Erlang processes. Since Friday, those 3 processes have returned more than 240 million HTTP responses to Campfire users, averaging 1200-1500 requests per second at peak times. The average response time is hovering at around 2.8ms from the time the request gets to the Erlang process to the time we’ve performed the necessary MySQL queries and returned a response to our proxy servers. We don’t have any numbers to compare this with the C program that it replaced, but It’s safe to say the Erlang poller is pretty fast. It’s also much easier to manage 3 Erlang processes than it was the 240 processes that our C poller required.

The diversity of language choices allows programmers to find exactly the right level of abstraction. When do you need programmer efficiency, and when do you need the code to execute efficiently? One needs a variety of languages so one can always get the right level abstraction for a given project. I think this is part of what Larry Wall was talking about:

Doing it right involves treating the evolution of the language as a pragmatic scope, or as a set of pragmatic scopes. You have to be able to name your dialect, kind of like a URL, so there needs to be a universal root language, and ways of warping that universal root language into whatever dialect you like. This is actually near the heart of the vision for Perl 6. We don’t see Perl 6 as a single language, but as the root for a family of related languages. As a family, there are shared cultural values that can be passed back and forth among sibling languages as well as to the descendants.

As I previously wrote:

Larry Wall is a deep thinker. And he has been developing a deep philosophy of computer languages. Yet the world of Java has, quite surprisingly, opened up and become a world of many languages: Groovy, JavaFX, JRuby, hecl, Jython, etc. No one would have guessed, in 2002, how much Java was going to open up. But while Larry Wall has been thinking about Perl 6, the Java community went ahead and created the reality that he was merely thinking about: “So there needs to be a universal root language… [and] shared cultural values that can be passed back and forth among sibling languages as well as to the descendants.”

I’ve written a little Java in the past, but I’ve always hated how heavy it is. Double casting a variable when you declare it leads to code that strikes me as overly verbose, for my purposes. Most of the time, I work on code that should be built quickly, and execution speed is not a concern. However, I’ve recently started studying Groovy, and I like it quite a lot. It is a light-weight scripting language that runs on the JVM. Using it, one can drop back to standard Java any time one needs a performance boost, or one needs to take advantage of a library that is specific to Java. You could knit together software made of Java pieces, using Groovy as the glue.

This, it seems to me, is the new style. We no longer face an either/or decision about high-level and low-level languages. Rather, the new pattern is, start high, and then drop down to a lower level whenever you need the extra power.