This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch kamelet-ctr2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit f488892bd06f6f39e39c5d9917ba84a3a68e21c3 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu May 2 14:55:13 2024 +0200 CAMEL-20514: camel-model - Add support for bean constructors for beans in route templates or kamelets --- .../camel/model/app/RegistryBeanDefinition.java | 193 +-------------------- .../java/org/apache/camel/xml/in/ModelParser.java | 18 +- .../java/org/apache/camel/xml/out/ModelWriter.java | 14 +- .../org/apache/camel/yaml/out/ModelWriter.java | 14 +- 4 files changed, 7 insertions(+), 232 deletions(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java index 936978bcfc7..7d698384f0d 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/app/RegistryBeanDefinition.java @@ -16,19 +16,12 @@ */ package org.apache.camel.model.app; -import java.util.Map; - import jakarta.xml.bind.annotation.XmlAccessType; import jakarta.xml.bind.annotation.XmlAccessorType; -import jakarta.xml.bind.annotation.XmlAttribute; -import jakarta.xml.bind.annotation.XmlElement; -import jakarta.xml.bind.annotation.XmlTransient; import jakarta.xml.bind.annotation.XmlType; -import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import org.apache.camel.model.BeanFactoryDefinition; import org.apache.camel.spi.Metadata; -import org.apache.camel.spi.Resource; -import org.apache.camel.spi.ResourceAware; /** * Define custom beans that can be used in your Camel routes and in general. @@ -36,188 +29,6 @@ import org.apache.camel.spi.ResourceAware; @Metadata(label = "configuration") @XmlType @XmlAccessorType(XmlAccessType.FIELD) -public class RegistryBeanDefinition implements ResourceAware { - - @XmlTransient - private Resource resource; - - @XmlAttribute(required = true) - private String name; - @XmlAttribute(required = true) - private String type; - @XmlAttribute - private String initMethod; - @XmlAttribute - private String destroyMethod; - @XmlAttribute - private String factoryMethod; - @XmlAttribute - private String factoryBean; - @XmlAttribute - private String builderClass; - @XmlAttribute - @Metadata(defaultValue = "build") - private String builderMethod; - @XmlAttribute - @Metadata(label = "advanced") - private String scriptLanguage; - @XmlElement(name = "constructors") - @XmlJavaTypeAdapter(BeanConstructorsAdapter.class) - private Map<Integer, Object> constructors; - @XmlElement(name = "properties") - @XmlJavaTypeAdapter(BeanPropertiesAdapter.class) - private Map<String, Object> properties; - @XmlElement(name = "script") - @Metadata(label = "advanced") - private String script; - - public String getName() { - return name; - } - - /** - * The name of the bean (bean id) - */ - public void setName(String name) { - this.name = name; - } - - public String getType() { - return type; - } - - /** - * The class name (fully qualified) of the bean - */ - public void setType(String type) { - this.type = type; - } - - public String getInitMethod() { - return initMethod; - } - - /** - * The name of the custom initialization method to invoke after setting bean properties. The method must have no - * arguments, but may throw any exception. - */ - public void setInitMethod(String initMethod) { - this.initMethod = initMethod; - } - - public String getDestroyMethod() { - return destroyMethod; - } - - /** - * The name of the custom destroy method to invoke on bean shutdown, such as when Camel is shutting down. The method - * must have no arguments, but may throw any exception. - */ - public void setDestroyMethod(String destroyMethod) { - this.destroyMethod = destroyMethod; - } - - public String getFactoryMethod() { - return factoryMethod; - } - - /** - * Name of method to invoke when creating the bean via a factory bean. - */ - public void setFactoryMethod(String factoryMethod) { - this.factoryMethod = factoryMethod; - } - - public String getFactoryBean() { - return factoryBean; - } - - /** - * Name of factory bean (bean id) to use for creating the bean. - */ - public void setFactoryBean(String factoryBean) { - this.factoryBean = factoryBean; - } - - public String getBuilderClass() { - return builderClass; - } - - /** - * Fully qualified class name of builder class to use for creating and configuring the bean. The builder will use - * the properties values to configure the bean. - */ - public void setBuilderClass(String builderClass) { - this.builderClass = builderClass; - } - - public String getBuilderMethod() { - return builderMethod; - } - - /** - * Name of method when using builder class. This method is invoked after configuring to create the actual bean. This - * method is often named build (used by default). - */ - public void setBuilderMethod(String builderMethod) { - this.builderMethod = builderMethod; - } - - public Map<Integer, Object> getConstructors() { - return constructors; - } - - /** - * Optional constructor arguments for creating the bean. Arguments correspond to specific index of the constructor - * argument list, starting from zero. - */ - public void setConstructors(Map<Integer, Object> constructors) { - this.constructors = constructors; - } - - public Map<String, Object> getProperties() { - return properties; - } - - /** - * Optional properties to set on the created bean. - */ - public void setProperties(Map<String, Object> properties) { - this.properties = properties; - } - - public String getScriptLanguage() { - return scriptLanguage; - } - - /** - * The script language to use when using inlined script for creating the bean, such as groovy, java, javascript etc. - */ - public void setScriptLanguage(String scriptLanguage) { - this.scriptLanguage = scriptLanguage; - } - - /** - * The script to execute that creates the bean when using scripting languages. - * - * If the script use the prefix <tt>resource:</tt> such as <tt>resource:classpath:com/foo/myscript.groovy</tt>, - * <tt>resource:file:/var/myscript.groovy</tt>, then its loaded from the external resource. - */ - public void setScript(String script) { - this.script = script; - } - - public String getScript() { - return script; - } - - @Override - public Resource getResource() { - return resource; - } +public class RegistryBeanDefinition extends BeanFactoryDefinition<RegistryBeanDefinition, RegistryBeanDefinition> { - @Override - public void setResource(Resource resource) { - this.resource = resource; - } } diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java index 3e5cc2f16a1..d3463869c5d 100644 --- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java +++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java @@ -1349,23 +1349,7 @@ public class ModelParser extends BaseParser { }, noElementHandler(), noValueHandler()); } protected RegistryBeanDefinition doParseRegistryBeanDefinition() throws IOException, XmlPullParserException { - return doParse(new RegistryBeanDefinition(), (def, key, val) -> switch (key) { - case "builderClass": def.setBuilderClass(val); yield true; - case "builderMethod": def.setBuilderMethod(val); yield true; - case "destroyMethod": def.setDestroyMethod(val); yield true; - case "factoryBean": def.setFactoryBean(val); yield true; - case "factoryMethod": def.setFactoryMethod(val); yield true; - case "initMethod": def.setInitMethod(val); yield true; - case "name": def.setName(val); yield true; - case "scriptLanguage": def.setScriptLanguage(val); yield true; - case "type": def.setType(val); yield true; - default: yield false; - }, (def, key) -> switch (key) { - case "constructors": def.setConstructors(new BeanConstructorsAdapter().unmarshal(doParseBeanConstructorsDefinition())); yield true; - case "properties": def.setProperties(new BeanPropertiesAdapter().unmarshal(doParseBeanPropertiesDefinition())); yield true; - case "script": def.setScript(doParseText()); yield true; - default: yield false; - }, noValueHandler()); + return doParse(new RegistryBeanDefinition(), beanFactoryDefinitionAttributeHandler(), beanFactoryDefinitionElementHandler(), noValueHandler()); } protected RestConfigurationDefinition doParseRestConfigurationDefinition() throws IOException, XmlPullParserException { return doParse(new RestConfigurationDefinition(), (def, key, val) -> switch (key) { diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java index f68bf1a8556..90c30a09cbb 100644 --- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java +++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java @@ -1989,18 +1989,8 @@ public class ModelWriter extends BaseWriter { } protected void doWriteRegistryBeanDefinition(String name, RegistryBeanDefinition def) throws IOException { startElement(name); - doWriteAttribute("factoryMethod", def.getFactoryMethod()); - doWriteAttribute("initMethod", def.getInitMethod()); - doWriteAttribute("scriptLanguage", def.getScriptLanguage()); - doWriteAttribute("builderClass", def.getBuilderClass()); - doWriteAttribute("name", def.getName()); - doWriteAttribute("builderMethod", def.getBuilderMethod()); - doWriteAttribute("destroyMethod", def.getDestroyMethod()); - doWriteAttribute("type", def.getType()); - doWriteAttribute("factoryBean", def.getFactoryBean()); - doWriteElement("constructors", new BeanConstructorsAdapter().marshal(def.getConstructors()), this::doWriteBeanConstructorsDefinition); - doWriteElement("script", def.getScript(), this::doWriteString); - doWriteElement("properties", new BeanPropertiesAdapter().marshal(def.getProperties()), this::doWriteBeanPropertiesDefinition); + doWriteBeanFactoryDefinitionAttributes(def); + doWriteBeanFactoryDefinitionElements(def); endElement(name); } protected void doWriteBlacklistServiceCallServiceFilterConfiguration(String name, BlacklistServiceCallServiceFilterConfiguration def) throws IOException { diff --git a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java index ff76966b22d..b230ecb8b3a 100644 --- a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java +++ b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java @@ -1989,18 +1989,8 @@ public class ModelWriter extends BaseWriter { } protected void doWriteRegistryBeanDefinition(String name, RegistryBeanDefinition def) throws IOException { startElement(name); - doWriteAttribute("factoryMethod", def.getFactoryMethod()); - doWriteAttribute("initMethod", def.getInitMethod()); - doWriteAttribute("scriptLanguage", def.getScriptLanguage()); - doWriteAttribute("builderClass", def.getBuilderClass()); - doWriteAttribute("name", def.getName()); - doWriteAttribute("builderMethod", def.getBuilderMethod()); - doWriteAttribute("destroyMethod", def.getDestroyMethod()); - doWriteAttribute("type", def.getType()); - doWriteAttribute("factoryBean", def.getFactoryBean()); - doWriteElement("constructors", new BeanConstructorsAdapter().marshal(def.getConstructors()), this::doWriteBeanConstructorsDefinition); - doWriteElement("script", def.getScript(), this::doWriteString); - doWriteElement("properties", new BeanPropertiesAdapter().marshal(def.getProperties()), this::doWriteBeanPropertiesDefinition); + doWriteBeanFactoryDefinitionAttributes(def); + doWriteBeanFactoryDefinitionElements(def); endElement(name); } protected void doWriteBlacklistServiceCallServiceFilterConfiguration(String name, BlacklistServiceCallServiceFilterConfiguration def) throws IOException {