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

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

commit 5c97f39a2fc8bbea4b212f60f9f6578d44cc8b9b
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Feb 12 08:29:41 2021 +0100

    [CAMEL-16156] Improve the usage of org.apache.camel.jmx.disabled
    
    Avoid using global system properties and use a thread local instead.  Move 
all the code for disableJmx/noStart/excludeRoutes to DefaultCamelContext
---
 ...omponentCamelContextSchedulerIsolationTest.java | 20 +-----
 .../org/apache/camel/spring/SpringTestSupport.java |  7 +-
 .../apache/camel/spring/SpringCamelContext.java    | 54 ++-------------
 .../apache/camel/test/junit5/CamelTestSupport.java |  5 +-
 .../spring/junit5/CamelAnnotationsHandler.java     | 50 +++++--------
 .../camel/test/spring/CamelAnnotationsHandler.java | 50 +++++--------
 .../apache/camel/test/junit4/CamelTestSupport.java |  5 +-
 .../org/apache/camel/impl/DefaultCamelContext.java | 81 ++++++++++++++++++++++
 8 files changed, 127 insertions(+), 145 deletions(-)

diff --git 
a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzComponentCamelContextSchedulerIsolationTest.java
 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzComponentCamelContextSchedulerIsolationTest.java
index e3ebc5b..97b38e2 100644
--- 
a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzComponentCamelContextSchedulerIsolationTest.java
+++ 
b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzComponentCamelContextSchedulerIsolationTest.java
@@ -17,7 +17,6 @@
 package org.apache.camel.component.quartz;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Test;
@@ -31,18 +30,17 @@ public class 
QuartzComponentCamelContextSchedulerIsolationTest {
 
     @AfterAll
     public static void afterTests() {
-        System.clearProperty(JmxSystemPropertyKeys.DISABLED);
     }
 
     @Test
     public void testSchedulerIsolationUnmanaged() throws Exception {
-        disableJMX();
+        DefaultCamelContext.setDisableJmx(true);
         testSchedulerIsolation();
     }
 
     @Test
     public void testSchedulerIsolationManaged() throws Exception {
-        enableJMX();
+        DefaultCamelContext.setDisableJmx(false);
         testSchedulerIsolation();
     }
 
@@ -78,18 +76,4 @@ public class 
QuartzComponentCamelContextSchedulerIsolationTest {
         return getQuartzComponent(context).getScheduler();
     }
 
-    /**
-     * Disables the JMX agent.
-     */
-    private void disableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
-    }
-
-    /**
-     * Enables the JMX agent.
-     */
-    private void enableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "false");
-    }
-
 }
diff --git 
a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/SpringTestSupport.java
 
b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/SpringTestSupport.java
index 6f20929..0eb9e42 100644
--- 
a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/SpringTestSupport.java
+++ 
b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/SpringTestSupport.java
@@ -25,7 +25,7 @@ import java.util.StringJoiner;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.engine.DefaultPackageScanClassResolver;
 import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
 import org.apache.camel.impl.scan.InvertingPackageScanFilter;
@@ -49,14 +49,14 @@ public abstract class SpringTestSupport extends 
ContextTestSupport {
     public void setUp() throws Exception {
         // we want SpringTestSupport to startup faster and not use JMX by 
default and should stop seda quicker
         System.setProperty("CamelSedaPollTimeout", "10");
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, 
Boolean.toString(!useJmx()));
+        DefaultCamelContext.setDisableJmx(!useJmx());
         Class<?>[] excluded = excludeRoutes();
         if (excluded != null && excluded.length > 0) {
             StringJoiner excludedRoutes = new StringJoiner(",");
             for (Class<?> clazz : excluded) {
                 excludedRoutes.add(clazz.getName());
             }
-            System.setProperty(SpringCamelContext.EXCLUDE_ROUTES, 
excludedRoutes.toString());
+            DefaultCamelContext.setExcludeRoutes(excludedRoutes.toString());
         }
 
         applicationContext = createApplicationContext();
@@ -69,6 +69,7 @@ public abstract class SpringTestSupport extends 
ContextTestSupport {
     public void tearDown() throws Exception {
         super.tearDown();
         IOHelper.close(applicationContext);
+        DefaultCamelContext.clearOptions();
     }
 
     private static class ExcludingPackageScanClassResolver extends 
DefaultPackageScanClassResolver {
diff --git 
a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
 
b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
index 938f801..b7f4510 100644
--- 
a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
+++ 
b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java
@@ -16,25 +16,19 @@
  */
 package org.apache.camel.spring;
 
-import java.util.HashSet;
 import java.util.Optional;
-import java.util.Set;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.Processor;
 import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.component.event.EventComponent;
 import org.apache.camel.component.event.EventEndpoint;
 import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
-import org.apache.camel.impl.scan.InvertingPackageScanFilter;
 import org.apache.camel.spi.BeanProcessorFactory;
 import org.apache.camel.spi.BeanRepository;
 import org.apache.camel.spi.Injector;
 import org.apache.camel.spi.ModelJAXBContextFactory;
-import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spring.spi.ApplicationContextBeanRepository;
 import org.apache.camel.spring.spi.SpringInjector;
@@ -42,8 +36,6 @@ import 
org.apache.camel.spring.spi.SpringManagementMBeanAssembler;
 import org.apache.camel.support.DefaultRegistry;
 import org.apache.camel.support.ProcessorEndpoint;
 import org.apache.camel.support.ResolverHelper;
-import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.StopWatch;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeansException;
@@ -72,16 +64,12 @@ public class SpringCamelContext extends DefaultCamelContext
     public static final String EXCLUDE_ROUTES = "CamelTestSpringExcludeRoutes";
 
     private static final Logger LOG = 
LoggerFactory.getLogger(SpringCamelContext.class);
-    private static final ThreadLocal<Boolean> NO_START = new ThreadLocal<>();
     private ApplicationContext applicationContext;
     private EventComponent eventComponent;
     private boolean shutdownEager = true;
 
     public SpringCamelContext() {
         super(false);
-        if (Boolean.getBoolean(JmxSystemPropertyKeys.DISABLED)) {
-            disableJMX();
-        }
         setManagementMBeanAssembler(new SpringManagementMBeanAssembler(this));
     }
 
@@ -90,14 +78,6 @@ public class SpringCamelContext extends DefaultCamelContext
         setApplicationContext(applicationContext);
     }
 
-    public static void setNoStart(boolean b) {
-        if (b) {
-            NO_START.set(true);
-        } else {
-            NO_START.set(null);
-        }
-    }
-
     /**
      * @deprecated its better to create and boot Spring the standard Spring 
way and to get hold of CamelContext using
      *             the Spring API.
@@ -123,23 +103,10 @@ public class SpringCamelContext extends 
DefaultCamelContext
 
     @Override
     public void start() {
-        // for example from unit testing we want to start Camel later 
(manually)
-        if (Boolean.TRUE.equals(NO_START.get())) {
-            LOG.trace("Ignoring start() as NO_START is false");
-            return;
-        }
-
-        if (!isStarted() && !isStarting()) {
-            try {
-                StopWatch watch = new StopWatch();
-                super.start();
-                LOG.debug("start() took {} millis", watch.taken());
-            } catch (Exception e) {
-                throw wrapRuntimeCamelException(e);
-            }
-        } else {
-            // ignore as Camel is already started
-            LOG.trace("Ignoring start() as Camel is already started");
+        try {
+            super.start();
+        } catch (Exception e) {
+            throw wrapRuntimeCamelException(e);
         }
     }
 
@@ -329,17 +296,4 @@ public class SpringCamelContext extends DefaultCamelContext
         return !isStopping() && !isStopped();
     }
 
-    protected PackageScanClassResolver createPackageScanClassResolver() {
-        PackageScanClassResolver resolver = 
super.createPackageScanClassResolver();
-        String excluded = System.getProperty(EXCLUDE_ROUTES);
-        if (ObjectHelper.isNotEmpty(excluded)) {
-            Set<Class<?>> excludedClasses = new HashSet<>();
-            for (String str : excluded.split(",")) {
-                excludedClasses.add(getClassResolver().resolveClass(str));
-            }
-            resolver.addFilter(new InvertingPackageScanFilter(new 
AssignableToPackageScanFilter(excludedClasses)));
-        }
-        return resolver;
-    }
-
 }
diff --git 
a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
 
b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
index 16604ab..258ecb2 100644
--- 
a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
+++ 
b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
@@ -40,7 +40,6 @@ import org.apache.camel.RoutesBuilder;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.Service;
 import org.apache.camel.ServiceStatus;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
 import org.apache.camel.builder.AdviceWith;
@@ -983,14 +982,14 @@ public abstract class CamelTestSupport
      * Disables the JMX agent. Must be called before the {@link #setUp()} 
method.
      */
     protected void disableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+        DefaultCamelContext.setDisableJmx(true);
     }
 
     /**
      * Enables the JMX agent. Must be called before the {@link #setUp()} 
method.
      */
     protected void enableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "false");
+        DefaultCamelContext.setDisableJmx(false);
     }
 
     /**
diff --git 
a/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelAnnotationsHandler.java
 
b/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelAnnotationsHandler.java
index e0a8583..f11f331 100644
--- 
a/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelAnnotationsHandler.java
+++ 
b/components/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelAnnotationsHandler.java
@@ -27,10 +27,10 @@ import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 
 import org.apache.camel.ExtendedCamelContext;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
 import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.debugger.DefaultDebugger;
 import org.apache.camel.spi.Breakpoint;
 import org.apache.camel.spi.Debugger;
@@ -57,20 +57,7 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void cleanup(Class<?> testClass) {
-        SpringCamelContext.setNoStart(false);
-
-        if (CamelSpringTestHelper.getOriginalJmxDisabled() == null) {
-            System.clearProperty(JmxSystemPropertyKeys.DISABLED);
-        } else {
-            System.setProperty(JmxSystemPropertyKeys.DISABLED,
-                    CamelSpringTestHelper.getOriginalJmxDisabled());
-        }
-        if (CamelSpringTestHelper.getOriginalExcludeRoutes() == null) {
-            System.clearProperty(SpringCamelContext.EXCLUDE_ROUTES);
-        } else {
-            System.setProperty(SpringCamelContext.EXCLUDE_ROUTES,
-                    CamelSpringTestHelper.getOriginalExcludeRoutes());
-        }
+        DefaultCamelContext.clearOptions();
     }
 
     /**
@@ -80,25 +67,22 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void handleExcludeRoutes(ConfigurableApplicationContext 
context, Class<?> testClass) {
-        
CamelSpringTestHelper.setOriginalExcludeRoutesValue(System.getProperty(SpringCamelContext.EXCLUDE_ROUTES));
+        String key = SpringCamelContext.EXCLUDE_ROUTES;
+        String exists = System.getProperty(key);
+        if (exists != null) {
+            LOGGER.warn("The JVM property " + key + " is set, but not 
supported anymore.");
+        }
 
         if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
-            Class[] routes = 
testClass.getAnnotation(ExcludeRoutes.class).value();
+            Class<?>[] routes = 
testClass.getAnnotation(ExcludeRoutes.class).value();
             // need to setup this as a JVM system property
             StringJoiner routesBuilder = new StringJoiner(",");
-            for (Class clazz : routes) {
+            for (Class<?> clazz : routes) {
                 routesBuilder.add(clazz.getName());
             }
-            String key = SpringCamelContext.EXCLUDE_ROUTES;
             String value = routesBuilder.toString();
-
-            String exists = System.getProperty(key);
-            if (exists != null) {
-                LOGGER.warn("Cannot use @ExcludeRoutes as JVM property " + key 
+ " has already been set.");
-            } else {
-                LOGGER.info("@ExcludeRoutes annotation found. Setting up JVM 
property {}={}", key, value);
-                System.setProperty(key, value);
-            }
+            LOGGER.info("@ExcludeRoutes annotation found. Setting up JVM 
property {}={}", key, value);
+            DefaultCamelContext.setExcludeRoutes(value);
         }
     }
 
@@ -109,29 +93,27 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void handleDisableJmx(ConfigurableApplicationContext 
context, Class<?> testClass) {
-        
CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED));
-
         if (testClass.isAnnotationPresent(DisableJmx.class)) {
             if (testClass.getAnnotation(DisableJmx.class).value()) {
                 LOGGER.info("Disabling Camel JMX globally as DisableJmx 
annotation was found and disableJmx is set to true.");
-                System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+                DefaultCamelContext.setDisableJmx(true);
             } else {
                 LOGGER.info("Enabling Camel JMX as DisableJmx annotation was 
found and disableJmx is set to false.");
-                System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+                DefaultCamelContext.setDisableJmx(false);
             }
         } else if (!testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
             // route coverage need JMX so do not disable it by default
             LOGGER.info(
                     "Disabling Camel JMX globally for tests by default.  Use 
the DisableJMX annotation to override the default setting.");
-            System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+            DefaultCamelContext.setDisableJmx(true);
         } else {
             LOGGER.info("Enabling Camel JMX as EnableRouteCoverage is used.");
-            System.setProperty(JmxSystemPropertyKeys.DISABLED, "false");
+            DefaultCamelContext.setDisableJmx(false);
         }
     }
 
     /**
-     * Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
+     * Handles disabling of JMX on Camel contexts based on {@link 
EnableRouteCoverage}.
      *
      * @param context   the initialized Spring context
      * @param testClass the test class being executed
diff --git 
a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
 
b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
index 246da12..9be399b 100644
--- 
a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
+++ 
b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
@@ -27,10 +27,10 @@ import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 
 import org.apache.camel.ExtendedCamelContext;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
 import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.debugger.DefaultDebugger;
 import org.apache.camel.spi.Breakpoint;
 import org.apache.camel.spi.Debugger;
@@ -57,20 +57,7 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void cleanup(Class<?> testClass) {
-        SpringCamelContext.setNoStart(false);
-
-        if (CamelSpringTestHelper.getOriginalJmxDisabled() == null) {
-            System.clearProperty(JmxSystemPropertyKeys.DISABLED);
-        } else {
-            System.setProperty(JmxSystemPropertyKeys.DISABLED,
-                    CamelSpringTestHelper.getOriginalJmxDisabled());
-        }
-        if (CamelSpringTestHelper.getOriginalExcludeRoutes() == null) {
-            System.clearProperty(SpringCamelContext.EXCLUDE_ROUTES);
-        } else {
-            System.setProperty(SpringCamelContext.EXCLUDE_ROUTES,
-                    CamelSpringTestHelper.getOriginalExcludeRoutes());
-        }
+        DefaultCamelContext.clearOptions();
     }
 
     /**
@@ -80,25 +67,22 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void handleExcludeRoutes(ConfigurableApplicationContext 
context, Class<?> testClass) {
-        
CamelSpringTestHelper.setOriginalExcludeRoutesValue(System.getProperty(SpringCamelContext.EXCLUDE_ROUTES));
+        String key = SpringCamelContext.EXCLUDE_ROUTES;
+        String exists = System.getProperty(key);
+        if (exists != null) {
+            LOGGER.warn("The JVM property " + key + " is set, but not 
supported anymore.");
+        }
 
         if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
-            Class[] routes = 
testClass.getAnnotation(ExcludeRoutes.class).value();
+            Class<?>[] routes = 
testClass.getAnnotation(ExcludeRoutes.class).value();
             // need to setup this as a JVM system property
             StringJoiner routesBuilder = new StringJoiner(",");
-            for (Class clazz : routes) {
+            for (Class<?> clazz : routes) {
                 routesBuilder.add(clazz.getName());
             }
-            String key = SpringCamelContext.EXCLUDE_ROUTES;
             String value = routesBuilder.toString();
-
-            String exists = System.getProperty(key);
-            if (exists != null) {
-                LOGGER.warn("Cannot use @ExcludeRoutes as JVM property " + key 
+ " has already been set.");
-            } else {
-                LOGGER.info("@ExcludeRoutes annotation found. Setting up JVM 
property {}={}", key, value);
-                System.setProperty(key, value);
-            }
+            LOGGER.info("@ExcludeRoutes annotation found. Setting up JVM 
property {}={}", key, value);
+            DefaultCamelContext.setExcludeRoutes(value);
         }
     }
 
@@ -109,29 +93,27 @@ public final class CamelAnnotationsHandler {
      * @param testClass the test class being executed
      */
     public static void handleDisableJmx(ConfigurableApplicationContext 
context, Class<?> testClass) {
-        
CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED));
-
         if (testClass.isAnnotationPresent(DisableJmx.class)) {
             if (testClass.getAnnotation(DisableJmx.class).value()) {
                 LOGGER.info("Disabling Camel JMX globally as DisableJmx 
annotation was found and disableJmx is set to true.");
-                System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+                DefaultCamelContext.setDisableJmx(true);
             } else {
                 LOGGER.info("Enabling Camel JMX as DisableJmx annotation was 
found and disableJmx is set to false.");
-                System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+                DefaultCamelContext.setDisableJmx(false);
             }
         } else if (!testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
             // route coverage need JMX so do not disable it by default
             LOGGER.info(
                     "Disabling Camel JMX globally for tests by default.  Use 
the DisableJMX annotation to override the default setting.");
-            System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+            DefaultCamelContext.setDisableJmx(true);
         } else {
             LOGGER.info("Enabling Camel JMX as EnableRouteCoverage is used.");
-            System.setProperty(JmxSystemPropertyKeys.DISABLED, "false");
+            DefaultCamelContext.setDisableJmx(false);
         }
     }
 
     /**
-     * Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
+     * Handles disabling of JMX on Camel contexts based on {@link 
EnableRouteCoverage}.
      *
      * @param context   the initialized Spring context
      * @param testClass the test class being executed
diff --git 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
index c462024..0f462de 100644
--- 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
+++ 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
@@ -60,7 +60,6 @@ import org.apache.camel.RoutesBuilder;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.Service;
 import org.apache.camel.ServiceStatus;
-import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.api.management.ManagedCamelContext;
 import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
 import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
@@ -1094,14 +1093,14 @@ public abstract class CamelTestSupport extends 
TestSupport {
      * Disables the JMX agent. Must be called before the {@link #setUp()} 
method.
      */
     protected void disableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
+        DefaultCamelContext.setDisableJmx(true);
     }
 
     /**
      * Enables the JMX agent. Must be called before the {@link #setUp()} 
method.
      */
     protected void enableJMX() {
-        System.setProperty(JmxSystemPropertyKeys.DISABLED, "false");
+        DefaultCamelContext.setDisableJmx(false);
     }
 
     /**
diff --git 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index 9916e9f..d44192c 100644
--- 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -17,9 +17,11 @@
 package org.apache.camel.impl;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 import java.util.function.Function;
 
 import org.apache.camel.CamelContext;
@@ -31,6 +33,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.StartupStep;
 import org.apache.camel.ValueHolder;
+import org.apache.camel.api.management.JmxSystemPropertyKeys;
 import org.apache.camel.builder.AdviceWith;
 import org.apache.camel.builder.AdviceWithRouteBuilder;
 import org.apache.camel.impl.engine.DefaultExecutorServiceManager;
@@ -38,6 +41,8 @@ import org.apache.camel.impl.engine.RouteService;
 import org.apache.camel.impl.engine.SimpleCamelContext;
 import org.apache.camel.impl.engine.TransformerKey;
 import org.apache.camel.impl.engine.ValidatorKey;
+import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
+import org.apache.camel.impl.scan.InvertingPackageScanFilter;
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.model.FaultToleranceConfigurationDefinition;
 import org.apache.camel.model.HystrixConfigurationDefinition;
@@ -60,6 +65,7 @@ import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.DataType;
 import org.apache.camel.spi.ExecutorServiceManager;
 import org.apache.camel.spi.ModelReifierFactory;
+import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.StartupStepRecorder;
@@ -68,6 +74,7 @@ import org.apache.camel.spi.Validator;
 import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultRegistry;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StopWatch;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,6 +84,7 @@ import org.slf4j.LoggerFactory;
 public class DefaultCamelContext extends SimpleCamelContext implements 
ModelCamelContext {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(DefaultCamelContext.class);
+    protected static final ThreadLocal<OptionHolder> OPTIONS = 
ThreadLocal.withInitial(OptionHolder::new);
 
     private Model model = new DefaultModel(this);
 
@@ -111,6 +119,73 @@ public class DefaultCamelContext extends 
SimpleCamelContext implements ModelCame
 
     public DefaultCamelContext(boolean init) {
         super(init);
+        if (isDisableJmx()) {
+            disableJMX();
+        }
+    }
+
+    public static void setNoStart(boolean b) {
+        getOptions().noStart = b;
+    }
+
+    public static boolean isNoStart() {
+        return getOptions().noStart;
+    }
+
+    public static void setDisableJmx(boolean b) {
+        getOptions().disableJmx = b;
+    }
+
+    public static boolean isDisableJmx() {
+        return getOptions().disableJmx;
+    }
+
+    public static String getExcludeRoutes() {
+        return getOptions().excludeRoutes;
+    }
+
+    public static void setExcludeRoutes(String s) {
+        getOptions().excludeRoutes = s;
+    }
+
+    public static void clearOptions() {
+        OPTIONS.set(new OptionHolder());
+    }
+
+    private static OptionHolder getOptions() {
+        return OPTIONS.get();
+    }
+
+    @Override
+    public void start() {
+        // for example from unit testing we want to start Camel later 
(manually)
+        if (isNoStart()) {
+            LOG.trace("Ignoring start() as NO_START is false");
+            return;
+        }
+
+        if (!isStarted() && !isStarting()) {
+            StopWatch watch = new StopWatch();
+            super.start();
+            LOG.debug("start() took {} millis", watch.taken());
+        } else {
+            // ignore as Camel is already started
+            LOG.trace("Ignoring start() as Camel is already started");
+        }
+    }
+
+    @Override
+    protected PackageScanClassResolver createPackageScanClassResolver() {
+        PackageScanClassResolver resolver = 
super.createPackageScanClassResolver();
+        String excluded = getExcludeRoutes();
+        if (ObjectHelper.isNotEmpty(excluded)) {
+            Set<Class<?>> excludedClasses = new HashSet<>();
+            for (String str : excluded.split(",")) {
+                excludedClasses.add(getClassResolver().resolveClass(str));
+            }
+            resolver.addFilter(new InvertingPackageScanFilter(new 
AssignableToPackageScanFilter(excludedClasses)));
+        }
+        return resolver;
     }
 
     @Override
@@ -681,4 +756,10 @@ public class DefaultCamelContext extends 
SimpleCamelContext implements ModelCame
                 ? new TransformerKey(def.getScheme())
                 : new TransformerKey(new DataType(def.getFromType()), new 
DataType(def.getToType()));
     }
+
+    protected static class OptionHolder {
+        public boolean noStart;
+        public boolean disableJmx = 
Boolean.getBoolean(JmxSystemPropertyKeys.DISABLED);
+        public String excludeRoutes;
+    }
 }

Reply via email to