001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import javax.xml.bind.annotation.XmlAccessType;
020 import javax.xml.bind.annotation.XmlAccessorType;
021 import javax.xml.bind.annotation.XmlAttribute;
022 import javax.xml.bind.annotation.XmlRootElement;
023 import javax.xml.bind.annotation.XmlTransient;
024
025 import org.apache.camel.Endpoint;
026 import org.apache.camel.Processor;
027 import org.apache.camel.processor.PollEnricher;
028 import org.apache.camel.processor.aggregate.AggregationStrategy;
029 import org.apache.camel.spi.RouteContext;
030 import org.apache.camel.util.ObjectHelper;
031
032 /**
033 * Represents an XML <pollEnrich/> element
034 *
035 * @see org.apache.camel.processor.Enricher
036 */
037 @XmlRootElement(name = "pollEnrich")
038 @XmlAccessorType(XmlAccessType.FIELD)
039 public class PollEnrichDefinition extends NoOutputDefinition<PollEnrichDefinition> {
040 @XmlAttribute(name = "uri")
041 private String resourceUri;
042 // TODO: For Camel 3.0 we should remove this ref attribute as you can do that in the uri, by prefixing with ref:
043 @XmlAttribute(name = "ref")
044 private String resourceRef;
045 @XmlAttribute
046 private Long timeout;
047 @XmlAttribute(name = "strategyRef")
048 private String aggregationStrategyRef;
049 @XmlTransient
050 private AggregationStrategy aggregationStrategy;
051 @XmlAttribute
052 private Boolean pollMultiple;
053
054 public PollEnrichDefinition() {
055 }
056
057 public PollEnrichDefinition(AggregationStrategy aggregationStrategy, String resourceUri, long timeout, Boolean pollMultiple) {
058 this.aggregationStrategy = aggregationStrategy;
059 this.resourceUri = resourceUri;
060 this.timeout = timeout;
061 this.pollMultiple = pollMultiple;
062 }
063
064 @Override
065 public String toString() {
066 return "PollEnrich[" + description() + " " + aggregationStrategy + "]";
067 }
068
069 protected String description() {
070 return FromDefinition.description(getResourceUri(), getResourceRef(), (Endpoint) null);
071 }
072
073 @Override
074 public String getShortName() {
075 return "pollEnrich";
076 }
077
078 @Override
079 public String getLabel() {
080 return "pollEnrich[" + description() + "]";
081 }
082
083 @Override
084 public Processor createProcessor(RouteContext routeContext) throws Exception {
085 if (ObjectHelper.isEmpty(resourceUri) && ObjectHelper.isEmpty(resourceRef)) {
086 throw new IllegalArgumentException("Either uri or ref must be provided for resource endpoint");
087 }
088
089 // lookup endpoint
090 Endpoint endpoint;
091 if (resourceUri != null) {
092 endpoint = routeContext.resolveEndpoint(resourceUri);
093 } else {
094 endpoint = routeContext.resolveEndpoint(null, resourceRef);
095 }
096
097 PollEnricher enricher;
098 if (timeout != null) {
099 enricher = new PollEnricher(null, endpoint.createPollingConsumer(), timeout, pollMultiple);
100 } else {
101 // if no timeout then we should block, and there use a negative timeout
102 enricher = new PollEnricher(null, endpoint.createPollingConsumer(), -1, pollMultiple);
103 }
104
105 if (aggregationStrategyRef != null) {
106 aggregationStrategy = routeContext.lookup(aggregationStrategyRef, AggregationStrategy.class);
107 }
108 if (aggregationStrategy == null) {
109 enricher.setDefaultAggregationStrategy();
110 } else {
111 enricher.setAggregationStrategy(aggregationStrategy);
112 }
113
114 return enricher;
115 }
116
117 public String getResourceUri() {
118 return resourceUri;
119 }
120
121 public void setResourceUri(String resourceUri) {
122 this.resourceUri = resourceUri;
123 }
124
125 public String getResourceRef() {
126 return resourceRef;
127 }
128
129 public void setResourceRef(String resourceRef) {
130 this.resourceRef = resourceRef;
131 }
132
133 public Long getTimeout() {
134 return timeout;
135 }
136
137 public void setTimeout(Long timeout) {
138 this.timeout = timeout;
139 }
140
141 public String getAggregationStrategyRef() {
142 return aggregationStrategyRef;
143 }
144
145 public void setAggregationStrategyRef(String aggregationStrategyRef) {
146 this.aggregationStrategyRef = aggregationStrategyRef;
147 }
148
149 public AggregationStrategy getAggregationStrategy() {
150 return aggregationStrategy;
151 }
152
153 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
154 this.aggregationStrategy = aggregationStrategy;
155 }
156
157 public Boolean isPollMultiple() {
158 return pollMultiple;
159 }
160
161 public void setPollMultiple(Boolean pollMultiple) {
162 this.pollMultiple = pollMultiple;
163 }
164 }