[groovy-user] Problem with Closures in Equinox/OSGI

7 views
Skip to first unread message

klaus777

unread,
Feb 25, 2011, 12:51:18 AM2/25/11
to us...@groovy.codehaus.org

Hi,

I have the following simple use of closure:

cache=["x":["op":"register", "userid":"milima11", "date":1298610760140]]
event=["op":"search", "userid":"milima11"]
println cache.values().each ({it.remove('op') }).findAll
({it.userid!=event.userid })

When running this in Eclipse, there doesn't seem to be a problem. But
running from Equinox (OSGI Container) using JSR223, it works a couple of
times then starts producing the following exception.

It seems closures are the problem here. When I remove them, everything works
fine.
I need your help, please.

at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeImpl(GroovyScriptEngineImpl.java:368)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeFunction(GroovyScriptEngineImpl.java:158)
at
com.mixmet.scripting.ScriptActivator$ScriptActivatorTask$1$1.run(ScriptActivator.java:222)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException:
java.lang.NoClassDefFoundError: sun/reflect/MethodAccessorImpl
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:923)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:282)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovyScriptEngineImpl.java:380)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovyScriptEngineImpl.java:374)
at
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.invokeImpl(GroovyScriptEngineImpl.java:363)
... 5 more
Caused by: java.lang.NoClassDefFoundError: sun/reflect/MethodAccessorImpl
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
at
sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
at java.security.AccessController.doPrivileged(Native Method)
at
sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
at
sun.reflect.MethodAccessorGenerator.generateMethod(MethodAccessorGenerator.java:59)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:28)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:282)
at groovy.lang.Closure.call(Closure.java:295)
at
org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1220)
at
org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1196)
at org.codehaus.groovy.runtime.dgm$110.invoke(Unknown Source)
at
org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:270)
at
org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
at Script1.search(Script1.groovy:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
at
groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:227)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:77)
at Script1.processEvent(Script1.groovy:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
... 13 more
Caused by: java.lang.ClassNotFoundException: sun.reflect.MethodAccessorImpl
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:696)
at
groovy.lang.GroovyClassLoader$InnerLoader.loadClass(GroovyClassLoader.java:449)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:793)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)


--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3399642.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


jetzgradnet

unread,
Feb 25, 2011, 6:08:47 AM2/25/11
to us...@groovy.codehaus.org

Hi Klaus,

could you please provide some more information:
How does your OSGi installation look like? Basic Equinox + some bundles or
Eclipse app?
How do you call this groovy code?
Do you put the groovy script in a bundle?
Do you have a Groovy bundle, which re-evaulates this piece of code
periodically?
Do you create bundles on the fly and install them?
Is the script cached?
How often does it work and when does it start to disbehave?

Regards,

Wolfgang
--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3399946.html

klaus777

unread,
Feb 25, 2011, 10:35:38 AM2/25/11
to us...@groovy.codehaus.org

Hi Wolfgang,

Your questions are very pointed. In fact, I'm trying to build a small
language-independent framework where people can just drop their script in a
folder and they get executed by the OSGI container (in this case, Felix).

To achieve this, I have a meta bundle that watch some folders (just as the
FileInstall bundle does). When a new zip file is detected, it picks it up,
add the necessary OSGI artifacts (jars, classes, manifest, etc.) and deploy
it as a bundle that gets pick up by FileInstall, deployed and activated.

Things seem to work pretty good except this closure issue. What might be of
interest is that I have one single instance of the Groovy script engine.
That is,a script C is deployed and invoked by some other bundles D and E,
the very same instance of C will be used to serve both D and E. Could this
be relevant to this exception?

On a side note, could this result into some race condition? How thread safe
are these scripting engines in general?

Thanks,
Klaus.
--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3400292.html

Wolfgang Schell

unread,
Feb 25, 2011, 11:49:03 AM2/25/11
to us...@groovy.codehaus.org

Hm, this sounds strange (i.e. that it works for a time and then stops
working).

Could you please show the contents (Manifest and contents as file list) of a
created bundle?
unzip -l my-groovy-bundle.jar
unzip -p my-groovy-bundle.jar META-INF/MANIFEST.MF

I have a handy script (see [1]), which makes this more readable: use like
this:
jinspect my-groovy-bundle.jar
jinspect -m my-groovy-bundle.jar

Also interessting would be the output of the "ss" command from the Equinox
console (start with -console).

How do you call the script from the client bundle? Do you register the
GroovyScriptEngine as OSGi service? When the invocation of the script now
longer works, did you install another script inbetween?

Does your meta-bundle wrap grovy-all.jar or do you have it installed as
regular OSGi bundle?

If the code is not classified, would you mind putting it somewhere, so I can
have a look?

Regards,

Wolfgang

[1]
http://blog.jetztgrad.net/2010/01/script-to-inspect-jar-and-war-files-grails-applications-and-plugins/

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3400420.html

Wolfgang Schell

unread,
Mar 9, 2011, 8:12:44 AM3/9/11
to us...@groovy.codehaus.org
Hi Klaus,

(how) did you solve your problem?

Regards,

Wolfgang

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3415473.html

klaus777

unread,
Mar 9, 2011, 2:29:45 PM3/9/11
to us...@groovy.codehaus.org
Hi Wolfgang,

I haven't solved the problem yet. I'm looking at a way to isolate the code
that I can send to you but it is not obvious. For now I'm compiling the
script each time before executing it. This is clearly not a production ready
solution but it allows me to move forward.

If you wish I can give you access to SVN so you can checkout the unchanged
code.

Klaus.

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3416125.html

Wolfgang Schell

unread,
Mar 9, 2011, 2:50:51 PM3/9/11
to us...@groovy.codehaus.org
BTW, which Groovy version do you use? I think there were some fixes in 1.7.8,
which are related to caching of CallSites, so maybe you could try different
Groovy versions (latest 1.6.x, 1.7.x, 1.8-RC1).

Wolfgang

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p3416150.html

henrik

unread,
Mar 29, 2011, 11:27:13 AM3/29/11
to us...@groovy.codehaus.org
We have the same problem. Here's the stacktrace:

16:41:41,516 ERROR [BaseCategory] Exception when calling 'sample'


org.codehaus.groovy.runtime.InvokerInvocationException:
java.lang.NoClassDefFoundError: sun/reflect/MethodAccessorImpl
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at

org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:282)
at

com.eviware.loadui.groovy.GroovyContextProxy.invokeMethod(GroovyContextProxy.java:232)
at
org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:799)
at
org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:776)
at
com.eviware.loadui.groovy.categories.GroovyRunner.sample(GroovyRunner.java:42)
at
com.eviware.loadui.impl.component.categories.RunnerBase.doSample(RunnerBase.java:478)
at
com.eviware.loadui.impl.component.categories.RunnerBase.access$700(RunnerBase.java:68)
at
com.eviware.loadui.impl.component.categories.RunnerBase$Worker.run(RunnerBase.java:754)
at
com.eviware.loadui.util.dispatch.CustomThreadPoolExecutor$Worker.run(CustomThreadPoolExecutor.java:188)
at java.lang.Thread.run(Thread.java:662)


Caused by: java.lang.NoClassDefFoundError: sun/reflect/MethodAccessorImpl
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
at
sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
at java.security.AccessController.doPrivileged(Native Method)
at
sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
at
sun.reflect.MethodAccessorGenerator.generateMethod(MethodAccessorGenerator.java:59)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:28)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at

groovy.lang.MetaClassImpl$GetBeanMethodMetaProperty.getProperty(MetaClassImpl.java:3468)
at
org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.ja
va:61)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:235)
at
GroovyWebPageRunner$_run_closure12.doCall(GroovyWebPageRunner:241)


at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
... 13 more
Caused by: java.lang.ClassNotFoundException: sun.reflect.MethodAccessorImpl

at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)


at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at
groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:696)
at

groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:793)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 33 more

We use Felix/OSGi and I have noticed that this error occurs in Groovy 1.7.7
and later, but not in 1.7.6 and earlier.

* The Groovy scripts are read from the file system by a separate bundle.
* We use Java code to periodically reload the Groovy code (if the file has
been changed).
* We do not create bundles on the fly.
* Like for Klaus, it works a few times (roughly 10) but then fails. This
happens without any reloading of the Groovy script.

The Groovy code is called at GroovyRunner.java:42, which looks like this:

return ( TerminalMessage )InvokerHelper.invokeMethod( delegate, "sample",
new Object[] { triggerMessage, sampleId } );


Regards
/Henrik

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p4269570.html

henrik

unread,
Apr 4, 2011, 7:36:30 AM4/4/11
to us...@groovy.codehaus.org
We've solved this.

The problem is because sun.reflect isn't exposed in all OSGi bundles. The
problem is described in detail here:
http://www.mail-archive.com/us...@felix.apache.org/msg07721.html

The fix is to set this framework property in the Felix configuration file:

org.osgi.framework.bootdelegation=sun.reflect


Still not sure why Groovy 1.7.7+ provoked this while 1.7.6 didn't though...

Regards

/Henrik

--
View this message in context: http://groovy.329449.n5.nabble.com/Problem-with-Closures-in-Equinox-OSGI-tp3399642p4281578.html

Reply all
Reply to author
Forward
0 new messages