This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 6ba7e47 CAMEL-16441: camel-core - Optimize EndpointKey can be NormalizedUri 6ba7e47 is described below commit 6ba7e47f7e378afca7fccb06b9cd343dcf6fc760 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Apr 1 17:32:16 2021 +0200 CAMEL-16441: camel-core - Optimize EndpointKey can be NormalizedUri --- .../camel/impl/engine/AbstractCamelContext.java | 50 ++++++++------------- .../camel/impl/engine/DefaultEndpointRegistry.java | 11 ++--- .../org/apache/camel/impl/engine/EndpointKey.java | 51 ---------------------- .../impl/engine/ProvisionalEndpointRegistry.java | 3 +- .../camel/impl/engine/SimpleCamelContext.java | 3 +- .../impl/lw/LightweightRuntimeCamelContext.java | 23 +++------- .../builder/endpoint/AbstractEndpointBuilder.java | 6 +-- .../org/apache/camel/support/NormalizedUri.java | 33 +++++++++++--- 8 files changed, 63 insertions(+), 117 deletions(-) diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java index 079045f..afa7832 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java @@ -323,7 +323,7 @@ public abstract class AbstractCamelContext extends BaseService private volatile TransformerRegistry<TransformerKey> transformerRegistry; private volatile ValidatorRegistry<ValidatorKey> validatorRegistry; private volatile StartupStepRecorder startupStepRecorder = new DefaultStartupStepRecorder(); - private EndpointRegistry<EndpointKey> endpoints; + private EndpointRegistry<NormalizedUri> endpoints; private RuntimeEndpointRegistry runtimeEndpointRegistry; private ShutdownRoute shutdownRoute = ShutdownRoute.Default; private ShutdownRunningTask shutdownRunningTask = ShutdownRunningTask.CompleteCurrentTaskOnly; @@ -725,7 +725,7 @@ public abstract class AbstractCamelContext extends BaseService } @Override - public EndpointRegistry<EndpointKey> getEndpointRegistry() { + public EndpointRegistry<NormalizedUri> getEndpointRegistry() { return endpoints; } @@ -737,7 +737,7 @@ public abstract class AbstractCamelContext extends BaseService @Override public Map<String, Endpoint> getEndpointMap() { Map<String, Endpoint> answer = new TreeMap<>(); - for (Map.Entry<EndpointKey, Endpoint> entry : endpoints.entrySet()) { + for (Map.Entry<NormalizedUri, Endpoint> entry : endpoints.entrySet()) { answer.put(entry.getKey().get(), entry.getValue()); } return answer; @@ -756,13 +756,7 @@ public abstract class AbstractCamelContext extends BaseService if (endpoints.isEmpty()) { return null; } - EndpointKey key; - if (uri instanceof EndpointKey) { - key = (EndpointKey) uri; - } else { - key = new EndpointKey(uri.getUri(), true); - } - return endpoints.get(key); + return endpoints.get(uri); } @Override @@ -795,8 +789,8 @@ public abstract class AbstractCamelContext extends BaseService answer.add(oldEndpoint); stopServices(oldEndpoint); } else { - List<EndpointKey> toRemove = new ArrayList<>(); - for (Map.Entry<EndpointKey, Endpoint> entry : endpoints.entrySet()) { + List<NormalizedUri> toRemove = new ArrayList<>(); + for (Map.Entry<NormalizedUri, Endpoint> entry : endpoints.entrySet()) { oldEndpoint = entry.getValue(); if (EndpointHelper.matchEndpoint(this, oldEndpoint.getEndpointUri(), uri)) { try { @@ -808,7 +802,7 @@ public abstract class AbstractCamelContext extends BaseService toRemove.add(entry.getKey()); } } - for (EndpointKey key : toRemove) { + for (NormalizedUri key : toRemove) { endpoints.remove(key); } } @@ -827,9 +821,11 @@ public abstract class AbstractCamelContext extends BaseService public NormalizedEndpointUri normalizeUri(String uri) { try { uri = EndpointHelper.resolveEndpointUriPropertyPlaceholders(this, uri); - uri = URISupport.normalizeUri(uri); - return new NormalizedUri(uri); + return NormalizedUri.newNormalizedUri(uri, false); } catch (Exception e) { + if (e instanceof ResolveEndpointFailedException) { + throw e; + } throw new ResolveEndpointFailedException(uri, e); } } @@ -901,7 +897,7 @@ public abstract class AbstractCamelContext extends BaseService Endpoint answer = null; if (!prototype) { // use optimized method to get the endpoint uri - EndpointKey key = getEndpointKeyPreNormalized(uri); + NormalizedUri key = NormalizedUri.newNormalizedUri(uri, true); // only lookup and reuse existing endpoints if not prototype scoped answer = endpoints.get(key); } @@ -1031,18 +1027,8 @@ public abstract class AbstractCamelContext extends BaseService * @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 DefaultEndpointRegistry} - * - * @param uri the endpoint uri which is pre normalized - * @return the key - */ - protected EndpointKey getEndpointKeyPreNormalized(String uri) { - return new EndpointKey(uri, true); + protected NormalizedUri getEndpointKey(String uri) { + return NormalizedUri.newNormalizedUri(uri, false); } /** @@ -1052,12 +1038,12 @@ public abstract class AbstractCamelContext extends BaseService * @param endpoint the endpoint * @return the key */ - protected EndpointKey getEndpointKey(String uri, Endpoint endpoint) { + protected NormalizedUri getEndpointKey(String uri, Endpoint endpoint) { if (endpoint != null && !endpoint.isSingleton()) { int counter = endpointKeyCounter.incrementAndGet(); - return new EndpointKey(uri + ":" + counter); + return NormalizedUri.newNormalizedUri(uri + ":" + counter, false); } else { - return new EndpointKey(uri); + return NormalizedUri.newNormalizedUri(uri, false); } } @@ -4922,7 +4908,7 @@ public abstract class AbstractCamelContext extends BaseService protected abstract RestRegistryFactory createRestRegistryFactory(); - protected abstract EndpointRegistry<EndpointKey> createEndpointRegistry(Map<EndpointKey, Endpoint> endpoints); + protected abstract EndpointRegistry<NormalizedUri> createEndpointRegistry(Map<NormalizedUri, Endpoint> endpoints); protected abstract TransformerRegistry<TransformerKey> createTransformerRegistry(); diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultEndpointRegistry.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultEndpointRegistry.java index 2d48e2f..7d37bcd 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultEndpointRegistry.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultEndpointRegistry.java @@ -22,30 +22,31 @@ import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.spi.EndpointRegistry; import org.apache.camel.support.CamelContextHelper; +import org.apache.camel.support.NormalizedUri; /** * Default implementation of {@link org.apache.camel.spi.EndpointRegistry} */ -public class DefaultEndpointRegistry extends AbstractDynamicRegistry<EndpointKey, Endpoint> - implements EndpointRegistry<EndpointKey> { +public class DefaultEndpointRegistry extends AbstractDynamicRegistry<NormalizedUri, Endpoint> + implements EndpointRegistry<NormalizedUri> { public DefaultEndpointRegistry(CamelContext context) { super(context, CamelContextHelper.getMaximumEndpointCacheSize(context)); } - public DefaultEndpointRegistry(CamelContext context, Map<EndpointKey, Endpoint> endpoints) { + public DefaultEndpointRegistry(CamelContext context, Map<NormalizedUri, Endpoint> endpoints) { this(context); putAll(endpoints); } @Override public boolean isStatic(String key) { - return isStatic(new EndpointKey(key)); + return isStatic(NormalizedUri.newNormalizedUri(key, false)); } @Override public boolean isDynamic(String key) { - return isDynamic(new EndpointKey(key)); + return isDynamic(NormalizedUri.newNormalizedUri(key, false)); } @Override diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/EndpointKey.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/EndpointKey.java deleted file mode 100644 index 8e97cd2..0000000 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/EndpointKey.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.engine; - -import org.apache.camel.ValueHolder; -import org.apache.camel.spi.NormalizedEndpointUri; -import org.apache.camel.support.EndpointHelper; -import org.apache.camel.util.StringHelper; - -/** - * Key used in {@link DefaultEndpointRegistry} in {@link AbstractCamelContext}, to ensure a consistent lookup. - */ -public final class EndpointKey extends ValueHolder<String> implements NormalizedEndpointUri { - - public EndpointKey(String uri) { - this(uri, false); - } - - /** - * Optimized when the uri is already normalized. - */ - public EndpointKey(String uri, boolean normalized) { - super(normalized ? uri : EndpointHelper.normalizeEndpointUri(uri)); - StringHelper.notEmpty(uri, "uri"); - } - - @Override - public String getUri() { - return get(); - } - - @Override - public String toString() { - return get(); - } - -} diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/ProvisionalEndpointRegistry.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/ProvisionalEndpointRegistry.java index e3a44ad..e71f098 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/ProvisionalEndpointRegistry.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/ProvisionalEndpointRegistry.java @@ -21,12 +21,13 @@ import java.util.HashMap; import org.apache.camel.Endpoint; import org.apache.camel.spi.EndpointRegistry; import org.apache.camel.support.LRUCacheFactory; +import org.apache.camel.support.NormalizedUri; /** * A provisional (temporary) {@link EndpointRegistry} that is only used during startup of Apache Camel to make starting * Camel faster while {@link LRUCacheFactory} is warming up etc. */ -class ProvisionalEndpointRegistry extends HashMap<EndpointKey, Endpoint> implements EndpointRegistry<EndpointKey> { +class ProvisionalEndpointRegistry extends HashMap<NormalizedUri, Endpoint> implements EndpointRegistry<NormalizedUri> { @Override public void start() { diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java index 874e930..3c46c48 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java @@ -81,6 +81,7 @@ import org.apache.camel.spi.ValidatorRegistry; import org.apache.camel.spi.XMLRoutesDefinitionLoader; import org.apache.camel.support.DefaultRegistry; import org.apache.camel.support.DefaultUuidGenerator; +import org.apache.camel.support.NormalizedUri; import org.apache.camel.support.ResolverHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -547,7 +548,7 @@ public class SimpleCamelContext extends AbstractCamelContext { } @Override - protected EndpointRegistry<EndpointKey> createEndpointRegistry(Map<EndpointKey, Endpoint> endpoints) { + protected EndpointRegistry<NormalizedUri> createEndpointRegistry(Map<NormalizedUri, Endpoint> endpoints) { return new DefaultEndpointRegistry(getCamelContextReference(), endpoints); } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java index 74af3ab..af98c56 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java @@ -64,7 +64,6 @@ import org.apache.camel.impl.converter.CoreTypeConverterRegistry; import org.apache.camel.impl.engine.DefaultComponentResolver; import org.apache.camel.impl.engine.DefaultDataFormatResolver; import org.apache.camel.impl.engine.DefaultLanguageResolver; -import org.apache.camel.impl.engine.EndpointKey; import org.apache.camel.spi.AnnotationBasedProcessorFactory; import org.apache.camel.spi.AsyncProcessorAwaitManager; import org.apache.camel.spi.BeanIntrospection; @@ -786,7 +785,7 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat @Override public Endpoint hasEndpoint(String uri) { - return endpoints.get(new EndpointKey(uri)); + return endpoints.get(NormalizedUri.newNormalizedUri(uri, false)); } @Override @@ -1252,13 +1251,7 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat @Override public Endpoint hasEndpoint(NormalizedEndpointUri uri) { - EndpointKey key; - if (uri instanceof EndpointKey) { - key = (EndpointKey) uri; - } else { - key = getEndpointKeyPreNormalized(uri.getUri()); - } - return endpoints.get(key); + return endpoints.get(uri); } @Override @@ -1288,11 +1281,10 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat if (!normalized) { uri = normalizeEndpointUri(uri); } - String scheme; Endpoint answer = null; if (!prototype) { // use optimized method to get the endpoint uri - EndpointKey key = getEndpointKeyPreNormalized(uri); + NormalizedUri key = NormalizedUri.newNormalizedUri(uri, true); // only lookup and reuse existing endpoints if not prototype scoped answer = endpoints.get(key); } @@ -1323,7 +1315,7 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat Endpoint answer; String scheme = null; // use optimized method to get the endpoint uri - EndpointKey key = getEndpointKeyPreNormalized(uri); + NormalizedUri key = NormalizedUri.newNormalizedUri(uri, true); answer = endpoints.get(key); // unknown scheme if (answer == null) { @@ -1332,16 +1324,11 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat return answer; } - protected EndpointKey getEndpointKeyPreNormalized(String uri) { - return new EndpointKey(uri, true); - } - @Override public NormalizedEndpointUri normalizeUri(String uri) { try { uri = resolvePropertyPlaceholders(uri); - uri = normalizeEndpointUri(uri); - return new NormalizedUri(uri); + return NormalizedUri.newNormalizedUri(uri, false); } catch (Exception e) { throw new ResolveEndpointFailedException(uri, e); } diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java index a279a09..f540929 100644 --- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java +++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java @@ -126,15 +126,15 @@ public class AbstractEndpointBuilder { } if (params.isEmpty()) { - answer = new NormalizedUri(targetScheme + "://" + targetPath); + answer = NormalizedUri.newNormalizedUri(targetScheme + "://" + targetPath, false); } else { try { // build query string from parameters String query = URISupport.createQueryString(params, encode); if (targetPath.contains("?")) { - answer = new NormalizedUri(targetScheme + "://" + targetPath + "&" + query); + answer = NormalizedUri.newNormalizedUri(targetScheme + "://" + targetPath + "&" + query, false); } else { - answer = new NormalizedUri(targetScheme + "://" + targetPath + "?" + query); + answer = NormalizedUri.newNormalizedUri(targetScheme + "://" + targetPath + "?" + query, false); } } catch (URISyntaxException e) { throw RuntimeCamelException.wrapRuntimeCamelException(e); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java b/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java index e774a26..cae8153 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/NormalizedUri.java @@ -16,23 +16,44 @@ */ package org.apache.camel.support; +import org.apache.camel.ValueHolder; import org.apache.camel.spi.NormalizedEndpointUri; -public final class NormalizedUri implements NormalizedEndpointUri { +/** + * Implementation of {@link NormalizedEndpointUri}. + * + * Use the {@link #newNormalizedUri(String, boolean)} as factory method. + */ +public final class NormalizedUri extends ValueHolder<String> implements NormalizedEndpointUri { - private final String uri; + // must extends ValueHolder to let this class be used as key for Camels endpoint registry + + private NormalizedUri(String value) { + super(value); + } - public NormalizedUri(String uri) { - this.uri = uri; + /** + * Creates a new {@link NormalizedUri} instance + * + * @param uri the uri + * @param normalized whether its already normalized + * @return the created normalized uri + */ + public static NormalizedUri newNormalizedUri(String uri, boolean normalized) { + if (normalized) { + return new NormalizedUri(uri); + } else { + return new NormalizedUri(EndpointHelper.normalizeEndpointUri(uri)); + } } @Override public String getUri() { - return uri; + return get(); } @Override public String toString() { - return uri; + return get(); } }