Skip to content

Commit

Permalink
CAMEL-13283: Add @BindRegistry annotation to allow to bind beans/clas…
Browse files Browse the repository at this point in the history
…ses to registry.
  • Loading branch information
davsclaus committed Mar 6, 2019
1 parent 31ed633 commit 2073b04
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
Expand Up @@ -34,7 +34,7 @@ public class Main extends MainSupport {
public Main() { public Main() {
} }


public Main(Class configurationClass) { public Main(Class... configurationClass) {
super(configurationClass); super(configurationClass);
} }


Expand Down
Expand Up @@ -79,7 +79,7 @@ public abstract class MainSupport extends ServiceSupport {


protected CamelContext camelContext; protected CamelContext camelContext;
protected List<RouteBuilder> routeBuilders = new ArrayList<>(); protected List<RouteBuilder> routeBuilders = new ArrayList<>();
protected Class configurationClass; protected List<Class> configurationClasses;
protected String routeBuilderClasses; protected String routeBuilderClasses;
protected String fileWatchDirectory; protected String fileWatchDirectory;
protected boolean fileWatchDirectoryRecursively; protected boolean fileWatchDirectoryRecursively;
Expand Down Expand Up @@ -112,9 +112,9 @@ public void run() {
} }
} }


protected MainSupport(Class configurationClass) { protected MainSupport(Class... configurationClasses) {
this(); this();
this.configurationClass = configurationClass; addConfigurationClass(configurationClasses);
} }


protected MainSupport() { protected MainSupport() {
Expand Down Expand Up @@ -429,16 +429,23 @@ public int getExitCode() {
return exitCode.get(); return exitCode.get();
} }


public Class getConfigurationClass() { public List<Class> getConfigurationClasses() {
return configurationClass; return configurationClasses;
} }


/** /**
* Sets optional configuration class which allows to do any initial configuration. * Sets optional configuration classes which allows to do any initial configuration.
* The class can/should have a method named <tt>configure</tt> which is called. * The class can/should have a method named <tt>configure</tt> which is called.
*/ */
public void setConfigurationClass(Class configurationClass) { public void setConfigurationClasses(List<Class> configurationClasses) {
this.configurationClass = configurationClass; this.configurationClasses = configurationClasses;
}

public void addConfigurationClass(Class... configurationClasses) {
if (this.configurationClasses == null) {
this.configurationClasses = new ArrayList<>();
}
this.configurationClasses.addAll(Arrays.asList(configurationClasses));
} }


public String getRouteBuilderClasses() { public String getRouteBuilderClasses() {
Expand Down Expand Up @@ -517,6 +524,11 @@ public boolean isAutoConfigurationEnabled() {
* - camel.language.name.option2=value2 * - camel.language.name.option2=value2
* Where name is the name of the component, dataformat or language such as seda,direct,jaxb. * Where name is the name of the component, dataformat or language such as seda,direct,jaxb.
* <p/> * <p/>
* The auto configuration also works for any options on components
* that is a complex type (not standard Java type) and there has been an explicit single
* bean instance registered to the Camel registry via the {@link org.apache.camel.spi.Registry#bind(String, Object)} method
* or by using the {@link org.apache.camel.BindToRegistry} annotation style.
* <p/>
* This option is default enabled. * This option is default enabled.
*/ */
public void setAutoConfigurationEnabled(boolean autoConfigurationEnabled) { public void setAutoConfigurationEnabled(boolean autoConfigurationEnabled) {
Expand Down Expand Up @@ -691,14 +703,16 @@ protected void postProcessCamelContext(CamelContext camelContext) throws Excepti
camelContext.getManagementStrategy().addEventNotifier(notifier); camelContext.getManagementStrategy().addEventNotifier(notifier);
} }


if (configurationClass != null) { if (configurationClasses != null) {
// create instance of configuration class as it may do dependency injection and bind to registry for (Class clazz : configurationClasses) {
Object config = camelContext.getInjector().newInstance(configurationClass); // create instance of configuration class as it may do dependency injection and bind to registry
// invoke configure method if exists Object config = camelContext.getInjector().newInstance(clazz);
Method method = findMethod(configurationClass, "configure"); // invoke configure method if exists
if (method != null) { Method method = findMethod(clazz, "configure");
log.info("Calling configure method on configuration class: {}", configurationClass); if (method != null) {
invokeMethod(method, config); log.info("Calling configure method on configuration class: {}", clazz.getName());
invokeMethod(method, config);
}
} }
} }


Expand Down Expand Up @@ -841,12 +855,18 @@ public void addRouteBuilder(RouteBuilder routeBuilder) {
getRouteBuilders().add(routeBuilder); getRouteBuilders().add(routeBuilder);
} }


public void addRouteBuilder(Class routeBuilder) { public void addRouteBuilder(Class... routeBuilder) {
String existing = routeBuilderClasses; String existing = routeBuilderClasses;
if (existing != null) { if (existing == null) {
existing = existing + "," + routeBuilder.getName(); existing = "";
} else { }
existing = routeBuilder.getName(); if (routeBuilder != null) {
for (Class clazz : routeBuilder) {
if (!existing.isEmpty()) {
existing = existing + ",";
}
existing = existing + clazz.getName();
}
} }
setRouteBuilderClasses(existing); setRouteBuilderClasses(existing);
} }
Expand Down
Expand Up @@ -34,7 +34,9 @@ public class MainIoCTest extends Assert {
@Test @Test
public void testMainIoC() throws Exception { public void testMainIoC() throws Exception {
// use configuration class // use configuration class
Main main = new Main(MyConfiguration.class); Main main = new Main();
// add the configuration
main.addConfigurationClass(MyConfiguration.class);
// add as class so we get IoC // add as class so we get IoC
main.addRouteBuilder(MyRouteBuilder.class); main.addRouteBuilder(MyRouteBuilder.class);
main.start(); main.start();
Expand Down
Expand Up @@ -29,10 +29,10 @@ private MyApplication() {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// use Camels Main class // use Camels Main class
Main main = new Main(); Main main = new Main();
// lets use a configuration class // lets use a configuration class (you can specify multiple classes)
// properties are automatic loaded from application.properties // (properties are automatic loaded from application.properties)
main.setConfigurationClass(MyConfiguration.class); main.addConfigurationClass(MyConfiguration.class);
// and add the routes // and add the routes (you can specify multiple classes)
main.addRouteBuilder(MyRouteBuilder.class); main.addRouteBuilder(MyRouteBuilder.class);
// now keep the application running until the JVM is terminated (ctrl + c or sigterm) // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
main.run(args); main.run(args);
Expand Down

0 comments on commit 2073b04

Please sign in to comment.