Skip to content

Commit

Permalink
CAMEL-10792: Allow Registry to bind beans.
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Feb 26, 2019
1 parent 33ce2e3 commit a050d7c
Show file tree
Hide file tree
Showing 38 changed files with 197 additions and 103 deletions.
2 changes: 2 additions & 0 deletions MIGRATION.md
Expand Up @@ -112,6 +112,8 @@ TODO: Add the other moved classes/packages etc


#### Generic Information #### Generic Information


The class `SimpleRegistry` is moved from `org.apache.camel.impl` to `org.apache.camel.support`. Also you should favour using the `org.apache.camel.support.DefaultRegistry` instead.

The classes from `org.apache.camel.impl` that was intended to support Camel developers building custom components has been moved out of `camel-core` into `camel-support` into the `org.apache.camel.support` package. If you have built custom Camel components that may have used some of these APIs you would then need to migrate. The classes from `org.apache.camel.impl` that was intended to support Camel developers building custom components has been moved out of `camel-core` into `camel-support` into the `org.apache.camel.support` package. If you have built custom Camel components that may have used some of these APIs you would then need to migrate.


All the classes in `org.apache.camel.util.component` has been moved from the camel-core JAR to the package `org.apache.camel.support.component` in the `camel-support` JAR. All the classes in `org.apache.camel.util.component` has been moved from the camel-core JAR to the package `org.apache.camel.support.component` in the `camel-support` JAR.
Expand Down
Expand Up @@ -25,7 +25,7 @@


import org.apache.camel.RuntimeCamelException; import org.apache.camel.RuntimeCamelException;
import org.apache.camel.Service; import org.apache.camel.Service;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.BeanRepository;
import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.support.LifecycleStrategySupport;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
Expand All @@ -37,14 +37,14 @@
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


/** /**
* The OsgiServiceRegistry support to get the service object from the bundle context * The OSGi {@link BeanRepository} support to lookup beans from the OSGi service registry from its bundle context.
*/ */
public class OsgiServiceRegistry extends LifecycleStrategySupport implements Registry, Service, ServiceListener { public class OsgiBeanRepository extends LifecycleStrategySupport implements BeanRepository, Service, ServiceListener {
private static final Logger LOG = LoggerFactory.getLogger(OsgiServiceRegistry.class); private static final Logger LOG = LoggerFactory.getLogger(OsgiBeanRepository.class);
private final BundleContext bundleContext; private final BundleContext bundleContext;
private final Map<ServiceReference<?>, AtomicLong> serviceReferenceUsageMap = new ConcurrentHashMap<>(); private final Map<ServiceReference<?>, AtomicLong> serviceReferenceUsageMap = new ConcurrentHashMap<>();


public OsgiServiceRegistry(BundleContext bc) { public OsgiBeanRepository(BundleContext bc) {
bundleContext = bc; bundleContext = bc;
bundleContext.addServiceListener(this); bundleContext.addServiceListener(this);
} }
Expand Down
Expand Up @@ -16,10 +16,8 @@
*/ */
package org.apache.camel.core.osgi; package org.apache.camel.core.osgi;


import org.apache.camel.CamelContext;
import org.apache.camel.impl.CompositeRegistry;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Registry; import org.apache.camel.support.DefaultRegistry;
import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ObjectHelper;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.slf4j.Logger; import org.slf4j.Logger;
Expand All @@ -32,9 +30,11 @@ private OsgiCamelContextHelper() {
// helper class // helper class
} }


public static void osgiUpdate(DefaultCamelContext camelContext, BundleContext bundleContext) { public static void osgiUpdate(DefaultCamelContext camelContext, BundleContext bundleContext, OsgiBeanRepository beanRepository) {
ObjectHelper.notNull(bundleContext, "BundleContext"); ObjectHelper.notNull(bundleContext, "BundleContext");


LOG.debug("Using OsgiBeanRepository");
camelContext.setRegistry(new DefaultRegistry(beanRepository));
LOG.debug("Using OsgiCamelContextNameStrategy"); LOG.debug("Using OsgiCamelContextNameStrategy");
camelContext.setNameStrategy(new OsgiCamelContextNameStrategy(bundleContext)); camelContext.setNameStrategy(new OsgiCamelContextNameStrategy(bundleContext));
LOG.debug("Using OsgiManagementNameStrategy"); LOG.debug("Using OsgiManagementNameStrategy");
Expand All @@ -51,28 +51,9 @@ public static void osgiUpdate(DefaultCamelContext camelContext, BundleContext bu
camelContext.setLanguageResolver(new OsgiLanguageResolver(bundleContext)); camelContext.setLanguageResolver(new OsgiLanguageResolver(bundleContext));
LOG.debug("Using OsgiDataFormatResolver"); LOG.debug("Using OsgiDataFormatResolver");
camelContext.setDataFormatResolver(new OsgiDataFormatResolver(bundleContext)); camelContext.setDataFormatResolver(new OsgiDataFormatResolver(bundleContext));
}

public static Registry wrapRegistry(CamelContext camelContext, Registry registry, BundleContext bundleContext) {
ObjectHelper.notNull(bundleContext, "BundleContext");


OsgiServiceRegistry osgiServiceRegistry = null;
Registry resultingRegistry = registry;
if (registry instanceof OsgiServiceRegistry) {
osgiServiceRegistry = (OsgiServiceRegistry)registry;
} else {
LOG.debug("Wrapping Registry in OsgiServiceRegistry");
osgiServiceRegistry = new OsgiServiceRegistry(bundleContext);
CompositeRegistry compositeRegistry = new CompositeRegistry();
compositeRegistry.addRegistry(osgiServiceRegistry);
compositeRegistry.addRegistry(registry);
resultingRegistry = compositeRegistry;
}

// Need to clean up the OSGi service when camel context is closed. // Need to clean up the OSGi service when camel context is closed.
camelContext.addLifecycleStrategy(osgiServiceRegistry); camelContext.addLifecycleStrategy(beanRepository);

return resultingRegistry;
} }

} }
Expand Up @@ -25,6 +25,7 @@
import org.apache.camel.core.osgi.utils.BundleContextUtils; import org.apache.camel.core.osgi.utils.BundleContextUtils;
import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader; import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
Expand All @@ -34,14 +35,13 @@ public class OsgiDefaultCamelContext extends DefaultCamelContext {
private final BundleContext bundleContext; private final BundleContext bundleContext;


public OsgiDefaultCamelContext(BundleContext bundleContext) { public OsgiDefaultCamelContext(BundleContext bundleContext) {
this(bundleContext, new OsgiServiceRegistry(bundleContext)); this(bundleContext, new OsgiBeanRepository(bundleContext));
} }


public OsgiDefaultCamelContext(BundleContext bundleContext, Registry registry) { public OsgiDefaultCamelContext(BundleContext bundleContext, OsgiBeanRepository osgiBeanRepository) {
super(registry); super();
this.bundleContext = bundleContext; this.bundleContext = bundleContext;
setRegistry(OsgiCamelContextHelper.wrapRegistry(this, registry, bundleContext)); OsgiCamelContextHelper.osgiUpdate(this, bundleContext, osgiBeanRepository);
OsgiCamelContextHelper.osgiUpdate(this, bundleContext);
// setup the application context classloader with the bundle classloader // setup the application context classloader with the bundle classloader
setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle())); setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle()));
} }
Expand Down
Expand Up @@ -21,7 +21,8 @@
import org.apache.camel.Endpoint; import org.apache.camel.Endpoint;
import org.apache.camel.component.file.FileComponent; import org.apache.camel.component.file.FileComponent;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultRegistry;
import org.junit.Test; import org.junit.Test;


public class OsgiComponentResolverTest extends CamelOsgiTestSupport { public class OsgiComponentResolverTest extends CamelOsgiTestSupport {
Expand All @@ -37,8 +38,8 @@ public void testOsgiResolverFindComponentTest() throws Exception {


@Test @Test
public void testOsgiResolverFindComponentFallbackTest() throws Exception { public void testOsgiResolverFindComponentFallbackTest() throws Exception {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("allstar-component", new SampleComponent(true)); registry.bind("allstar-component", new SampleComponent(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand All @@ -50,9 +51,9 @@ public void testOsgiResolverFindComponentFallbackTest() throws Exception {


@Test @Test
public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception { public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("allstar", new SampleComponent(false)); registry.bind("allstar", new SampleComponent(false));
registry.put("allstar-component", new SampleComponent(true)); registry.bind("allstar-component", new SampleComponent(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand Down
Expand Up @@ -22,17 +22,18 @@
import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultRegistry;
import org.junit.Test; import org.junit.Test;


public class OsgiDataFormatResolverTest extends CamelOsgiTestSupport { public class OsgiDataFormatResolverTest extends CamelOsgiTestSupport {




@Test @Test
public void testOsgiResolverFindDataFormatFallbackTest() throws Exception { public void testOsgiResolverFindDataFormatFallbackTest() throws Exception {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("allstar-dataformat", new SampleDataFormat(true)); registry.bind("allstar-dataformat", new SampleDataFormat(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand All @@ -44,9 +45,9 @@ public void testOsgiResolverFindDataFormatFallbackTest() throws Exception {


@Test @Test
public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception { public void testOsgiResolverFindLanguageDoubleFallbackTest() throws Exception {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("allstar", new SampleDataFormat(false)); registry.bind("allstar", new SampleDataFormat(false));
registry.put("allstar-dataformat", new SampleDataFormat(true)); registry.bind("allstar-dataformat", new SampleDataFormat(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand Down
Expand Up @@ -22,8 +22,9 @@
import org.apache.camel.Expression; import org.apache.camel.Expression;
import org.apache.camel.Predicate; import org.apache.camel.Predicate;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.spi.Language; import org.apache.camel.spi.Language;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultRegistry;
import org.junit.Test; import org.junit.Test;


public class OsgiLanguageResolverTest extends CamelOsgiTestSupport { public class OsgiLanguageResolverTest extends CamelOsgiTestSupport {
Expand All @@ -38,8 +39,8 @@ public void testOsgiResolverFindLanguageTest() throws IOException {


@Test @Test
public void testOsgiResolverFindLanguageFallbackTest() throws IOException { public void testOsgiResolverFindLanguageFallbackTest() throws IOException {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("fuffy-language", new SampleLanguage(true)); registry.bind("fuffy-language", new SampleLanguage(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand All @@ -51,9 +52,9 @@ public void testOsgiResolverFindLanguageFallbackTest() throws IOException {


@Test @Test
public void testOsgiResolverFindLanguageDoubleFallbackTest() throws IOException { public void testOsgiResolverFindLanguageDoubleFallbackTest() throws IOException {
SimpleRegistry registry = new SimpleRegistry(); Registry registry = new DefaultRegistry();
registry.put("fuffy", new SampleLanguage(false)); registry.bind("fuffy", new SampleLanguage(false));
registry.put("fuffy-language", new SampleLanguage(true)); registry.bind("fuffy-language", new SampleLanguage(true));


CamelContext camelContext = new DefaultCamelContext(registry); CamelContext camelContext = new DefaultCamelContext(registry);


Expand Down
Expand Up @@ -21,13 +21,14 @@
import org.apache.camel.component.event.EventComponent; import org.apache.camel.component.event.EventComponent;
import org.apache.camel.component.event.EventEndpoint; import org.apache.camel.component.event.EventEndpoint;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.spi.Injector; import org.apache.camel.spi.Injector;
import org.apache.camel.spi.ManagementMBeanAssembler;
import org.apache.camel.spi.ModelJAXBContextFactory; import org.apache.camel.spi.ModelJAXBContextFactory;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;
import org.apache.camel.spring.spi.ApplicationContextRegistry; import org.apache.camel.spring.spi.ApplicationContextBeanRepository;
import org.apache.camel.spring.spi.SpringInjector; import org.apache.camel.spring.spi.SpringInjector;
import org.apache.camel.spring.spi.SpringManagementMBeanAssembler; import org.apache.camel.spring.spi.SpringManagementMBeanAssembler;
import org.apache.camel.support.DefaultRegistry;
import org.apache.camel.support.ProcessorEndpoint; import org.apache.camel.support.ProcessorEndpoint;
import org.apache.camel.util.StopWatch; import org.apache.camel.util.StopWatch;
import org.slf4j.Logger; import org.slf4j.Logger;
Expand Down Expand Up @@ -255,7 +256,8 @@ protected Endpoint convertBeanToEndpoint(String uri, Object bean) {


@Override @Override
protected Registry createRegistry() { protected Registry createRegistry() {
return new ApplicationContextRegistry(getApplicationContext()); BeanRepository repository = new ApplicationContextBeanRepository(getApplicationContext());
return new DefaultRegistry(repository);
} }


@Override @Override
Expand Down
Expand Up @@ -21,20 +21,20 @@
import java.util.Set; import java.util.Set;


import org.apache.camel.NoSuchBeanException; import org.apache.camel.NoSuchBeanException;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.BeanRepository;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException; import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;


/** /**
* A {@link Registry} implementation which looks up the objects in the Spring * A {@link BeanRepository} implementation which looks up the objects in the Spring
* {@link ApplicationContext} * {@link ApplicationContext}
*/ */
public class ApplicationContextRegistry implements Registry { public class ApplicationContextBeanRepository implements BeanRepository {
private ApplicationContext applicationContext; private ApplicationContext applicationContext;


public ApplicationContextRegistry(ApplicationContext applicationContext) { public ApplicationContextBeanRepository(ApplicationContext applicationContext) {
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }


Expand Down
Expand Up @@ -169,6 +169,7 @@
import org.apache.camel.support.OrderedComparator; import org.apache.camel.support.OrderedComparator;
import org.apache.camel.support.ProcessorEndpoint; import org.apache.camel.support.ProcessorEndpoint;
import org.apache.camel.support.ResolverHelper; import org.apache.camel.support.ResolverHelper;
import org.apache.camel.support.SimpleRegistry;
import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.service.ServiceHelper; import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport; import org.apache.camel.support.service.ServiceSupport;
Expand Down Expand Up @@ -315,16 +316,6 @@ public AbstractCamelContext() {
this(true); this(true);
} }


/**
* Creates the {@link CamelContext} using the given JNDI context as the registry
*
* @param jndiContext the JNDI context
*/
public AbstractCamelContext(Context jndiContext) {
this();
setJndiContext(jndiContext);
}

/** /**
* Creates the {@link CamelContext} using the given registry * Creates the {@link CamelContext} using the given registry
* *
Expand Down Expand Up @@ -2574,16 +2565,6 @@ public <T> T getRegistry(Class<T> type) {
return null; return null;
} }


/**
* Sets the registry to the given JNDI context
*
* @param jndiContext is the JNDI context to use as the registry
* @see #setRegistry(Registry)
*/
public void setJndiContext(Context jndiContext) {
setRegistry(new JndiRegistry(jndiContext));
}

public void setRegistry(Registry registry) { public void setRegistry(Registry registry) {
// wrap the registry so we always do property placeholder lookups // wrap the registry so we always do property placeholder lookups
if (!(registry instanceof PropertyPlaceholderDelegateRegistry)) { if (!(registry instanceof PropertyPlaceholderDelegateRegistry)) {
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.apache.camel.runtimecatalog.RuntimeCamelCatalog; import org.apache.camel.runtimecatalog.RuntimeCamelCatalog;
import org.apache.camel.runtimecatalog.impl.DefaultRuntimeCamelCatalog; import org.apache.camel.runtimecatalog.impl.DefaultRuntimeCamelCatalog;
import org.apache.camel.spi.AsyncProcessorAwaitManager; import org.apache.camel.spi.AsyncProcessorAwaitManager;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.spi.CamelContextNameStrategy; import org.apache.camel.spi.CamelContextNameStrategy;
import org.apache.camel.spi.ClassResolver; import org.apache.camel.spi.ClassResolver;
import org.apache.camel.spi.ComponentResolver; import org.apache.camel.spi.ComponentResolver;
Expand Down Expand Up @@ -70,29 +71,43 @@
import org.apache.camel.spi.UnitOfWorkFactory; import org.apache.camel.spi.UnitOfWorkFactory;
import org.apache.camel.spi.UuidGenerator; import org.apache.camel.spi.UuidGenerator;
import org.apache.camel.spi.ValidatorRegistry; import org.apache.camel.spi.ValidatorRegistry;
import org.apache.camel.support.DefaultRegistry;
import org.apache.camel.support.SimpleRegistry;


/** /**
* Represents the context used to configure routes and the policies to use. * Represents the context used to configure routes and the policies to use.
*/ */
public class DefaultCamelContext extends AbstractCamelContext { public class DefaultCamelContext extends AbstractCamelContext {


/** /**
* Creates the {@link CamelContext} using {@link JndiRegistry} as registry, * Creates the {@link CamelContext} using {@link DefaultRegistry} as registry.
* but will silently fallback and use {@link SimpleRegistry} if JNDI cannot be used.
* <p/> * <p/>
* Use one of the other constructors to force use an explicit registry / JNDI. * Use one of the other constructors to force use an explicit registry.
*/ */
public DefaultCamelContext() { public DefaultCamelContext() {
super(); super();
} }


/**
* Creates the {@link CamelContext} using the given {@link BeanRepository}
* as first-choice repository, and the {@link SimpleRegistry} as fallback, via
* the {@link DefaultRegistry} implementation.
*
* @param repository the bean repository.
*/
public DefaultCamelContext(BeanRepository repository) {
super(new DefaultRegistry(repository));
}

/** /**
* Creates the {@link CamelContext} using the given JNDI context as the registry * Creates the {@link CamelContext} using the given JNDI context as the registry
* *
* @param jndiContext the JNDI context * @param jndiContext the JNDI context
* @deprecated create a new {@link JndiRegistry} and use the constructor that accepts this registry.
*/ */
@Deprecated
public DefaultCamelContext(Context jndiContext) { public DefaultCamelContext(Context jndiContext) {
super(jndiContext); this(new JndiRegistry(jndiContext));
} }


/** /**
Expand All @@ -104,6 +119,7 @@ public DefaultCamelContext(Registry registry) {
super(registry); super(registry);
} }


@Deprecated
public DefaultCamelContext(boolean init) { public DefaultCamelContext(boolean init) {
super(init); super(init);
} }
Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.apache.camel.ProducerTemplate; import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.CompositeRegistry; import org.apache.camel.impl.CompositeRegistry;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.support.SimpleRegistry;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;


/** /**
Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.apache.camel.ProducerTemplate; import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry; import org.apache.camel.support.SimpleRegistry;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;


public final class CamelSimpleExpressionPerfTestRunner { public final class CamelSimpleExpressionPerfTestRunner {
Expand Down

0 comments on commit a050d7c

Please sign in to comment.