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.processor.exceptionpolicy;
018
019import org.apache.camel.model.WhenDefinition;
020
021/**
022 * Exception policy key is a compound key for storing:
023 * <b>route id </b> + <b>exception class</b> + <b>when</b> => <b>exception type</b>.
024 * <p/>
025 * This is used by Camel to store the onException types configured that has or has not predicates attached (when).
026 */
027public final class ExceptionPolicyKey {
028
029    private final String routeId;
030    private final Class<? extends Throwable> exceptionClass;
031    private final WhenDefinition when;
032
033    /**
034     * @deprecated will be removed in the near future, use the other constructor
035     */
036    @Deprecated
037    public ExceptionPolicyKey(Class<? extends Throwable> exceptionClass, WhenDefinition when) {
038        this(null, exceptionClass, when);
039    }
040
041    /**
042     * Key for exception clause
043     *
044     * @param routeId          the route, or use <tt>null</tt> for a global scoped
045     * @param exceptionClass   the exception class
046     * @param when             optional predicate when the exception clause should trigger
047     */
048    public ExceptionPolicyKey(String routeId, Class<? extends Throwable> exceptionClass, WhenDefinition when) {
049        this.routeId = routeId;
050        this.exceptionClass = exceptionClass;
051        this.when = when;
052    }
053
054    public Class<?> getExceptionClass() {
055        return exceptionClass;
056    }
057
058    public WhenDefinition getWhen() {
059        return when;
060    }
061
062    public String getRouteId() {
063        return routeId;
064    }
065
066    /**
067     * @deprecated will be removed in the near future. Use the constructor instead.
068     */
069    @Deprecated
070    public static ExceptionPolicyKey newInstance(Class<? extends Throwable> exceptionClass) {
071        return new ExceptionPolicyKey(exceptionClass, null);
072    }
073
074    /**
075     * @deprecated will be removed in the near future. Use the constructor instead.
076     */
077    @Deprecated
078    public static ExceptionPolicyKey newInstance(Class<? extends Throwable> exceptionClass, WhenDefinition when) {
079        return new ExceptionPolicyKey(exceptionClass, when);
080    }
081
082    @Override
083    public boolean equals(Object o) {
084        if (this == o) {
085            return true;
086        }
087        if (o == null || getClass() != o.getClass()) {
088            return false;
089        }
090
091        ExceptionPolicyKey that = (ExceptionPolicyKey) o;
092
093        if (exceptionClass != null ? !exceptionClass.equals(that.exceptionClass) : that.exceptionClass != null) {
094            return false;
095        }
096        if (routeId != null ? !routeId.equals(that.routeId) : that.routeId != null) {
097            return false;
098        }
099        if (when != null ? !when.equals(that.when) : that.when != null) {
100            return false;
101        }
102
103        return true;
104    }
105
106    @Override
107    public int hashCode() {
108        int result = routeId != null ? routeId.hashCode() : 0;
109        result = 31 * result + (exceptionClass != null ? exceptionClass.hashCode() : 0);
110        result = 31 * result + (when != null ? when.hashCode() : 0);
111        return result;
112    }
113
114    @Override
115    public String toString() {
116        return "ExceptionPolicyKey[route: " + (routeId != null ? routeId : "<global>") + ", " + exceptionClass + (when != null ? " " + when : "") + "]";
117    }
118}