Skip to content

Commit

Permalink
CAMEL-20514: camel-model - Add support for bean constructors for bean…
Browse files Browse the repository at this point in the history
…s in route templates or kamelets (#14040)
  • Loading branch information
davsclaus authored May 2, 2024
1 parent 9585e55 commit 80ac25e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,208 +16,19 @@
*/
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.
*/
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1349,23 +1349,7 @@ protected ComponentScanDefinition doParseComponentScanDefinition() throws IOExce
}, 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1989,18 +1989,8 @@ protected void doWriteComponentScanDefinition(String name, ComponentScanDefiniti
}
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1989,18 +1989,8 @@ protected void doWriteComponentScanDefinition(String name, ComponentScanDefiniti
}
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 {
Expand Down

0 comments on commit 80ac25e

Please sign in to comment.