Archive for the ‘Java’ category

GroovyMag December 2008 is out

December 1st, 2008

The December 2008 GroovyMag is out now.  I could tell you all about the articles contained within – some meta-thoughts on GORM’s strategy, iUI iPhone development with Grails, Testing Plugin use, Rich Swing Apps with Groovy, and so on.  Or… you could just watch the video below before you go buy your copy.  :)


 

Switching Grails views

November 22nd, 2008

This is a repost from my post over at GroovyMag.

I’m planning to do this on a couple other projects, and thought I’d share this with you. It’s not specifically something related to GroovyMag magazine, but is a technique I’m using in a Grails project to make it easy to switch layouts based on the user.

All the default generated views use the meta tag ‘layout’ to point to a GSP file which will get merged with your view file (using sitemesh) to generate the final rendered HTML. Unfortunately these are always hardcoded to ‘main’ rather than a configurable option. So, I’ve taken to modifying the templates used during generation to point to a configuration value.

Run “grails install-templates” then look in src/templates/scaffolding. You’ll see the create/edit/list/show GSP files which are used as the basis for generation.

Edit the files such that the meta line

<meta name=”layout” content=”main” />

looks like this

<meta name=”layout” content=”\${session?.layout?:grailsApplication.config.sitemesh.layout}” />

(Note the Elvis :? operator above)

Then edit your grails-app/conf/Config.groovy file to have a line that reads

sitemesh.layout = “main”

If you then generate views for an app, everything will continue to work as it normally does by default; specifically, all the views will use the grails-app/views/layouts/main.gsp as a sitemesh wrapper for rendering.

However, you’re now able to set the layout to something different for each user by setting the session.layout value.

If a user has authenticated and is an administrator, perhaps you’d want to have

session.layout = “admin”

in a controller action, which would use the grails-app/views/layouts/admin.gsp file for rendering views for an admin.

If views for entire controllers require a different view, you should be able to set the global config value for all users by doing something like

grailsApplication.config.sitemesh.layout = “layout2″

in a controller action.

TIOBE and Groovy and Grails random thoughts

November 13th, 2008

So having recently launched a magazine on the Groovy language, I decided to check out the TIOBE index and see where Groovy sits.  Not terribly high (#41) but not too bad for a relatively new language.  Certainly higher than many others with much longer pedigrees.  The TIOBE index also has a couple other interesting notes:

  • The trend for the last year for dynamic languages (vs statically typed languages) has gone down slightly. 
  • Perl is at an all-time low

Not much to do with me really, I was just somewhat relieved to see Perl start slowing down.  Yes, it’s going to be around for a long time, and yes, it’s powerful and CPAN has functionality to do everything except take out my garbage.  It’s just a difficult language for me to be productive in.  Always has been, likely always will be.  So, to the extent that alternatives make headway against Perl, I’m happy.  :)

How does all this affect Groovy?  Groovy is a unique beast, being able to be statically or dynamically typed, and compiling down to Java.  Many fans are eager to say Groovy *is* Java, but can be more if you want it to be.  Groovy brings the power of Java to the ‘scripting world’, and brings the power of scripting to the Java world, yet even that nifty phrase feels like it’s shortchanging Groovy some.

G2One, a consultancy based around Groovy and Grails (founded by the project leads of both Groovy and Grails) was recently acquired by SpringSource.  SpringSource is a consultancy and product development company built around the Spring framework.  I have to admit, not coming from a Java background, I still don’t 100% grasp how Spring fits in to the Java ecosystem, but I’m learning fast.  The shorthand version I’m getting from people is that the Spring framework simplifies Java development.  Anything that simplifies Java development is good in my book, but it apparently didn’t go far enough, because Grails seems to simplify Spring development, by bringing a standardized approach to web development and building on top of the Spring framework where appropriate.

I suspect that the acquisition will see a tighter integration of more Spring framework functionality in to the Grails framework.  It’s too early for me to say whether that would necessarily be a good thing.  Frankly, as a primarily non-Java person looking at the web development world in Java, the no-nonsense Grails approach has been much more productive for me than anything else (and I’d looked and tried out multiple web approaches in Java before discovering Grails).

SpringSource has an application server which looks to be a bundle of a modified Tomcat system and the Spring framework.  I installed it, and there seems to be less server management functionality than even a base Tomcat gives you.  From a ‘bloat’ standpoint, perhaps that’s good, but it seems to me that there’s still work to do to address the needs of the small web hosting business.

Why address those needs?  Because that’s where many businesses start off.  Many of the ‘enterprises’ of tomorrow will start with modest shared hosting accounts today.  Right now, it’ a PITA to have a Java web app (and specifically a Grails app, but others as well) live at the root of a domain.  It’s very easy to have “http://domain.com/myWebApp/” but harder to have “http://domain.com/” point to a standard WAR file.  Tomcat lets you do this once per server instance by specifically naming your WAR file “ROOT.war”.  Totally ridiculous, imo, and we need a Java hosting system that can manage mappings between virtual hosts and application installations (if only WAR files).  

“But Java is for enterprise applications” many Java die-hards will be thinking.  That’s only because it’s ceded the low-end hosting space to PHP and others by default, not because of any long-term strategy.  I’ll reiterate – this is important to Java’s long term future in the web world (and what part of the software space isn’t web-related?).  Smaller companies (which often grow in to larger companies later) will make their decisions based on a number of factors, but hosting support is one.  Developer availability is another, and developer familiarity with hosting options plays a part in what those developers end up devoting time to learning.

Virtualized servers solve the problem to some extent, but it’s the virtual host mapping which makes LAMP-style servers attractive to many people.  Multiple domains, all with root-path goodness, on one server with multiple discrete directories hosting various code.  I realize I’m harping on this over and over, but it’s been shocking to me how poor the support for this sort of thing is.  No decent interfaces, no way to delegate this sort of management to anyone except a server admin who has to futz around with multiple XML files on a server.  This went out of fashion in the LAMP world about 10 years ago, and I believe played no small part in the fast adoption rate of LAMP applications as the dominant face of public web applications.

Have I missed something?  Is it so simple to do this that no one even bothers to write about it because everyone ‘knows’ how to do it?  Or is this another area of Java which people have quietly abandoned (like cross-platform desktop multimedia).  To the extent that Sun is renewing their efforts behind JavaFX, we may see some traction for desktop Java again.  I’d like to see them (or anyone) make the same commitment to low-end Java hosting.

SpringSource, with its Grails acquisition, may be in a good position to address the needs of this market, helping to nurture a next generation of Java developers getting their first start with Java on Groovy and Grails web applications.  I’ve no idea if this is in their plans, but it doesn’t seem out of the realm of possibility.  Or does it?  Am I barking up the wrong tree?  Or just barking?  ;)

 

 

SpringSource acquires G2One

November 11th, 2008

I wrote up a small synopsis over at the GroovyMag blog about the acquisition, then just got pinged that my own quote about it made it in to PC World.  I’m famous.  ;)

My initial reaction was that this might hurt Groovy and Grails development, but I don’t think that’ll happen.  At least unless Spring ends up going back on whatever commitments they may have made privately to Graeme and Guillaume from G2One.  G2One would have no reason to agree to be acquired unless they thought it would ultimately be a good thing for the communities they serve.  Additionally, this doesn’t strike me as one of those ‘cash out and retire’ sort of deals.  SpringSource is buying Graeme and Guillaume’s names, reputations and experience, if nothing else, and likely wouldn’t *let* them just walk away.  What would have been the point of an acquisition if your primary assets leave the next day?  :)

Ultimately, we’ll wait and see how this plays out, but I’m cautiously optimistic that this will help mainstream adoption of Groovy and Grails technology.

 

 

Regular expressions are like broccoli…

November 10th, 2008

This was the analogy I came up with when teaching a PHP class recently.  I needed to review the topic of regular expressions, but it’s one that neither I nor the students seemed to have much interest in.  The examples that I had were adequate, but explaining the examples was still unnatural.  The students didn’t want to spend much time on regex, but I pressed ahead.  I suggested that regular expressions were like the broccoli of programming – something that left a bad taste in your mouth, but was ‘good for you’.  Now, usually the broccoli I eat is drenched in ranch dressing or balsamic vinegar, so it doesn’t leave a bad taste in my mouth.  And I know I’m not joined by 100% of the population out there – there are people who *enjoy* eating broccoli (and enjoy the taste as well).  They are a minority, just like people who enjoy writing and using regular expressions are a minority in the programming community.

Given my premise that regular expressions are like broccoli, what comparisons would you draw to other programming concepts or functions?

Closures are like carbohydrates?  Or chocolate?

Classes are like ________?

Functions are like _______ ?

Unit testing is like _______?

What catchy similes can you help me come up with?  Are these similes?  Or metaphors?  I used to say that an analogy was to a metaphor as a simile was to a __________ (this was one of those comparison fill-in-the-blanks you get on standardized tests).  Can you fill in that blank for me?  :)

 

GroovyMag on sale for $3.49 – one day only

November 5th, 2008

To celebrate our US elections, GroovyMag is on sale for $3.49 today only (November 5, 2008).  President-elect Obama won 349 electoral votes, and in an effort to ‘spread the wealth’, GroovyMag has lowered the price to reflect the electoral vote count.  Visit http://www.groovymag.com/main.issues to purchase your copy today!

GroovyMag is coming soon…

October 29th, 2008

I’m getting ready to launch GroovyMag – the magazine for Groovy and Grails developers – next week.  This will initially be a PDF-only product, tentatively priced at $4.99 – but we may move to print quickly if the demand is there.  The first issue is a bit over 30 pages (we’re playing with layout right now, so it’s not final yet), and I’ve been fortunate to have a stellar set of contributors for the first issue.  Dave Klein, Andres Almiray, Shawn Hartsock and Robert Fischer are all known and respected in the community, and I’ve been honored to have them help launch this magazine with their articles.

Check out http://www.groovymag.com to sign up to be notified once the magazine goes on sale!

Stupid Groovy Tricks

August 27th, 2008

Someone recently asked how to get a random element from an array.  In PHP it’s pretty straightforward – array_rand($x) – but there’s not (AFAICT) a built-in way in Groovy to do this.  So I wrote a little something which should hopefully demonstrate the power of the metaClass stuff to boot.

ArrayList.metaClass.getRand = { number ->
 if(number==0) {
   return delegate[new Random().nextInt(delegate.size)]
 } else {
   def tempList = []
   def counter = 0
   while(counter>number) {
     tempList.add(delegate[new Random().nextInt(delegate.size)])
     counter++
   }
   return tempList
 }
}
def names = ['mike','matt','mark','lesley','jean','ron','jeff','martine']
println names.getRand()
println names.getRand(5)

The first call to getRand() on the names list will just return one name from the list.  The second will return 5 random entries (and some could theoretically be repeated).  I’ve added this getRand() method to the base ArrayList class at runtime, even though it’s likely declared ‘final’ in Java’s base libraries.  This is one of the core powers of Groovy, and is pretty slick, imo.

Grails scaffolding improvement

August 27th, 2008

I put together a small change to Grails’ scaffolding templates to help with one to many and many to many relationships.  Grails (well, GORM really) will manage these relationships under the hood, but you were always left to do the interface on your own.  This small set of file changes will give you better relationship management when you ‘generate all’ or ‘generate templates’ for you domains.

Here’s a small screenshot of what an editing screen looks like:

To use these, simply ‘grails install-templates’ in your project, replace the two files from the SVN repo (see link above) and generate away.  This only deals with editing right now, not the ‘create’ step.  That could be modified, I’m sure, I just didn’t do it (or did it but didn’t make it work right).  If you’d like to contribute to this project, let me know.

Groovy and Grails magazine – any interest?

August 5th, 2008

I’ve been cautiously exploring the idea of a Groovy and Grails magazine.  I’ve got some interest from some well-known community members about writing articles, and I’ve got some leads on advertisers.  The last missing piece of the equation is readers.  Are you interested in a Groovy and Grails monthly magazine?  I’m envisioning this as PDF to start with, with an eventual print component if the demand is there.

Why a monthly magazine?  Specifically in the tech industry, it seems we don’t quite have enough time to digest things before the next big thing is arriving, and print is “out of date” before it gets printed.  With breaking news stuff, I think that’s probably true.  But with programming, there’s always more to it than first meets the eye.

There’s a lot of content out there in blogs, but the quality and depth just aren’t always sufficient to ge the point across.  Books are great, but suffer greatly from the “out of date” issue I just mentioned.  I just got a new Grails book a few days ago and it’s still referencing Grails 0.6 (not as gospel, but it was obviously the ‘current’ version when some or much of the book was written).  In my mind, a well done magazine fills the void between blogs and books, balancing the depth and timeliness issues.  The php|architect magazine series is a good example of this, and the MySQL magazine quarterly is another one.  Both are chock full of good, useful content for people who need to dig deep in to issues but still stay abreast of current trends.

In any event, if you’re interested in learning more, contact me or post a comment here.  You can also register your interest (let me know how many of you would be interested in a regular magazine effort) by going to http://groovymag.com and entering your email address (no you won’t get spammed or sold to!)

This isn’t quite a ‘done deal’ yet, but I’d like to know what the community interest in such a project is at this stage as I do more preparation and planning.

Thanks!