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