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.spi; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.Component; 024import org.apache.camel.util.IntrospectionSupport; 025import org.apache.camel.util.ServiceHelper; 026import org.apache.camel.util.StringHelper; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * Helper for creating configured {@link Component}s used by the 032 * {@link RestProducerFactory} contract. 033 * 034 * When {@link RestProducerFactory} contract is used it could instantiate, start 035 * and register the underlying component. During this process we have no way of 036 * configuring component properties, most notably the SSL properties. 037 */ 038public final class RestProducerFactoryHelper { 039 040 private static final Logger LOG = LoggerFactory.getLogger(RestProducerFactoryHelper.class); 041 042 private RestProducerFactoryHelper() { 043 // helper class 044 } 045 046 public static void setupComponentFor(final String url, final CamelContext camelContext, 047 final Map<String, Object> componentProperties) throws Exception { 048 final String scheme = StringHelper.before(url, ":"); 049 050 setupComponent(scheme, camelContext, componentProperties); 051 } 052 053 public static Component setupComponent(final String componentName, final CamelContext camelContext, 054 final Map<String, Object> componentProperties) throws Exception { 055 if (componentProperties == null || componentProperties.isEmpty()) { 056 return camelContext.getComponent(componentName); 057 } 058 059 final Component existing = camelContext.getComponent(componentName, false, false); 060 if (existing != null) { 061 if (!componentProperties.isEmpty()) { 062 LOG.warn( 063 "Found existing `{}` component already present in the Camel context. Not setting component" 064 + " properties on the existing component. You can either prevent the component creation or" 065 + " set the given properties on the component. Component properties given: {}", 066 componentName, componentProperties); 067 } 068 069 return existing; 070 } 071 072 // component was not added to the context we can configure it 073 final Component newlyCreated = camelContext.getComponent(componentName, true, false); 074 // need to make a copy of the component properties as 075 // IntrospectionSupport::setProperties will remove any that are set and 076 // we might be called multiple times 077 final Map<String, Object> copyOfComponentProperties = new HashMap<>(componentProperties); 078 IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), newlyCreated, 079 copyOfComponentProperties); 080 ServiceHelper.startService(newlyCreated); 081 082 return newlyCreated; 083 } 084}