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.io.IOException; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.ExtendedCamelContext; 023import org.apache.camel.NoFactoryAvailableException; 024import org.apache.camel.RuntimeCamelException; 025import org.apache.camel.spi.FactoryFinder; 026import org.apache.camel.spi.XMLRoutesDefinitionLoader; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030/** 031 * Factory to create the {@link XMLRoutesDefinitionLoader} implementation to be used. 032 * 033 * @see XMLRoutesDefinitionLoader 034 */ 035public class XMLRoutesDefinitionLoaderResolver { 036 037 public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/"; 038 039 private static final Logger LOG = LoggerFactory.getLogger(XMLRoutesDefinitionLoaderResolver.class); 040 041 private FactoryFinder factoryFinder; 042 043 public XMLRoutesDefinitionLoader resolve(CamelContext context) { 044 // use factory finder to find a custom implementations 045 Class<?> type = null; 046 try { 047 type = findFactory("xmlroutes-loader", context); 048 } catch (Exception e) { 049 // ignore 050 } 051 052 if (type != null) { 053 if (LOG.isDebugEnabled()) { 054 LOG.debug("Found XMLRoutesDefinitionLoader: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), "xmlroutes-loader"); 055 } 056 if (XMLRoutesDefinitionLoader.class.isAssignableFrom(type)) { 057 XMLRoutesDefinitionLoader answer = (XMLRoutesDefinitionLoader) context.getInjector().newInstance(type, false); 058 LOG.info("Detected and using XMLRoutesDefinitionLoader: {}", answer); 059 return answer; 060 } else { 061 throw new IllegalArgumentException("Type is not a XMLRoutesDefinitionLoader implementation. Found: " + type.getName()); 062 } 063 } 064 065 throw new RuntimeCamelException("Cannot find XML routes loader in classpath. Add either camel-xml-io or camel-xml-jaxb to the classpath."); 066 } 067 068 private Class<?> findFactory(String name, CamelContext context) throws IOException { 069 if (factoryFinder == null) { 070 factoryFinder = context.adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH); 071 } 072 return factoryFinder.findClass(name).orElse(null); 073 } 074 075} 076