Sep 20

RE: It’s not about one-liners

Groovy, JavaScript, Perl, Ruby, Tech 3 Comments »

I agree with Cedric.

The “best” programming language, doesn’t mean the one that creates the lowest wc -l.

I actually always found myself writing quite “verbose” Perl code, for example.

However, I do feel that languages such as Ruby, Groovy, and yes… even Perl allow me to get closer to the zen of “expressing everything I want, and need to get across… but not more”. Every operator/method tells me a lot.

For example. Compare the code for taking out some text from a string:

Groovy:

name = “Dion ‘Sick Boy’ Almaer”
name -= “‘Sick Boy’”

and in Java? ergh.

Aug 03

I’ll name that developer in 5 lines of code!

Groovy, Java, Perl, Tech 7 Comments »

You know the feeling when you open up some code from a fellow programmer, and it says a lot about them?

Their code style speaks to you as does the style of an essay does.

My latest experience is looking at various Groovy code snippets. There have been two extremes that I have come across:

I like my Java, just with a twist

These folks are happy in Java land, and the code looks just like it. There are just a few things differing. Maybe the odd ‘;’ is left off of the statements. Here and there variables aren’t staticly typed. But, mainly, it looks like Java

I want to be different

Some of the guys who have really bitten into Groovy really try hard to have their code NOT look anything like Java. Everything is groovy-fied to the extreme.

This reminds me of some days when I was deep in the Perl community. Since the language gives you 55 ways to do something, you see even more contrasting styles. In that environment you could find the hacker, the one liner, the I have a Comp Sci degree so I code correctly!, the obfuscator, and more.

Fun :)

Aug 02

NanoWeb: PicoContainer + Groovy = no XML again?

Groovy, Java, Lightweight Containers, Tech, Web Frameworks No Comments »

Here we have another one in the “get me away from angle brackets” category.

Kris Thompson pointed to the Nice clean, dynamic MVC framework: NanoWeb.

NanoWeb is a simple and powerful dynamic MVC framework based on Java servlet technology,Groovy and NanoContainer. NanoWeb borrows many ideas from WebWork and Struts, most importantly the concepts of actions and views. Its main difference from these frameworks is that it is 100% dynamic and requires no configuration beyond mapping of to servlets in web.xml

Pelle Braendgaard wrote a nice introduction to NanoWeb, in which he walks you through a simple application.

I liked his quote after writing the Groovy action:

I am no Groovy expert. As a matter of fact this was my first attempt at groovy and I have no idea what I

Jul 30

Springing forward with Groovy

Groovy, Lightweight Containers, Tech 2 Comments »

We have really been enjoying writing WebWork actions in Groovy. It is a pleasure, and I had a really nice chat with Rod Johnson about how cool it would be to write beans for Spring in Groovy.

Rod then managed to whip up some code that lets this happen, so you could do:

<bean id=”property”
singleton=”false”
class=”org.springframework.beans.groovy.GroovyFactory”
factory-method=”dynamicObject”>
<constructor-arg index=”0″><value>org/springframework/beans/groovy/PropertyHello.groovy</value></constructor-arg>

<!– Reload seconds –>
<constructor-arg index=”1″><value>20</value></constructor-arg>

<!– Property on created Groovy object –>
<property name=”message”><value>hello world property</value></property>
</bean>

Works like a charm :)

We also talked about maybe allowing other languages to be plugged in. Imagine a config such as:

<bean id=”something”>
<script language=”groovy” file=”org/springframework/beans/groovy/PropertyHello.groovy” />

<property name=”reloadSecs”><value>20</value></property>

<property name=”someDI”><ref bean=”SomeBean”/></property>
</bean>

We also talked a little about having Groovy as a config option, and at then Bing Ran emailed the groovy-dev list with a concrete example of how it would look:

import org.springframework.jndi.JndiObjectFactoryBean
import org.springframework.orm.hibernate.LocalSessionFactoryBean
import net.sf.hibernate.dialect.MySQLDialect
import org.apache.commons.dbcp.BasicDataSource
import product.ProductDaoImpl

DataSource myDataSource = new JndiObjectFactoryBean {
jndiName = “java:comp/env/jdbc/myds”
}

DataSource myDataSource2 = new BasicDataSource(”destroy-method”:”close”) {
driverClassName = “org.hsqldb.jdbcDriver”
url = “jdbc:hsqldb:hsql://localhost:9001″
username = “sa”
password = “”
}

SessionFactory mySessionFactory = new LocalSessionFactoryBean {
mappingResources = ["product.hbm.xml"]
hibernateProperties = [hibernate.dialect : MySQLDialect.class]
dataSource = myDataSource2
}

Dao myProductDao = new ProductDaoImpl {
sessionFactory = mySessionFactory
}

The general idea is to use a constructor like structure to describe the wiring of objects.

1. implicit property setting: since we know that an assignment in the Groovy constructor call is to set the property, we save <property> pair, my mind doesn’t need to use any extra energy to interpret it.

2. better list/handling. Groovy list/map construct really shines here, instead of saying:

<property name=”mappingResources”>
<list>
<value>product.hbm.xml</value>
</list>
<list>
<value>customer.hbm.xml</value>
</list>
</property>

I used:

mappingResources = ["product.hbm.xml", "customer.hbm.xml"]

Instead of:

<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>net.sf.hibernate.dialect.MySQLDialect</prop>
</props>
<props>
<prop key=”hibernate.someotherkey”>some other value</prop>
</props>
</property>

I did:

hibernateProperties = [
"hibernate.dialect" : MySQLDialect.class,
"hibernate.someotherkey" : "some other value"
]

3. Possible strong type checking: the interpreter, when compile the groovy config , can check if the property settings is match the types. In the xml file, everything is just a bean, and I have to read the class name to “infer” what type of beans they are: a data source, transaction manager, a DAO, an Interceptor, etc. This makes reading the config quite a whirl.

4. Possible tree like structure: instead of:

<bean id=”myHibernateInterceptor” class=”org.springframework.orm.hibernate.HibernateInterceptor”>
<property name=”sessionFactory”>
<ref bean=”mySessionFactory”/>
</property>
</bean>

<bean id=”myProductDaoTarget” class=”product.ProductDaoImpl”>
<property name=”sessionFactory”>
<ref bean=”mySessionFactory”/>
</property>
</bean>

<bean id=”myProductDao” class=”org.springframework.aop.framework.ProxyFactoryBean”>
<property name=”proxyInterfaces”>
<value>product.ProductDao</value>
</property>
<property name=”interceptorNames”>
<list>
<value>myHibernateInterceptor</value>
<value>myProductDaoTarget</value>
</list>
</property>
</bean>

I can do something like this:

ProxyFactory myProductDaoFactory = new org.springframework.aop.framework.ProxyFactoryBean {
proxyInterfaces = product.ProductDao.class
interceptors = [
new HibernateInterceptor {sessionFactory = mySessionFactory},
new product.ProductDaoImpl {sessionFactory = mySessionFactory}
]
}

After we have macros in Groovy we can build configurations that configure huge number of otherwise similarly configured beans such as DAOs:

#def_dao ["dao1", dao1Impl]

#def_dao ["dao2", dao2Impl]

#def_dao ["dao3", dao3Impl]

#def_dao ["dao4", dao4Impl]

The possibility is unlimited.

Now, a good binding of Groovy will not only simplify the config, but probably more importantly, simplify writing Spring beans.

Here is an example:

public class ProductDaoImpl implements ProductDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List loadProductsByCategory(final String category) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(this.sessionFactory);
return (List) hibernateTemplate.execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
List result = session.find(”from test.Product product where product.category=?”, category, Hibernate.STRING);
return result;
}
}
);
}
}

Here is the same Dao impl in Groovy:

class ProductDaoImpl implements ProductDao {
property SessionFactory sessionFactory;

List loadProductsByCategory(String category) {
ht = new HibernateTemplate(sessionFactory);
return ht.execute {|session|
session.find(”from test.Product product where product.category= ${category} “)
}
}
}

The property and the Closure features make it look a lot cleaner. Groovy could well suited to writing Spring beans: each unit is small and clearly intended. We don’t need too much compiler help to make it work and there is no compile and deploy cycle.

Further down the road, I imagine that macros/annotations can help us build a groovy data access layer that sits on top of the Spring JDBC templates to implement a mean and clean persistent mechanism: no XML mapping files, auto-interfacing (defining the interfaces and impls in the same file), etc.. Would that be cool?

Now we have a strong use case for Groovy!

-bing

All early days, but cool :)

Jul 22

Tuples and Globals in Groovy

Groovy, Tech No Comments »

Tuples

I am really looking forward to life with Groovy when tuples are supported.

I always liked working with these guys in other languages (including Perl):

my ($name, $address, @otherstuff) = parseContactsFile();

Most of the time I get around this by trying to return back real things:

contact = parseContactsFile()
println contact.name

however for some things it can be nice… e.g. from:

new File(”input.txt”).eachLine { line |
s = line.split(”\s*=\s*”)

println “Name: ${s[0]}”
println “Value: ${s[1]}”
}

to:

new File(”input.txt”).eachLine { line |
(name, value) = line.split(”\s*=\s*”)

println “Name: ${name}”
println “Value: ${value}”
}

Globals

A lot of questions from people hacking up simple Groovy scripts result from their assumptions not being met.

A common one is thinking that you can create a variable at the top of a script and have it scoped so you can use it inside methods / closures somewhere down the pike.

Globals are not considered to be friendly folk, but for simple scripting they can be a pleasure.

I just hope that when implemented in Groovy they don’t go the TCL world, so we don’t see: upvar foo and global foo all over the shop!

Jul 22

Truly scary: Just In Time Groovy Libraries

Groovy, Tech No Comments »

Now this is scary :)

<jstrachan> brianm: some code should arrive soon in groovy where we can auto-download jars & dependencies from maven repos based on class loaders
<jstrachan> philip milne (ex-sun) has done it & plugged it into groovy
<jstrachan> so you can do things like
<jstrachan> new com.foo.Whatnot().doSomething()
<jstrachan> and it’ll load the latest version of whatnot.jar from maven & add it & dependencies to your classpath & run it
<brianm> hahahahahahaha
<jstrachan> would make scripting & using other libraries/frameworks really easy
<jstrachan> :)
<brianm> remote classlaoader pointed at jar in repo baby
<brianm> why download?

From Brian Mc

What would be cooler is:

% groovysh

1> new com.newpackage.ObjectThatWorksRemoteControl()
(thank you Dion. I just created a new object that will work a remote control and exported the jar to the maven ibiblio repository for others to use)

Jul 21

Implementing Tapestry Component Methods with Groovy

Groovy, Java, Tech, Web Frameworks 16 Comments »

After playing with Groovy and WebWork 2, Michael Henderson has written about his Tapestry-Groovy integration.

It will be really great to be able to use Groovy as the config language (instead of XML, and even maybe instead of the SDL in HiveMind), as well as for the Components and Pages themselves. Since Tapestry is such a nice OO/component view of an application, Groovy fits in nicely. Hell, we could use Groovy instead of OGNL too (if it made sense!)

Jul 21

Christian Groovin’ with Webwork 2

Groovy, Tech, Web Frameworks 18 Comments »

My Adigio co-founder Christian Parker is having fun with Groovy. We went to a talk by Rod Cope at the Boulder JUG last week, and it got us thinking about where Groovy can fit into a web application that CP is working on.

Groovy as Actions. Groovy as the View. Groovy as the XML configuration. Groovy everywhere!

In this entry Christian talks about the work he did integrating Groovy with WebWork 2. It will be really cool when you can put it in debug mode, which would enable you to tweak the Groovy action and hit reload to test. No more compile/build step!

Jul 19

James Gosling thinks that Groovy should be more out there

Groovy, Tech No Comments »

Darryl Taft has a nice chat with James Gosling.

He talks about many topics, including Groovy. In general he likes it, but wishes it was a bit more daring. He even had a list of questions / thoughts for the Groovy team, and hopefully James will ask him for the list. I bet there are some interesting items!

I think Groovy is pretty interesting. I think they’re being fairly conservative. I think they could be a little more outlandish and get a little more interesting.

remember sort of reading through their syntax list and saying, “Oh, why didn’t you do this and why didn’t you do that?” There were some things about the way that they did sort of the equivalence of declarations and some things about the way they did case switch statements

Jul 18

“Groovy bashers” – Everyone doesn’t have to like a language

Groovy, Tech 97 Comments »

Jason Bell states “I was wondering how long it would take before the Groovy bashing would start“.

Elliotte Rusty Harold also had some thoughts, and James responded to them too.

Whenever a new language comes a long a group of people come out and say “it sucks. I hate [insert feature here] about it!”.

Groovy may not be your bag baybee. That’s all cool :)

You like Python? Great… it is a very nice language. Ruby? Perl? Java? ditto.

Noone is coming to take away your toy, or force you into using something :)

I personally think that there is value to Groovy. I like the way that I can use it as Glue technology. I don’t think that the rigid static typing way is for me on all projects (or part of projects) and I like how I can switch between a .java and a .groovy.

I prefer looking at my ant scripts in Groovy syntax (if they get complicated and I need to do iteration, or anything in the slightest programming-ish, then I want to get away from build.xml). I like Groovy Swing/SWT for putting together GUIs easily. I like unit testing with Groovy. I like using WebWork Actions with Groovy. I like writing scripts that reuse my core Java business logic with Groovy. Etc. Etc. Etc.

It doesn’t save the world. There are other great languages. Use what you feel most connected with!