Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-15105: part 8 #9742

Merged
merged 3 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.camel.spi.UuidGenerator;
import org.apache.camel.spring.xml.CamelContextFactoryBean;
import org.apache.camel.support.DefaultUuidGenerator;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.SimpleUuidGenerator;
import org.apache.camel.xml.jaxb.DefaultModelJAXBContextFactory;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -68,7 +69,7 @@ public void testCustomModelJAXBContextFactory() throws Exception {
factory.afterPropertiesSet();

ModelJAXBContextFactory modelJAXBContextFactory
= factory.getContext().getCamelContextExtension().getModelJAXBContextFactory();
= PluginHelper.getModelJAXBContextFactory(factory.getContext());

assertTrue(modelJAXBContextFactory instanceof CustomModelJAXBContextFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import org.apache.camel.catalog.RuntimeCamelCatalog;
import org.apache.camel.console.DevConsoleResolver;
Expand All @@ -43,9 +44,7 @@
import org.apache.camel.spi.LifecycleStrategy;
import org.apache.camel.spi.LogListener;
import org.apache.camel.spi.ManagementMBeanAssembler;
import org.apache.camel.spi.ModelJAXBContextFactory;
import org.apache.camel.spi.ModelToXMLDumper;
import org.apache.camel.spi.ModelineFactory;
import org.apache.camel.spi.NormalizedEndpointUri;
import org.apache.camel.spi.PeriodTaskResolver;
import org.apache.camel.spi.PeriodTaskScheduler;
Expand Down Expand Up @@ -421,20 +420,6 @@ default Registry getRegistry() {
*/
void setRouteFactory(RouteFactory routeFactory);

/**
* Returns the JAXB Context factory used to create Models.
*
* @return the JAXB Context factory used to create Models.
*/
ModelJAXBContextFactory getModelJAXBContextFactory();

/**
* Sets a custom JAXB Context factory to be used
*
* @param modelJAXBContextFactory a JAXB Context factory
*/
void setModelJAXBContextFactory(ModelJAXBContextFactory modelJAXBContextFactory);

/**
* Gets the {@link DeferServiceFactory} to use.
*/
Expand Down Expand Up @@ -707,16 +692,6 @@ default Registry getRegistry() {
*/
void setBasePackageScan(String basePackageScan);

/**
* Gets the {@link ModelineFactory}.
*/
ModelineFactory getModelineFactory();

/**
* Sets a custom {@link ModelineFactory}.
*/
void setModelineFactory(ModelineFactory modelineFactory);

/**
* The {@link CamelContext} have additional phases that are not defined in {@link ServiceStatus} and this method
* provides the phase ordinal value.
Expand Down Expand Up @@ -766,4 +741,12 @@ default Registry getRegistry() {
* @param module the instance of the extension
*/
<T> void addContextPlugin(Class<T> type, T module);

/**
* Allows lazy installation of custom plugins to the Camel context.
*
* @param type the type of the extension
* @param module the instance of the extension
*/
<T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.camel.spi;

import java.util.function.Supplier;

/**
* A manager for internal plugins. This is part of the internal Camel API and not meant for public usage.
*/
Expand All @@ -37,4 +39,12 @@ public interface PluginManager {
* @param module the instance of the extension
*/
<T> void addContextPlugin(Class<T> type, T module);

/**
* Allows lazy installation of custom plugins to the Camel context.
*
* @param type the type of the extension
* @param module the instance of the extension
*/
<T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module);
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,13 @@ public abstract class AbstractCamelContext extends BaseService
volatile ModelToXMLDumper modelToXMLDumper;
volatile RestBindingJaxbDataFormatFactory restBindingJaxbDataFormatFactory;
volatile RuntimeCamelCatalog runtimeCamelCatalog;
volatile ModelineFactory modelineFactory;
volatile ProcessorFactory processorFactory;
volatile PeriodTaskResolver periodTaskResolver;
volatile PeriodTaskScheduler periodTaskScheduler;
volatile InternalProcessorFactory internalProcessorFactory;
volatile InterceptEndpointFactory interceptEndpointFactory;
volatile RouteFactory routeFactory;
volatile AsyncProcessorAwaitManager asyncProcessorAwaitManager;
volatile ModelJAXBContextFactory modelJAXBContextFactory;
volatile UnitOfWorkFactory unitOfWorkFactory;
volatile BeanIntrospection beanIntrospection;
volatile boolean eventNotificationApplicable;
Expand Down Expand Up @@ -380,6 +378,8 @@ protected AbstractCamelContext(boolean build) {
camelContextExtension.addContextPlugin(FactoryFinderResolver.class, createFactoryFinderResolver());
camelContextExtension.addContextPlugin(PackageScanClassResolver.class, createPackageScanClassResolver());
camelContextExtension.addContextPlugin(PackageScanResourceResolver.class, createPackageScanResourceResolver());
camelContextExtension.lazyAddContextPlugin(ModelineFactory.class, this::createModelineFactory);
camelContextExtension.lazyAddContextPlugin(ModelJAXBContextFactory.class, this::createModelJAXBContextFactory);

if (build) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import org.apache.camel.CamelContextAware;
import org.apache.camel.Endpoint;
Expand Down Expand Up @@ -62,9 +63,7 @@
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.NormalizedEndpointUri;
import org.apache.camel.spi.PeriodTaskResolver;
import org.apache.camel.spi.PeriodTaskScheduler;
Expand Down Expand Up @@ -368,40 +367,6 @@ public FactoryFinder getFactoryFinder(String path) {
return factories.computeIfAbsent(path, camelContext::createFactoryFinder);
}

@Override
public ModelJAXBContextFactory getModelJAXBContextFactory() {
if (camelContext.modelJAXBContextFactory == null) {
synchronized (camelContext.lock) {
if (camelContext.modelJAXBContextFactory == null) {
setModelJAXBContextFactory(camelContext.createModelJAXBContextFactory());
}
}
}
return camelContext.modelJAXBContextFactory;
}

@Override
public void setModelJAXBContextFactory(final ModelJAXBContextFactory modelJAXBContextFactory) {
camelContext.modelJAXBContextFactory = camelContext.getInternalServiceManager().addService(modelJAXBContextFactory);
}

@Override
public ModelineFactory getModelineFactory() {
if (camelContext.modelineFactory == null) {
synchronized (camelContext.lock) {
if (camelContext.modelineFactory == null) {
setModelineFactory(camelContext.createModelineFactory());
}
}
}
return camelContext.modelineFactory;
}

@Override
public void setModelineFactory(ModelineFactory modelineFactory) {
camelContext.modelineFactory = camelContext.getInternalServiceManager().addService(modelineFactory);
}

@Override
public PeriodTaskResolver getPeriodTaskResolver() {
if (camelContext.periodTaskResolver == null) {
Expand Down Expand Up @@ -943,4 +908,15 @@ public <T> void addContextPlugin(Class<T> type, T module) {
final T addedModule = camelContext.getInternalServiceManager().addService(module);
pluginManager.addContextPlugin(type, addedModule);
}

@Override
public <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module) {
pluginManager.lazyAddContextPlugin(type, () -> lazyInitAndAdd(module));
}

private <T> T lazyInitAndAdd(Supplier<T> supplier) {
T module = supplier.get();

return camelContext.getInternalServiceManager().addService(module);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ public <T> void addContextPlugin(Class<T> type, T module) {
}
}
}

@Override
public <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module) {
if (module != null) {
extensions.put(type, module);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesBuilderLoader;
import org.apache.camel.spi.RoutesLoader;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.ResolverHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
Expand Down Expand Up @@ -90,7 +91,7 @@ public Collection<RoutesBuilder> findRoutesBuilders(Collection<Resource> resourc

// first we need to parse for modeline to gather all the configurations
if (camelContext.isModeline()) {
ModelineFactory factory = camelContext.getCamelContextExtension().getModelineFactory();
ModelineFactory factory = PluginHelper.getModelineFactory(camelContext);
for (Resource resource : resources) {
RoutesBuilderLoader loader = resolveRoutesBuilderLoader(resource);
// gather resources for modeline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj
case "InternalProcessorFactory": target.setInternalProcessorFactory(property(camelContext, org.apache.camel.spi.InternalProcessorFactory.class, value)); return true;
case "lightweight":
case "Lightweight": target.setLightweight(property(camelContext, boolean.class, value)); return true;
case "modeljaxbcontextfactory":
case "ModelJAXBContextFactory": target.setModelJAXBContextFactory(property(camelContext, org.apache.camel.spi.ModelJAXBContextFactory.class, value)); return true;
case "modeltoxmldumper":
case "ModelToXMLDumper": target.setModelToXMLDumper(property(camelContext, org.apache.camel.spi.ModelToXMLDumper.class, value)); return true;
case "modelinefactory":
case "ModelineFactory": target.setModelineFactory(property(camelContext, org.apache.camel.spi.ModelineFactory.class, value)); return true;
case "name":
case "Name": target.setName(property(camelContext, java.lang.String.class, value)); return true;
case "periodtaskresolver":
Expand Down Expand Up @@ -134,12 +130,8 @@ public Class<?> getOptionType(String name, boolean ignoreCase) {
case "InternalProcessorFactory": return org.apache.camel.spi.InternalProcessorFactory.class;
case "lightweight":
case "Lightweight": return boolean.class;
case "modeljaxbcontextfactory":
case "ModelJAXBContextFactory": return org.apache.camel.spi.ModelJAXBContextFactory.class;
case "modeltoxmldumper":
case "ModelToXMLDumper": return org.apache.camel.spi.ModelToXMLDumper.class;
case "modelinefactory":
case "ModelineFactory": return org.apache.camel.spi.ModelineFactory.class;
case "name":
case "Name": return java.lang.String.class;
case "periodtaskresolver":
Expand Down Expand Up @@ -212,12 +204,8 @@ public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
case "InternalProcessorFactory": return target.getInternalProcessorFactory();
case "lightweight":
case "Lightweight": return target.isLightweight();
case "modeljaxbcontextfactory":
case "ModelJAXBContextFactory": return target.getModelJAXBContextFactory();
case "modeltoxmldumper":
case "ModelToXMLDumper": return target.getModelToXMLDumper();
case "modelinefactory":
case "ModelineFactory": return target.getModelineFactory();
case "name":
case "Name": return target.getName();
case "periodtaskresolver":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
Expand Down Expand Up @@ -57,9 +58,7 @@
import org.apache.camel.spi.InternalProcessorFactory;
import org.apache.camel.spi.LogListener;
import org.apache.camel.spi.ManagementMBeanAssembler;
import org.apache.camel.spi.ModelJAXBContextFactory;
import org.apache.camel.spi.ModelToXMLDumper;
import org.apache.camel.spi.ModelineFactory;
import org.apache.camel.spi.NormalizedEndpointUri;
import org.apache.camel.spi.PeriodTaskResolver;
import org.apache.camel.spi.PeriodTaskScheduler;
Expand Down Expand Up @@ -109,16 +108,6 @@ public void setRegistry(Registry registry) {
throw new UnsupportedOperationException();
}

@Override
public ModelJAXBContextFactory getModelJAXBContextFactory() {
return camelContext.getCamelContextExtension().getModelJAXBContextFactory();
}

@Override
public void setModelJAXBContextFactory(ModelJAXBContextFactory modelJAXBContextFactory) {
throw new UnsupportedOperationException();
}

@Override
public DataFormatResolver getDataFormatResolver() {
return camelContext.getCamelContextExtension().getDataFormatResolver();
Expand Down Expand Up @@ -299,16 +288,6 @@ public void setProcessorFactory(ProcessorFactory processorFactory) {
throw new UnsupportedOperationException();
}

@Override
public ModelineFactory getModelineFactory() {
return camelContext.getCamelContextExtension().getModelineFactory();
}

@Override
public void setModelineFactory(ModelineFactory modelineFactory) {
throw new UnsupportedOperationException();
}

@Override
public InternalProcessorFactory getInternalProcessorFactory() {
return camelContext.getCamelContextExtension().getInternalProcessorFactory();
Expand Down Expand Up @@ -761,6 +740,11 @@ public <T> void addContextPlugin(Class<T> type, T module) {
camelContext.getCamelContextExtension().addContextPlugin(type, module);
}

@Override
public <T> void lazyAddContextPlugin(Class<T> type, Supplier<T> module) {
camelContext.getCamelContextExtension().lazyAddContextPlugin(type, module);
}

Endpoint doGetEndpoint(String uri, boolean normalized, boolean prototype) {
StringHelper.notEmpty(uri, "uri");
// in case path has property placeholders then try to let property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ private void setupCustomServices() {
ModelJAXBContextFactory modelJAXBContextFactory = getBeanForType(ModelJAXBContextFactory.class);
if (modelJAXBContextFactory != null) {
LOG.info("Using custom ModelJAXBContextFactory: {}", modelJAXBContextFactory);
getContext().getCamelContextExtension().setModelJAXBContextFactory(modelJAXBContextFactory);
getContext().getCamelContextExtension().addContextPlugin(ModelJAXBContextFactory.class, modelJAXBContextFactory);
}
ClassResolver classResolver = getBeanForType(ClassResolver.class);
if (classResolver != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.PluginHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -52,7 +53,7 @@ protected Registry createRegistry() throws Exception {
@BeforeEach
public void setUp() throws Exception {
super.setUp();
jaxbContext = (JAXBContext) context.getCamelContextExtension().getModelJAXBContextFactory().newJAXBContext();
jaxbContext = (JAXBContext) PluginHelper.getModelJAXBContextFactory(context).newJAXBContext();
}

protected Object parseUri(String uri) throws JAXBException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.Processor;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.support.PluginHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -40,7 +41,7 @@ public class CamelContextAddRouteDefinitionsFromXmlTest extends ContextTestSuppo
@BeforeEach
public void setUp() throws Exception {
super.setUp();
jaxbContext = (JAXBContext) context.getCamelContextExtension().getModelJAXBContextFactory().newJAXBContext();
jaxbContext = (JAXBContext) PluginHelper.getModelJAXBContextFactory(context).newJAXBContext();
}

protected Object parseUri(String uri) throws JAXBException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public static void afterConfigure(final CamelContext camelContext) throws Except
}
ModelJAXBContextFactory mjcf = getSingleBeanOfType(registry, ModelJAXBContextFactory.class);
if (mjcf != null) {
ecc.getCamelContextExtension().setModelJAXBContextFactory(mjcf);
ecc.getCamelContextExtension().addContextPlugin(ModelJAXBContextFactory.class, mjcf);
}
ClassResolver cr = getSingleBeanOfType(registry, ClassResolver.class);
if (cr != null) {
Expand Down
Loading