001    package 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    
022    import java.io.IOException;
023    import java.net.URL;
024    import java.util.Enumeration;
025    import java.util.Properties;
026    
027    import org.codehaus.plexus.util.PropertyUtils;
028    import org.slf4j.ILoggerFactory;
029    
030    /**
031     * Slf4jConfiguration factory, loading implementations from <code>META-INF/maven/slf4j-configuration.properties</code>
032     * configuration files in class loader.
033     *
034     * @author Hervé Boutemy
035     */
036    public class Slf4jConfigurationFactory
037    {
038        public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
039    
040        public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
041        {
042            try
043            {
044                Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
045    
046                String key = loggerFactory.getClass().getCanonicalName();
047    
048                while ( resources.hasMoreElements() )
049                {
050                    URL resource = resources.nextElement();
051    
052                    Properties conf = PropertyUtils.loadProperties( resource.openStream() );
053    
054                    String impl = conf.getProperty( key );
055    
056                    if ( impl != null )
057                    {
058                        return (Slf4jConfiguration) Class.forName( impl ).newInstance();
059                    }
060                }
061            }
062            catch ( IOException e )
063            {
064                e.printStackTrace();
065            }
066            catch ( InstantiationException e )
067            {
068                e.printStackTrace();
069            }
070            catch ( IllegalAccessException e )
071            {
072                e.printStackTrace();
073            }
074            catch ( ClassNotFoundException e )
075            {
076                e.printStackTrace();
077            }
078    
079            return new BaseSlf4jConfiguration();
080        }
081    }