Author: olamy Date: Thu Nov 19 21:48:32 2009 New Revision: 882311 URL: http://svn.apache.org/viewvc?rev=882311&view=rev Log: [MCHECKSTYLE-123] refactoring to prevent duplicate codes.
Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleExecutorRequest.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleResults.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/DefaultCheckstyleExecutor.java Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleExecutorRequest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleExecutorRequest.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleExecutorRequest.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleExecutorRequest.java Thu Nov 19 21:48:32 2009 @@ -35,8 +35,7 @@ */ public class CheckstyleExecutorRequest { - private Configuration configuration; - + /** * Specifies the names filter of the source files to be used for Checkstyle. */ @@ -69,22 +68,27 @@ private ByteArrayOutputStream stringOutputStream; - public CheckstyleExecutorRequest(Configuration configuration) - { - this.configuration = configuration; - } + private String propertiesLocation; + + // + + private String configLocation; - public Configuration getConfiguration() - { - return configuration; - } + private String propertyExpansion; + + private String headerLocation; + + private String cacheFile; + + private String suppressionsFileExpression; + + private String encoding; - public CheckstyleExecutorRequest setConfiguration( Configuration configuration ) + public CheckstyleExecutorRequest( ) { - this.configuration = configuration; - return this; - } - + //nothing + } + public String getIncludes() { return includes; @@ -228,4 +232,79 @@ return this; } + public String getConfigLocation() + { + return configLocation; + } + + public CheckstyleExecutorRequest setConfigLocation( String configLocation ) + { + this.configLocation = configLocation; + return this; + } + + public String getPropertyExpansion() + { + return propertyExpansion; + } + + public CheckstyleExecutorRequest setPropertyExpansion( String propertyExpansion ) + { + this.propertyExpansion = propertyExpansion; + return this; + } + + public String getHeaderLocation() + { + return headerLocation; + } + + public CheckstyleExecutorRequest setHeaderLocation( String headerLocation ) + { + this.headerLocation = headerLocation; + return this; + } + + public String getCacheFile() + { + return cacheFile; + } + + public CheckstyleExecutorRequest setCacheFile( String cacheFile ) + { + this.cacheFile = cacheFile; + return this; + } + + public String getSuppressionsFileExpression() + { + return suppressionsFileExpression; + } + + public CheckstyleExecutorRequest setSuppressionsFileExpression( String suppressionsFileExpression ) + { + this.suppressionsFileExpression = suppressionsFileExpression; + return this; + } + + public String getEncoding() + { + return encoding; + } + + public CheckstyleExecutorRequest setEncoding( String encoding ) + { + this.encoding = encoding; + return this; + } + + public String getPropertiesLocation() + { + return propertiesLocation; + } + + public void setPropertiesLocation( String propertiesLocation ) + { + this.propertiesLocation = propertiesLocation; + } } Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java Thu Nov 19 21:48:32 2009 @@ -278,8 +278,7 @@ * </code> * </p> * - * @parameter expression="${checkstyle.header.file}" - * default-value="LICENSE.txt" + * @parameter expression="${checkstyle.header.file}" default-value="LICENSE.txt" * @since 2.0-beta-2 */ private String headerLocation; @@ -578,67 +577,28 @@ try { - // checkstyle will always use the context classloader in order - // to load resources (dtds), - // so we have to fix it - ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader(); - Thread.currentThread().setContextClassLoader( checkstyleClassLoader ); - - - String configFile = getConfigFile(); - Properties overridingProperties = getOverridingProperties(); - CheckstyleResults results; - - Configuration config = ConfigurationLoader.loadConfiguration( configFile, - new PropertiesExpander( overridingProperties ) ); - String effectiveEncoding = - StringUtils.isNotEmpty( encoding ) ? encoding : System.getProperty( "file.encoding", "UTF-8" ); - if ( StringUtils.isEmpty( encoding ) ) - { - getLog().warn( - "File encoding has not been set, using platform encoding " + effectiveEncoding - + ", i.e. build is platform dependent!" ); - } - Configuration[] modules = config.getChildren(); - for ( int i = 0; i < modules.length; i++ ) - { - Configuration module = modules[i]; - if ( "Checker".equals( module.getName() ) - || "com.puppycrawl.tools.checkstyle.Checker".equals( module.getName() ) ) - { - if ( module instanceof DefaultConfiguration ) - { - ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding ); - } - else - { - getLog().warn( "Failed to configure file encoding on module " + module ); - } - } - if ("TreeWalker".equals(module.getName()) - || "com.puppycrawl.tools.checkstyle.TreeWalker".equals(module.getName())) - { - if (module instanceof DefaultConfiguration) - { - ((DefaultConfiguration) module).addAttribute("cacheFile", cacheFile); - } - else - { - getLog().warn("Failed to configure cache file on module " + module); - } - } - } - - results = executeCheckstyle( config ); + getLog().info( "in mojo headerLocation " + headerLocation ); + CheckstyleExecutorRequest request = new CheckstyleExecutorRequest(); + request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput ) + .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes ) + .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() ) + .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory ) + .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation ) + .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation ) + .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation ) + .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression ) + .setEncoding( encoding ).setPropertiesLocation( propertiesLocation ); + + CheckstyleResults results = checkstyleExecutor.executeCheckstyle( request ); ResourceBundle bundle = getBundle( locale ); generateReportStatics(); - generateMainReport( results, config, bundle ); + generateMainReport( results, bundle ); if ( enableRSS ) { - CheckstyleRssGeneratorRequest request = + CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest = new CheckstyleRssGeneratorRequest( this.project, this.getCopyright(), outputDirectory, getLog() ); - checkstyleRssGenerator.generateRSS( results, request ); + checkstyleRssGenerator.generateRSS( results, checkstyleRssGeneratorRequest ); } } @@ -694,7 +654,7 @@ return copyright; } - private void generateMainReport( CheckstyleResults results, Configuration config, ResourceBundle bundle ) + private void generateMainReport( CheckstyleResults results, ResourceBundle bundle ) { CheckstyleReportGenerator generator = new CheckstyleReportGenerator( getSink(), bundle, project.getBasedir(), siteTool ); @@ -703,7 +663,7 @@ generator.setEnableSeveritySummary( enableSeveritySummary ); generator.setEnableFilesSummary( enableFilesSummary ); generator.setEnableRSS( enableRSS ); - generator.setCheckstyleConfig( config ); + generator.setCheckstyleConfig( results.getConfiguration() ); if ( linkXRef ) { String relativePath = PathTool.getRelativePath( getOutputDirectory(), xrefLocation.getAbsolutePath() ); @@ -786,18 +746,6 @@ } } - private CheckstyleResults executeCheckstyle( Configuration config ) - throws MavenReportException, CheckstyleException, CheckstyleExecutorException - { - CheckstyleExecutorRequest request = new CheckstyleExecutorRequest( config ); - request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput ).setExcludes( excludes ) - .setFailsOnError( failsOnError ).setIncludes( includes ) - .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() ).setLog( getLog() ) - .setProject( project ).setSourceDirectory( sourceDirectory ).setStringOutputStream( stringOutputStream ) - .setSuppressionsLocation( suppressionsLocation ).setTestSourceDirectory( testSourceDirectory ); - - return checkstyleExecutor.executeCheckstyle( request ); - } /** {...@inheritdoc} */ public String getOutputName() @@ -887,148 +835,6 @@ return (File[]) files.toArray( EMPTY_FILE_ARRAY ); } - - private Properties getOverridingProperties() - throws MavenReportException - { - Properties p = new Properties(); - - try - { - File propertiesFile = locator.resolveLocation( propertiesLocation, "checkstyle-checker.properties" ); - - if ( propertiesFile != null ) - { - p.load( new FileInputStream( propertiesFile ) ); - } - - if ( StringUtils.isNotEmpty( propertyExpansion ) ) - { - // Convert \ to \\, so that p.load will convert it back properly - propertyExpansion = StringUtils.replace( propertyExpansion, "\\", "\\\\" ); - p.load( new ByteArrayInputStream( propertyExpansion.getBytes() ) ); - } - - // Workaround for MCHECKSTYLE-48 - // Make sure that "config/maven-header.txt" is the default value - // for headerLocation, if configLocation="config/maven_checks.xml" - if ( "config/maven_checks.xml".equals( configLocation ) ) - { - if ( "LICENSE.txt".equals( headerLocation ) ) - { - headerLocation = "config/maven-header.txt"; - } - } - if ( StringUtils.isNotEmpty( headerLocation ) ) - { - try - { - File headerFile = locator.resolveLocation( headerLocation, "checkstyle-header.txt" ); - - if ( headerFile != null ) - { - p.setProperty( "checkstyle.header.file", headerFile.getAbsolutePath() ); - } - } - catch ( IOException e ) - { - throw new MavenReportException( "Unable to process header location: " + headerLocation, e ); - } - } - - if ( cacheFile != null ) - { - p.setProperty( "checkstyle.cache.file", cacheFile ); - } - } - catch ( IOException e ) - { - throw new MavenReportException( "Failed to get overriding properties", e ); - } - - if ( suppressionsFileExpression != null ) - { - String suppresionFile = getSuppressionLocation(); - - if ( suppresionFile != null ) - { - p.setProperty( suppressionsFileExpression, suppresionFile ); - } - } - - return p; - } - - private String getConfigFile() - throws MavenReportException - { - try - { - File configFile = locator.getResourceAsFile( configLocation, "checkstyle-checker.xml" ); - - if ( configFile == null ) - { - throw new MavenReportException( "Unable to process config location: " + configLocation ); - } - return configFile.getAbsolutePath(); - } - catch ( org.codehaus.plexus.resource.loader.ResourceNotFoundException e ) - { - throw new MavenReportException( "Unable to find configuration file at location " - + configLocation, e ); - } - catch ( FileResourceCreationException e ) - { - throw new MavenReportException( "Unable to process configuration file location " - + configLocation, e ); - } - - } - - private String getSuppressionLocation() - throws MavenReportException - { - try - { - File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" ); - - if ( suppressionsFile == null ) - { - return null; - } - - return suppressionsFile.getAbsolutePath(); - } - catch ( IOException e ) - { - throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e ); - } - } - - private FilterSet getSuppressions() - throws MavenReportException - { - try - { - File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" ); - - if ( suppressionsFile == null ) - { - return null; - } - - return SuppressionsLoader.loadSuppressions( suppressionsFile.getAbsolutePath() ); - } - catch ( CheckstyleException ce ) - { - throw new MavenReportException( "failed to load suppressions location: " + suppressionsLocation, ce ); - } - catch ( IOException e ) - { - throw new MavenReportException( "Failed to process supressions location: " + suppressionsLocation, e ); - } - } - private DefaultLogger getConsoleListener() throws MavenReportException { Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java Thu Nov 19 21:48:32 2009 @@ -22,6 +22,7 @@ import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.AutomaticBean; +import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; import org.codehaus.plexus.util.StringUtils; @@ -50,6 +51,8 @@ private List events; private SeverityLevel severityLevel; + + private Configuration checkstyleConfiguration; /** * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists @@ -59,6 +62,17 @@ this.sourceDirectories = new ArrayList(); this.sourceDirectories.add( sourceDirectory ); } + /** + * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists + * @param configuration checkstyle configuration + * @since 2.5 + */ + public CheckstyleReportListener( File sourceDirectory, Configuration configuration ) + { + this.sourceDirectories = new ArrayList(); + this.sourceDirectories.add( sourceDirectory ); + this.checkstyleConfiguration = configuration; + } /** * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists @@ -147,6 +161,7 @@ */ public CheckstyleResults getResults() { + results.setConfiguration( checkstyleConfiguration ); return results; } @@ -157,5 +172,22 @@ { this.results = results; } + + /** + * @since 2.5 + */ + public Configuration getCheckstyleConfiguration() + { + return checkstyleConfiguration; + } + + /** + * @since 2.5 + */ + public void setCheckstyleConfiguration( Configuration checkstyleConfiguration ) + { + this.checkstyleConfiguration = checkstyleConfiguration; + } + } Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleResults.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleResults.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleResults.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleResults.java Thu Nov 19 21:48:32 2009 @@ -20,6 +20,7 @@ */ import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; import java.util.HashMap; @@ -38,6 +39,8 @@ public class CheckstyleResults { private Map files; + + private Configuration configuration; public CheckstyleResults() { @@ -131,4 +134,14 @@ return count; } + + public Configuration getConfiguration() + { + return configuration; + } + + public void setConfiguration( Configuration configuration ) + { + this.configuration = configuration; + } } Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Thu Nov 19 21:48:32 2009 @@ -388,60 +388,16 @@ try { - // checkstyle will always use the context classloader in order - // to load resources (dtds), - // so we have to fix it - ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader(); - Thread.currentThread().setContextClassLoader( checkstyleClassLoader ); - String configFile = getConfigFile(); - Properties overridingProperties = getOverridingProperties(); - Configuration config = ConfigurationLoader - .loadConfiguration( configFile, new PropertiesExpander( overridingProperties ) ); - String effectiveEncoding = StringUtils.isNotEmpty( encoding ) ? encoding : System - .getProperty( "file.encoding", "UTF-8" ); - if ( StringUtils.isEmpty( encoding ) ) - { - getLog().warn( - "File encoding has not been set, using platform encoding " + effectiveEncoding - + ", i.e. build is platform dependent!" ); - } - Configuration[] modules = config.getChildren(); - for ( int i = 0; i < modules.length; i++ ) - { - Configuration module = modules[i]; - if ( "Checker".equals( module.getName() ) - || "com.puppycrawl.tools.checkstyle.Checker".equals( module.getName() ) ) - { - if ( module instanceof DefaultConfiguration ) - { - ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding ); - } - else - { - getLog().warn( "Failed to configure file encoding on module " + module ); - } - } - if ( "TreeWalker".equals( module.getName() ) - || "com.puppycrawl.tools.checkstyle.TreeWalker".equals( module.getName() ) ) - { - if ( module instanceof DefaultConfiguration ) - { - ( (DefaultConfiguration) module ).addAttribute( "cacheFile", cacheFile ); - } - else - { - getLog().warn( "Failed to configure cache file on module " + module ); - } - } - } - - CheckstyleExecutorRequest request = new CheckstyleExecutorRequest( config ); + CheckstyleExecutorRequest request = new CheckstyleExecutorRequest(); request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput ) .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes ) .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() ) .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory ) .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation ) - .setTestSourceDirectory( testSourceDirectory ); + .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation ) + .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation ) + .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression ) + .setEncoding( encoding ).setPropertiesLocation( propertiesLocation ); checkstyleExecutor.executeCheckstyle( request ); @@ -576,122 +532,6 @@ return false; } } - - private String getConfigFile() - throws MojoExecutionException - { - try - { - File configFile = locator.getResourceAsFile( configLocation, "checkstyle-checker.xml" ); - - if ( configFile == null ) - { - throw new MojoExecutionException( "Unable to process config location: " + configLocation ); - } - return configFile.getAbsolutePath(); - } - catch ( org.codehaus.plexus.resource.loader.ResourceNotFoundException e ) - { - throw new MojoExecutionException( "Unable to find configuration file at location " + configLocation, e ); - } - catch ( FileResourceCreationException e ) - { - throw new MojoExecutionException( "Unable to process configuration file location " + configLocation, e ); - } - - } - - private Properties getOverridingProperties() - throws MojoExecutionException - { - Properties p = new Properties(); - - try - { - File propertiesFile = locator.resolveLocation( propertiesLocation, "checkstyle-checker.properties" ); - - if ( propertiesFile != null ) - { - p.load( new FileInputStream( propertiesFile ) ); - } - - if ( StringUtils.isNotEmpty( propertyExpansion ) ) - { - // Convert \ to \\, so that p.load will convert it back properly - propertyExpansion = StringUtils.replace( propertyExpansion, "\\", "\\\\" ); - p.load( new ByteArrayInputStream( propertyExpansion.getBytes() ) ); - } - - // Workaround for MCHECKSTYLE-48 - // Make sure that "config/maven-header.txt" is the default value - // for headerLocation, if configLocation="config/maven_checks.xml" - if ( "config/maven_checks.xml".equals( configLocation ) ) - { - if ( "LICENSE.txt".equals( headerLocation ) ) - { - headerLocation = "config/maven-header.txt"; - } - } - if ( StringUtils.isNotEmpty( headerLocation ) ) - { - try - { - File headerFile = locator.resolveLocation( headerLocation, "checkstyle-header.txt" ); - - if ( headerFile != null ) - { - p.setProperty( "checkstyle.header.file", headerFile.getAbsolutePath() ); - } - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Unable to process header location: " + headerLocation, e ); - } - } - - if ( cacheFile != null ) - { - p.setProperty( "checkstyle.cache.file", cacheFile ); - } - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Failed to get overriding properties", e ); - } - - if ( suppressionsFileExpression != null ) - { - String suppresionFile = getSuppressionLocation(); - - if ( suppresionFile != null ) - { - p.setProperty( suppressionsFileExpression, suppresionFile ); - } - } - - return p; - } - - private String getSuppressionLocation() - throws MojoExecutionException - { - try - { - File suppressionsFile = locator.resolveLocation( suppressionsLocation, "checkstyle-suppressions.xml" ); - - if ( suppressionsFile == null ) - { - return null; - } - - return suppressionsFile.getAbsolutePath(); - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Failed to process supressions location: " + suppressionsLocation, e ); - } - } - private DefaultLogger getConsoleListener() throws MojoExecutionException { Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/DefaultCheckstyleExecutor.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/DefaultCheckstyleExecutor.java?rev=882311&r1=882310&r2=882311&view=diff ============================================================================== --- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/DefaultCheckstyleExecutor.java (original) +++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/DefaultCheckstyleExecutor.java Thu Nov 19 21:48:32 2009 @@ -18,7 +18,9 @@ */ package org.apache.maven.plugin.checkstyle; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -27,15 +29,24 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Properties; import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.resource.ResourceManager; +import org.codehaus.plexus.resource.loader.FileResourceCreationException; +import org.codehaus.plexus.resource.loader.ResourceNotFoundException; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.ConfigurationLoader; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.PackageNamesLoader; +import com.puppycrawl.tools.checkstyle.PropertiesExpander; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.FilterSet; import com.puppycrawl.tools.checkstyle.filters.SuppressionsLoader; @@ -46,6 +57,7 @@ * @version $Id$ */ public class DefaultCheckstyleExecutor + extends AbstractLogEnabled implements CheckstyleExecutor { @@ -59,6 +71,15 @@ public CheckstyleResults executeCheckstyle( CheckstyleExecutorRequest request ) throws CheckstyleExecutorException, CheckstyleException { + // checkstyle will always use the context classloader in order + // to load resources (dtds), + // so we have to fix it + ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader(); + Thread.currentThread().setContextClassLoader( checkstyleClassLoader ); + + + getLogger().info( "executeCheckstyle start headerLocation : " + request.getHeaderLocation() ); + locator.setOutputDirectory( new File( request.getProject().getBuild().getDirectory() ) ); File[] files; try { @@ -94,11 +115,11 @@ throw new CheckstyleExecutorException( e.getMessage(), e ); } - if (classPathStrings == null) + if ( classPathStrings == null ) { classPathStrings = Collections.EMPTY_LIST; } - + List urls = new ArrayList( classPathStrings.size() ); Iterator iter = classPathStrings.iterator(); @@ -148,8 +169,8 @@ { checker.addFilter( filterSet ); } - - checker.configure( request.getConfiguration() ); + Configuration configuration = getConfiguration( request ); + checker.configure( configuration ); AuditListener listener = request.getListener(); @@ -163,7 +184,7 @@ checker.addListener( request.getConsoleListener() ); } - CheckstyleReportListener sinkListener = new CheckstyleReportListener( request.getSourceDirectory() ); + CheckstyleReportListener sinkListener = new CheckstyleReportListener( request.getSourceDirectory(), configuration ); if ( request.isIncludeTestSourceDirectory() && ( request.getTestSourceDirectory() != null ) && ( request.getTestSourceDirectory().exists() ) && ( request.getTestSourceDirectory().isDirectory() ) ) { @@ -201,6 +222,156 @@ return sinkListener.getResults(); } + public Configuration getConfiguration( CheckstyleExecutorRequest request ) + throws CheckstyleExecutorException + { + try + { + // checkstyle will always use the context classloader in order + // to load resources (dtds), + // so we have to fix it + ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader(); + Thread.currentThread().setContextClassLoader( checkstyleClassLoader ); + String configFile = getConfigFile( request ); + Properties overridingProperties = getOverridingProperties( request ); + Configuration config = ConfigurationLoader + .loadConfiguration( configFile, new PropertiesExpander( overridingProperties ) ); + String effectiveEncoding = StringUtils.isNotEmpty( request.getEncoding() ) ? request.getEncoding() : System + .getProperty( "file.encoding", "UTF-8" ); + if ( StringUtils.isEmpty( request.getEncoding() ) ) + { + request.getLog().warn( + "File encoding has not been set, using platform encoding " + effectiveEncoding + + ", i.e. build is platform dependent!" ); + } + Configuration[] modules = config.getChildren(); + for ( int i = 0; i < modules.length; i++ ) + { + Configuration module = modules[i]; + if ( "Checker".equals( module.getName() ) + || "com.puppycrawl.tools.checkstyle.Checker".equals( module.getName() ) ) + { + if ( module instanceof DefaultConfiguration ) + { + ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding ); + } + else + { + request.getLog().warn( "Failed to configure file encoding on module " + module ); + } + } + if ( "TreeWalker".equals( module.getName() ) + || "com.puppycrawl.tools.checkstyle.TreeWalker".equals( module.getName() ) ) + { + if ( module instanceof DefaultConfiguration ) + { + ( (DefaultConfiguration) module ).addAttribute( "cacheFile", request.getCacheFile() ); + } + else + { + request.getLog().warn( "Failed to configure cache file on module " + module ); + } + } + } + return config; + } + catch ( CheckstyleException e ) + { + throw new CheckstyleExecutorException( "Failed during checkstyle configuration", e ); + } + } + + private Properties getOverridingProperties( CheckstyleExecutorRequest request ) + throws CheckstyleExecutorException + { + Properties p = new Properties(); + + try + { + if ( request.getPropertiesLocation() != null ) + { + getLogger().info( "request.getPropertiesLocation() " + request.getPropertiesLocation() ); + File propertiesFile = locator.getResourceAsFile( request.getPropertiesLocation(), + "checkstyle-checker.properties" ); + + if ( propertiesFile != null ) + { + p.load( new FileInputStream( propertiesFile ) ); + } + } + + if ( StringUtils.isNotEmpty( request.getPropertyExpansion() ) ) + { + String propertyExpansion = request.getPropertyExpansion(); + // Convert \ to \\, so that p.load will convert it back properly + propertyExpansion = StringUtils.replace( propertyExpansion, "\\", "\\\\" ); + p.load( new ByteArrayInputStream( propertyExpansion.getBytes() ) ); + } + + // Workaround for MCHECKSTYLE-48 + // Make sure that "config/maven-header.txt" is the default value + // for headerLocation, if configLocation="config/maven_checks.xml" + String headerLocation = request.getHeaderLocation(); + if ( "config/maven_checks.xml".equals( request.getConfigLocation() ) ) + { + + if ( "LICENSE.txt".equals( request.getHeaderLocation() ) ) + { + headerLocation = "config/maven-header.txt"; + } + } + getLogger().info( "headerLocation " + headerLocation ); + if ( StringUtils.isNotEmpty( headerLocation ) ) + { + try + { + File headerFile = locator.getResourceAsFile( headerLocation, "checkstyle-header.txt" ); + + if ( headerFile != null ) + { + p.setProperty( "checkstyle.header.file", headerFile.getAbsolutePath() ); + } + } + catch ( FileResourceCreationException e ) + { + throw new CheckstyleExecutorException( "Unable to process header location: " + headerLocation, e ); + } + catch ( ResourceNotFoundException e ) + { + throw new CheckstyleExecutorException( "Unable to process header location: " + headerLocation, e ); + } + } + + if ( request.getCacheFile() != null ) + { + p.setProperty( "checkstyle.cache.file", request.getCacheFile() ); + } + } + catch ( IOException e ) + { + throw new CheckstyleExecutorException( "Failed to get overriding properties", e ); + } + catch ( FileResourceCreationException e ) + { + throw new CheckstyleExecutorException( "Failed to get overriding properties", e ); + } + catch ( ResourceNotFoundException e ) + { + throw new CheckstyleExecutorException( "Failed to get overriding properties", e ); + } + if ( request.getSuppressionsFileExpression() != null ) + { + String suppresionFile = request.getSuppressionsFileExpression(); + + if ( suppresionFile != null ) + { + p.setProperty( request.getSuppressionsFileExpression(), suppresionFile ); + } + } + + return p; + } + private File[] getFilesToProcess( CheckstyleExecutorRequest request ) throws IOException { @@ -259,4 +430,32 @@ + request.getSuppressionsLocation(), e ); } } + + private String getConfigFile( CheckstyleExecutorRequest request ) + throws CheckstyleExecutorException + { + try + { + getLogger().info( "request.getConfigLocation() " + request.getConfigLocation() ); + File configFile = locator.getResourceAsFile( request.getConfigLocation(), "checkstyle-checker.xml" ); + + if ( configFile == null ) + { + throw new CheckstyleExecutorException( "Unable to process config location: " + + request.getConfigLocation() ); + } + return configFile.getAbsolutePath(); + } + catch ( org.codehaus.plexus.resource.loader.ResourceNotFoundException e ) + { + throw new CheckstyleExecutorException( "Unable to find configuration file at location " + + request.getConfigLocation(), e ); + } + catch ( FileResourceCreationException e ) + { + throw new CheckstyleExecutorException( "Unable to process configuration file location " + + request.getConfigLocation(), e ); + } + + } }