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
commit 1c2875720f826535ae2b7f111b657126fa00b6fa Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Aug 12 15:02:46 2019 +0200 CAMEL-13850: Optimize model classes to provide changeable properties that support property placeholders to avoid reflection. Work in progress. --- .../model/DefinitionPropertiesProviderHelper.java | 43 +++++++++++++++++ .../org/apache/camel/model/FromDefinition.java | 18 +------ .../model/FromDefinitionPropertiesProvider.java | 50 +++++++++++++++++++ .../java/org/apache/camel/model/LogDefinition.java | 29 +---------- .../model/LogDefinitionPropertiesProvider.java | 56 ++++++++++++++++++++++ .../camel/model/OptionalIdentifiedDefinition.java | 2 +- .../camel/model/ProcessorDefinitionHelper.java | 1 + .../camel/model/PropertyPlaceholderAware.java | 12 ++++- ...re.java => ToDefinitionPropertiesProvider.java} | 34 ++++++++----- 9 files changed, 186 insertions(+), 59 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/model/DefinitionPropertiesProviderHelper.java b/core/camel-core/src/main/java/org/apache/camel/model/DefinitionPropertiesProviderHelper.java new file mode 100644 index 0000000..7da3ffd --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/model/DefinitionPropertiesProviderHelper.java @@ -0,0 +1,43 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.model; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; + +public class DefinitionPropertiesProviderHelper { + + private static final Map<Class, Function<Object, PropertyPlaceholderAware>> MAP; + static { + Map<Class, Function<Object, PropertyPlaceholderAware>> map = new HashMap<>(); + map.put(FromDefinition.class, FromDefinitionPropertiesProvider::new); + map.put(LogDefinition.class, LogDefinitionPropertiesProvider::new); + map.put(ToDefinition.class, ToDefinitionPropertiesProvider::new); + MAP = map; + } + + public static Optional<PropertyPlaceholderAware> provider(Object definition) { + Function<Object, PropertyPlaceholderAware> func = MAP.get(definition.getClass()); + if (func != null) { + return Optional.of(func.apply(definition)); + } + return Optional.empty(); + } + +} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/FromDefinition.java b/core/camel-core/src/main/java/org/apache/camel/model/FromDefinition.java index 6d61c6d..fb4d762 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/FromDefinition.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/FromDefinition.java @@ -37,7 +37,7 @@ import org.apache.camel.spi.Metadata; @Metadata(label = "eip,endpoint,routing") @XmlRootElement(name = "from") @XmlAccessorType(XmlAccessType.FIELD) -public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> implements PropertyPlaceholderAware, EndpointRequiredDefinition { +public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> implements EndpointRequiredDefinition { @XmlAttribute @Metadata(required = true) private String uri; @XmlTransient @@ -45,14 +45,7 @@ public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> @XmlTransient private EndpointConsumerBuilder endpointConsumerBuilder; - private final Map<String, Supplier<String>> readPlaceholders = new HashMap<>(); - private final Map<String, Consumer<String>> writePlaceholders = new HashMap<>(); - public FromDefinition() { - readPlaceholders.put("id", this::getId); - readPlaceholders.put("uri", this::getUri); - writePlaceholders.put("id", this::setId); - writePlaceholders.put("uri", this::setUri); } public FromDefinition(String uri) { @@ -150,13 +143,4 @@ public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> this.uri = null; } - @Override - public Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { - return readPlaceholders; - } - - @Override - public Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { - return writePlaceholders; - } } diff --git a/core/camel-core/src/main/java/org/apache/camel/model/FromDefinitionPropertiesProvider.java b/core/camel-core/src/main/java/org/apache/camel/model/FromDefinitionPropertiesProvider.java new file mode 100644 index 0000000..5953f84 --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/model/FromDefinitionPropertiesProvider.java @@ -0,0 +1,50 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.model; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.apache.camel.CamelContext; + +public class FromDefinitionPropertiesProvider implements PropertyPlaceholderAware { + + private final FromDefinition definition; + private final Map<String, Supplier<String>> readPlaceholders = new HashMap<>(); + private final Map<String, Consumer<String>> writePlaceholders = new HashMap<>(); + + public FromDefinitionPropertiesProvider(Object obj) { + this.definition = (FromDefinition) obj; + + readPlaceholders.put("id", definition::getId); + readPlaceholders.put("uri", definition::getUri); + writePlaceholders.put("id", definition::setId); + writePlaceholders.put("uri", definition::setUri); + } + + @Override + public Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { + return readPlaceholders; + } + + @Override + public Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { + return writePlaceholders; + } +} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/LogDefinition.java b/core/camel-core/src/main/java/org/apache/camel/model/LogDefinition.java index 62281e5..fb84545 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/LogDefinition.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/LogDefinition.java @@ -16,17 +16,12 @@ */ package org.apache.camel.model; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Supplier; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; -import org.apache.camel.CamelContext; import org.apache.camel.LoggingLevel; import org.apache.camel.spi.Metadata; import org.slf4j.Logger; @@ -37,7 +32,7 @@ import org.slf4j.Logger; @Metadata(label = "eip,configuration") @XmlRootElement(name = "log") @XmlAccessorType(XmlAccessType.FIELD) -public class LogDefinition extends NoOutputDefinition<LogDefinition> implements PropertyPlaceholderAware { +public class LogDefinition extends NoOutputDefinition<LogDefinition> { @XmlAttribute(required = true) private String message; @@ -52,20 +47,7 @@ public class LogDefinition extends NoOutputDefinition<LogDefinition> implements @XmlTransient private Logger logger; - private final Map<String, Supplier<String>> readPlaceholders = new HashMap<>(); - private final Map<String, Consumer<String>> writePlaceholders = new HashMap<>(); - public LogDefinition() { - readPlaceholders.put("id", this::getId); - readPlaceholders.put("message", this::getMessage); - readPlaceholders.put("logName", this::getLogName); - readPlaceholders.put("marker", this::getMarker); - readPlaceholders.put("loggerRef", this::getLoggerRef); - writePlaceholders.put("id", this::setId); - writePlaceholders.put("message", this::setMessage); - writePlaceholders.put("logName", this::setLogName); - writePlaceholders.put("marker", this::setMarker); - writePlaceholders.put("loggerRef", this::setLoggerRef); } public LogDefinition(String message) { @@ -156,13 +138,4 @@ public class LogDefinition extends NoOutputDefinition<LogDefinition> implements this.logger = logger; } - @Override - public Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { - return readPlaceholders; - } - - @Override - public Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { - return writePlaceholders; - } } diff --git a/core/camel-core/src/main/java/org/apache/camel/model/LogDefinitionPropertiesProvider.java b/core/camel-core/src/main/java/org/apache/camel/model/LogDefinitionPropertiesProvider.java new file mode 100644 index 0000000..a60ffa8 --- /dev/null +++ b/core/camel-core/src/main/java/org/apache/camel/model/LogDefinitionPropertiesProvider.java @@ -0,0 +1,56 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.model; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; + +import org.apache.camel.CamelContext; + +public class LogDefinitionPropertiesProvider implements PropertyPlaceholderAware { + + private final LogDefinition definition; + private final Map<String, Supplier<String>> readPlaceholders = new HashMap<>(); + private final Map<String, Consumer<String>> writePlaceholders = new HashMap<>(); + + public LogDefinitionPropertiesProvider(Object obj) { + this.definition = (LogDefinition) obj; + + readPlaceholders.put("id", definition::getId); + readPlaceholders.put("message", definition::getMessage); + readPlaceholders.put("logName", definition::getLogName); + readPlaceholders.put("marker", definition::getMarker); + readPlaceholders.put("loggerRef", definition::getLoggerRef); + writePlaceholders.put("id", definition::setId); + writePlaceholders.put("message", definition::setMessage); + writePlaceholders.put("logName", definition::setLogName); + writePlaceholders.put("marker", definition::setMarker); + writePlaceholders.put("loggerRef", definition::setLoggerRef); + } + + @Override + public Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { + return readPlaceholders; + } + + @Override + public Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { + return writePlaceholders; + } +} diff --git a/core/camel-core/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java b/core/camel-core/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java index dd0c216..5f520f5 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java @@ -31,7 +31,7 @@ import org.apache.camel.spi.NodeIdFactory; @XmlType(name = "optionalIdentifiedDefinition") @XmlAccessorType(XmlAccessType.PROPERTY) // must use XmlAccessType.PROPERTY which is required by camel-spring / camel-blueprint for their namespace parsers -public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> implements NamedNode { +public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedDefinition<T>> implements NamedNode, PropertyPlaceholderAware { private String id; private Boolean customId; diff --git a/core/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java b/core/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java index 280965b..ff809f6 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java @@ -721,6 +721,7 @@ public final class ProcessorDefinitionHelper { if (LOG.isTraceEnabled()) { LOG.trace("There are {} properties on: {}", readProperties.size(), definition); } + // lookup and resolve properties for String based properties for (Map.Entry<String, Supplier<String>> entry : readProperties.entrySet()) { String name = entry.getKey(); diff --git a/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java b/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java index 9bc6b4a..c4fb8b7 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java @@ -22,6 +22,7 @@ import java.util.function.Supplier; import org.apache.camel.CamelContext; +// TODO: Rename interface public interface PropertyPlaceholderAware { /** @@ -29,10 +30,17 @@ public interface PropertyPlaceholderAware { * * @return key/values of options */ - Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext); + default Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { + PropertyPlaceholderAware aware = DefinitionPropertiesProviderHelper.provider(this).orElse(null); + return aware != null ? aware.getReadPropertyPlaceholderOptions(camelContext) : null; + } /** * To update an existing property using the function with the ket/value and returning the changed value */ - Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext); + default Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { + PropertyPlaceholderAware aware = DefinitionPropertiesProviderHelper.provider(this).orElse(null); + return aware != null ? aware.getWritePropertyPlaceholderOptions(camelContext) : null; + } + } diff --git a/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java b/core/camel-core/src/main/java/org/apache/camel/model/ToDefinitionPropertiesProvider.java similarity index 50% copy from core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java copy to core/camel-core/src/main/java/org/apache/camel/model/ToDefinitionPropertiesProvider.java index 9bc6b4a..e0e64a5 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/PropertyPlaceholderAware.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/ToDefinitionPropertiesProvider.java @@ -16,23 +16,35 @@ */ package org.apache.camel.model; +import java.util.HashMap; import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; import org.apache.camel.CamelContext; -public interface PropertyPlaceholderAware { +public class ToDefinitionPropertiesProvider implements PropertyPlaceholderAware { - /** - * Gets the options on the model definition which supports property placeholders and can be resolved. - * - * @return key/values of options - */ - Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext); + private final ToDefinition definition; + private final Map<String, Supplier<String>> readPlaceholders = new HashMap<>(); + private final Map<String, Consumer<String>> writePlaceholders = new HashMap<>(); - /** - * To update an existing property using the function with the ket/value and returning the changed value - */ - Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext); + public ToDefinitionPropertiesProvider(Object obj) { + this.definition = (ToDefinition) obj; + + readPlaceholders.put("id", definition::getId); + readPlaceholders.put("uri", definition::getUri); + writePlaceholders.put("id", definition::setId); + writePlaceholders.put("uri", definition::setUri); + } + + @Override + public Map<String, Supplier<String>> getReadPropertyPlaceholderOptions(CamelContext camelContext) { + return readPlaceholders; + } + + @Override + public Map<String, Consumer<String>> getWritePropertyPlaceholderOptions(CamelContext camelContext) { + return writePlaceholders; + } }