Tropical Software Observations

30 January 2009

Posted by Unknown

at 3:41 PM

1 comments

Labels: ,

Injecting Grails Service class via Spring

A quick note on how to inject Service objects -- or for that matter any object -- via Spring.

Grails has always been able to support the injection of objects via Spring whether using XML or Spring DSL. A good scenario of using Spring DSL is to swap implementations easily.

In our project, Party Time, we use a lot of services. Most of these services implement Java interfaces and may be changed in the future. To be flexible in the changing of these service implementation, we auto-wire the services via Spring DSL.

For example, we have a Service class, LocalCDNService that implements the ICDNProvider interface. We configure the implementation by specifying in grails-app/conf/spring/resources.groovy:


beans = {
cdnProvider(LocalCDNService)
}


And from then onwards we can refer to the service object within our Grails controllers, taglibs, or services by simply declaring:


class ExampleController {
def cdnProvider
...
}


Contrast this to the common way of autowiring services by declaring:


class ExampleController {
def localCdnService
}


If we were to change CDN provider from LocalCDNService to AWSCDNService, we would probably need to change many classes. However if you were to use the Spring DSL way, you just need to change grails-app/conf/spring/resources.groovy:


beans = {
cdnProvider(AWSCDNService)
}


Hopefully this will shed more light to those new to using Spring within Grails.

20 January 2009

Posted by Unknown

at 9:24 PM

4 comments

Labels:

Sun Tech Days 2009, Singapore 20 January

Just finished the first day of Sun Tech Days 2009 -- in summary I give it 4 stars out of 5.

Arrived early and saw a huge crowd of people queuing up for registration. The registration process was efficient -- I got my badge, orange goodie bag, and was quickly shown the way to the foyer. I'll be frank, I was expecting a light breakfast. No breakfast, only coffee and tea. Well as long as they serve coffee I am happy.

The welcoming ceremony started at around 9:10 am. Man, it was hilarious to see the big guys dressed as some Chinese gods. Check out some pictures and video in Arun's blog.

We were given a small Chinese rattle drum on the way into the hall. It's cool to use those small drums instead of clapping. This drum rattling event was supposed to be in Singapore's Book of Records for largest number of developers drumming away. Well whatever floats their boat.

Next thing I remember is seeing Mr Jeff Jackson taking on the stage to coordinate a few demo sessions. Between the sessions I remember lots of mentions of Glassfish, social networking, Google, YouTube, Ruby, OpenSolaris, etc.

Notable demos in the technology showcase:

  1. Mr Arun Gupta demoing Glassfish Prelude -- nice to see Felix container there. Which reminds me, I need to check with the folks at Qi4J. It's been a while since I've dabbled with OSGi stuff.
  2. jMaki demo -- yawn yawn.
  3. Simon Ritter's Wii controller hack -- very cool stuff. I hope he will put the projects and code that he used for the Wii hack in his blog soon.
  4. Somebody got lucky and went home with a Toshiba laptop. I am still jealous.

Next I went on to the JavaFX: The New Platform for Rich Internet Applications track session. Well I had my doubts about JavaFX before but not after this session. Man I'm going to pull out my favorite book on animation and graphics -- Foundation Actionscript 3.0 Animation:Making Things Move and try some exercises with JavaFX. The Wow factor is definitively there but I'm also wondering aloud if this could be another one of those J2ME feel-good-sessions: you know everyone feels really excited to see the rallying around J2ME in McNealy's days as the next platform to bring computing power to everyone's palm, and people end up jumping in droves to Android and iPhone development. Anyway -- a big applause to the demo, it was smooth and very entertaining. Especially when the sharks swim away!

After that I went on to the DTrace: Bridging the Observability Gap for Java and Scripting Applications. DTrace is one of those technologies that I follow closely but could not grasp fully at the same time. It's like a myth that -- that until I really see it as really really cool, I have to keep my mouth shut in telling others about it.

Peter Karlsson was the speaker and though he had some hiccups with his demo, I really wished he didn't give up. He should've probably stuck his fingers on the console, fixed the problems and continued calmly. But I guess keeping calm is easier said and than done when you are running out of time. I enjoyed it anyway but since I will be attending the 9400 – Exposing the Depths of Java Applications with DTrace I thought I will have my chance to know DTrace better later.

Then we are let out for a 15 minute break. I had another cup of coffee and some cakes. Nice. Next to my lab session on DTrace.

Here is a summary of the LAB-9400: Exposing the Depths of Java Applications with DTrace session:
  1. Peter Karlsson and Joey Shen are conducting this session
  2. Many didn't come with their laptop and I'm not sure what they got out from this since the session is pretty code intensive
  3. Of those with laptops I'm the only one with Mac so the bootable USB of OpenSolaris won't work on me. Oh well at least I have DTrace built in on my Mac OSX 10.5.6. heh
  4. The lab slides are about 200+ pages -- we were told that it would take about 8 hours to cover them all. We have like about 2 hours. :-O
  5. The depth of DTrace is overwhelming -- I barely understand the D language: probe, predicate, and action scripting.
  6. While Peter was talking about the introduction of DTrace, I did a quick search on "Mac OSX dtrace dtracetoolkit", I realize that my mac already have most of the DTraceToolkit available. Try "ls /usr/bin/*.d"
  7. Later I got a good grasp at how DTrace and D language works -- I began to open up the D scripts on my Mac to see the gory details of the scripts.
  8. Too bad Java 6 on Mac doesn't support DTrace yet. (Apple tends to bump up Java guys)
  9. DTrace is more than a super steroid debugger -- you can turn it into all kinds of monitoring instrumentation. (Need to google for an anti-keylogger D script)
  10. This session is way too short and too brief. If Peter and Joey are reading this, may I suggest to you guys this: pick a topic short enough to fit into 1.5 hours. Stop telling me about all the cool stuff I can do with DTrace. I know that already, or else I wouldn't be sitting there in the first place. Show me how to "X-Ray" my Java apps!
  11. At the end of this session, I am reminded of the saying, "the teacher only shows you the door, you have to go in yourself". Well thanks, I will be writing about D scripts shortly.

Next, maybe I had too much coffee, was palpitating. By now it's already 4.30pm, I had another coffee in this very short break.

On my next session track, I have the choice of going to:
  1. MySQL: The Database for Web 2.0
  2. Java SE 6 Update 10: Simplified Java Deployment
  3. Thriving on OpenSolaris
I chose the OpenSolaris session because I wanted to see some OpenSolaris 2008.11 in action. Here are my thoughts of that session:
  1. I was late a bit and didn't get the speaker's name. I got the overall impression that he is floating the on clouds with OpenSolaris here.
  2. Way too much time playing with Compiz on the Gnome desktop. C'mon guys, it's Compiz -- lets move on already!
  3. And seriously, somebody should tell the crowd that OpenSolaris is using the Gnome desktop -- we're developers, we will get it even if you need to explain such things as X11, desktop client and Unix stuff like that.
  4. The ZFS part was not impressive. I have seen better webminar videos on this. yawn
  5. The DTrace part was a waste of time. He should've probably skipped it altogether or summed it up in a one paragraph or politely pointed to some online resource.
  6. The speaker fumbled way too often on the command line. Stop using "clear", use "ctrl+l" to clear the screen. (hold on, is OpenSolaris using Bash by default?)
5.45pm, rushed to the Groovy and Grails session track. Boy, I have something serious to say about this.
  1. I was expecting to see some experts sum up to us the latest JRuby progress in this session.
  2. Why the heck is the description of the track about JRuby on Rails yet titled as Groovy and Grails? If I knew it was really on Groovy and Grails I would have skipped it and gone to the Porting Open Source Packages to OpenSolaris session.
  3. Mr Chuk Munn Lee, Sir, I don't think you did your homework on Grails. I sort of tuned out when I heard Mr Chuk said that Grails doesn't support many-to-many relationship. I thought I heard wrong, but he followed by explaining how you need to tweak some mappings to get that feature.
  4. The example on Groovy was too shallow and the Grails part was not good. I think the audience would appreciate what Grails is all about by starting the introduction to domain modeling and GORM instead of starting with "How many of you have programmed taglibs in JSP before?"
  5. Mr Arun Gupta was 3 seats away on my left -- he looked tuned out too. (Lol)
On my way out, I handed in my feedback form and was surprised to get a nice flap-over-bag. It's basically a small soft messenger bag. I'm happy to find the latest OpenSolaris 2008.11 inside. See the other goodies I got today below.

09 January 2009

Posted by Unknown

at 5:59 PM

0 comments

Labels: , , ,

Consume RSS Feeds easily with Groovy and Rome

This is what I did to start consuming RSS feeds with Groovy within 20 minutes!

1. Download Rome.
2. Download Rome Fetcher.
3. Download JDOM is you haven't done so.
4. Extract rome-x.jar, rom-fetcher-x.jar and jdom.jar to a tmp directory -- I placed them in lib/.
5. Start your Groovy console with the classpath set to the jar files, like this:
$ groovyConsole -cp lib/rome-1.0RC1.jar:lib/rome-fetcher-0.9.jar:lib/jdom.jar
6. Use the code below:


import com.sun.syndication.fetcher.*
import com.sun.syndication.fetcher.impl.*
import com.sun.syndication.feed.synd.SyndFeed


FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
SyndFeed feed = feedFetcher.retrieveFeed(new URL("http://feeds.digg.com/digg/topic/programming/popular.rss"));


feed.entries.each {
println " -> ${it} "
}

println " == ${feed.entries?.size()}"


7. Finally, start reading the below docs:

Testing Grails Service class

Testing Grails service classes is easy if the service does not call any domain persistence methods. Meaning if your service class does cause some domain object to persist to database, it's going to be painful to try to mock the calls and check the result. And I'm not even sure if it's worth it to try.

The problem is that grails-testing newbies like me will want to try to do unit testing for services. In short, don't do that unless your service classes are simple and easy. (Keep this in mind when you start your project -- you won't see this unless you've experienced the pain before).

The best way that I found so far is to make service tests into integration tests. This mean putting service tests into the "integration" folder under the test directory. For more explanation on the difference between Unit Testing and Integration Testing in Grails, go here.

Testing Service classes is easy once you understand that it is:

  • it is an integration test -- will talk to the database
  • it is slow -- probably because of the data connection overhead
  • will persist data to database -- make sure your test environment database settings don't point to a production database
A typical service test looks like this:
class FooServiceTests extends GroovyTestCase {

def fooService

void setUp() {
fooService = new FooService // *1
fooService.otherService = new OtherService() // *2
}


void testServiceCall() {
def user = new User(name:"Test", email:"test@name.com")
user.save(flush:true) // *3
fooService.doSomething(user) // *4
assertEquals user.result, "expected result"
}

}
A couple of helpful points as indicated by the numbers above:
  1. You have to initialize the service class yourself, there is no DI.
  2. If the service has other service classes inside it, initialize and assign it manually
  3. Remember to always flush to persist to the database immediately -- else you might get weird things like no "id" for the supposedly saved domain object.
  4. In integration mode, fooService will act like a "headless" Grails app

I hope this is helpful to you -- I may not be right, so feedback and corrections are welcomed.

05 January 2009

Posted by Irregular Zero

at 5:51 PM

0 comments

Google Friend Connect & Facebook Connect

Google Friend Connect

A Google, Yahoo!, AIM or OpenID account can easily be used for third party authentication.

You start it off by logging into your Google account and going to http://www.google.com/friendconnect/.
  • From there, click on 'Set up a new site' and enter your site name and url.
  • You'll be given two files, canvas.html and rpc_relay.html, to copy onto your web site.
  • The last step is testing for those two files.
After that, it's a matter of choosing the gadgets you wish to add, customizing it, and then copying the code generated into one of your site pages. The code is normal HTML and Javascript.

From the current gadgets available, you can add on to your site to make it more 'sociable', like knowing the site members, adding in a chat box and reviewing/rating stuff.

You can create your own custom gadgets from these sites:
http://code.google.com/apis/gadgets/
http://code.google.com/apis/opensocial/

Facebook Connect

Only a Facebook account can be used for authentication with Facebook Connect.

Login into your Facebook account and follow the instructions located here:
http://wiki.developers.facebook.com/index.php/Trying_Out_Facebook_Connect

The main page for developers for Facebook Connect is http://developers.facebook.com/connect.php.

Facebook uses it's own Facebook Markup Language (FBML), in contrast to normal HTML, to facilitate creation of all Facebook applications. Facebook Connect is therefore embedded into user pages using FBML.

Facebook Connect is more of an expansion of Facebook onto other sites, depending on the application, it allows for data on a user site to be displayed back in Facebook.

HOW TO: Add Facebook Connect to Your Blog in 8 Minutes