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;
018
019/**
020 * Error binding property to a bean.
021 */
022public class PropertyBindingException extends RuntimeCamelException {
023
024    private final Object target;
025    private final String propertyName;
026    private final Object value;
027    private final String optionPrefix;
028    private final String optionKey;
029
030    public PropertyBindingException(Object target, String propertyName, Object value) {
031        this.target = target;
032        this.propertyName = propertyName;
033        this.value = value;
034        this.optionPrefix = null;
035        this.optionKey = null;
036    }
037
038    public PropertyBindingException(Object target, String propertyName, Object value, Throwable e) {
039        initCause(e);
040        this.target = target;
041        this.propertyName = propertyName;
042        this.value = value;
043        this.optionPrefix = null;
044        this.optionKey = null;
045    }
046
047    public PropertyBindingException(Object target, Throwable e) {
048        initCause(e);
049        this.target = target;
050        this.propertyName = null;
051        this.value = null;
052        this.optionPrefix = null;
053        this.optionKey = null;
054    }
055
056    public PropertyBindingException(Object target, String propertyName, Object value, String optionPrefix, String optionKey, Throwable e) {
057        initCause(e);
058        this.target = target;
059        this.propertyName = propertyName;
060        this.value = value;
061        this.optionPrefix = optionPrefix;
062        this.optionKey = optionKey;
063    }
064
065    @Override
066    public String getMessage() {
067        String stringValue = value != null ? value.toString() : "";
068        String key = propertyName;
069        if (optionPrefix != null && optionKey != null) {
070            key = optionPrefix + "." + optionKey;
071        }
072        if (key != null) {
073            return "Error binding property (" + key + "=" + stringValue + ") with name: " + propertyName
074                    + " on bean: " + target + " with value: " + stringValue;
075        } else {
076            return "Error binding properties on bean: " + target;
077        }
078    }
079
080    public Object getTarget() {
081        return target;
082    }
083
084    public String getPropertyName() {
085        return propertyName;
086    }
087
088    public Object getValue() {
089        return value;
090    }
091
092    public String getOptionPrefix() {
093        return optionPrefix;
094    }
095
096    public String getOptionKey() {
097        return optionKey;
098    }
099
100}