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;
}
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();
}
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;
}
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;
}
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;
}
};
}
Aggregations