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 */
017package org.apache.camel.model;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.Processor;
027import org.apache.camel.processor.AOPProcessor;
028import org.apache.camel.spi.RouteContext;
029
030/**
031 * Represents an XML <aop/> element
032 *
033 * @deprecated will be removed in the future. You can for example use {@link Processor} and
034 * {@link org.apache.camel.spi.InterceptStrategy} to do AOP in Camel.
035 * @version 
036 */
037@XmlRootElement(name = "aop")
038@XmlAccessorType(XmlAccessType.FIELD)
039@Deprecated
040public class AOPDefinition extends OutputDefinition<AOPDefinition> {
041    @XmlAttribute
042    private String beforeUri;
043    @XmlAttribute
044    private String afterUri;
045    @XmlAttribute
046    private String afterFinallyUri;
047
048    public AOPDefinition() {
049    }
050
051    @Override
052    public String toString() {
053        return "AOP[" + getOutputs() + "]";
054    }
055
056    public String getBeforeUri() {
057        return beforeUri;
058    }
059
060    public void setBeforeUri(String beforeUri) {
061        this.beforeUri = beforeUri;
062    }
063
064    public String getAfterUri() {
065        return afterUri;
066    }
067
068    public void setAfterUri(String afterUri) {
069        this.afterUri = afterUri;
070    }
071
072    public String getAfterFinallyUri() {
073        return afterFinallyUri;
074    }
075
076    public void setAfterFinallyUri(String afterFinallyUri) {
077        this.afterFinallyUri = afterFinallyUri;
078    }
079
080    @Override
081    public String getShortName() {
082        return "aop";
083    }
084
085    @Override
086    public String getLabel() {
087        return "aop";
088    }
089
090    @Override
091    public Processor createProcessor(final RouteContext routeContext) throws Exception {
092        // either before or after must be provided
093        if (beforeUri == null && afterUri == null && afterFinallyUri == null) {
094            throw new IllegalArgumentException("At least one of before, after or afterFinally must be provided on: " + this);
095        }
096
097        // use a pipeline to assemble the before and target processor
098        // and the after if not afterFinally
099        Collection<ProcessorDefinition<?>> pipe = new ArrayList<ProcessorDefinition<?>>();
100
101        Processor finallyProcessor = null;
102
103        if (beforeUri != null) {
104            pipe.add(new ToDefinition(beforeUri));
105        }
106        pipe.addAll(getOutputs());
107
108        if (afterUri != null) {
109            pipe.add(new ToDefinition(afterUri));
110        } else if (afterFinallyUri != null) {
111            finallyProcessor = createProcessor(routeContext, new ToDefinition(afterFinallyUri));
112        }
113
114        Processor tryProcessor = createOutputsProcessor(routeContext, pipe);
115
116        // the AOP processor is based on TryProcessor so we do not have any catches
117        return new AOPProcessor(tryProcessor, null, finallyProcessor);
118    }
119
120    /**
121     * Uses a AOP around.
122     *
123     * @param beforeUri the uri of the before endpoint
124     * @param afterUri  the uri of the after endpoint
125     * @return the builder
126     */
127    public AOPDefinition around(String beforeUri, String afterUri) {
128        this.beforeUri = beforeUri;
129        this.afterUri = afterUri;
130        this.afterFinallyUri = null;
131        return this;
132    }
133
134    /**
135     * Uses a AOP around with after being invoked in a finally block
136     *
137     * @param beforeUri the uri of the before endpoint
138     * @param afterUri  the uri of the after endpoint
139     * @return the builder
140     */
141    public AOPDefinition aroundFinally(String beforeUri, String afterUri) {
142        this.beforeUri = beforeUri;
143        this.afterUri = null;
144        this.afterFinallyUri = afterUri;
145        return this;
146    }
147
148    /**
149     * Uses a AOP before.
150     *
151     * @param beforeUri the uri of the before endpoint
152     * @return the builder
153     */
154    public AOPDefinition before(String beforeUri) {
155        this.beforeUri = beforeUri;
156        this.afterUri = null;
157        this.afterFinallyUri = null;
158        return this;
159    }
160
161    /**
162     * Uses a AOP after.
163     *
164     * @param afterUri  the uri of the after endpoint
165     * @return the builder
166     */
167    public AOPDefinition after(String afterUri) {
168        this.beforeUri = null;
169        this.afterUri = afterUri;
170        this.afterFinallyUri = null;
171        return this;
172    }
173
174    /**
175     * Uses a AOP after with after being invoked in a finally block.
176     *
177     * @param afterUri  the uri of the after endpoint
178     * @return the builder
179     */
180    public AOPDefinition afterFinally(String afterUri) {
181        this.beforeUri = null;
182        this.afterUri = null;
183        this.afterFinallyUri = afterUri;
184        return this;
185    }
186}