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.model.cloud; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import javax.xml.bind.annotation.XmlAccessType; 026import javax.xml.bind.annotation.XmlAccessorType; 027import javax.xml.bind.annotation.XmlElement; 028import javax.xml.bind.annotation.XmlRootElement; 029import javax.xml.bind.annotation.XmlTransient; 030 031import org.apache.camel.CamelContext; 032import org.apache.camel.ExtendedCamelContext; 033import org.apache.camel.NoFactoryAvailableException; 034import org.apache.camel.cloud.ServiceChooser; 035import org.apache.camel.cloud.ServiceChooserFactory; 036import org.apache.camel.model.IdentifiedType; 037import org.apache.camel.model.ProcessorDefinition; 038import org.apache.camel.model.PropertyDefinition; 039import org.apache.camel.spi.Metadata; 040import org.apache.camel.support.CamelContextHelper; 041import org.apache.camel.support.PropertyBindingSupport; 042import org.apache.camel.util.ObjectHelper; 043 044@Metadata(label = "routing,cloud,service-discovery") 045@XmlRootElement(name = "serviceChooserConfiguration") 046@XmlAccessorType(XmlAccessType.FIELD) 047public class ServiceCallServiceChooserConfiguration extends IdentifiedType implements ServiceChooserFactory { 048 @XmlTransient 049 private final ServiceCallDefinition parent; 050 @XmlTransient 051 private final String factoryKey; 052 @XmlElement(name = "properties") 053 @Metadata(label = "advanced") 054 private List<PropertyDefinition> properties; 055 056 public ServiceCallServiceChooserConfiguration() { 057 this(null, null); 058 } 059 060 public ServiceCallServiceChooserConfiguration(ServiceCallDefinition parent, String factoryKey) { 061 this.parent = parent; 062 this.factoryKey = factoryKey; 063 } 064 065 public ServiceCallDefinition end() { 066 return this.parent; 067 } 068 069 public ProcessorDefinition<?> endParent() { 070 return this.parent.end(); 071 } 072 073 // ************************************************************************* 074 // 075 // ************************************************************************* 076 077 public List<PropertyDefinition> getProperties() { 078 return properties; 079 } 080 081 /** 082 * Set client properties to use. 083 * <p/> 084 * These properties are specific to what service call implementation are in 085 * use. For example if using ribbon, then the client properties are define 086 * in com.netflix.client.config.CommonClientConfigKey. 087 */ 088 public void setProperties(List<PropertyDefinition> properties) { 089 this.properties = properties; 090 } 091 092 /** 093 * Adds a custom property to use. 094 * <p/> 095 * These properties are specific to what service call implementation are in 096 * use. For example if using ribbon, then the client properties are define 097 * in com.netflix.client.config.CommonClientConfigKey. 098 */ 099 public ServiceCallServiceChooserConfiguration property(String key, String value) { 100 if (properties == null) { 101 properties = new ArrayList<>(); 102 } 103 PropertyDefinition prop = new PropertyDefinition(); 104 prop.setKey(key); 105 prop.setValue(value); 106 properties.add(prop); 107 return this; 108 } 109 110 protected Map<String, String> getPropertiesAsMap(CamelContext camelContext) throws Exception { 111 Map<String, String> answer; 112 113 if (properties == null || properties.isEmpty()) { 114 answer = Collections.emptyMap(); 115 } else { 116 answer = new HashMap<>(); 117 for (PropertyDefinition prop : properties) { 118 // support property placeholders 119 String key = CamelContextHelper.parseText(camelContext, prop.getKey()); 120 String value = CamelContextHelper.parseText(camelContext, prop.getValue()); 121 answer.put(key, value); 122 } 123 } 124 125 return answer; 126 } 127 128 // ************************************************************************* 129 // Factory 130 // ************************************************************************* 131 132 @Override 133 public ServiceChooser newInstance(CamelContext camelContext) throws Exception { 134 ObjectHelper.notNull(factoryKey, "ServiceChooser factoryKey"); 135 136 ServiceChooser answer; 137 138 // First try to find the factory from the registry. 139 ServiceChooserFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceChooserFactory.class); 140 if (factory != null) { 141 // If a factory is found in the registry do not re-configure it as 142 // it should be pre-configured. 143 answer = factory.newInstance(camelContext); 144 } else { 145 146 Class<?> type; 147 try { 148 // Then use Service factory. 149 type = camelContext.adapt(ExtendedCamelContext.class).getFactoryFinder(ServiceCallDefinitionConstants.RESOURCE_PATH).findClass(factoryKey).orElse(null); 150 } catch (Exception e) { 151 throw new NoFactoryAvailableException(ServiceCallDefinitionConstants.RESOURCE_PATH + factoryKey, e); 152 } 153 154 if (type != null) { 155 if (ServiceChooserFactory.class.isAssignableFrom(type)) { 156 factory = (ServiceChooserFactory)camelContext.getInjector().newInstance(type, false); 157 } else { 158 throw new NoFactoryAvailableException("Resolving ServiceChooser: " + factoryKey + " detected type conflict: Not a ServiceChooserFactory implementation. Found: " 159 + type.getName()); 160 } 161 } 162 163 try { 164 Map<String, Object> parameters = new HashMap<>(); 165 camelContext.adapt(ExtendedCamelContext.class).getBeanIntrospection().getProperties(this, parameters, null, false); 166 167 parameters.replaceAll((k, v) -> { 168 if (v instanceof String) { 169 try { 170 v = camelContext.resolvePropertyPlaceholders((String)v); 171 } catch (Exception e) { 172 throw new IllegalArgumentException(String.format("Exception while resolving %s (%s)", k, v.toString()), e); 173 } 174 } 175 176 return v; 177 }); 178 179 // Convert properties to Map<String, String> 180 parameters.put("properties", getPropertiesAsMap(camelContext)); 181 182 postProcessFactoryParameters(camelContext, parameters); 183 184 PropertyBindingSupport.build().bind(camelContext, factory, parameters); 185 186 answer = factory.newInstance(camelContext); 187 } catch (Exception e) { 188 throw new IllegalArgumentException(e); 189 } 190 } 191 192 return answer; 193 } 194 195 // ************************************************************************* 196 // Utilities 197 // ************************************************************************* 198 199 protected void postProcessFactoryParameters(CamelContext camelContext, Map<String, Object> parameters) throws Exception { 200 } 201}