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.lang.reflect.Field; 020import java.net.URI; 021import java.net.URISyntaxException; 022import java.util.Map; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.Component; 026import org.apache.camel.EndpointConfiguration; 027import org.apache.camel.RuntimeCamelException; 028import org.apache.camel.URIField; 029import org.apache.camel.util.IntrospectionSupport; 030import org.apache.camel.util.URISupport; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Some helper methods for working with {@link EndpointConfiguration} instances 036 * 037 */ 038@Deprecated 039public final class ConfigurationHelper { 040 041 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationHelper.class); 042 043 private ConfigurationHelper() { 044 //Utility Class 045 } 046 047 public interface ParameterSetter { 048 049 /** 050 * Sets the parameter on the configuration. 051 * 052 * @param camelContext the camel context 053 * @param config the configuration 054 * @param name the name of the parameter 055 * @param value the value to set 056 * @throws RuntimeCamelException is thrown if error setting the parameter 057 */ 058 <T> void set(CamelContext camelContext, EndpointConfiguration config, String name, T value) throws RuntimeCamelException; 059 } 060 061 public static EndpointConfiguration createConfiguration(String uri, CamelContext context) throws Exception { 062 int schemeSeparator = uri.indexOf(':'); 063 if (schemeSeparator == -1) { 064 // not an URIConfiguration 065 return null; 066 } 067 String scheme = uri.substring(0, schemeSeparator); 068 069 Component component = context.getComponent(scheme); 070 if (LOG.isTraceEnabled()) { 071 LOG.trace("Lookup for Component handling \"{}:\" configuration returned {}", 072 new Object[]{scheme, component != null ? component.getClass().getName() : "<null>"}); 073 } 074 if (component != null) { 075 EndpointConfiguration config = component.createConfiguration(scheme); 076 if (config instanceof DefaultEndpointConfiguration) { 077 ((DefaultEndpointConfiguration) config).setURI(uri); 078 } 079 return config; 080 } else { 081 // no component to create the configuration 082 return null; 083 } 084 } 085 086 public static void populateFromURI(CamelContext camelContext, EndpointConfiguration config, ParameterSetter setter) { 087 URI uri = config.getURI(); 088 089 setter.set(camelContext, config, EndpointConfiguration.URI_SCHEME, uri.getScheme()); 090 setter.set(camelContext, config, EndpointConfiguration.URI_SCHEME_SPECIFIC_PART, uri.getSchemeSpecificPart()); 091 setter.set(camelContext, config, EndpointConfiguration.URI_AUTHORITY, uri.getAuthority()); 092 setter.set(camelContext, config, EndpointConfiguration.URI_USER_INFO, uri.getUserInfo()); 093 setter.set(camelContext, config, EndpointConfiguration.URI_HOST, uri.getHost()); 094 setter.set(camelContext, config, EndpointConfiguration.URI_PORT, Integer.toString(uri.getPort())); 095 setter.set(camelContext, config, EndpointConfiguration.URI_PATH, uri.getPath()); 096 setter.set(camelContext, config, EndpointConfiguration.URI_QUERY, uri.getQuery()); 097 setter.set(camelContext, config, EndpointConfiguration.URI_FRAGMENT, uri.getFragment()); 098 099 // now parse query and set custom parameters 100 Map<String, Object> parameters; 101 try { 102 parameters = URISupport.parseParameters(uri); 103 for (Map.Entry<String, Object> pair : parameters.entrySet()) { 104 setter.set(camelContext, config, pair.getKey(), pair.getValue()); 105 } 106 } catch (URISyntaxException e) { 107 throw new RuntimeCamelException(e); 108 } 109 } 110 111 public static Field findConfigurationField(EndpointConfiguration config, String name) { 112 if (config != null && name != null) { 113 Class<?> clazz = config.getClass(); 114 Field[] fields = clazz.getDeclaredFields(); 115 116 Field found; 117 URIField anno; 118 for (final Field field : fields) { 119 anno = field.getAnnotation(URIField.class); 120 if (anno == null ? field.getName().equals(name) : anno.component().equals(name) 121 || (anno.component().equals(EndpointConfiguration.URI_QUERY) && anno.parameter().equals(name))) { 122 123 found = field; 124 LOG.trace("Found field {}.{} as candidate for parameter {}", clazz.getName(), found.getName(), name); 125 return found; 126 } 127 } 128 } 129 return null; 130 } 131 132 public static Object getConfigurationParameter(EndpointConfiguration config, String name) { 133 Field field = findConfigurationField(config, name); 134 return getConfigurationParameter(config, field); 135 } 136 137 public static Object getConfigurationParameter(EndpointConfiguration config, Field field) { 138 if (field != null) { 139 try { 140 return IntrospectionSupport.getProperty(config, field.getName()); 141 } catch (Exception e) { 142 throw new RuntimeCamelException("Failed to get property '" + field.getName() + "' on " + config + " due " + e.getMessage(), e); 143 } 144 } 145 return null; 146 } 147 148 public static <T> void setConfigurationField(CamelContext camelContext, EndpointConfiguration config, String name, T value) { 149 Field field = findConfigurationField(config, name); 150 if (field == null) { 151 return; 152 } 153 154 try { 155 IntrospectionSupport.setProperty(camelContext.getTypeConverter(), config, name, value); 156 } catch (Exception e) { 157 throw new RuntimeCamelException("Failed to set property '" + name + "' on " + config + " due " + e.getMessage(), e); 158 } 159 } 160 161 public static class FieldParameterSetter implements ParameterSetter { 162 @Override 163 public <T> void set(CamelContext camelContext, EndpointConfiguration config, String name, T value) { 164 setConfigurationField(camelContext, config, name, value); 165 } 166 } 167}