Ran into an interesting situation in our production servers today.  We’ve been sending out e-mail asynchronously locally without any issues.  As soon as we put the code into production we started to see the following errors:


org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest
at BackgroundThreadManager$1.run(BackgroundThreadManager.java:90)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest
at grails.util.GrailsWebUtil.bindMockWebRequest(GrailsWebUtil.java:58)
at grails.util.GrailsWebUtil$bindMockWebRequest.call(Unknown Source)
at org.grails.mail.MailMessageBuilder.renderMailView(MailMessageBuilder.groovy:206)
at org.grails.mail.MailMessageBuilder.body(MailMessageBuilder.groovy:137)

From past experience with Spring I knew the spring-test jar file contains the Mock classes. I didn’t realize until digging into the mail plugin that it was used to help render views. The code used in the plugin is as follows. It appears to use the MockHttpServletRequest where there isn’t a real HttpServletRequest (like in a background thread or scheduled with Quartz). The MockHttpServletRequest allows the mail plugin to still find you view templates and use them outside the scope of a real request, that’s pretty handy.


if(!requestAttributes) {
def servletContext = ServletContextHolder.getServletContext()
def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
requestAttributes = GrailsWebUtil.bindMockWebRequest(applicationContext)
unbindRequest = true
}

After scratching my head for a while I simply copied the jar file in $GRAILS_HOME/lib/org.springframework.test-3.0.0.RELEASE.jar into my local application directory and all was well again. I also stumbled on this JIRA issue referencing the same problem, and another solution.

http://jira.codehaus.org/browse/GRAILSPLUGINS-1885

I stumbled upon this post http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/.  It’s an excellent read and gives some insight into some of the inner workings of both Grails and Hibernate.

I’ve run into this error a few times over the past few months.  It just came up again today from a colleague of mine and I thought it’d help to write a blurb about it.

EasyMock supports interfaces.  It’s a not so perfect world so we don’t always have the opportunity to just mock interfaces.  Fortunately there’s an extension to EasyMock that supports mocking classes.  See the easy mock documentation here: http://easymock.org/Downloads.html

java.lang.IllegalArgumentException: not a proxy instance
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:637)
at org.easymock.EasyMock.getControl(EasyMock.java:1600)
at org.easymock.EasyMock.replay(EasyMock.java:1502)
at com.x.y.z.MyCacheTest.testInvalidNumProcesses (…..)

Basically all you need to do is make sure you’re importing the right EasyMock packages.  So instead of

import static org.easymock.EasyMock.createMock;

replace the above with:

import static org.easymock.classextension.EasyMock.createMock;

and any other imports that reference easymock.EasyMock such as:

import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.reset;
import static org.easymock.classextension.EasyMock.verify;

I ran into this issue a few times and finally decided it’s time to document it.  When installing VMWare Tools on an Ubuntu VM you need to do the following before running the vmware tools installer:


# apt-get install build-essential linux-headers-`uname -r`

After you run the installer and get to the point where you have to tell the tool where your  C header files are
What is the location of the directory of C header files that match your running
kernel? [/usr/src/linux/include]

C Header Shell Snapshot

Enter the following:
/lib/modules/2.6.28-11-server/build/include

There’s a known issue with the latest release of Apache Sling.  The Felix webconsole bundle (version 1.2.8) is broken and throws a filter error when clicking on the “Configuration” tab under /system/console.

To get around this you can either:

1. Visit the Apache Felix site and get version 1.2.10+

2. Type in the exact Component to edit.  In my case I was trying to disable Anonymous access via the SlingAuthenticator while trying to get the 46 line blog example going.

http://localhost:8080/system/console/configMgr/org.apache.sling.engine.impl.auth.SlingAuthenticator

Found this useful:

http://bhandler.spaces.live.com/blog/cns!70F64BC910C9F7F3!1231.entry?wa=wsignin1.0

Today I was trying to adapt some legacy code into a new web application using Spring 2.5.  It took me a while to figure out the issue and I finally found the answer.  As the name implies, a static factory has 1 goal, to return an object (hence the term factory).  Our legacy code was using a static initializer to create a connection to a JavaSpace.  The code looked like this:


public static final void init ( String spaceUrl )
{
... do init stuff here...
}

Then I got the Spring error.  I was going down the wrong path b/c Spring actually called the static initializer and created the space connection w/o exception.  It was only after that Spring puked with this:


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'space' defined in class path resource [socialservicesContext.xml]: Initialization of bean failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:413)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:735)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at com.fm.util.AdHelper.main(AdHelper.java:55)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:978)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 13 more

The issue was that Spring expects an object returned from the factory-method and we weren’t doing it.

Hope that helps someone.

If you’re trying to set up struts2 for the first time and want to use Freemarker as the default result type, simply do the following in struts.xml

<struts>
<package name=”default” extends=”struts-default”>

<!–  Defines various result types. Notice that “freemarker” is the defualt –>
<result-types>
<result-type name=”freemarker” class=”org.apache.struts2.views.freemarker.FreemarkerResult” default=”true”/>
….
</result-types>
…interceptors
… actions

Next Page »