Groovy Button Code and using javax.mail

User c1ce6b3d19

10-08-2012 19:16:51

I am trying to use a groovy script to send a simple email when a button is pressed.  


Executing the script places a fresh mail-1.4.5.jar at C:\Users\jonlee\.groovy\grapes\javax.mail\mail\jars, which contains the MimeMessage.class.  


Perhaps I haven't included the correct @Grab command or import statements?


Here is my groovy script


@Grab(group='javax.mail', module='mail', version='1.4.5')
import com.im.df.api.dml.*;
import com.im.df.api.support.SelectionDescription
import com.im.ijc.core.api.util.IJCCoreUtils

init = { widget ->
}

destroy = { widget ->
}

evaluate = { widget ->

def ant = new AntBuilder()
ant.mail(mailhost:'mymailserver.sample.com', mailport:25, subject:'Test message') {
from(address:'foo@localhost')
cc(address:'[email protected]')
to(address:'[email protected]')
message("Test mail sent")
}
}

on_change = { widget, button ->
}

This is my groovy output



: java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage starting from ModuleCL@6708f8e0[org.apache.tools.ant] with possible defining loaders null and declared parents []
at org.apache.tools.ant.taskdefs.email.EmailTask.execute(EmailTask.java:592)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at groovy.util.AntBuilder.performTask(AntBuilder.java:260)
at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:220)
at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:147)
at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:170)
at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at script1344625346681611304173$_run_closure3.doCall(script1344625346681611304173.groovy:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)
at groovy.lang.Closure.call(Closure.java:410)
at com.im.ijc.widgets.button.ParsedButtonScript$1.run(ParsedButtonScript.java:157)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:162)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage starting from ModuleCL@6708f8e0[org.apache.tools.ant] with possible defining loaders null and declared parents []
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:264)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.tools.ant.taskdefs.email.EmailTask.execute(EmailTask.java:452)
... 41 more
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:262)
... 45 more



Thanks,


Jon

ChemAxon 37bb34e128

13-08-2012 12:30:54

Hi Jon,


as far as I can tell your script looks ok. I'll try to do the same on my machine and will get back to you soon, sorry.


Cheers,


-vita

ChemAxon 37bb34e128

15-08-2012 15:02:50

Hi Jon,


it looks like you've hit the tricky one. I can reproduce the issue and it's a classloader problem. Long story short - @Grape annotation does not work with module classloaders in IJC. I am sorry to say that, but I can't think of any workaround or simple fix. I filed IJC-4771 to cover this, but as I said the fix is unknown at the moment.


Thanks for your understanding,


-vita

User c1ce6b3d19

05-09-2012 17:31:41

Vita,


Yes, this will block our ability to use our own jars and third party jars.  


Can we place jars somewhere else?  


Can we have the button groovy scripts to call other groovy scripts that might be able to use @grape?


 


Perhaps we can call out to web services (using accessible jars) that can call our own code on the IJC server, for example.


 


Thanks for your help,


Jon

ChemAxon 37bb34e128

06-09-2012 07:50:26

Hi Jon,


re. using own/3rd party jars - This depends on the library you want to use and I believe that @Grape will work for most libraries even inside IJC. It generally works fine for self-contained libraries, eg the libraries that don't plug into some other library/framework, which then resolves the @Graped library classes by name. Ant extensions are typical example of libraries that will not work, because they plug into the Ant framework, which is bundled in IJC and loaded by IJC classloader. But I would not give up on @Grape just because it does not work for Ant extensions. It may work perfectly well for other libraries including your own libraries.


re. placing jars somewhere else - Well there is no such place really that would work for all libraries. We could try to find a place for Ant extensions, but that would likely require to place jars somewhere in the IJC installation directory. I am not sure how feasible this is for you. It may be ok if you maintain a shared IJC installation, but would become a nightmare if you let your users to install IJC locally on their PCs.


re. using webservices - Sure, there should be no problem with this approach. You can call a webservice which then will send an email for example.


 


Cheers,
-vita