You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/06/12 20:40:30 UTC

[camel] 02/05: (chores) camel-base-engine: cleanup duplicated code in LifecycleStrategySupport

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 7439d9fd063315c895dd1391b5abbb4e1fc698ec
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Jun 12 13:57:08 2023 +0200

    (chores) camel-base-engine: cleanup duplicated code in LifecycleStrategySupport
---
 .../engine/DefaultAutowiredLifecycleStrategy.java  | 37 ++-------------------
 .../camel/main/MainAutowiredLifecycleStrategy.java | 34 +------------------
 .../camel/support/LifecycleStrategySupport.java    | 38 ++++++++++++++++++++++
 3 files changed, 42 insertions(+), 67 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
index df01623e0e9..157c3c89926 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
@@ -16,18 +16,13 @@
  */
 package org.apache.camel.impl.engine;
 
-import java.util.Set;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.Ordered;
 import org.apache.camel.spi.AutowiredLifecycleStrategy;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.Language;
-import org.apache.camel.spi.PropertyConfigurer;
-import org.apache.camel.spi.PropertyConfigurerGetter;
 import org.apache.camel.support.LifecycleStrategySupport;
-import org.apache.camel.support.PluginHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -87,35 +82,9 @@ class DefaultAutowiredLifecycleStrategy extends LifecycleStrategySupport impleme
     }
 
     private void autwire(String name, String kind, Object target) {
-        PropertyConfigurer pc = PluginHelper.getConfigurerResolver(camelContext)
-                .resolvePropertyConfigurer(name + "-" + kind, camelContext);
-        if (pc instanceof PropertyConfigurerGetter) {
-            PropertyConfigurerGetter getter = (PropertyConfigurerGetter) pc;
-            String[] names = getter.getAutowiredNames();
-            if (names != null) {
-                for (String option : names) {
-                    // is there already a configured value?
-                    Object value = getter.getOptionValue(target, option, true);
-                    if (value == null) {
-                        Class<?> type = getter.getOptionType(option, true);
-                        if (type != null) {
-                            Set<?> set = camelContext.getRegistry().findByType(type);
-                            if (set.size() == 1) {
-                                value = set.iterator().next();
-                            }
-                        }
-                        if (value != null) {
-                            boolean hit = pc.configure(camelContext, target, option, value, true);
-                            if (hit) {
-                                LOG.info(
-                                        "Autowired property: {} on {}: {} as exactly one instance of type: {} ({}) found in the registry",
-                                        option, kind, name, type.getName(), value.getClass().getName());
-                            }
-                        }
-                    }
-                }
-            }
-        }
+        doAutoWire(name, kind, target, camelContext);
     }
 
+
+
 }
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java b/core/camel-main/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
index 0c83365531e..1c969910bb5 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
@@ -18,7 +18,6 @@ package org.apache.camel.main;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
@@ -27,10 +26,7 @@ import org.apache.camel.VetoCamelContextStartException;
 import org.apache.camel.spi.AutowiredLifecycleStrategy;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.Language;
-import org.apache.camel.spi.PropertyConfigurer;
-import org.apache.camel.spi.PropertyConfigurerGetter;
 import org.apache.camel.support.LifecycleStrategySupport;
-import org.apache.camel.support.PluginHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -129,35 +125,7 @@ public class MainAutowiredLifecycleStrategy extends LifecycleStrategySupport imp
     }
 
     private void autwire(String name, String kind, Object target) {
-        PropertyConfigurer pc = PluginHelper.getConfigurerResolver(camelContext)
-                .resolvePropertyConfigurer(name + "-" + kind, camelContext);
-        if (pc instanceof PropertyConfigurerGetter) {
-            PropertyConfigurerGetter getter = (PropertyConfigurerGetter) pc;
-            String[] names = getter.getAutowiredNames();
-            if (names != null) {
-                for (String option : names) {
-                    // is there already a configured value?
-                    Object value = getter.getOptionValue(target, option, true);
-                    if (value == null) {
-                        Class<?> type = getter.getOptionType(option, true);
-                        if (type != null) {
-                            Set<?> set = camelContext.getRegistry().findByType(type);
-                            if (set.size() == 1) {
-                                value = set.iterator().next();
-                            }
-                        }
-                        if (value != null) {
-                            boolean hit = pc.configure(camelContext, target, option, value, true);
-                            if (hit) {
-                                LOG.info(
-                                        "Autowired property: {} on {}: {} as exactly one instance of type: {} ({}) found in the registry",
-                                        option, kind, name, type.getName(), value.getClass().getName());
-                            }
-                        }
-                    }
-                }
-            }
-        }
+        doAutoWire(name, kind, target, camelContext);
     }
 
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
index 7c085e26bd6..e43ed8318a1 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
@@ -17,6 +17,7 @@
 package org.apache.camel.support;
 
 import java.util.Collection;
+import java.util.Set;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.function.Consumer;
 
@@ -34,11 +35,16 @@ import org.apache.camel.spi.OnCamelContextStarted;
 import org.apache.camel.spi.OnCamelContextStarting;
 import org.apache.camel.spi.OnCamelContextStopped;
 import org.apache.camel.spi.OnCamelContextStopping;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerGetter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A useful base class for {@link LifecycleStrategy} implementations.
  */
 public abstract class LifecycleStrategySupport implements LifecycleStrategy {
+    private static final Logger LOG = LoggerFactory.getLogger(LifecycleStrategySupport.class);
 
     // *******************************
     //
@@ -238,4 +244,36 @@ public abstract class LifecycleStrategySupport implements LifecycleStrategy {
     public void onThreadPoolRemove(CamelContext camelContext, ThreadPoolExecutor threadPool) {
         // noop
     }
+
+    protected static void doAutoWire(String name, String kind, Object target, CamelContext camelContext) {
+        PropertyConfigurer pc = PluginHelper.getConfigurerResolver(camelContext)
+                .resolvePropertyConfigurer(name + "-" + kind, camelContext);
+        if (pc instanceof PropertyConfigurerGetter) {
+            PropertyConfigurerGetter getter = (PropertyConfigurerGetter) pc;
+            String[] names = getter.getAutowiredNames();
+            if (names != null) {
+                for (String option : names) {
+                    // is there already a configured value?
+                    Object value = getter.getOptionValue(target, option, true);
+                    if (value == null) {
+                        Class<?> type = getter.getOptionType(option, true);
+                        if (type != null) {
+                            Set<?> set = camelContext.getRegistry().findByType(type);
+                            if (set.size() == 1) {
+                                value = set.iterator().next();
+                            }
+                        }
+                        if (value != null) {
+                            boolean hit = pc.configure(camelContext, target, option, value, true);
+                            if (hit) {
+                                LOG.info(
+                                        "Autowired property: {} on {}: {} as exactly one instance of type: {} ({}) found in the registry",
+                                        option, kind, name, type.getName(), value.getClass().getName());
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
 }