I merged activation with mail into one bundle to fix our situation. My concern with trading around classloaders is that the call to build the CommandMap (what loads the mailcap files) is static (CommandMap.getDefaultCommandMap()) and holds the reference, so if another bundle is able to make the call before you and doesn't do your classloader swapping, you'll end up without the mailcap entries from javamail.
----- "Paul Mietz Egli" <[email protected]> wrote: > On Oct 27, 2009, at 7:52 AM, [email protected] wrote: > > > To clarify our situation a bit, the activation bundle finds its own > > > META-INF/mailcap.defaults but is unable to see META-INF/mailcap in > > > the javamail bundle because of the separation of classloaders. > > Given this needed relationship, I believe we've run out of options > > > except to put activation and javamail in the same bundle. > > I ran into the exact same problem with mail and activation. The only > > way I was able to work around it was to swap classloaders inside the > > method that makes the javamail calls. Here are the relevant parts of > > the code: > > /* > * this is the interface exported by the bundle > */ > public interface MyMailSender { > public void sendMail(String text); > } > > /** > * @scr.component metatype="true" enabled="true" immediate="true" > * @scr.service interface="MyMailSender" > */ > public class MyMailSenderImpl { > private ClassLoader localClassLoader; > private Session mailSession; > > protected void activate(ComponentContext context) { > Properties properties = new Properties(); > // set javamail properties > session = Session.getInstance(properties); > localClassLoader = session.getClass().getClassLoader() > } > > public void sendMail(String text) { > ClassLoader previousClassLoader = Thread.currentThread > ().getContextClassLoader(); > try { > /* > * Switch to the classloader that loaded up the JavaMail > > Session > * object. This classloader will have access to the > resources > * required to dynamically load the provider and > transport. > */ > Thread.currentThread().setContextClassLoader > (localClassLoader); > > // now you can send your mail message > MimeMessage msg = new MimeMessage(session); > msg.setText(text); > Transport transport = session.getTransport("smtp"); > transport.connect(); > transport.sendMessage(msg, new InternetAddress[] { new > InternetAddress("[email protected]") }); > transport.close(); > } > // catch all the various javamail exceptions here > finally { > Thread.currentThread().setContextClassLoader > (prevClassloader); > } > } > } > > Hope this helps! > > p.
