You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/12/15 15:56:12 UTC

svn commit: r1049573 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: impl/DefaultCamelContext.java impl/EndpointKey.java impl/EndpointRegistry.java util/ValueHolder.java

Author: davsclaus
Date: Wed Dec 15 14:56:12 2010
New Revision: 1049573

URL: http://svn.apache.org/viewvc?rev=1049573&view=rev
Log:
CAMEL-3434: Using EndpointKey holder class to ensure consistent lookup/add/remove of endpoints in EndpointRegistry on CamelContext.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointKey.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ValueHolder.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1049573&r1=1049572&r2=1049573&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Wed Dec 15 14:56:12 2010
@@ -136,7 +136,7 @@ public class DefaultCamelContext extends
     private CamelContextNameStrategy nameStrategy = new DefaultCamelContextNameStrategy();
     private String managementName;
     private ClassLoader applicationContextClassLoader;
-    private final Map<String, Endpoint> endpoints = new EndpointRegistry();
+    private final Map<EndpointKey, Endpoint> endpoints = new EndpointRegistry();
     private final AtomicInteger endpointKeyCounter = new AtomicInteger();
     private final List<EndpointStrategy> endpointStrategies = new ArrayList<EndpointStrategy>();
     private final Map<String, Component> components = new HashMap<String, Component>();
@@ -333,19 +333,17 @@ public class DefaultCamelContext extends
 
     public Map<String, Endpoint> getEndpointMap() {
         synchronized (endpoints) {
-            return new TreeMap<String, Endpoint>(endpoints);
+            TreeMap<String, Endpoint> answer = new TreeMap<String, Endpoint>();
+            for (Map.Entry<EndpointKey, Endpoint> entry : endpoints.entrySet()) {
+                answer.put(entry.getKey().get(), entry.getValue());
+            }
+            return answer;
         }
     }
 
     public Endpoint hasEndpoint(String uri) {
-        // normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
-        try {
-            uri = URISupport.normalizeUri(uri);
-        } catch (Exception e) {
-            throw new ResolveEndpointFailedException(uri, e);
-        }
         synchronized (endpoints) {
-            return endpoints.get(uri);
+            return endpoints.get(getEndpointKey(uri));
         }
     }
 
@@ -353,11 +351,11 @@ public class DefaultCamelContext extends
         Endpoint oldEndpoint;
         synchronized (endpoints) {
             startServices(endpoint);
-            oldEndpoint = endpoints.remove(uri);
+            oldEndpoint = endpoints.remove(getEndpointKey(uri));
             for (LifecycleStrategy strategy : lifecycleStrategies) {
                 strategy.onEndpointAdd(endpoint);
             }
-            addEndpointToRegistry(endpoint);
+            addEndpointToRegistry(uri, endpoint);
             if (oldEndpoint != null) {
                 stopServices(oldEndpoint);
             }
@@ -367,7 +365,7 @@ public class DefaultCamelContext extends
 
     public Collection<Endpoint> removeEndpoints(String uri) throws Exception {
         Collection<Endpoint> answer = new ArrayList<Endpoint>();
-        Endpoint oldEndpoint = endpoints.remove(uri);
+        Endpoint oldEndpoint = endpoints.remove(getEndpointKey(uri));
         if (oldEndpoint != null) {
             answer.add(oldEndpoint);
             stopServices(oldEndpoint);
@@ -380,7 +378,7 @@ public class DefaultCamelContext extends
                 }
             }
             for (Endpoint endpoint : answer) {
-                endpoints.remove(endpoint.getEndpointUri());
+                endpoints.remove(getEndpointKey(endpoint.getEndpointUri()));
             }
         }
 
@@ -409,11 +407,7 @@ public class DefaultCamelContext extends
         }
 
         // normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
-        try {
-            uri = URISupport.normalizeUri(uri);
-        } catch (Exception e) {
-            throw new ResolveEndpointFailedException(uri, e);
-        }
+        uri = normalizeEndpointUri(uri);
 
         if (log.isTraceEnabled()) {
             log.trace("Getting endpoint with normalized uri: " + uri);
@@ -422,7 +416,7 @@ public class DefaultCamelContext extends
         Endpoint answer;
         String scheme = null;
         synchronized (endpoints) {
-            answer = endpoints.get(uri);
+            answer = endpoints.get(getEndpointKey(uri));
             if (answer == null) {
                 try {
                     // Use the URI prefix to find the component.
@@ -449,7 +443,7 @@ public class DefaultCamelContext extends
 
                     if (answer != null) {
                         addService(answer);
-                        answer = addEndpointToRegistry(answer);
+                        answer = addEndpointToRegistry(uri, answer);
                     }
                 } catch (Exception e) {
                     throw new ResolveEndpointFailedException(uri, e);
@@ -488,7 +482,8 @@ public class DefaultCamelContext extends
             for (Endpoint endpoint : getEndpoints()) {
                 Endpoint newEndpoint = strategy.registerEndpoint(endpoint.getEndpointUri(), endpoint);
                 if (newEndpoint != null) {
-                    endpoints.put(getEndpointKey(newEndpoint.getEndpointUri(), newEndpoint), newEndpoint);
+                    // put will replace existing endpoint with the new endpoint
+                    endpoints.put(getEndpointKey(endpoint.getEndpointUri()), newEndpoint);
                 }
             }
         }
@@ -497,17 +492,63 @@ public class DefaultCamelContext extends
     /**
      * Strategy to add the given endpoint to the internal endpoint registry
      *
+     * @param uri      uri of the endpoint
      * @param endpoint the endpoint to add
      * @return the added endpoint
      */
-    protected Endpoint addEndpointToRegistry(Endpoint endpoint) {
+    protected Endpoint addEndpointToRegistry(String uri, Endpoint endpoint) {
+        ObjectHelper.notEmpty(uri, "uri");
+        ObjectHelper.notNull(endpoint, "endpoint");
+
         for (EndpointStrategy strategy : endpointStrategies) {
-            endpoint = strategy.registerEndpoint(endpoint.getEndpointUri(), endpoint);
+            endpoint = strategy.registerEndpoint(uri, endpoint);
         }
-        endpoints.put(getEndpointKey(endpoint.getEndpointUri(), endpoint), endpoint);
+        endpoints.put(getEndpointKey(uri, endpoint), endpoint);
         return endpoint;
     }
 
+    /**
+     * Normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order.
+     *
+     * @param uri the uri
+     * @return normalized uri
+     * @throws ResolveEndpointFailedException if uri cannot be normalized
+     */
+    protected static String normalizeEndpointUri(String uri) {
+        try {
+            uri = URISupport.normalizeUri(uri);
+        } catch (Exception e) {
+            throw new ResolveEndpointFailedException(uri, e);
+        }
+        return uri;
+    }
+
+    /**
+     * Gets the endpoint key to use for lookup or whe adding endpoints to the {@link EndpointRegistry}
+     *
+     * @param uri the endpoint uri
+     * @return the key
+     */
+    protected EndpointKey getEndpointKey(String uri) {
+        return new EndpointKey(uri);
+    }
+
+    /**
+     * Gets the endpoint key to use for lookup or whe adding endpoints to the {@link EndpointRegistry}
+     *
+     * @param uri      the endpoint uri
+     * @param endpoint the endpoint
+     * @return the key
+     */
+    protected EndpointKey getEndpointKey(String uri, Endpoint endpoint) {
+        if (endpoint != null && !endpoint.isSingleton()) {
+            int counter = endpointKeyCounter.incrementAndGet();
+            return new EndpointKey(uri + ":" + counter);
+        } else {
+            return new EndpointKey(uri);
+        }
+    }
+
     // Route Management Methods
     // -----------------------------------------------------------------------
 
@@ -2163,15 +2204,6 @@ public class DefaultCamelContext extends
         this.uuidGenerator = uuidGenerator;
     }
 
-    protected String getEndpointKey(String uri, Endpoint endpoint) {
-        if (endpoint.isSingleton()) {
-            return uri;
-        } else {
-            int counter = endpointKeyCounter.incrementAndGet();
-            return uri + ":" + counter;
-        }
-    }
-
     protected Map<String, RouteService> getRouteServices() {
         return routeServices;
     }
@@ -2225,7 +2257,7 @@ public class DefaultCamelContext extends
     }
 
     /**
-     * Reset conext counter to a preset value. Mostly used for tests to ensure a predictable getName()
+     * Reset context counter to a preset value. Mostly used for tests to ensure a predictable getName()
      *
      * @param value new value for the context counter
      */

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointKey.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointKey.java?rev=1049573&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointKey.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointKey.java Wed Dec 15 14:56:12 2010
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ValueHolder;
+
+/**
+ * Key used in {@link EndpointRegistry} in {@link DefaultCamelContext},
+ * to ensure a consistent lookup.
+ */
+final class EndpointKey extends ValueHolder<String> {
+
+    EndpointKey(String uri) {
+        // must normalize key
+        super(DefaultCamelContext.normalizeEndpointUri(uri));
+        ObjectHelper.notEmpty(uri, "uri");
+    }
+
+    @Override
+    public String toString() {
+        return get();
+    }
+
+}

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java?rev=1049573&r1=1049572&r2=1049573&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/EndpointRegistry.java Wed Dec 15 14:56:12 2010
@@ -25,7 +25,7 @@ import org.apache.camel.util.LRUCache;
  *
  * @version $Revision$
  */
-public class EndpointRegistry extends LRUCache<String, Endpoint> {
+public class EndpointRegistry extends LRUCache<EndpointKey, Endpoint> {
 
     public EndpointRegistry() {
         // use a cache size of 1000

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ValueHolder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ValueHolder.java?rev=1049573&r1=1049572&r2=1049573&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ValueHolder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ValueHolder.java Wed Dec 15 14:56:12 2010
@@ -22,6 +22,10 @@ package org.apache.camel.util;
 public class ValueHolder<V> {
     private V value;
 
+    /**
+     * @deprecated should be immutable, will be removed in Camel 3.0
+     */
+    @Deprecated
     public ValueHolder() {
     }
 
@@ -33,8 +37,34 @@ public class ValueHolder<V> {
         return value;
     }
 
+    /**
+     * @deprecated should be immutable, will be removed in Camel 3.0
+     */
+    @Deprecated
     public void set(V val) {
         value = val;
     }
-    
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        ValueHolder that = (ValueHolder) o;
+
+        if (value != null ? !value.equals(that.value) : that.value != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return value != null ? value.hashCode() : 0;
+    }
 }