use of org.apache.camel.component.bean.BeanProcessor in project camel by apache.
the class XmlServiceExporterBean method create.
@Override
public T create(CreationalContext<T> creationalContext) {
try {
CamelContext context = isNotEmpty(exporter.getCamelContextId()) ? getReferenceByName(manager, exporter.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context);
Bean<?> bean = manager.resolve(manager.getBeans(exporter.getServiceRef()));
if (bean == null) {
throw new UnsatisfiedResolutionException("No bean with name [" + exporter.getServiceRef() + "] is deployed!");
}
@SuppressWarnings("unchecked") T service = (T) manager.getReference(bean, type, manager.createCreationalContext(bean));
Endpoint endpoint = getMandatoryEndpoint(context, exporter.getUri());
try {
// need to start endpoint before we create consumer
startService(endpoint);
Consumer consumer = endpoint.createConsumer(new BeanProcessor(service, context));
// add and start consumer
context.addService(consumer, true, true);
} catch (Exception cause) {
throw new FailedToCreateConsumerException(endpoint, cause);
}
return service;
} catch (Exception cause) {
throw new CreationException("Error while creating instance for " + this, cause);
}
}
use of org.apache.camel.component.bean.BeanProcessor in project camel by apache.
the class DefaultManagementObjectStrategy method getManagedObjectForProcessor.
@SuppressWarnings({ "deprecation", "unchecked" })
public Object getManagedObjectForProcessor(CamelContext context, Processor processor, ProcessorDefinition<?> definition, Route route) {
ManagedProcessor answer = null;
if (definition instanceof RecipientListDefinition) {
// special for RecipientListDefinition, as the processor is wrapped in a pipeline as last
Pipeline pipeline = (Pipeline) processor;
Iterator<Processor> it = pipeline.getProcessors().iterator();
while (it.hasNext()) {
processor = it.next();
}
} else if (definition instanceof ThreadsDefinition) {
// special for ThreadsDefinition, as the processor is wrapped in a pipeline as first
Pipeline pipeline = (Pipeline) processor;
Iterator<Processor> it = pipeline.getProcessors().iterator();
processor = it.next();
}
// unwrap delegates as we want the real target processor
Processor target = processor;
while (target != null) {
// skip error handlers
if (target instanceof ErrorHandler) {
return false;
}
if (target instanceof ConvertBodyProcessor) {
answer = new ManagedConvertBody(context, (ConvertBodyProcessor) target, definition);
} else if (target instanceof ChoiceProcessor) {
answer = new ManagedChoice(context, (ChoiceProcessor) target, definition);
} else if (target instanceof Delayer) {
answer = new ManagedDelayer(context, (Delayer) target, definition);
} else if (target instanceof Throttler) {
answer = new ManagedThrottler(context, (Throttler) target, definition);
} else if (target instanceof DynamicRouter) {
answer = new ManagedDynamicRouter(context, (DynamicRouter) target, (org.apache.camel.model.DynamicRouterDefinition) definition);
} else if (target instanceof RoutingSlip) {
answer = new ManagedRoutingSlip(context, (RoutingSlip) target, (org.apache.camel.model.RoutingSlipDefinition) definition);
} else if (target instanceof FilterProcessor) {
answer = new ManagedFilter(context, (FilterProcessor) target, (ExpressionNode) definition);
} else if (target instanceof LogProcessor) {
answer = new ManagedLog(context, (LogProcessor) target, definition);
} else if (target instanceof LoopProcessor) {
answer = new ManagedLoop(context, (LoopProcessor) target, (org.apache.camel.model.LoopDefinition) definition);
} else if (target instanceof MarshalProcessor) {
answer = new ManagedMarshal(context, (MarshalProcessor) target, (org.apache.camel.model.MarshalDefinition) definition);
} else if (target instanceof UnmarshalProcessor) {
answer = new ManagedUnmarshal(context, (UnmarshalProcessor) target, (org.apache.camel.model.UnmarshalDefinition) definition);
} else if (target instanceof CircuitBreakerLoadBalancer) {
answer = new ManagedCircuitBreakerLoadBalancer(context, (CircuitBreakerLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof FailOverLoadBalancer) {
answer = new ManagedFailoverLoadBalancer(context, (FailOverLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RandomLoadBalancer) {
answer = new ManagedRandomLoadBalancer(context, (RandomLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RoundRobinLoadBalancer) {
answer = new ManagedRoundRobinLoadBalancer(context, (RoundRobinLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof StickyLoadBalancer) {
answer = new ManagedStickyLoadBalancer(context, (StickyLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof TopicLoadBalancer) {
answer = new ManagedTopicLoadBalancer(context, (TopicLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof WeightedLoadBalancer) {
answer = new ManagedWeightedLoadBalancer(context, (WeightedLoadBalancer) target, (org.apache.camel.model.LoadBalanceDefinition) definition);
} else if (target instanceof RecipientList) {
answer = new ManagedRecipientList(context, (RecipientList) target, (RecipientListDefinition) definition);
} else if (target instanceof Splitter) {
answer = new ManagedSplitter(context, (Splitter) target, (org.apache.camel.model.SplitDefinition) definition);
} else if (target instanceof MulticastProcessor) {
answer = new ManagedMulticast(context, (MulticastProcessor) target, definition);
} else if (target instanceof SamplingThrottler) {
answer = new ManagedSamplingThrottler(context, (SamplingThrottler) target, definition);
} else if (target instanceof Resequencer) {
answer = new ManagedResequencer(context, (Resequencer) target, definition);
} else if (target instanceof RollbackProcessor) {
answer = new ManagedRollback(context, (RollbackProcessor) target, definition);
} else if (target instanceof StreamResequencer) {
answer = new ManagedResequencer(context, (StreamResequencer) target, definition);
} else if (target instanceof SetBodyProcessor) {
answer = new ManagedSetBody(context, (SetBodyProcessor) target, (org.apache.camel.model.SetBodyDefinition) definition);
} else if (target instanceof RemoveHeaderProcessor) {
answer = new ManagedRemoveHeader(context, (RemoveHeaderProcessor) target, definition);
} else if (target instanceof RemoveHeadersProcessor) {
answer = new ManagedRemoveHeaders(context, (RemoveHeadersProcessor) target, definition);
} else if (target instanceof SetHeaderProcessor) {
answer = new ManagedSetHeader(context, (SetHeaderProcessor) target, (org.apache.camel.model.SetHeaderDefinition) definition);
} else if (target instanceof RemovePropertyProcessor) {
answer = new ManagedRemoveProperty(context, (RemovePropertyProcessor) target, definition);
} else if (target instanceof RemovePropertiesProcessor) {
answer = new ManagedRemoveProperties(context, (RemovePropertiesProcessor) target, definition);
} else if (target instanceof SetPropertyProcessor) {
answer = new ManagedSetProperty(context, (SetPropertyProcessor) target, (org.apache.camel.model.SetPropertyDefinition) definition);
} else if (target instanceof ExchangePatternProcessor) {
answer = new ManagedSetExchangePattern(context, (ExchangePatternProcessor) target, definition);
} else if (target instanceof ScriptProcessor) {
answer = new ManagedScript(context, (ScriptProcessor) target, (org.apache.camel.model.ScriptDefinition) definition);
} else if (target instanceof StopProcessor) {
answer = new ManagedStop(context, (StopProcessor) target, definition);
} else if (target instanceof ThreadsProcessor) {
answer = new ManagedThreads(context, (ThreadsProcessor) target, definition);
} else if (target instanceof ThrowExceptionProcessor) {
answer = new ManagedThrowException(context, (ThrowExceptionProcessor) target, definition);
} else if (target instanceof TransformProcessor) {
answer = new ManagedTransformer(context, (TransformProcessor) target, (org.apache.camel.model.TransformDefinition) definition);
} else if (target instanceof PredicateValidatingProcessor) {
answer = new ManagedValidate(context, (PredicateValidatingProcessor) target, (org.apache.camel.model.ValidateDefinition) definition);
} else if (target instanceof WireTapProcessor) {
answer = new ManagedWireTapProcessor(context, (WireTapProcessor) target, definition);
} else if (target instanceof SendDynamicProcessor) {
answer = new ManagedSendDynamicProcessor(context, (SendDynamicProcessor) target, definition);
} else if (target instanceof SendProcessor) {
SendProcessor sp = (SendProcessor) target;
// special for sending to throughput logger
if (sp.getDestination() instanceof LogEndpoint) {
LogEndpoint le = (LogEndpoint) sp.getDestination();
if (le.getLogger() instanceof ThroughputLogger) {
ThroughputLogger tl = (ThroughputLogger) le.getLogger();
answer = new ManagedThroughputLogger(context, tl, definition);
}
}
// regular send processor
if (answer == null) {
answer = new ManagedSendProcessor(context, (SendProcessor) target, definition);
}
} else if (target instanceof BeanProcessor) {
answer = new ManagedBeanProcessor(context, (BeanProcessor) target, definition);
} else if (target instanceof IdempotentConsumer) {
answer = new ManagedIdempotentConsumer(context, (IdempotentConsumer) target, (org.apache.camel.model.IdempotentConsumerDefinition) definition);
} else if (target instanceof AggregateProcessor) {
answer = new ManagedAggregateProcessor(context, (AggregateProcessor) target, (org.apache.camel.model.AggregateDefinition) definition);
} else if (target instanceof Enricher) {
answer = new ManagedEnricher(context, (Enricher) target, (org.apache.camel.model.EnrichDefinition) definition);
} else if (target instanceof PollEnricher) {
answer = new ManagedPollEnricher(context, (PollEnricher) target, (org.apache.camel.model.PollEnrichDefinition) definition);
} else if (target instanceof org.apache.camel.spi.ManagementAware) {
return ((org.apache.camel.spi.ManagementAware<Processor>) target).getManagedObject(processor);
}
// special for custom load balancer
if (definition instanceof LoadBalanceDefinition) {
LoadBalanceDefinition lb = (LoadBalanceDefinition) definition;
if (lb.getLoadBalancerType() instanceof CustomLoadBalancerDefinition) {
answer = new ManagedCustomLoadBalancer(context, (LoadBalancer) target, (LoadBalanceDefinition) definition);
}
}
if (answer != null) {
// break out as we found an answer
break;
}
// no answer yet, so unwrap any delegates and try again
if (target instanceof DelegateProcessor) {
target = ((DelegateProcessor) target).getProcessor();
} else {
// no delegate so we dont have any target to try next
break;
}
}
if (answer == null && definition instanceof ProcessDefinition) {
answer = new ManagedProcess(context, target, (ProcessDefinition) definition);
} else if (answer == null) {
// fallback to a generic processor
answer = new ManagedProcessor(context, target, definition);
}
answer.setRoute(route);
answer.init(context.getManagementStrategy());
return answer;
}
use of org.apache.camel.component.bean.BeanProcessor in project camel by apache.
the class BeanInfoTest method testBeanProcessor.
public void testBeanProcessor() throws Exception {
BeanProcessor processor = new BeanProcessor(bean, info);
processor.process(exchange);
assertEquals("Hello James!", exchange.getIn().getBody());
}
use of org.apache.camel.component.bean.BeanProcessor in project camel by apache.
the class CamelServiceExporter method afterPropertiesSet.
public void afterPropertiesSet() throws Exception {
// lets bind the URI to a pojo
notNull(uri, "uri");
// Always resolve the camel context by using the camelContextID
if (ObjectHelper.isNotEmpty(camelContextId)) {
camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
}
notNull(camelContext, "camelContext");
if (serviceRef != null && getService() == null && applicationContext != null) {
setService(applicationContext.getBean(serviceRef));
}
Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
notNull(getService(), "service");
Object proxy = getProxyForService();
try {
// need to start endpoint before we create consumer
ServiceHelper.startService(endpoint);
consumer = endpoint.createConsumer(new BeanProcessor(proxy, camelContext));
// add and start consumer
camelContext.addService(consumer, true, true);
} catch (Exception e) {
throw new FailedToCreateConsumerException(endpoint, e);
}
}
use of org.apache.camel.component.bean.BeanProcessor in project camel by apache.
the class BeanDefinition method createProcessor.
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
BeanProcessor answer;
Class<?> clazz = bean != null ? bean.getClass() : null;
BeanHolder beanHolder;
if (ObjectHelper.isNotEmpty(ref)) {
// lets cache by default
if (isCacheBean()) {
// cache the registry lookup which avoids repeat lookup in the registry
beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
// bean holder will check if the bean exists
bean = beanHolder.getBean();
} else {
// we do not cache so we invoke on-demand
beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
}
answer = new BeanProcessor(beanHolder);
} else {
if (bean == null) {
if (beanType == null && beanClass == null) {
throw new IllegalArgumentException("bean, ref or beanType must be provided");
}
// the clazz is either from beanType or beanClass
if (beanType != null) {
try {
clazz = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
} else {
clazz = beanClass;
}
// create a bean if there is a default public no-arg constructor
if (isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz);
ObjectHelper.notNull(bean, "bean", this);
}
}
// to a bean name but the String is being invoke instead
if (bean instanceof String) {
throw new IllegalArgumentException("The bean instance is a java.lang.String type: " + bean + ". We suppose you want to refer to a bean instance by its id instead. Please use ref.");
}
// the holder should either be bean or type based
if (bean != null) {
beanHolder = new ConstantBeanHolder(bean, routeContext.getCamelContext());
} else {
if (isCacheBean() && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
// we can only cache if we can create an instance of the bean, and for that we need a public constructor
beanHolder = new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext()).createCacheHolder();
} else {
if (ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
beanHolder = new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
} else {
// this is only for invoking static methods on the bean
beanHolder = new ConstantStaticTypeBeanHolder(clazz, routeContext.getCamelContext());
}
}
}
answer = new BeanProcessor(beanHolder);
}
// check for multiParameterArray setting
if (multiParameterArray != null) {
answer.setMultiParameterArray(multiParameterArray);
}
// check for method exists
if (method != null) {
answer.setMethod(method);
// which we only want to do if we cache the bean
if (isCacheBean()) {
BeanInfo beanInfo = beanHolder.getBeanInfo();
if (bean != null) {
// there is a bean instance, so check for any methods
if (!beanInfo.hasMethod(method)) {
throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, bean, method));
}
} else if (clazz != null) {
// there is no bean instance, so check for static methods only
if (!beanInfo.hasStaticMethod(method)) {
throw ObjectHelper.wrapRuntimeCamelException(new MethodNotFoundException(null, clazz, method, true));
}
}
}
}
return answer;
}
Aggregations