[MNG-5390] mvn -rf (no argument) results in NPE

Created a simple primordial logger that implements the error methods required between CLI parsing and logger initialization. I don't want to have to look to see if the logger is null.
This commit is contained in:
Jason van Zyl 2012-11-30 15:05:12 -08:00
parent 4c8b3009d1
commit d5e5717dd7
3 changed files with 291 additions and 187 deletions

View File

@ -40,6 +40,7 @@
import org.apache.maven.cli.event.DefaultEventSpyContext;
import org.apache.maven.cli.event.ExecutionEventLogger;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.cli.logging.Slf4jStdoutLogger;
import org.apache.maven.cli.transfer.ConsoleMavenTransferListener;
import org.apache.maven.cli.transfer.QuietMavenTransferListener;
import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
@ -262,6 +263,12 @@ private void initialize( CliRequest cliRequest )
private void cli( CliRequest cliRequest )
throws Exception
{
//
// Parsing errors can happen during the processing of the arguments and we prefer not having to check if the logger is null
// and make construct this so we can use an SLF4J logger everywhere
//
slf4jLogger = new Slf4jStdoutLogger();
CLIManager cliManager = new CLIManager();
try

View File

@ -1,187 +0,0 @@
package org.apache.maven.cli.logging;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.PrintStream;
import org.apache.maven.Maven;
import org.codehaus.plexus.logging.AbstractLogger;
import org.codehaus.plexus.logging.Logger;
/**
* Logs to a user-supplied {@link PrintStream}.
*
* @author Benjamin Bentmann
*/
public class PrintStreamLogger
extends AbstractLogger
{
public interface Provider
{
PrintStream getStream();
}
private Provider provider;
private static final String FATAL_ERROR = "[FATAL] ";
private static final String ERROR = "[ERROR] ";
private static final String WARNING = "[WARNING] ";
private static final String INFO = "[INFO] ";
private static final String DEBUG = "[DEBUG] ";
public PrintStreamLogger( Provider provider )
{
super( Logger.LEVEL_INFO, Maven.class.getName() );
if ( provider == null )
{
throw new IllegalArgumentException( "output stream provider missing" );
}
this.provider = provider;
}
public PrintStreamLogger( PrintStream out )
{
super( Logger.LEVEL_INFO, Maven.class.getName() );
setStream( out );
}
public void setStream( final PrintStream out )
{
if ( out == null )
{
throw new IllegalArgumentException( "output stream missing" );
}
this.provider = new Provider()
{
public PrintStream getStream()
{
return out;
}
};
}
public void debug( String message, Throwable throwable )
{
if ( isDebugEnabled() )
{
PrintStream out = provider.getStream();
out.print( DEBUG );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void info( String message, Throwable throwable )
{
if ( isInfoEnabled() )
{
PrintStream out = provider.getStream();
out.print( INFO );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void warn( String message, Throwable throwable )
{
if ( isWarnEnabled() )
{
PrintStream out = provider.getStream();
out.print( WARNING );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void error( String message, Throwable throwable )
{
if ( isErrorEnabled() )
{
PrintStream out = provider.getStream();
out.print( ERROR );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void fatalError( String message, Throwable throwable )
{
if ( isFatalErrorEnabled() )
{
PrintStream out = provider.getStream();
out.print( FATAL_ERROR );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void close()
{
PrintStream out = provider.getStream();
if ( out == System.out || out == System.err )
{
out.flush();
}
else
{
out.close();
}
}
public Logger getChildLogger( String arg0 )
{
return this;
}
}

View File

@ -0,0 +1,284 @@
package org.apache.maven.cli.logging;
import java.io.PrintStream;
import org.slf4j.Logger;
import org.slf4j.Marker;
public class Slf4jStdoutLogger
implements Logger
{
private static final String ERROR = "[ERROR] ";
private PrintStream out = System.out;
//
// These are the only methods we need in our primordial logger
//
public void error( String msg )
{
out.print( ERROR );
out.println( msg );
}
public void error( String msg, Throwable t )
{
error( msg );
if ( null != t )
{
t.printStackTrace( out );
}
}
//
// Don't need any of this
//
public String getName()
{
return null;
}
public boolean isTraceEnabled()
{
return false;
}
public void trace( String msg )
{
}
public void trace( String format, Object arg )
{
}
public void trace( String format, Object arg1, Object arg2 )
{
}
public void trace( String format, Object... arguments )
{
}
public void trace( String msg, Throwable t )
{
}
public boolean isTraceEnabled( Marker marker )
{
return false;
}
public void trace( Marker marker, String msg )
{
}
public void trace( Marker marker, String format, Object arg )
{
}
public void trace( Marker marker, String format, Object arg1, Object arg2 )
{
}
public void trace( Marker marker, String format, Object... argArray )
{
}
public void trace( Marker marker, String msg, Throwable t )
{
}
public boolean isDebugEnabled()
{
return false;
}
public void debug( String msg )
{
}
public void debug( String format, Object arg )
{
}
public void debug( String format, Object arg1, Object arg2 )
{
}
public void debug( String format, Object... arguments )
{
}
public void debug( String msg, Throwable t )
{
}
public boolean isDebugEnabled( Marker marker )
{
return false;
}
public void debug( Marker marker, String msg )
{
}
public void debug( Marker marker, String format, Object arg )
{
}
public void debug( Marker marker, String format, Object arg1, Object arg2 )
{
}
public void debug( Marker marker, String format, Object... arguments )
{
}
public void debug( Marker marker, String msg, Throwable t )
{
}
public boolean isInfoEnabled()
{
return false;
}
public void info( String msg )
{
}
public void info( String format, Object arg )
{
}
public void info( String format, Object arg1, Object arg2 )
{
}
public void info( String format, Object... arguments )
{
}
public void info( String msg, Throwable t )
{
}
public boolean isInfoEnabled( Marker marker )
{
return false;
}
public void info( Marker marker, String msg )
{
}
public void info( Marker marker, String format, Object arg )
{
}
public void info( Marker marker, String format, Object arg1, Object arg2 )
{
}
public void info( Marker marker, String format, Object... arguments )
{
}
public void info( Marker marker, String msg, Throwable t )
{
}
public boolean isWarnEnabled()
{
return false;
}
public void warn( String msg )
{
}
public void warn( String format, Object arg )
{
}
public void warn( String format, Object... arguments )
{
}
public void warn( String format, Object arg1, Object arg2 )
{
}
public void warn( String msg, Throwable t )
{
}
public boolean isWarnEnabled( Marker marker )
{
return false;
}
public void warn( Marker marker, String msg )
{
}
public void warn( Marker marker, String format, Object arg )
{
}
public void warn( Marker marker, String format, Object arg1, Object arg2 )
{
}
public void warn( Marker marker, String format, Object... arguments )
{
}
public void warn( Marker marker, String msg, Throwable t )
{
}
public boolean isErrorEnabled()
{
return false;
}
public void error( String format, Object arg )
{
}
public void error( String format, Object arg1, Object arg2 )
{
}
public void error( String format, Object... arguments )
{
}
public boolean isErrorEnabled( Marker marker )
{
return false;
}
public void error( Marker marker, String msg )
{
}
public void error( Marker marker, String format, Object arg )
{
}
public void error( Marker marker, String format, Object arg1, Object arg2 )
{
}
public void error( Marker marker, String format, Object... arguments )
{
}
public void error( Marker marker, String msg, Throwable t )
{
}
}