groovygroovlet

Duplicate class error with Groovlets


I am trying to work with a Catch-all groovy script for my groovlets. This is what I did

public class GroovletServletCatchAll extends GroovyServlet { 
    public URLConnection getResourceConnection(String name) throws ResourceException { 
        return super.getResourceConnection("CatchAll.groovy"); 
    } 
}

Now, with any code in CatchAll.groovy file, I get the error

jndi:/localhost/web_app/CatchAll.groovy: 1: 
Invalid duplicate class definition of class CatchAll. 
One of the classes is an explicit generated class using the class statement, 
the other is a class generated from the script body based on the file name. 

Why is the first class getting generated? I do not have any other class in my code. Only the Catch-all script and the extended servlet.


This was the stripped code for which I got the above mentioned error

  println """
  Hello, ${request.remoteHost}: ${new Date()}
  """

Oddly enough, if I remove the new Date() clause above, the first error goes away, and I get an stackOverflow in the CatchAll constructor (keeps on calling itself).

This was what I saw repeating(among other trace) in the logs

    at groovy.lang.GroovyObjectSupport.<init>(GroovyObjectSupport.java:32)
    at groovy.lang.Script.<init>(Script.java:40)
    at groovy.lang.Script.<init>(Script.java:37)
    at CatchAll.<init>(CatchAll.groovy)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

I have posted the same question here as well


Solution

  • I could not go deep enough to resolve the strange behavior, but there was an easier solution. I added this to direct all the requests to a single script.

    <init-param>
        <param-name>resource.name.replacement</param-name>
        <param-value>CatchAll.groovy</param-value>
    </init-param>
    
    <init-param>
        <param-name>resource.name.regex</param-name>
        <param-value>/.*</param-value>
    </init-param>