Search in sources :

Example 6 with CamelContextAware

use of org.apache.camel.CamelContextAware in project camel by apache.

the class DefaultCamelContext method resolveLanguage.

// Helper methods
// -----------------------------------------------------------------------
public Language resolveLanguage(String language) {
    Language answer;
    synchronized (languages) {
        answer = languages.get(language);
        // check if the language is singleton, if so return the shared instance
        if (answer instanceof IsSingleton) {
            boolean singleton = ((IsSingleton) answer).isSingleton();
            if (singleton) {
                return answer;
            }
        }
        // language not known or not singleton, then use resolver
        answer = getLanguageResolver().resolveLanguage(language, this);
        // inject CamelContext if aware
        if (answer != null) {
            if (answer instanceof CamelContextAware) {
                ((CamelContextAware) answer).setCamelContext(this);
            }
            if (answer instanceof Service) {
                try {
                    startService((Service) answer);
                } catch (Exception e) {
                    throw ObjectHelper.wrapRuntimeCamelException(e);
                }
            }
            languages.put(language, answer);
        }
    }
    return answer;
}
Also used : IsSingleton(org.apache.camel.IsSingleton) CamelContextAware(org.apache.camel.CamelContextAware) Language(org.apache.camel.spi.Language) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MalformedObjectNameException(javax.management.MalformedObjectNameException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) IOException(java.io.IOException) LoadPropertiesException(org.apache.camel.util.LoadPropertiesException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException)

Example 7 with CamelContextAware

use of org.apache.camel.CamelContextAware in project camel by apache.

the class DefaultCamelContext method startService.

private void startService(Service service) throws Exception {
    // camel context has been started
    if (service instanceof StartupListener) {
        StartupListener listener = (StartupListener) service;
        addStartupListener(listener);
    }
    if (service instanceof CamelContextAware) {
        CamelContextAware aware = (CamelContextAware) service;
        aware.setCamelContext(this);
    }
    service.start();
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) StartupListener(org.apache.camel.StartupListener)

Example 8 with CamelContextAware

use of org.apache.camel.CamelContextAware in project camel by apache.

the class DefaultCamelBeanPostProcessor method postProcessBeforeInitialization.

/**
     * Apply this post processor to the given new bean instance <i>before</i> any bean
     * initialization callbacks (like <code>afterPropertiesSet</code>
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.
     * 
     * @param bean the new bean instance
     * @param beanName the name of the bean
     * @return the bean instance to use, either the original or a wrapped one; if
     * <code>null</code>, no subsequent BeanPostProcessors will be invoked
     * @throws Exception is thrown if error post processing bean
     */
public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
    LOG.trace("Camel bean processing before initialization for bean: {}", beanName);
    // some beans cannot be post processed at this given time, so we gotta check beforehand
    if (!canPostProcessBean(bean, beanName)) {
        return bean;
    }
    injectFields(bean, beanName);
    injectMethods(bean, beanName);
    if (bean instanceof CamelContextAware && canSetCamelContext(bean, beanName)) {
        CamelContextAware contextAware = (CamelContextAware) bean;
        CamelContext context = getOrLookupCamelContext();
        if (context == null) {
            LOG.warn("No CamelContext defined yet so cannot inject into bean: " + beanName);
        } else {
            contextAware.setCamelContext(context);
        }
    }
    return bean;
}
Also used : CamelContext(org.apache.camel.CamelContext) CamelContextAware(org.apache.camel.CamelContextAware)

Example 9 with CamelContextAware

use of org.apache.camel.CamelContextAware in project camel by apache.

the class DefaultCamelBeanPostProcessor method canSetCamelContext.

protected boolean canSetCamelContext(Object bean, String beanName) {
    if (bean instanceof CamelContextAware) {
        CamelContextAware camelContextAware = (CamelContextAware) bean;
        CamelContext context = camelContextAware.getCamelContext();
        if (context != null) {
            LOG.trace("CamelContext already set on bean with id [{}]. Will keep existing CamelContext on bean.", beanName);
            return false;
        }
    }
    return true;
}
Also used : CamelContext(org.apache.camel.CamelContext) CamelContextAware(org.apache.camel.CamelContextAware)

Example 10 with CamelContextAware

use of org.apache.camel.CamelContextAware in project camel by apache.

the class ICalDataFormatAutoConfiguration method configureICalDataFormatFactory.

@Bean(name = "ical-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(ICalDataFormat.class)
public DataFormatFactory configureICalDataFormatFactory(final CamelContext camelContext, final ICalDataFormatConfiguration configuration) {
    return new DataFormatFactory() {

        public DataFormat newInstance() {
            ICalDataFormat dataformat = new ICalDataFormat();
            if (CamelContextAware.class.isAssignableFrom(ICalDataFormat.class)) {
                CamelContextAware contextAware = CamelContextAware.class.cast(dataformat);
                if (contextAware != null) {
                    contextAware.setCamelContext(camelContext);
                }
            }
            try {
                Map<String, Object> parameters = new HashMap<>();
                IntrospectionSupport.getProperties(configuration, parameters, null, false);
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters);
            } catch (Exception e) {
                throw new RuntimeCamelException(e);
            }
            return dataformat;
        }
    };
}
Also used : DataFormatFactory(org.apache.camel.spi.DataFormatFactory) CamelContextAware(org.apache.camel.CamelContextAware) HashMap(java.util.HashMap) ICalDataFormat(org.apache.camel.component.ical.ICalDataFormat) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

CamelContextAware (org.apache.camel.CamelContextAware)82 HashMap (java.util.HashMap)70 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)69 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)69 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)69 Bean (org.springframework.context.annotation.Bean)69 RuntimeCamelException (org.apache.camel.RuntimeCamelException)47 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 Scope (org.springframework.context.annotation.Scope)24 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelContext (org.apache.camel.CamelContext)3 Service (org.apache.camel.Service)3 StatefulService (org.apache.camel.StatefulService)3 SuspendableService (org.apache.camel.SuspendableService)3 IOException (java.io.IOException)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)2 IsSingleton (org.apache.camel.IsSingleton)2 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)2 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)2