2007-06-20

Groovy and Grails: Introduction



Groovy and Grails very much excite me.

Groovy is an alternative programming language for the Java Virtual Machine (JVM). Java Specification Request 241 (JSR 241) states that "Groovy is an agile, dynamic programming language for the Java Virtual Machine". This alternative language for the JVM has been around for a few years; see the blog of James Strachan. A programmer can put into a few lines of Groovy code the same task that would take several more lines of Java.

According to the Grails official web site, Grails is "an open-source web application framework that leverages the Groovy language and complements Java Web development"; in other words, a programmer can use Grails to build web applications in a simple, organised manner using the Groovy and Java languages. The idea behind Grails is to build upon some of the desirable functionalities available in the web application framework Rails, but with functionality more familiar to Java developers. The official Grails web site does a better job of fully explaining Grails than what I care to do here.

So, I'd like to share my journey exploring Groovy and Grails....

Quick note: I recently purchased a new laptop (or "notebook")--a Lenovo Z61m running Microsoft Windows Vista Home Premium operating system. Some software do not install or run properly on Vista, but this should be a temporary state of affairs with most major Windows software manufacturers. Thankfully, Java appears to run well on Vista; so, once again, Java has proven itself a reliable platform.

To get started, I wanted to use a good IDE for Java development, so I downloaded my favourite Java IDE: JDeveloper (aka "JDev"). Right now, I have Studio Edition Version 10.1.3.2.0.4066. When I downloaded JDev, I selected the Base Install (does not include JDK) rather than the Full Install (includes JDK). I'd also downloaded and installed the latest JDK (JDK 6u1) from Sun. I decided to do this so as to have JDev running with the latest JDK.

Next, I downloaded a free Groovy plug-in/add-on/extension for JDev through the Help-->Check for Updates dialog in JDev (this ability to easily download plug-ins for JDev is fantastic).

Then, I downloaded Grails from the official Grails web site. At first, I thought that since the Groovy plug-in for JDev included the necessary Groovy library, that I would not need to download Groovy for Grails to run; i.e., I thought that I'd be able to tell Grails to use the Groovy library in the JDev extensions directory. Apparently, this did not work; so I downloaded Groovy from the official Groovy web site. After installing both Grails and Groovy, and setting Windows environment variables that include JAVA_HOME, GRAILS_HOME, and GROOVY_HOME, as well as updating the Path variable to include references to bin directories of Groovy and Grails, I was able to create a new Grails project.

Before attempting to work with Grails, it helps to test if it exists by typing the following request for Grails Help at the command line:

grails /?

Next, at the command line, try to create a new Grails application using the create-app command:

grails create-app my_grails_example

You should see output from the command line that looks something like the following:



The resulting directory structure will appear like this:
C:\Users\mikequentel\my_grails_example
├───grails-app
│   ├───conf
│   ├───controllers
│   ├───domain
│   ├───i18n
│   ├───services
│   ├───taglib
│   ├───utils
│   └───views
│       └───layouts
├───hibernate
├───lib
├───plugins
│   └───core
│       └───grails-app
│           ├───taglib
│           └───utils
├───scripts
├───spring
├───src
│   ├───groovy
│   ├───java
│   └───test
├───test
│   ├───integration
│   └───unit
└───web-app
  ├───css
  │   └───tree
  │       ├───check
  │       ├───default
  │       ├───folders
  │       └───menu
  ├───images
  │   └───tree
  │       ├───check
  │       ├───default
  │       ├───folders
  │       └───menu
  ├───js
  │   ├───prototype
  │   └───yahoo
  │       └───assets
  ├───META-INF
  └───WEB-INF
      ├───classes
      ├───grails-app
      │   └───i18n
      ├───lib
      ├───spring
      └───tld

Because Grails includes a Jetty container, you can immediately test the web application (though you will probably want to configure Grails to run on another Servlet Container or J2EE platform). Navigate to the base directory of your Grails application (in this example, navigate to my_grails_example). Then, use the run-app command:

grails run-app my_grails_example

You should see something like this at the command line:



Once this runs, you can then launch a browser window and enter the URL provided in the end of the output at the command line (in this example, http://localhost:8080/my_grails_example). You should see something like this:



Because this is just a skeleton of a Grails application, there is no list of controllers as might be suggested in the welcome message.

After I create an example with some more functionality, I shall show you how Groovy and Grails can provide a powerful tool set for developing web applications.

2007-05-28

Singleton in Java



Here is an example of the Singleton creational pattern in Java:


/*
Copyright (c) 2007 Mike Quentel

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/



public class SingletonExample  {

 /**
  * The Singleton contains a private static data member
  *   representing itself.
  */
 private static SingletonExample singleton;

 /**
  * Private constructor.
  */
 private SingletonExample() { }

 /**
  * The getSelf() accessor lazily instantiates
  *   the Singleton.
  *   The synchronized qualifier is
  *   used to prevent multiple threads
  *   from creating multiple instances.
  */
 public static synchronized SingletonExample getSelf() {
  if (singleton == null) {
    singleton = new SingletonExample();
  } //if (singleton == null)...
  return singleton;
 } //public static synchronized...

 /**
  * Clone() overridden to prevent cloning.
  */
 public Object clone() throws CloneNotSupportedException {
   throw new CloneNotSupportedException();
 } //public Object clone...

} //public class SingletonExample...

2007-05-27

Dapple



Recently, I downloaded a 3D map viewer, called "Dapple", built on top of NASA World Wind .NET version. Geosoft, Inc., took the .NET World Wind and extended it, as well as modified it in general, so as to appeal more to scientific users.
I love that Dapple has a clean user interface combined with an easy ability to add WMS layers. Also, one can use a "Dap Server", which Geosoft offer as an alternative means of storing and sharing geographic data.

Dapple is licensed under the very permissive and developer-friendly software licence known as MIT License. This gives one flexibility and freedom quite similar to that of BSD License.

In the above screen shot, you can see the TerraServer USGS Topo DRG WMS overlayed on the default NASA World Wind globe base layer. With the relief of the base layer, the topos take on a more interesting and meaningful appearance than flat 2D maps. If you might be wondering why the user interface has a tan-coloured appearance in some of the windows: that's just my selected Windows background colour. I like using this tan colour (RGB: 255,204,153 ; HEX: #FFCC99) for most of my applications (especially the background of a code editor).

I also like very much the navigation overview map in the lower left corner of the user interface. This provides a very easy navigation tool, especially if one uses Dapple on a laptop/notebook computer lacking a standard mouse.

Geosoft have started an excellent enhancement to World Wind, and I look forward to future functionality in this.

2007-05-26

Initialisation



This commences a new blog of software, electronics, and GIS-related ideas, thoughts, and opinions. There will exist here a mix of all the above technologies, and more. But in case you might wonder, I have developed GIS-related software for many years. During that time, I gained insights not only into geospatial application development, but many forms of software development in general.

The name "Mike Quentel Software" is just a blog title; not a company or organisation name.

I have other blogs and websites:
Mike Quentel -- main website and centre for all blogs and resources about Mike Quentel.
Meditations -- collection of observations and ideas of Mike Quentel concerning the world in general.
My Space
Facebook