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.impl;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.EndpointConfiguration;
024import org.apache.camel.RuntimeCamelException;
025import org.apache.camel.util.ObjectHelper;
026import org.apache.camel.util.UnsafeUriCharactersEncoder;
027
028/**
029 * Default implementation of {@link EndpointConfiguration}.
030 *
031 * @version 
032 */
033@Deprecated
034public abstract class DefaultEndpointConfiguration implements EndpointConfiguration {
035
036    private final CamelContext camelContext;
037    private URI uri;
038
039    public DefaultEndpointConfiguration(CamelContext camelContext) {
040        ObjectHelper.notNull(camelContext, "CamelContext");
041        this.camelContext = camelContext;
042    }
043
044    public DefaultEndpointConfiguration(CamelContext camelContext, String uri) {
045        this(camelContext);
046        try {
047            setURI(new URI(uri));
048        } catch (URISyntaxException e) {
049            throw new RuntimeCamelException(e);
050        }
051    }
052
053    @Override
054    public URI getURI() {
055        return uri;
056    }
057
058    public void setURI(URI uri) {
059        this.uri = uri;
060        parseURI();
061    }
062
063    public void setURI(String uri) {
064        try {
065            String encoded = UnsafeUriCharactersEncoder.encode(uri);
066            setURI(new URI(encoded));
067        } catch (URISyntaxException e) {
068            throw new RuntimeCamelException("Cannot parse uri: " + uri, e);
069        }
070    }
071
072    @Override
073    @SuppressWarnings("unchecked")
074    public <T> T getParameter(String name) {
075        return (T)ConfigurationHelper.getConfigurationParameter(this, name);
076    }
077
078    @Override
079    public <T> void setParameter(String name, T value) {
080        ConfigurationHelper.setConfigurationField(camelContext, this, name, value);
081    }
082
083    protected CamelContext getCamelContext() {
084        return camelContext;
085    }
086
087    protected void parseURI() {
088        ConfigurationHelper.populateFromURI(camelContext, this, new ConfigurationHelper.FieldParameterSetter());
089    }
090}