001package org.apache.maven.cli.logging;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.net.URL;
024import java.util.Enumeration;
025import java.util.LinkedHashMap;
026import java.util.Map;
027import java.util.Properties;
028import java.util.Set;
029
030import org.apache.maven.cli.logging.impl.UnsupportedSlf4jBindingConfiguration;
031import org.codehaus.plexus.util.PropertyUtils;
032import org.slf4j.ILoggerFactory;
033
034/**
035 * Slf4jConfiguration factory, loading implementations from <code>META-INF/maven/slf4j-configuration.properties</code>
036 * configuration files in class loader: key is the class name of the ILoggerFactory, value is the class name of
037 * the corresponding Slf4jConfiguration.
038 *
039 * @author Hervé Boutemy
040 * @since 3.1.0
041 */
042public class Slf4jConfigurationFactory
043{
044    public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
045
046    public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
047    {
048        Map<URL, Set<Object>> supported = new LinkedHashMap<>();
049
050        String slf4jBinding = loggerFactory.getClass().getCanonicalName();
051
052        try
053        {
054            Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
055
056            while ( resources.hasMoreElements() )
057            {
058                URL resource = resources.nextElement();
059
060                Properties conf = PropertyUtils.loadProperties( resource.openStream() );
061
062                String impl = conf.getProperty( slf4jBinding );
063
064                if ( impl != null )
065                {
066                    return (Slf4jConfiguration) Class.forName( impl ).newInstance();
067                }
068
069                supported.put( resource, conf.keySet() );
070            }
071        }
072        catch ( IOException | ClassNotFoundException | IllegalAccessException | InstantiationException e )
073        {
074            e.printStackTrace();
075        }
076
077        return new UnsupportedSlf4jBindingConfiguration( slf4jBinding, supported );
078    }
079}