mirror of https://github.com/apache/maven.git
refactor reporter
git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@290070 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f91b54b41c
commit
cbfc69ab90
|
@ -23,6 +23,7 @@ import org.apache.maven.tools.repoclean.index.ArtifactIndexer;
|
||||||
import org.apache.maven.tools.repoclean.phase.DiscoveryPhase;
|
import org.apache.maven.tools.repoclean.phase.DiscoveryPhase;
|
||||||
import org.apache.maven.tools.repoclean.phase.RewritePhase;
|
import org.apache.maven.tools.repoclean.phase.RewritePhase;
|
||||||
import org.apache.maven.tools.repoclean.report.FileReporter;
|
import org.apache.maven.tools.repoclean.report.FileReporter;
|
||||||
|
import org.apache.maven.tools.repoclean.report.Reporter;
|
||||||
import org.codehaus.plexus.PlexusConstants;
|
import org.codehaus.plexus.PlexusConstants;
|
||||||
import org.codehaus.plexus.PlexusContainer;
|
import org.codehaus.plexus.PlexusContainer;
|
||||||
import org.codehaus.plexus.context.Context;
|
import org.codehaus.plexus.context.Context;
|
||||||
|
@ -78,7 +79,7 @@ public class RepositoryCleaner
|
||||||
{
|
{
|
||||||
Logger logger = getLogger();
|
Logger logger = getLogger();
|
||||||
|
|
||||||
FileReporter repoReporter = null;
|
Reporter repoReporter = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
repoReporter = new FileReporter( reportsBase, "repository.report.txt",
|
repoReporter = new FileReporter( reportsBase, "repository.report.txt",
|
||||||
|
|
|
@ -0,0 +1,125 @@
|
||||||
|
package org.apache.maven.tools.repoclean.report;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2001-2005 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base implementation of reporter.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
|
||||||
|
* @version $Id$
|
||||||
|
*/
|
||||||
|
public abstract class AbstractReporter
|
||||||
|
implements Reporter
|
||||||
|
{
|
||||||
|
protected static final String WARN_LEVEL = "[WARNING]";
|
||||||
|
|
||||||
|
protected static final String ERROR_LEVEL = "[ERROR]";
|
||||||
|
|
||||||
|
protected boolean hasError;
|
||||||
|
|
||||||
|
protected boolean hasWarning;
|
||||||
|
|
||||||
|
protected final boolean warningsEnabled;
|
||||||
|
|
||||||
|
protected AbstractReporter( boolean warningsEnabled )
|
||||||
|
{
|
||||||
|
this.warningsEnabled = warningsEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasWarning()
|
||||||
|
{
|
||||||
|
return hasWarning;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasError()
|
||||||
|
{
|
||||||
|
return hasError;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getSourceLine()
|
||||||
|
{
|
||||||
|
NullPointerException npe = new NullPointerException();
|
||||||
|
|
||||||
|
StackTraceElement element = npe.getStackTrace()[2];
|
||||||
|
|
||||||
|
return "Reported from: (" + element.getClassName() + "." + element.getMethodName() + "(..):" +
|
||||||
|
element.getLineNumber() + ")\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String format( String level, String source, String message )
|
||||||
|
{
|
||||||
|
return format( level, source, message, null );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String format( String level, String source, String message, Throwable error )
|
||||||
|
{
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
buffer.append( level );
|
||||||
|
buffer.append( " " );
|
||||||
|
buffer.append( source );
|
||||||
|
buffer.append( " " );
|
||||||
|
buffer.append( message );
|
||||||
|
if ( error != null )
|
||||||
|
{
|
||||||
|
buffer.append( formatThrowable( error ) );
|
||||||
|
}
|
||||||
|
return buffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatThrowable( Throwable throwable )
|
||||||
|
{
|
||||||
|
StringWriter sWriter = new StringWriter();
|
||||||
|
PrintWriter pWriter = new PrintWriter( sWriter );
|
||||||
|
|
||||||
|
throwable.printStackTrace( pWriter );
|
||||||
|
|
||||||
|
return sWriter.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void write( String message )
|
||||||
|
throws ReportWriteException;
|
||||||
|
|
||||||
|
public void warn( String message )
|
||||||
|
throws ReportWriteException
|
||||||
|
{
|
||||||
|
if ( warningsEnabled )
|
||||||
|
{
|
||||||
|
hasWarning = true;
|
||||||
|
String source = getSourceLine();
|
||||||
|
write( format( WARN_LEVEL, source, message ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error( String message, Throwable error )
|
||||||
|
throws ReportWriteException
|
||||||
|
{
|
||||||
|
hasError = true;
|
||||||
|
String source = getSourceLine();
|
||||||
|
write( format( ERROR_LEVEL, source, message, error ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void error( String message )
|
||||||
|
throws ReportWriteException
|
||||||
|
{
|
||||||
|
hasError = true;
|
||||||
|
String source = getSourceLine();
|
||||||
|
write( format( ERROR_LEVEL, source, message ) );
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,39 +21,21 @@ import org.codehaus.plexus.util.IOUtil;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jdcasey
|
* @author jdcasey
|
||||||
*/
|
*/
|
||||||
public class FileReporter
|
public class FileReporter
|
||||||
implements Reporter
|
extends AbstractReporter
|
||||||
{
|
{
|
||||||
|
|
||||||
private static final String WARN_LEVEL = "[WARNING] ";
|
|
||||||
|
|
||||||
private static final String ERROR_LEVEL = "[ERROR] ";
|
|
||||||
|
|
||||||
private File reportsFile;
|
private File reportsFile;
|
||||||
|
|
||||||
private List messages = new ArrayList();
|
|
||||||
|
|
||||||
private boolean hasError = false;
|
|
||||||
|
|
||||||
private boolean hasWarning = false;
|
|
||||||
|
|
||||||
private Writer writer;
|
private Writer writer;
|
||||||
|
|
||||||
private final boolean warningsEnabled;
|
|
||||||
|
|
||||||
public FileReporter( File reportsBase, String reportPath, boolean warningsEnabled )
|
public FileReporter( File reportsBase, String reportPath, boolean warningsEnabled )
|
||||||
{
|
{
|
||||||
this.warningsEnabled = warningsEnabled;
|
super( warningsEnabled );
|
||||||
|
|
||||||
this.reportsFile = new File( reportsBase, reportPath );
|
this.reportsFile = new File( reportsBase, reportPath );
|
||||||
|
|
||||||
|
@ -87,7 +69,7 @@ public class FileReporter
|
||||||
IOUtil.close( writer );
|
IOUtil.close( writer );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void write( Object message )
|
protected void write( String message )
|
||||||
throws ReportWriteException
|
throws ReportWriteException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -97,14 +79,7 @@ public class FileReporter
|
||||||
open();
|
open();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( message instanceof List )
|
writer.write( message );
|
||||||
{
|
|
||||||
writer.write( format( (List) message ).toString() );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
writer.write( String.valueOf( message ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.write( '\n' );
|
writer.write( '\n' );
|
||||||
|
|
||||||
|
@ -116,97 +91,4 @@ public class FileReporter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasWarning()
|
|
||||||
{
|
|
||||||
return hasWarning;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasError()
|
|
||||||
{
|
|
||||||
return hasError;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void warn( String message )
|
|
||||||
throws ReportWriteException
|
|
||||||
{
|
|
||||||
if ( warningsEnabled )
|
|
||||||
{
|
|
||||||
hasWarning = true;
|
|
||||||
String source = getSourceLine();
|
|
||||||
write( new AppendingList( 3 ).append( WARN_LEVEL ).append( source ).append( message ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error( String message, Throwable error )
|
|
||||||
throws ReportWriteException
|
|
||||||
{
|
|
||||||
hasError = true;
|
|
||||||
String source = getSourceLine();
|
|
||||||
write( new AppendingList( 4 ).append( ERROR_LEVEL ).append( source ).append( message ).append( error ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void error( String message )
|
|
||||||
throws ReportWriteException
|
|
||||||
{
|
|
||||||
hasError = true;
|
|
||||||
String source = getSourceLine();
|
|
||||||
write( new AppendingList( 3 ).append( ERROR_LEVEL ).append( source ).append( message ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getSourceLine()
|
|
||||||
{
|
|
||||||
NullPointerException npe = new NullPointerException();
|
|
||||||
|
|
||||||
StackTraceElement element = npe.getStackTrace()[2];
|
|
||||||
|
|
||||||
return " Reported from: (" + element.getClassName() + "." + element.getMethodName() + "(..):" +
|
|
||||||
element.getLineNumber() + ")\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
private CharSequence format( List messageParts )
|
|
||||||
{
|
|
||||||
StringBuffer buffer = new StringBuffer();
|
|
||||||
for ( Iterator it = messageParts.iterator(); it.hasNext(); )
|
|
||||||
{
|
|
||||||
Object part = it.next();
|
|
||||||
if ( part instanceof Throwable )
|
|
||||||
{
|
|
||||||
part = formatThrowable( (Throwable) part );
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.append( part );
|
|
||||||
}
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String formatThrowable( Throwable throwable )
|
|
||||||
{
|
|
||||||
StringWriter sWriter = new StringWriter();
|
|
||||||
PrintWriter pWriter = new PrintWriter( sWriter );
|
|
||||||
|
|
||||||
throwable.printStackTrace( pWriter );
|
|
||||||
|
|
||||||
return sWriter.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class AppendingList
|
|
||||||
extends ArrayList
|
|
||||||
{
|
|
||||||
public AppendingList()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public AppendingList( int size )
|
|
||||||
{
|
|
||||||
super( size );
|
|
||||||
}
|
|
||||||
|
|
||||||
public AppendingList append( Object item )
|
|
||||||
{
|
|
||||||
super.add( item );
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue