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.impl;
018    
019    import java.util.Map;
020    import java.util.Set;
021    import java.util.SortedMap;
022    import java.util.TreeMap;
023    
024    import org.apache.camel.Component;
025    import org.apache.camel.Endpoint;
026    import org.apache.camel.InvalidPropertyException;
027    import org.apache.camel.RuntimeCamelException;
028    import org.apache.camel.util.IntrospectionSupport;
029    
030    /**
031     * Default implementation for components which do not inherit from {@link UriEndpointComponent} and
032     * do not have Endpoint classes annotated with {@link org.apache.camel.spi.UriEndpoint}
033     */
034    public class DefaultComponentConfiguration extends ComponentConfigurationSupport {
035    
036        public DefaultComponentConfiguration(Component component) {
037            super(component);
038        }
039    
040        @Override
041        public Object getEndpointParameter(Endpoint endpoint, String name) throws RuntimeCamelException {
042            try {
043                return IntrospectionSupport.getProperty(endpoint, name);
044            } catch (Exception e) {
045                throw new RuntimeCamelException("Failed to get property " + name + " on endpoint " + endpoint + " due to " + e.getMessage(), e);
046            }
047        }
048    
049        @Override
050        public void setEndpointParameter(Endpoint endpoint, String name, Object value) throws RuntimeCamelException {
051            boolean answer;
052            try {
053                answer = IntrospectionSupport.setProperty(endpoint, name, value);
054            } catch (Exception e) {
055                throw new RuntimeCamelException(
056                        "Failed to set property " + name + " with value " + value + " on endpoint " + endpoint + " due to " + e.getMessage(), e);
057            }
058            if (!answer) {
059                throw new InvalidPropertyException(endpoint, name);
060            }
061        }
062    
063        /**
064         * Since we have no parameter metadata lets just return parameter configurations for each parameter we
065         * have right now.
066         *
067         * @return configurations for each current property value
068         */
069        @Override
070        public SortedMap<String, ParameterConfiguration> getParameterConfigurationMap() {
071            SortedMap<String, ParameterConfiguration> answer = new TreeMap<String, ParameterConfiguration>();
072            Set<Map.Entry<String, Object>> entries = getParameters().entrySet();
073            for (Map.Entry<String, Object> entry : entries) {
074                String name = entry.getKey();
075                Object value = entry.getValue();
076                Class<?> type = (value != null) ? value.getClass() : String.class;
077                answer.put(name, new ParameterConfiguration(name, type));
078            }
079            return answer;
080        }
081    
082    }