You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "orpiske (via GitHub)" <gi...@apache.org> on 2023/02/23 10:15:36 UTC

[GitHub] [camel] orpiske commented on a diff in pull request #9400: CAMEL-15105: decouple the ExtendedCamelContext from CamelContext

orpiske commented on code in PR #9400:
URL: https://github.com/apache/camel/pull/9400#discussion_r1115472629


##########
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java:
##########
@@ -0,0 +1,1156 @@
+/*
+ * 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.impl.engine;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.Endpoint;
+import org.apache.camel.ErrorHandlerFactory;
+import org.apache.camel.ExchangeConstantProvider;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.Processor;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.Route;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.Service;
+import org.apache.camel.catalog.RuntimeCamelCatalog;
+import org.apache.camel.console.DevConsoleResolver;
+import org.apache.camel.health.HealthCheckResolver;
+import org.apache.camel.spi.AnnotationBasedProcessorFactory;
+import org.apache.camel.spi.AsyncProcessorAwaitManager;
+import org.apache.camel.spi.BeanIntrospection;
+import org.apache.camel.spi.BeanProcessorFactory;
+import org.apache.camel.spi.BeanProxyFactory;
+import org.apache.camel.spi.BootstrapCloseable;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.spi.CamelDependencyInjectionAnnotationFactory;
+import org.apache.camel.spi.CliConnectorFactory;
+import org.apache.camel.spi.ComponentNameResolver;
+import org.apache.camel.spi.ComponentResolver;
+import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.spi.DataFormatResolver;
+import org.apache.camel.spi.Debugger;
+import org.apache.camel.spi.DebuggerFactory;
+import org.apache.camel.spi.DeferServiceFactory;
+import org.apache.camel.spi.EndpointStrategy;
+import org.apache.camel.spi.EndpointUriFactory;
+import org.apache.camel.spi.EventNotifier;
+import org.apache.camel.spi.ExchangeFactory;
+import org.apache.camel.spi.ExchangeFactoryManager;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.FactoryFinderResolver;
+import org.apache.camel.spi.HeadersMapFactory;
+import org.apache.camel.spi.InterceptEndpointFactory;
+import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.spi.InternalProcessorFactory;
+import org.apache.camel.spi.LanguageResolver;
+import org.apache.camel.spi.LifecycleStrategy;
+import org.apache.camel.spi.LogListener;
+import org.apache.camel.spi.ManagementMBeanAssembler;
+import org.apache.camel.spi.ManagementStrategy;
+import org.apache.camel.spi.ManagementStrategyFactory;
+import org.apache.camel.spi.ModelJAXBContextFactory;
+import org.apache.camel.spi.ModelToXMLDumper;
+import org.apache.camel.spi.ModelineFactory;
+import org.apache.camel.spi.NodeIdFactory;
+import org.apache.camel.spi.NormalizedEndpointUri;
+import org.apache.camel.spi.PackageScanClassResolver;
+import org.apache.camel.spi.PackageScanResourceResolver;
+import org.apache.camel.spi.PeriodTaskResolver;
+import org.apache.camel.spi.PeriodTaskScheduler;
+import org.apache.camel.spi.ProcessorExchangeFactory;
+import org.apache.camel.spi.ProcessorFactory;
+import org.apache.camel.spi.PropertiesComponent;
+import org.apache.camel.spi.ReactiveExecutor;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.spi.ResourceLoader;
+import org.apache.camel.spi.RestBindingJaxbDataFormatFactory;
+import org.apache.camel.spi.RouteController;
+import org.apache.camel.spi.RouteFactory;
+import org.apache.camel.spi.RouteStartupOrder;
+import org.apache.camel.spi.RoutesLoader;
+import org.apache.camel.spi.StartupStepRecorder;
+import org.apache.camel.spi.UnitOfWorkFactory;
+import org.apache.camel.spi.UriFactoryResolver;
+import org.apache.camel.support.EndpointHelper;
+import org.apache.camel.support.NormalizedUri;
+import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class DefaultCamelContextExtension implements ExtendedCamelContext {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultCamelContextExtension.class);
+    
+    private final AbstractCamelContext camelContext;
+    private final ThreadLocal<Boolean> isSetupRoutes = new ThreadLocal<>();
+    private List<InterceptStrategy> interceptStrategies = new ArrayList<>();
+    private final Map<String, FactoryFinder> factories = new ConcurrentHashMap<>();
+    private Set<LogListener> logListeners;
+    private volatile String description;
+    @Deprecated
+    private ErrorHandlerFactory errorHandlerFactory;
+    private String basePackageScan;
+    private boolean lightweight;
+
+
+    public DefaultCamelContextExtension(AbstractCamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public byte getStatusPhase() {
+        return camelContext.getStatusPhase();
+    }
+
+    @Override
+    public String getName() {
+        return camelContext.getNameStrategy().getName();
+    }
+
+    @Override
+    public void setName(String name) {
+        // use an explicit name strategy since an explicit name was provided to be used
+        camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(name));
+    }
+
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+    @Override
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @Override
+    public Endpoint hasEndpoint(NormalizedEndpointUri uri) {
+        if (camelContext.getEndpointRegistry().isEmpty()) {
+            return null;
+        }
+        return camelContext.getEndpointRegistry().get(uri);
+    }
+
+    @Override
+    public NormalizedEndpointUri normalizeUri(String uri) {
+        try {
+            uri = EndpointHelper.resolveEndpointUriPropertyPlaceholders(camelContext, uri);
+            return NormalizedUri.newNormalizedUri(uri, false);
+        } catch (ResolveEndpointFailedException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new ResolveEndpointFailedException(uri, e);
+        }
+    }
+
+    @Override
+    public Endpoint getEndpoint(NormalizedEndpointUri uri) {
+        return camelContext.doGetEndpoint(uri.getUri(), null, true, false);
+    }
+
+    @Override
+    public Endpoint getPrototypeEndpoint(String uri) {
+        return camelContext.doGetEndpoint(uri, null, false, true);
+    }
+
+    @Override
+    public Endpoint getPrototypeEndpoint(NormalizedEndpointUri uri) {
+        return camelContext.doGetEndpoint(uri.getUri(), null, true, true);
+    }
+
+    @Override
+    public Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> parameters) {
+        return camelContext.doGetEndpoint(uri.getUri(), parameters, true, false);
+    }
+
+    @Override
+    public void registerEndpointCallback(EndpointStrategy strategy) {
+        if (!camelContext.getEndpointStrategies().contains(strategy)) {
+            // let it be invoked for already registered endpoints so it can
+            // catch-up.
+            camelContext.getEndpointStrategies().add(strategy);
+            for (Endpoint endpoint : camelContext.getEndpoints()) {
+                Endpoint newEndpoint = strategy.registerEndpoint(endpoint.getEndpointUri(), endpoint);
+                if (newEndpoint != null) {
+                    // put will replace existing endpoint with the new endpoint
+                    camelContext.getEndpointRegistry().put(camelContext.getEndpointKey(endpoint.getEndpointUri()), newEndpoint);
+                }
+            }
+        }
+    }
+
+    @Override
+    public List<RouteStartupOrder> getRouteStartupOrder() {
+        return camelContext.getRouteStartupOrder();
+    }
+
+    @Override
+    public boolean isSetupRoutes() {
+        Boolean answer = isSetupRoutes.get();
+        return answer != null && answer;
+    }
+
+    @Override
+    public void addBootstrap(BootstrapCloseable bootstrap) {
+        camelContext.bootstraps.add(bootstrap);
+    }
+
+    @Override
+    public List<Service> getServices() {
+        return Collections.unmodifiableList(camelContext.servicesToStop);
+    }
+
+    @Override
+    public String resolvePropertyPlaceholders(String text, boolean keepUnresolvedOptional) {
+        if (text != null && text.contains(PropertiesComponent.PREFIX_TOKEN)) {
+            // the parser will throw exception if property key was not found
+            String answer = camelContext.getPropertiesComponent().parseUri(text, keepUnresolvedOptional);
+            LOG.debug("Resolved text: {} -> {}", text, answer);
+            return answer;
+        }
+        // is the value a known field (currently we only support
+        // constants from Exchange.class)
+        if (text != null && text.startsWith("Exchange.")) {
+            String field = StringHelper.after(text, "Exchange.");
+            String constant = ExchangeConstantProvider.lookup(field);
+            if (constant != null) {
+                LOG.debug("Resolved constant: {} -> {}", text, constant);
+                return constant;
+            } else {
+                throw new IllegalArgumentException("Constant field with name: " + field + " not found on Exchange.class");
+            }
+        }
+
+        // return original text as is
+        return text;
+    }
+
+    @Override
+    public CamelBeanPostProcessor getBeanPostProcessor() {
+        if (camelContext.beanPostProcessor == null) {
+            synchronized (camelContext.lock) {
+                if (camelContext.beanPostProcessor == null) {
+                    setBeanPostProcessor(camelContext.createBeanPostProcessor());
+                }
+            }
+        }
+        return camelContext.beanPostProcessor;

Review Comment:
   You'll see this pattern of accessing internal `AbstractCamelContext` member variables in this class. Because this is a big change, I am doing in steps. This will be cleaned up in future PRs.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org