users@glassfish.java.net

Re: Issues with JavaMail in OSGi Bundle

From: Robert Weeks <RWeeks_at_ext-inc.com>
Date: Tue, 17 Aug 2010 11:33:42 -0700

Hi Sahoo -

I am sorry - I never saw this reply come in!

I will try this out and see if that helps with our issue and report back. Thanks for your reply.



Is the description given below correct?

You have an OSGi bundle which is making use of JavaMail APIs. When your bundle calls certain JavaMail API, JavaMail is using javax.activation, which in turn is failing to locate META-INF/mailcap resources bundled as part of JavaMail bundle resulting in UnsupportedDataTypeException.

If this is correct, can I request you to try setting the context class loader temporarily to JavaMail bundle's class loader before calling JavaMail API and resetting back to what it used to be after the call. e.g., something like this:

void foo() {
   ClassLoader tcl = Thread.currentThread().getContextClassLoader();
   try {
     Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
     // now call JavaMail API
     ...
   } finally {
     Thread.currentThread().setContextClassLoader(tcl);
   }
}

Your bundle should import javax.mail package.

On Saturday 07 August 2010 07:15 PM, glassfish_at_javadesktop.org<mailto:glassfish_at_javadesktop.org> wrote:

Hello again -

I am still having major problems getting this to work in an OSGi bundle for me (via GF 3.0.1).
I have exhausted quite a few roads on this and am hitting a brick wall.

I have checked the mail.jar bundle that comes with gf, and all seems to be OK in there. I have updated my bnd files to match what is exported there as well (even directly importing package com.sun.mail.handlers;uses:="javax.activation,javax.mail.internet,javax.mail";version="1.4.3" to see if that would help).

I know the handlers are available - I even created one and tested it's dataflavor against the message that was created:

DataContentHandler dhmm = new com.sun.mail.handlers.multipart_mixed();
DataFlavor[] dtf = dhmm.getTransferDataFlavors();
for (DataFlavor tmpdf : dtf) {
        log.debug(" isSupported? " + tmpdf.getMimeType() + ":" + message.getDataHandler().isDataFlavorSupported(tmpdf));
}

And it shows it is supported: isSupported? multipart/mixed:true

I have also tried getting the DataHandler from the message that was created, created a new command map that mimics mailcap and setCommandMap on the DataHandler with it, and still no avail - I always get:

Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed;

Has anyone else using Javamail inside a bundle not seen this issue as well? I have seen how some people combine the jars, make sure they jars are in the same place, but it seems with the bundle included in GF - the jars for JAF and Javamail are already combined - so that wouldn't be a solution here.

I was even thinking of just embedding the jar with my bundle itself, but that didn't seem to help much either (using the Bundle-Classpath to point to the embedded jar).

Any help is appreciated.

Thanks!