I have no special talent. I'm only passionately curious - Albert Einstein
Grails SpringBeanListingController Comment on Grails SpringBeanListingController 0

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
	}
}


Grails Logging Gotchas Comment on Grails Logging Gotchas 0

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

Redirecting on Login with Spring Security Comment on Redirecting on Login with Spring Security 5

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

My take on running Java apps in the Cloud Comment on My take on running Java apps in the Cloud 0

  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

Grails Integration Testing with the new Spring Security Core Plugin Comment on Grails Integration Testing with the new Spring Security Core Plugin 0

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

Managing Application Configurations Comment on Managing Application Configurations 0

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

Binding Indexed Properties in Grails Comment on Binding Indexed Properties in Grails 0

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

Running a Jar file with Ant Comment on Running a Jar file with Ant 0

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

Acegi Security Plugin for Grails Comment on Acegi Security Plugin for Grails 1

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

Apache CXF on WAS 7.0 Comment on Apache CXF on WAS 7.0 6

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

Developing jsonp Services for jQuery Comment on Developing jsonp Services for jQuery 0

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

About

David Malone is a Java developer residing in the Twin Cities area.  He has been developing enterprise applications since 2004.  This is his personal blog, as well as his design and development workspace.