The Artima Developer Community
Sponsored Link

Java Buzz Forum
Feeling Groovy again

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
Feeling Groovy again Posted: Apr 26, 2004 10:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: Feeling Groovy again
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement
Ahh, some of the old feelings are coming back. I was on the phone with someone... asking me if I could put together a program that munged some database data and split it out into a file. I would normally use Perl, but I am feeling Groovy at the moment. So, as I was being explained the problem at hand, I opened up a new file and got to work. At the end of the explanation, when asked "when could it get done by?", I was able to say "how about now?". import groovy.sql.Sql import java.io.File sql = Sql.newInstance("jdbc:postgresql://dbhost/dbname", "user", "pass", "org.postgresql.Driver") new File("mungeddata.log").withWriter { writer | writer.writeLine "Foo, Bar, Baz" sql.eachRow("select foo, bar, baz from thetable") { writer.writeLine([it.foo, it.bar, it.baz].join(', ')); } } All wasn't perfect though. I really wanted to reuse a utility class that gets a DB connection by doing the Right Thing (gets a datasource if possible, plays nice with the pool, etc). So I tried to do: import groovy.sql.Sql import mypackage.DBUtil sql = Sql.newInstance(DBUtil.getConnection()) Unfortunately I got: groovy.lang.MissingMethodException: No such method: newInstance for class: groovy.sql.Sql with arguments: [org.postgresql.jdbc2.Jdbc2Connection@2a15cd] Ergh. getConnection returns "Connection". I even tried to cast it, but nothing worked. Oh well, I will have to check it out some more. The fact that (in theory) I can easily tie my "scripts" into my Java code is great. Sometimes you have to write a script that takes in some data, and plugs it into your domain. Often you do that with brute force and shove it into the DB, however it is really nice that now you can: Munge data in groovy Package data in a nice way in preparation for ... Accessing your service facade, passing in the nicely packaged data Great stuff. I do have a couple of frustrations though: Bad error reporting: When things go wrong (compile time) I often get a mega-stacktrace from hell. Really I just want a nice message "couldn't compile script due to X". Instead you end up with something like: ... many lines ... at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:350) at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:124) at org.codehaus.groovy.runtime.InvokerHelper.invokeMethodInvokerHelper. ava:101) at dumpmetadata.run(dumpmetadata.groovy:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl. ava:39) at sun.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAcces orImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetMethod.java:56) at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846) at groovy.lang.MetaClass.invokeMethod(MetaClass.java:268) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... many lines ... Some things just don't work like I want them to. For example I really wish the following would work: println [1, 2].join(' ') What do you think you get back? A "sorry old chap, put some parens around will you?". Nope. java.lang.NullPointerException at groovy.lang.MetaMethod.(MetaMethod.java:84) ... add many lines ... I know I just need to make this println([1, 2].join(' ')), but if you want to make parens optional, do it right :) Don't get me wrong.... I know these are growing pains. Hopefully we can take a leaf out of Howards book, and have line-precise errors ;)

Read: Feeling Groovy again

Topic: No Fluff Just Stuff - Day Two Previous Topic   Next Topic Topic: YAPC schedules online

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use