camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject camel git commit: CAMEL-8339 Use the application context classload to create the mail session if it is possible with thanks to Thomas
Date Thu, 12 Feb 2015 13:49:18 GMT
Repository: camel
Updated Branches:
  refs/heads/master cf99c40ce -> 689271b48


CAMEL-8339 Use the application context classload to create the mail session if it is possible
with thanks to Thomas


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/689271b4
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/689271b4
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/689271b4

Branch: refs/heads/master
Commit: 689271b4826330d35518e7ee34ec8e6ad63d4175
Parents: cf99c40
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Thu Feb 12 21:48:45 2015 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu Feb 12 21:48:45 2015 +0800

----------------------------------------------------------------------
 .../camel/component/mail/MailComponent.java     | 10 +++----
 .../camel/component/mail/MailConfiguration.java | 31 +++++++++++++++-----
 .../camel/component/mail/MailProducer.java      |  9 ++++++
 3 files changed, 37 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/689271b4/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
index 38b8f9f..9154818 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailComponent.java
@@ -32,7 +32,7 @@ import org.apache.camel.util.ObjectHelper;
 /**
  * Component for JavaMail.
  *
- * @version 
+ * @version
  */
 public class MailComponent extends UriEndpointComponent {
     private MailConfiguration configuration;
@@ -40,7 +40,6 @@ public class MailComponent extends UriEndpointComponent {
 
     public MailComponent() {
         super(MailEndpoint.class);
-        this.configuration = new MailConfiguration();
     }
 
     public MailComponent(MailConfiguration configuration) {
@@ -50,7 +49,6 @@ public class MailComponent extends UriEndpointComponent {
 
     public MailComponent(CamelContext context) {
         super(context, MailEndpoint.class);
-        this.configuration = new MailConfiguration();
     }
 
     @Override
@@ -58,8 +56,7 @@ public class MailComponent extends UriEndpointComponent {
         URI url = new URI(uri);
 
         // must use copy as each endpoint can have different options
-        ObjectHelper.notNull(configuration, "configuration");
-        MailConfiguration config = configuration.copy();
+        MailConfiguration config = getConfiguration().copy();
 
         // only configure if we have a url with a known protocol
         config.configure(url);
@@ -102,6 +99,9 @@ public class MailComponent extends UriEndpointComponent {
     }
 
     public MailConfiguration getConfiguration() {
+        if (configuration == null) {
+            configuration = new MailConfiguration(getCamelContext());
+        }
         return configuration;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/689271b4/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
index 7511e72..917f9c2 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
@@ -20,10 +20,12 @@ import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
+
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.net.ssl.SSLContext;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
@@ -33,10 +35,10 @@ import org.apache.camel.util.jsse.SSLContextParameters;
 /**
  * Represents the configuration data for communicating over email
  *
- * @version 
+ * @version
  */
 @UriParams
-public class MailConfiguration implements Cloneable {   
+public class MailConfiguration implements Cloneable {
 
     private JavaMailSender javaMailSender;
     private Properties javaMailProperties;
@@ -95,9 +97,14 @@ public class MailConfiguration implements Cloneable {
     private boolean peek = true;
     @UriParam
     private SSLContextParameters sslContextParameters;
+    private ClassLoader applicationClassLoader;
 
     public MailConfiguration() {
     }
+    
+    public MailConfiguration(CamelContext context) {
+        this.applicationClassLoader = context.getApplicationContextClassLoader();
+    }
 
     /**
      * Returns a copy of this configuration
@@ -173,11 +180,19 @@ public class MailConfiguration implements Cloneable {
         if (session != null) {
             answer.setSession(session);
         } else {
-            // use our authenticator that does no live user interaction but returns the already
configured username and password
-            Session session = Session.getInstance(answer.getJavaMailProperties(), new DefaultAuthenticator(getUsername(),
getPassword()));
-            // sets the debug mode of the underlying mail framework
-            session.setDebug(debugMode);
-            answer.setSession(session);
+            ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+            try {
+                if (applicationClassLoader != null) {
+                    Thread.currentThread().setContextClassLoader(applicationClassLoader);
+                }
+                // use our authenticator that does no live user interaction but returns the
already configured username and password
+                Session session = Session.getInstance(answer.getJavaMailProperties(), new
DefaultAuthenticator(getUsername(), getPassword()));
+                // sets the debug mode of the underlying mail framework
+                session.setDebug(debugMode);
+                answer.setSession(session);
+            } finally {
+                Thread.currentThread().setContextClassLoader(tccl);
+            }
         }
 
         return answer;
@@ -420,7 +435,7 @@ public class MailConfiguration implements Cloneable {
     public Map<Message.RecipientType, String> getRecipients() {
         return recipients;
     }
-    
+
     public String getReplyTo() {
         return replyTo;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/689271b4/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
index b7d033c..5eb5dc2 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailProducer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.mail;
 
 import java.io.IOException;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
@@ -38,7 +39,13 @@ public class MailProducer extends DefaultProducer {
     }
 
     public void process(final Exchange exchange) {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
         try {
+            ClassLoader applicationClassLoader = getEndpoint().getCamelContext().getApplicationContextClassLoader();
+            if (applicationClassLoader != null) {
+                Thread.currentThread().setContextClassLoader(applicationClassLoader);
+            }
+
             MimeMessage mimeMessage;
 
             final Object body = exchange.getIn().getBody();
@@ -60,6 +67,8 @@ public class MailProducer extends DefaultProducer {
             exchange.setException(e);
         } catch (IOException e) {
             exchange.setException(e);
+        } finally {
+            Thread.currentThread().setContextClassLoader(tccl);
         }
     }
 


Mime
View raw message