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