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