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.Enricher;
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 <enrich/> element
034     *
035     * @see Enricher
036     */
037    @XmlRootElement(name = "enrich")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class EnrichDefinition extends OutputDefinition<EnrichDefinition> {
040    
041        @XmlAttribute(name = "uri")
042        private String resourceUri;
043    
044        @XmlAttribute(name = "ref")
045        private String resourceRef;
046    
047        @XmlAttribute(name = "strategyRef", required = false)
048        private String aggregationStrategyRef;
049        
050        @XmlTransient
051        private AggregationStrategy aggregationStrategy;
052        
053        public EnrichDefinition() {
054            this(null, null);
055        }
056    
057        public EnrichDefinition(String resourceUri) {
058            this(null, resourceUri);
059        }
060        
061        public EnrichDefinition(AggregationStrategy aggregationStrategy, String resourceUri) {
062            this.aggregationStrategy = aggregationStrategy;
063            this.resourceUri = resourceUri;
064        }
065        
066        @Override
067        public String toString() {
068            return "Enrich[" + (resourceUri != null ? resourceUri : "ref:" + resourceRef) + " " + aggregationStrategy + "]";
069        }
070    
071        @Override
072        public String getShortName() {
073            return "enrich";
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            Enricher enricher = new Enricher(null, endpoint.createProducer());
091            if (aggregationStrategyRef != null) {
092                aggregationStrategy = routeContext.lookup(aggregationStrategyRef, AggregationStrategy.class);
093            }
094            if (aggregationStrategy == null) {
095                enricher.setDefaultAggregationStrategy();
096            } else {
097                enricher.setAggregationStrategy(aggregationStrategy);
098            }
099            return enricher;
100        }
101    
102        public String getResourceUri() {
103            return resourceUri;
104        }
105    
106        public void setResourceUri(String resourceUri) {
107            this.resourceUri = resourceUri;
108        }
109    
110        public String getResourceRef() {
111            return resourceRef;
112        }
113    
114        public void setResourceRef(String resourceRef) {
115            this.resourceRef = resourceRef;
116        }
117    
118        public String getAggregationStrategyRef() {
119            return aggregationStrategyRef;
120        }
121    
122        public void setAggregationStrategyRef(String aggregationStrategyRef) {
123            this.aggregationStrategyRef = aggregationStrategyRef;
124        }
125    
126        public AggregationStrategy getAggregationStrategy() {
127            return aggregationStrategy;
128        }
129    
130        public void setAggregationStrategy(AggregationStrategy aggregationStrategy) {
131            this.aggregationStrategy = aggregationStrategy;
132        }
133    }