Domain Specific Languages with Groovy

May 8th, 2008

In general a DSL, domain specific language, is a language built to attack a specific problem domain and the lexicon of that language matches the problem domain. Huh?! What?! Is that even English?! In a nutshell a DSL is a language that maps to a very specific problem and the keywords are ones the users/developers of that problem space are familiar with. For example you have the problem domain of interacting with Apple’s iPod. Instead of using a general purpose programming language like Java you could develop a DSL that attacks the specific problem of interacting with and iPod. You could have specific commands like play, load, fast-forward, etc. Doing this in Groovy is really simple with method pointers. An example method pointer would be:

say = System.out.&println 

So lets setup the iPod interaction scenario and create a DSL around that. The first thing we need is a Song class to hold song information:

class Song {
  def title
  def artist
  def album 

  static Song newSong(title, artist, album) {
    new Song(title: title, artist: artist, album: album)
  }

  String toString() {
    "${title} by ${artist} on ${album}"
  }
}

Next we need an iPod class to hold that information:

class iPod {
  def static songs = []
}

This now gives us a basis for creating a DSL. We could use the classes out of the box but they are not in the language the use would be used to. Let’s abstract the user away from the class model and create a DSL they would be familiar with. We will do this by setting up some method pointers:

create_song = Song.&newSong
say = System.out.&println
play = { iPod.songs.each{song -> say song} }
load = iPod.songs.&add

Now we can write a little DSL script for interacting with out iPod:

load(create_song("Blue Collar Love", "StarFlyer 59", "Silver"))
load(create_song("Chasing Cars", "Snow Patrol", "Eyes Open"))

play()

This is just a small example of what is possible. There could be other ways to accomplish the same thing but I like this way. With the

ExpandoMetaClass

and meta-class programming combined with method pointers you can create a really interesting DSL that can help users or developers of your application by abstracting away the hard stuff. You could also embed a scripting language into your application by using a DSL.

This is just another example of what makes Groovy so "Groovy"

Give me two Red Pills

May 7th, 2008

I just got out of a session about taking the Red Pill - Groovy and Meta-programming. This session was flat out awesome! The speaker was excellent and his demonstrations were impressive. In my group at work we have been building applications with Groovy for about two months and I have already swallowed the Red Pill, but after this session I want a second dose. There is so much more about Groovy that I need to learn to truly unlock its power. The meta-programming aspect of the language is my untapped market. The speaker did a great job of showing how Groovy enables more productivity by having Java examples and then showing the Groovy equivalents. There were many oohs, ahhhs and laughter about how easy certain things are. What was really cool is how he javap’ed the Groovy class to basically show the bytecode and prove that it is really Java under the covers. He did a great job pf pointing out that he has heard that Groovy may replace Java and laughed that off. He basically said it would be live "icing replacing cake", it is not going to happen. Groovy is Java and they live happily together. This is a really excellent point. You can use Groovy without fear that you are cheating on your precious Java. I will blog hopefully later about some really cool code examples of things he showed with the MetaClass programming. This stuff is really super cool programming magic.

The takeaways really are threefold:

  1. Try it! Use in unit tests because the meta-programming can really make unit testing rock for even the hardest of problems. Not only does it rock but it is like a drug, the more you use it the more you need it.
  2. If you need to build rapid applications for proof-of-concept, prototypes, etc. on a JVM you really can not afford to not use Groovy. It is just Java but with the icing to let you get were you need to go quickly. Groovy is not better than Java it is just the icing on the cake. You can build real applications for very low cost in development time. Not only that it is easy for Java programmers to pick up because it is a world they are already familiar with.
  3. Groovy can be used and is used it production. Oracle, IBM, LinkedIn, Sun, Mutual of Omaha, etc. are all examples of companies that use Groovy in production applications. Is is slower than Java, sure, but only in milliseconds. Not everything about a business application is performance. They use it, you can too!

I strongly encourage you to watch this presentation when it is posted for FREE on the JavaOne web site. I will post a link when it becomes available. Be fore you make your mind up on Groovy give this presentation a watch. Give Groovy a try before you relegate it to the insignificant. I have taken the Red Pill and am about to swallow my second.

It is all about YOU

May 7th, 2008

So I am at JavaOne 2008 and had a great time listening to the Day One keynote/general session. Sun had some entertaining demos and the speakers were good. There were some very interesting messages they were trying push:

  1. Java + You - This is the main focus and message at this JavaOne. Sun is extending its focus from business to consumer. The new slogan is "Java on all the screens of your life", meaning phone, computer, car and TV. These screens should bring new an innovative content and applications to our life. The message was that Java is already there and will only grow to help us as developers deliver the apps that consumers want/need. This is a really interesting pitch. It was more like business, been there done that, the consumer/social ecosystem is where the new money is. How to role Web 2.0, RIA, social networking, mobile apps play into the new business model and how to monetize that market.
  2. JavaFX - this is the technology Sun is betting on for hitting the Web 2.0/RIA crowd. They introduced this tech last year but had more to show this year. I find this approach really interesting as there are already major players in this market (Flex and Silverlight). I am not sure what Sun wants to gain by inventing its own instead of integrating with Flash/Flex. We should see this play out over the next couple months. It may be just to leverage the ubiquity of the JVM instead of Flash. The really interesting bits was the ability to drag and FX app out of a web page and save it your desktop and use it like a desktop app. This was extremely cool! That seamless interaction between the Web and the Desktop is the holy grail in my opinion. When you can abstract away the idea of the connection then you can truly innovate the way we work with our apps.
  3. Mobile is a viable platform - Sun and Java have existed on the mobile platform for years and this was just extending the idea with FX running on the devices. This was really just a personnel message to myself that if I want to stay relevant I need to get into the mobile space a bit.

The keynote overall was really cool, very software rockstarish. Loved that they brought Neil Young on stage and showed how he was able to fulfill a dream by meshing BluRay and Java technology to provide a true interactive music experience.

I recommend anyone hoping online and watching the archived streaming keynote. It will definitely be interesting to see how the social aspect of Web 2.0 change the way we build applications. The focus on usability by non-techs will be huge in the future. Time to get creative!

Variable initialization in BPEL 2.0

November 8th, 2007

When using variables, either global or scope, in a BPEL process you have to make sure you initialize them with a value before use or an exception will be thrown. You can do this easily with an <assign> tag during the execution of the process. Yet, what if you want to use a global variable for the life-span of the process and do not want to reassign it on every <receive> or <pick> that starts the sequence?

As of BPEL 2.0 you can initialize your variables with an inline from-spec in the XML. What??? This is really quite simple. In the variable declaration sections that could look like this:

<variables>
       <variable name="PostCount" type="xsd:long"/>
</variables>

You just need to modify it with a from-spec like so:

<variables>
       <variable name="PostCount" type="xsd:long">
             <from><literal>1</literal></from>
        </variable>
</variables>

The the <assign>, <copy> and <to> tags are implied and do not need to be set.

This makes it very easy to initialize a variable at any scope when it is defined so you do not hit the uninitiated error when running your BPEL process. This also allows you to have global variables that can be used during the entire life-span of the process without having to reset it every <receive> or <pick> event.

NOTE: Currently the Sun BPEL SE for OpenESB does not support this inline variable initialization which makes using global variables for tracking quite pointless. Hopefully the team will implement this as it is standard BPEL 2.0.

Maven repository tip #2

October 19th, 2007

I was trying to deploy my maven plug-in to the internal repository and was having a little bit of trouble. I could not get my plug-in to deploy properly because of ‘Permission denied" errors. This was easily solved by deploying to the file path of the repo server instead of the http path of the web repo. For example, when deploying to a repo http://myrepo/maven2 that is stored on the local file system at /repo/maven/maven2 you need your scp:// statement to use the file system path so it shows up correctly in the http path. For example:

<distributionManagement>
  <repository>
    <id>internal-repo</id>
    <name>Internal repository for Maven</name>
    <url>scp://myrepo/repo/maven/maven2</url>
  </repository>
</distributionManagement>

Also, you can configure the authentication to the server in your Maven configuration file at MAVEN_HOME/conf/settings.xml. You need to add the following configuration under the <servers> element:

<server>
  <id>{unique repo id}</id>
  <username>{server username}</username>
  <password>{server password}</password>
</server>

For example:

<server>
  <id>internal-repo</id>
  <username>joeuser</username>
  <password>P@ssw0rd</password>
</server>

The id would be the same id you use in the deployment element in the pom.xml file.

 

Maven 2 repository tip

October 18th, 2007

"It’s a formidable scent… It stings the nostrils. In a good way."

I recently had issues setting a dependency to Ant 1.7.0 for a project I was developing. The default local repository for Maven 2 is located at ~/.m2/repository. This resolves in Windows  to C:\Document and Settings\{username}\.m2\repository.  When setting up a dependency in a project to Ant 1.7.0 the build would fail trying to grab Ant because of a illegal character in the path. It looks like Maven or something doesn’t escape the path properly and the spaces were causing the build problems. The fix was to point Maven to a new local repository path that had no spaces.

To do change your local repository:

  1. Navigate to MAVEN_HOME/conf/
  2. Edit the settings.xml file
  3. Under the <settings> element add an element called <localRepository> and enter your new path there.

For example, <localRepository>/.m2/respository</localRepository>

Your maven builds will take a little longer the first time you run them again because it fills the repository fresh. You could also just cut and paste your older repository contents and put them in the new place to minimize re-download.

Firefox search tip

October 9th, 2007

I love Mozilla Firefox. I think it is the best browser available. It has great plug-ins and themes. It also has this really cool keyword creation utility. Basically I use this for quick searching on sites I go to a lot like Google or Wikipedia. What you can go is setup a keyword that can be used as a shortcut on the URL address bar. For example, I use git:, short for "Google It", for quick searching Google. Say for instance you want to search Google for books, I would type in the url address bar git: books and hit enter. The browser would then use Google to search for books. I also have one for Wikipedia where I could search for books by typing in the URL address bar wiki: books. Following are the steps to set this up:

First, go to your favorite search site and enter something to search and grab the URL from the address bar after the serach. For instance if you search Google for books you get an URL of:

http://www.google.com/search?hl=en&q=books&btnG=Google+Search

If you notice in the URL the q=books area. This is where any search phrase goes for searching Google.

Second, you need copy this URL and go create and new blank bookmark. Bookmarks->Organize Bookmarks…, this will launch the Bookmarks Manager and click the New Bookmark button. You should see the following screen:

image

Next you enter a new for the key word search like "Google Search"

image

You need to now paste the copied URL into the Location: area

image

You need to find the q=books in the URL and replace it with a %s which is built in as a variable to basically mean any text you type after the keyword

image

to:

image

Finally add a meaningful keyword to the bookmark that you want to use for searching this site

image

Click the "OK" button and close the Bookmarks Manager. You can now clear the URL address bar in Firefox and type "git: books and stuff" (and remember the space between the keyword and the search phrase and you do not need the quotes) and hit the Enter button on the keyboard. You should now see a Google search results page for "books and stuff".

This is really a powerful, time saving feature that I can not live without. It is hard for me to use any other browser because I rely so heavily on my keyword search. You basically can use this keyword tool for any URL that you want to dynamically replace some text in the URL with %s. This works great for sites that have searches you use a lot. I have keywords for Google, Wikipedia, Dictionary.com and Wookiepedia (I am a Star Wars fan). Create a Google bookmark and get in the habit of using it and you will never type www.google.com in the address bar again.

Let me know how this works for you. If you have any trouble I could be persuaded to post a How-To video if needed.

it has been awhile…

October 4th, 2007

So it has been awhile since I last blogged. I got out of the habit and really didn’t have much to say anyway. Like was moving fast and furious and I just didn’t make the time. So that begs the question, "What has changed now?"

Those that know me know I have worked at a large corporate 500 hundred business for the last 5 1/2 years. I worked on a variety of software projects will different levels of fulfillment. I had resigned myself to  never being fulfilled in my work but doing so I enabled my family to stay here at "home." I was very important to me to be able to provide my family with a since of "home" without chasing every whim/dream I might have. So, what has changed?

Well I am proud to report that I have taken a position of Senior Consultant for a software company called Gestalt. This company is so opposite from what I had been living in I can hardly explain all of it. I have found a "home" for my profession and it is still located at "home." You can visit their sight to see what they are about but they are a software developers nirvana, well so far. I feel that this company wants the best, invests in their people and truly believe people are they key to their success. A lot of companies, including my last, may say this but so far they have lived it.

To prove so the first day on the job I had my laptop ready and prepared. I was up and running within an hour. I was given something to work on right after getting set up. I felt like I was already contributing to matter how small. A couple of fellow workers took the time to take me to lunch and give me a little history of our group. Every question I had for our group was answered without that "I am busy so go away" type of answer. Most import I just finished new employee orientation where I got to learn a lot about the company and the culture. I was asked the question, "What is your take away for NEO?" I answered by stating that we have been introduced to the company my various executive management and not once did I hear them refer to us as resources. The day you are referred to as a resource is the day you are a nameless, faceless, expendable, replaceable cog in a monstrous wheel. I cam from that and do not want to go back. If you truly value your people, you will treat them like people, with names, faces, families, unique abilities and quirks that you use to build an extraordinary business. People in my field truly want to work, do extraordinary things with software, have people truly use and love our stuff. This is not a job, this is a passion! I got the message loud and clear, I love it and I am pumped to give this company even more.

You will see more blogs from me for a couple of reasons:

  1. It is practically demanded by our ScrumMaster and he has excellent reasons for it.
  2. It is a great way to share knowledge throughout our company.
  3. Believe it or not, it is actually encouraged.
  4. I want to because I have gained my passion again.

I still may blog my rants from time to time but mostly I will be blogging about software and technology. I know most of my friends will not care for this too much but this is truly my passion and what I can talk about. I promise to still throw in the funny rant or link. Pay attention and I will blog some little tips and tricks I have picked up they may just help the nontech type.

I am back to this blogs motto, "Never Underestimate Radical Vision", and now I have a chance to prove it.

Step down Weird Al…

June 15th, 2007

this guy has the comedy down, oh, wait.

really scary

June 15th, 2007

I don’t doubt this man’s conviction but I still find this a little scary. I do think that Kent should take note though.