I have no special talent. I'm only passionately curious - Albert Einstein
March 04, 2011
Grails SpringBeanListingController
Posted by Dave Malone
in Grails,
Spring,
Java
I developed this Grails controller so I can inspect the Spring configuration at any time. It has some basic wildcard support for searching for beans I suspect have been configured in the Spring application context but may not actually be there.
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware;
class SpringBeanListingController implements ApplicationContextAware{
ApplicationContext applicationContext
def index = {
for(String name : applicationContext.getBeanDefinitionNames()){
Object bean = applicationContext.getBean(name)
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}
}
def beanByName = {
String name = params.id
println "looking for bean with name $name"
if(name.endsWith('*')){
name = name.replaceAll("\\*", "");
println "performing wildcard search for beans with names beginning with $name"
for(String beanName : applicationContext.getBeanDefinitionNames()){
if(beanName.startsWith(name)){
Object bean = applicationContext.getBean(beanName)
println "bean name: $beanName"
println "bean type: " + bean.getClass().getName()
}
}
}else{
println "performing equals search"
Object bean = applicationContext.getBean(name)
if(bean){
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}else{
println "bean name $name is invalid"
}
}
}
def beansByType = {
String className = params.id
Class type = Class.forName(className)
Map beanMap = applicationContext.getBeansOfType(type)
for(Map.Entry beanMapping : beanMap.entrySet()){
String name = beanMapping.key
Object bean = beanMapping.value
println "bean name: $name"
println "bean type: " + bean.getClass().getName()
}
}
void setApplicationContext(ApplicationContext applicationContext){
this.applicationContext = applicationContext
}
}
October 15, 2010
Grails Logging Gotchas
Posted by Dave Malone
in Grails,
Spring,
Configuration,
Java,
Technology
One of the most frustrating experiences for me with Grails so far has been with what should be extremely simple; logging. There are countless blog entries on the topic, but few are thorough in covering multiple aspects of logging. The Grails documentation only seems to provide adequate documentation for one aspect - configuring the root logger, and appenders. From there, you're left with quite a few more questions. Since I've been moving full-steam ahead in my Grails development efforts, I figured I'd share some of my findings about logging.
Continue Reading
October 14, 2010
Redirecting on Login with Spring Security
Posted by Dave Malone
in Grails,
Spring,
Configuration,
Java
Well, here I am rewriting my blog software (again), this time in Grails. In the four years since I began writing my blog software, I've gone through three different versions of the Acegi Security Framework, which is now the Spring Security Framework. I'm now using the Spring Security Framework plugin for Grails, and I wanted to redirect users back to the page they were currently viewing after they successfully authenticate. A little searching on the web, and I found my solution.
Continue Reading
August 05, 2010
My take on running Java apps in the Cloud
Posted by Dave Malone
in Grails,
Java,
Technology
I recently began exploring the latest technology buzzword, the Cloud. As a Java developer, I was most interested in learning how to run Java applications purely in the Cloud, or integrating Java applications with Cloud based services. After some brief searching on Google, I began evaluating the Google App Engine (GAE), and the Amazon S3 (Simple Storage Services) and EC2 (Elastic Cloud Computing) products. I wanted to learn what it takes to run a Grails application in the cloud, and how much it would cost me to do so.
Continue Reading
August 04, 2010
Grails Integration Testing with the new Spring Security Core Plugin
Posted by Dave Malone
in Grails,
Java,
Technology
If you're not new to Grails, you'll understand that the system and it's plugins have been changing at a whirlwind pace. I had written a previous article on how to get the Acegi Security plugin for Grails to work correctly. Recently though, the Acegi Security plugin page advises that we use the Spring Security Core plugin instead. So, I started using that plugin, and I wanted to find a way to write integration tests on services that rely on an authenticated user being present in the Spring SecurityContextHolder. This article will outline two concepts; the first is adding createdBy and lastUpdatedBy user auditing to domain classes. The second concept is how to test these services in integration or unit tests with the dependancy on an "logged in" or authenticated user.
Continue Reading
May 05, 2010
Managing Application Configurations
Posted by Dave Malone
in Best Practices,
Spring,
Configuration,
Java,
Technology
Most applications require configurations specific to the environment they are being deployed to. Java applications are meant to be written once, deployed anywhere, so developers need a mechanism that allows them to deploy a single WAR or EAR file to any environment, instead of packaging a deployment for each specific environment. Using Spring, a simple naming convention for properties files, and a single environment variable (either on the server or from the command line) we can achieve this feat.
Continue Reading
April 30, 2010
Binding Indexed Properties in Grails
Posted by Dave Malone
in Grails,
Spring,
Configuration,
Java,
Technology
Here's an example of how to bind indexed properties to a Grails (or Spring) domain or command object without having to hand roll the parameter parsing and binding. The domain object graph is simple, and based on a project I'm currently working on, but this method can be applied to any sized object graph.
class LeagueMembersCommand implements Serializable{
String[] types
String[] firstNames
String[] lastNames
String[] emails
Integer[] handicaps
static constraints = {
types(nullable:false)
firstNames(nullable:false)
lastNames(nullable:false)
emails(nullable:false)
handicaps(nullable:false)
}
}
Continue Reading
April 29, 2010
Running a Jar file with Ant
Posted by Dave Malone
in Configuration,
Java,
Technology
I recently attempted to run a jar file that has multiple third party dependancies from an ant build script. I assumed that you could use a path element to define a path containing all of the external dependancies, and that you could use the built in java task in ant, pointing to my jar file, using the classpathref attribute to run the application. I quickly discovered that I was wrong, and there wasn't a very straightforward explanation on the web (that I could find) as to why.
Continue Reading
February 15, 2010
Acegi Security Plugin for Grails
Posted by Dave Malone
in Grails,
Configuration,
Java,
Technology
I've been attempting to develop an application using Grails 1.2.1, and the Acegi Security plugin for Grails v. 0.5.2 (also known as the Spring Security Plugin). Grails is still young, and so is it's group of plugins, so there are a lot of configurations that are flawed. This article outlines how to get up and running with Grails and the Acegi Security plugin v. 0.5.2, including how to get the JavaMail configurations working for use with Gmail
Continue Reading
February 10, 2010
Apache CXF on WAS 7.0
Posted by Dave Malone
in Bug,
Spring,
Configuration,
Java,
Technology
Apache CXF is an agile, open source web services framework that integrates out of the box with the Spring framework. This framework utilizes JAXB, and the new JAX-WS APIs, and does not require developers to use any proprietary APIs or annotations. CXF provides multiple transports - you can write a single service, and reuse it as a SOAP based web service, or a RESTful web service. The project, at the time of this writing, is very active, with new versions being released on a regular basis.
Continue Reading
February 02, 2010
Developing jsonp Services for jQuery
Posted by Dave Malone
in Javascript,
Java,
Technology
The documentation for successfully developing jsonp services for use with jQuery is not very straightforward. The examples consist of a number of ajax calls using jQuery's .ajax and .getJSON functions, but until recently did not contain any examples of performing remote calls to jsonp services. If you read the documentation for using the jsonp dataType with their ajax function, you will see a link to an article by Bob Ippolito outlining the first proposed jsonp format, and how to use it. The format documented on Bob's article is not valid when used with jQuery's functions (jQuery doesn't error out, it just doesn't do anything at all). Their most recent example of using the new Flickr jsonp API sheds light on the format that jQuery really expects for json returned by remote jsonp services.
Continue Reading