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


The following commit(s) were added to refs/heads/master by this push:
     new cd8f5a9  Add a beforeConfigure callback to MainListener (#3524)
cd8f5a9 is described below

commit cd8f5a957d066c7504b94dfad031113cdd9b693c
Author: Luca Burgazzoli <[email protected]>
AuthorDate: Tue Jan 28 04:47:55 2020 +0000

    Add a beforeConfigure callback to MainListener (#3524)
---
 .../org/apache/camel/main/BaseMainSupport.java     |  6 +++
 .../java/org/apache/camel/main/MainListener.java   |  7 ++++
 .../org/apache/camel/main/MainListenerSupport.java |  4 ++
 .../org/apache/camel/main/MainListenerTest.java    | 48 ++++++++++++++++++++++
 .../org/apache/camel/util/CollectionHelper.java    | 18 ++++++++
 5 files changed, 83 insertions(+)

diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java 
b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index d6540f4..3130f4b 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -571,6 +571,12 @@ public abstract class BaseMainSupport extends 
ServiceSupport {
 
     protected void postProcessCamelContext(CamelContext camelContext) throws 
Exception {
         configurePropertiesService(camelContext);
+
+        // allow to do configuration before its started
+        for (MainListener listener : listeners) {
+            listener.beforeConfigure(this);
+        }
+
         configureLifecycle(camelContext);
         autoconfigure(camelContext);
         configureRoutes(camelContext);
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java 
b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
index e2eb7ee..fb6a546 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
@@ -22,6 +22,13 @@ import org.apache.camel.CamelContext;
  * A lifecycle listener to receive callbacks when the Main is started and 
stopped.
  */
 public interface MainListener {
+    /**
+     * Callback invoked after the the CamelContext has been created and before 
the
+     * auto-configured step starts.
+     *
+     * @param main the main instance
+     */
+    void beforeConfigure(BaseMainSupport main);
 
     /**
      * Callback to configure the created CamelContext.
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java 
b/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java
index 03b4fe1..3008c58 100644
--- 
a/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java
+++ 
b/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java
@@ -22,6 +22,10 @@ import org.apache.camel.CamelContext;
  * A useful base class for {@link org.apache.camel.main.MainListener} 
implementations.
  */
 public class MainListenerSupport implements MainListener {
+    @Override
+    public void beforeConfigure(BaseMainSupport main) {
+        // noop
+    }
 
     @Override
     public void configure(CamelContext context) {
diff --git 
a/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java 
b/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java
new file mode 100644
index 0000000..e68059c
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.main;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.apache.camel.util.CollectionHelper.propertiesOf;
+
+public class MainListenerTest extends Assert {
+    @Test
+    public void testBeforeConfigure() {
+        Main main = new Main();
+        try {
+            main.setDefaultPropertyPlaceholderLocation("false");
+            main.setInitialProperties(propertiesOf(
+                "camel.context.name", "my-ctx"
+            ));
+            main.addMainListener(new MainListenerSupport() {
+                @Override
+                public void beforeConfigure(BaseMainSupport main) {
+                    
main.getCamelContext().getPropertiesComponent().setOverrideProperties(propertiesOf(
+                        "camel.context.name", "my-ctx-override"
+                    ));
+                }
+            });
+            main.start();
+
+            assertEquals("my-ctx-override", main.getCamelContext().getName());
+        } finally {
+            main.stop();
+        }
+    }
+}
diff --git 
a/core/camel-util/src/main/java/org/apache/camel/util/CollectionHelper.java 
b/core/camel-util/src/main/java/org/apache/camel/util/CollectionHelper.java
index fa98d59..ef9d5d1 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/CollectionHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/CollectionHelper.java
@@ -27,6 +27,7 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.function.Supplier;
 
@@ -204,4 +205,21 @@ public final class CollectionHelper {
             mapOf(HashMap::new, key, value, keyVals)
         );
     }
+
+    /**
+     * Build a {@link java.util.Properties} from varargs.
+     */
+    public static Properties propertiesOf(String key, String value, String... 
keyVals) {
+        Properties properties = new Properties();
+        properties.setProperty(key, value);
+
+        for (int i = 0; i < keyVals.length; i += 2) {
+            properties.setProperty(
+                keyVals[i],
+                keyVals[i + 1]
+            );
+        }
+
+        return properties;
+    }
 }

Reply via email to