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.processor; 018 019import java.io.IOException; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.spi.FactoryFinder; 023import org.apache.camel.spi.SendDynamicAware; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027public class SendDynamicAwareResolver { 028 029 public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/send-dynamic/"; 030 031 private static final Logger LOG = LoggerFactory.getLogger(SendDynamicAwareResolver.class); 032 033 private FactoryFinder factoryFinder; 034 035 public SendDynamicAware resolve(CamelContext context, String scheme) { 036 String name = scheme; 037 038 // use factory finder to find a custom implementations 039 Class<?> type = null; 040 try { 041 type = findFactory(name, context); 042 } catch (Exception e) { 043 // ignore 044 } 045 046 if (type != null) { 047 if (LOG.isDebugEnabled()) { 048 LOG.debug("Found SendDynamicAware: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), name); 049 } 050 if (SendDynamicAware.class.isAssignableFrom(type)) { 051 SendDynamicAware answer = (SendDynamicAware) context.getInjector().newInstance(type); 052 answer.setScheme(scheme); 053 return answer; 054 } else { 055 throw new IllegalArgumentException("Type is not a SendDynamicAware implementation. Found: " + type.getName()); 056 } 057 } 058 059 return null; 060 } 061 062 private Class<?> findFactory(String name, CamelContext context) throws ClassNotFoundException, IOException { 063 if (factoryFinder == null) { 064 factoryFinder = context.getFactoryFinder(RESOURCE_PATH); 065 } 066 return factoryFinder.findClass(name); 067 } 068 069}