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    
052        public PollEnrichDefinition() {
053        }
054    
055        public PollEnrichDefinition(AggregationStrategy aggregationStrategy, String resourceUri, long timeout) {
056            this.aggregationStrategy = aggregationStrategy;
057            this.resourceUri = resourceUri;
058            this.timeout = timeout;
059        }
060    
061        @Override
062        public String toString() {
063            return "PollEnrich[" + description() + " " + aggregationStrategy + "]";
064        }
065        
066        protected String description() {
067            return FromDefinition.description(getResourceUri(), getResourceRef(), (Endpoint) null);
068        }
069    
070        @Override
071        public String getShortName() {
072            return "pollEnrich";
073        }
074        
075        @Override
076        public String getLabel() {
077            return "pollEnrich[" + description() + "]";
078        }
079    
080        @Override
081        public Processor createProcessor(RouteContext routeContext) throws Exception {
082            if (ObjectHelper.isEmpty(resourceUri) && ObjectHelper.isEmpty(resourceRef)) {
083                throw new IllegalArgumentException("Either uri or ref must be provided for resource endpoint");
084            }
085    
086            // lookup endpoint
087            Endpoint endpoint;
088            if (resourceUri != null) {
089                endpoint = routeContext.resolveEndpoint(resourceUri);
090            } else {
091                endpoint = routeContext.resolveEndpoint(null, resourceRef);
092            }
093    
094            PollEnricher enricher;
095            if (timeout != null) {
096                enricher = new PollEnricher(null, endpoint.createPollingConsumer(), timeout);
097            } else {
098                // if no timeout then we should block, and there use a negative timeout
099                enricher = new PollEnricher(null, endpoint.createPollingConsumer(), -1);
100            }
101    
102            if (aggregationStrategyRef != null) {
103                aggregationStrategy = routeContext.mandatoryLookup(aggregationStrategyRef, AggregationStrategy.class);
104            }
105            if (aggregationStrategy == null) {
106                enricher.setDefaultAggregationStrategy();
107            } else {
108                enricher.setAggregationStrategy(aggregationStrategy);
109            }
110    
111            return enricher;
112        }
113    
114        public String getResourceUri() {
115            return resourceUri;
116        }
117    
118        public void setResourceUri(String resourceUri) {
119            this.resourceUri = resourceUri;
120        }
121    
122        public String getResourceRef() {
123            return resourceRef;
124        }
125    
126        public void setResourceRef(String resourceRef) {
127            this.resourceRef = resourceRef;
128        }
129    
130        public Long getTimeout() {
131            return timeout;
132        }
133    
134        public void setTimeout(Long timeout) {
135            this.timeout = timeout;
136        }
137    
138        public String getAggregationStrategyRef() {
139            return aggregationStrategyRef;
140        }
141    
142        public void setAggregationStrategyRef(String aggregationStrategyRef) {
143            this.aggregationStrategyRef = aggregationStrategyRef;
144        }
145    
146        public AggregationStrategy getAggregationStrategy() {
147            return aggregationStrategy;
148        }
149    
150        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
151            this.aggregationStrategy = aggregationStrategy;
152        }
153    }