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