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