camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r771303 - /camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
Date Mon, 04 May 2009 13:47:16 GMT
Author: davsclaus
Date: Mon May  4 13:47:16 2009
New Revision: 771303

URL: http://svn.apache.org/viewvc?rev=771303&view=rev
Log:
CAMEL-1583: ldap-producer is not thread safe. Applied patch with thanks to Christopher Hunt.

Modified:
    camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java

Modified: camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java?rev=771303&r1=771302&r2=771303&view=diff
==============================================================================
--- camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
(original)
+++ camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
Mon May  4 13:47:16 2009
@@ -34,14 +34,14 @@
  */
 public class LdapProducer<E extends Exchange> extends DefaultProducer {
     private static final transient Log LOG = LogFactory.getLog(LdapProducer.class);
-    private DirContext ldapContext;
+    private String remaining;
     private SearchControls controls;
     private String searchBase;
-
+    
     public LdapProducer(LdapEndpoint endpoint, String remaining, String base, int scope)
throws Exception {
         super(endpoint);
 
-        ldapContext = (DirContext)getEndpoint().getCamelContext().getRegistry().lookup(remaining);
+        this.remaining = remaining;
         searchBase = base;
         controls = new SearchControls();
         controls.setSearchScope(scope);
@@ -49,20 +49,28 @@
 
     public void process(Exchange exchange) throws Exception {
         String filter = exchange.getIn().getBody(String.class);
-
-        // could throw NamingException
-        List<SearchResult> data = new ArrayList<SearchResult>();
-        NamingEnumeration<SearchResult> namingEnumeration =
-            ldapContext.search(searchBase, filter, getControls());
-
-        while (namingEnumeration.hasMore()) {
-            data.add(namingEnumeration.next());
+        
+        // Obtain our ldap context. We do this by looking up the context in our registry.

+        // Note though that a new context is expected each time. Therefore if spring is
+        // being used then use prototype="scope". If you do not then you might experience
+        // concurrency issues as InitialContext is not required to support concurrency.
+        // On the other hand if you have a DirContext that is able to support concurrency
+        // then using the default singleton scope is entirely sufficient. Most DirContext
+        // classes will require prototype scope though.
+        DirContext ldapContext = (DirContext)getEndpoint().getCamelContext().getRegistry().lookup(remaining);
+        try {
+	        // could throw NamingException
+	        List<SearchResult> data = new ArrayList<SearchResult>();
+	        NamingEnumeration<SearchResult> namingEnumeration =
+	            ldapContext.search(searchBase, filter, getControls());
+	
+	        while (namingEnumeration.hasMore()) {
+	            data.add(namingEnumeration.next());
+	        }
+	        exchange.getOut().setBody(data);
+        } finally {
+            ldapContext.close();
         }
-        exchange.getOut().setBody(data);
-    }
-
-    public DirContext getDirContext() {
-        return ldapContext;
     }
 
     protected SearchControls getControls() {



Mime
View raw message