This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 924a056 CAMEL-17041: add alias for entity type class names (#6219) 924a056 is described below commit 924a056d72f6d0147261ea720462daf2fb973f55 Author: mschnitzler <schnitzler.michael+git...@gmail.com> AuthorDate: Wed Oct 6 19:30:30 2021 +0200 CAMEL-17041: add alias for entity type class names (#6219) The JpaComponent holds a map of alias names to classes. When the entity type is resolved from the URI the map is checked first. --- .../component/jpa/JpaComponentConfigurer.java | 11 ++++++ .../org/apache/camel/component/jpa/jpa.json | 1 + .../apache/camel/component/jpa/JpaComponent.java | 31 ++++++++++++++-- .../org/apache/camel/component/jpa/JpaTest.java | 7 ++++ .../camel/component/jpa/JpaWithAliasTest.java | 43 ++++++++++++++++++++++ .../component/dsl/JpaComponentBuilderFactory.java | 19 ++++++++++ 6 files changed, 108 insertions(+), 4 deletions(-) diff --git a/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java b/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java index 4460630..675c6e3 100644 --- a/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java +++ b/components/camel-jpa/src/generated/java/org/apache/camel/component/jpa/JpaComponentConfigurer.java @@ -21,6 +21,7 @@ public class JpaComponentConfigurer extends PropertyConfigurerSupport implements public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) { JpaComponent target = (JpaComponent) obj; switch (ignoreCase ? name.toLowerCase() : name) { + case "aliases": target.setAliases(property(camelContext, java.util.Map.class, value)); return true; case "autowiredenabled": case "autowiredEnabled": target.setAutowiredEnabled(property(camelContext, boolean.class, value)); return true; case "bridgeerrorhandler": @@ -42,6 +43,7 @@ public class JpaComponentConfigurer extends PropertyConfigurerSupport implements @Override public Class<?> getOptionType(String name, boolean ignoreCase) { switch (ignoreCase ? name.toLowerCase() : name) { + case "aliases": return java.util.Map.class; case "autowiredenabled": case "autowiredEnabled": return boolean.class; case "bridgeerrorhandler": @@ -64,6 +66,7 @@ public class JpaComponentConfigurer extends PropertyConfigurerSupport implements public Object getOptionValue(Object obj, String name, boolean ignoreCase) { JpaComponent target = (JpaComponent) obj; switch (ignoreCase ? name.toLowerCase() : name) { + case "aliases": return target.getAliases(); case "autowiredenabled": case "autowiredEnabled": return target.isAutowiredEnabled(); case "bridgeerrorhandler": @@ -81,5 +84,13 @@ public class JpaComponentConfigurer extends PropertyConfigurerSupport implements default: return null; } } + + @Override + public Object getCollectionValueType(Object target, String name, boolean ignoreCase) { + switch (ignoreCase ? name.toLowerCase() : name) { + case "aliases": return java.lang.Class.class; + default: return null; + } + } } diff --git a/components/camel-jpa/src/generated/resources/org/apache/camel/component/jpa/jpa.json b/components/camel-jpa/src/generated/resources/org/apache/camel/component/jpa/jpa.json index 15262bc..7af8f67 100644 --- a/components/camel-jpa/src/generated/resources/org/apache/camel/component/jpa/jpa.json +++ b/components/camel-jpa/src/generated/resources/org/apache/camel/component/jpa/jpa.json @@ -22,6 +22,7 @@ "lenientProperties": false }, "componentProperties": { + "aliases": { "kind": "property", "displayName": "Aliases", "group": "common", "label": "", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Class<java.lang.Object>>", "deprecated": false, "autowired": false, "secret": false, "description": "Maps an alias to a JPA entity class. The alias can then be used in the endpoint URI (instead of the fully qualified class name)." }, "entityManagerFactory": { "kind": "property", "displayName": "Entity Manager Factory", "group": "common", "label": "", "required": false, "type": "object", "javaType": "javax.persistence.EntityManagerFactory", "deprecated": false, "autowired": false, "secret": false, "description": "To use the EntityManagerFactory. This is strongly recommended to configure." }, "joinTransaction": { "kind": "property", "displayName": "Join Transaction", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "The camel-jpa component will join transaction by default. You can use this option to turn this off, for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can also be set [...] "sharedEntityManager": { "kind": "property", "displayName": "Shared Entity Manager", "group": "common", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to use Spring's SharedEntityManager for the consumer\/producer. Note in most cases joinTransaction should be set to false as this is not an EXTENDED EntityManager." }, diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java index 850ed18..38e15eb 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaComponent.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.jpa; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutorService; @@ -50,6 +51,8 @@ public class JpaComponent extends DefaultComponent { private boolean joinTransaction = true; @Metadata private boolean sharedEntityManager; + @Metadata + private Map<String, Class<?>> aliases = new HashMap<>(); public JpaComponent() { } @@ -103,6 +106,22 @@ public class JpaComponent extends DefaultComponent { this.sharedEntityManager = sharedEntityManager; } + public void addAlias(String alias, Class<?> clazz) { + this.aliases.put(alias, clazz); + } + + /** + * Maps an alias to a JPA entity class. The alias can then be used in the endpoint URI (instead of the fully + * qualified class name). + */ + public void setAliases(Map<String, Class<?>> aliases) { + this.aliases = aliases; + } + + public Map<String, Class<?>> getAliases() { + return aliases; + } + synchronized ExecutorService getOrCreatePollingConsumerExecutorService() { if (pollingConsumerExecutorService == null) { LOG.debug("Creating thread pool for JpaPollingConsumer to support polling using timeout"); @@ -126,11 +145,15 @@ public class JpaComponent extends DefaultComponent { endpoint.setParameters(params); } - // lets interpret the next string as a class + // lets interpret the next string as an alias or class if (ObjectHelper.isNotEmpty(path)) { - // provide the class loader of this component to work in OSGi environments as camel-jpa must be able - // to resolve the entity classes - Class<?> type = getCamelContext().getClassResolver().resolveClass(path, JpaComponent.class.getClassLoader()); + Class<?> type = aliases.get(path); + if (type == null) { + // provide the class loader of this component to work in OSGi environments as camel-jpa must be able + // to resolve the entity classes + type = getCamelContext().getClassResolver().resolveClass(path, JpaComponent.class.getClassLoader()); + } + if (type != null) { endpoint.setEntityType(type); } diff --git a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java index a5c024a..90653df 100644 --- a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java +++ b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java @@ -49,6 +49,7 @@ public class JpaTest { private static final Logger LOG = LoggerFactory.getLogger(JpaTest.class); protected CamelContext camelContext = new DefaultCamelContext(); protected ProducerTemplate template; + protected JpaComponent component; protected JpaEndpoint endpoint; protected JpaEndpoint listEndpoint; protected EntityManager entityManager; @@ -125,6 +126,8 @@ public class JpaTest { camelContext.start(); template = camelContext.createProducerTemplate(); + setUpComponent(); + Endpoint value = camelContext.getEndpoint(getEndpointUri()); assertNotNull(value, "Could not find endpoint!"); assertTrue(value instanceof JpaEndpoint, "Should be a JPA endpoint but was: " + value); @@ -152,6 +155,10 @@ public class JpaTest { return "jpa://" + SendEmail.class.getName(); } + protected void setUpComponent() { + // no set up in this test + } + @AfterEach public void tearDown() throws Exception { ServiceHelper.stopService(consumer, template); diff --git a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithAliasTest.java b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithAliasTest.java new file mode 100644 index 0000000..d7a6ea9 --- /dev/null +++ b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaWithAliasTest.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 + * + * 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.component.jpa; + +import java.util.Collections; + +import org.apache.camel.Component; +import org.apache.camel.examples.SendEmail; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JpaWithAliasTest extends JpaTest { + + @Override + protected String getEndpointUri() { + return "jpa://SendEmail"; + } + + @Override + protected void setUpComponent() { + Component compValue = camelContext.getComponent("jpa"); + assertNotNull(compValue, "Could not find component!"); + assertTrue(compValue instanceof JpaComponent, "Should be a JPA component but was: " + compValue); + component = (JpaComponent) compValue; + + component.setAliases(Collections.singletonMap("SendEmail", SendEmail.class)); + } +} diff --git a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JpaComponentBuilderFactory.java b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JpaComponentBuilderFactory.java index 4ced4d8..2507e19 100644 --- a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JpaComponentBuilderFactory.java +++ b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/JpaComponentBuilderFactory.java @@ -51,6 +51,24 @@ public interface JpaComponentBuilderFactory { */ interface JpaComponentBuilder extends ComponentBuilder<JpaComponent> { /** + * Maps an alias to a JPA entity class. The alias can then be used in + * the endpoint URI (instead of the fully qualified class name). + * + * The option is a: <code>java.util.Map&lt;java.lang.String, + * java.lang.Class&lt;java.lang.Object&gt;&gt;</code> + * type. + * + * Group: common + * + * @param aliases the value to set + * @return the dsl builder + */ + default JpaComponentBuilder aliases( + java.util.Map<java.lang.String, java.lang.Class<java.lang.Object>> aliases) { + doSetProperty("aliases", aliases); + return this; + } + /** * To use the EntityManagerFactory. This is strongly recommended to * configure. * @@ -202,6 +220,7 @@ public interface JpaComponentBuilderFactory { String name, Object value) { switch (name) { + case "aliases": ((JpaComponent) component).setAliases((java.util.Map) value); return true; case "entityManagerFactory": ((JpaComponent) component).setEntityManagerFactory((javax.persistence.EntityManagerFactory) value); return true; case "joinTransaction": ((JpaComponent) component).setJoinTransaction((boolean) value); return true; case "sharedEntityManager": ((JpaComponent) component).setSharedEntityManager((boolean) value); return true;