Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.codehaus.mojo.animal_sniffer.enforcer;
  
  /*
   * The MIT License
   *
   * Copyright (c) 2008 Kohsuke Kawaguchi and codehaus.org.
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  *
  */
 
 
 import java.io.File;
 import java.util.List;
 import java.util.Set;

Created by IntelliJ IDEA.

Author(s):
connollys
Since:
Sep 4, 2009 2:44:29 PM
 
 public class CheckSignatureRule
     implements EnforcerRule
 {
    
Signature module to use.

Required:
Parameter:
 
     protected Signature signature;

    
Class names to ignore signatures for (wildcards accepted).

Parameter:
 
     protected String[] ignores;

    
 
     protected String[] annotations;

    
Should dependencies be ignored.

Parameter:
default-value="true"
 
     protected boolean ignoreDependencies = true;

    
A list of artifact patterns to include. Patterns can include * as a wildcard match for any whole segment, valid patterns are:
  • groupId:artifactId
  • groupId:artifactId:type
  • groupId:artifactId:type:version
  • groupId:artifactId:type:classifier
  • groupId:artifactId:type:classifier:version

Since:
1.12
Parameter:
    private String[] includeDependencies = null;

    
A list of artifact patterns to exclude. Patterns can include * as a wildcard match for any whole segment, valid patterns are:
  • groupId:artifactId
  • groupId:artifactId:type
  • groupId:artifactId:type:version
  • groupId:artifactId:type:classifier
  • groupId:artifactId:type:classifier:version

Since:
1.12
Parameter:
    private String[] excludeDependencies = null;
    public void executeEnforcerRuleHelper helper )
        throws EnforcerRuleException
    {
        try
        {
            File outputDirectory = new File( (Stringhelper.evaluate"${project.build.outputDirectory}" ) );
            ArtifactResolver resolver = (ArtifactResolverhelper.getComponentArtifactResolver.class );
            MavenProject project = (MavenProjecthelper.evaluate"${project}" );
            ArtifactRepository localRepository = (ArtifactRepositoryhelper.evaluate"${localRepository}" );
            ArtifactFactory artifactFactory = (ArtifactFactoryhelper.getComponentArtifactFactory.class );
            if ( StringUtils.isEmpty.getVersion() ) )
            {
                helper.getLog().debug"Resolving signature " + .getGroupId() + ":" + .getArtifactId()
                                   + " version from dependencies" );
                String source = "dependencies";
                Dependency match = findMatchingDependencyproject.getDependencies() );
                if ( match == null )
                {
                    helper.getLog().debug"Resolving signature " + .getGroupId() + ":" + .getArtifactId()
                                       + " version from dependencyManagement" );
                    source = "dependencyManagement";
                    match = findMatchingDependencyproject.getDependencyManagement().getDependencies() );
                }
                if ( match != null )
                {
                    helper.getLog().info"Resolved signature " + .getGroupId() + ":" + .getArtifactId()
                                       + " version as " + match.getVersion() + " from " + source);
                    .setVersionmatch.getVersion() );
                }
            }
            helper.getLog().info"Checking unresolved references to " +  );
            org.apache.maven.artifact.Artifact a = .createArtifactartifactFactory );
            resolver.resolveaproject.getRemoteArtifactRepositories(), localRepository );
            // just check code from this module
            MavenLogger logger = new MavenLoggerhelper.getLog() );
            final Set ignoredPackages = buildPackageListoutputDirectoryprojectlogger );
            if (  != null )
            {
                for ( int i = 0; i < .i++ )
                {
                    String ignore = [i];
                    if ( ignore == null )
                    {
                        continue;
                    }
                    ignoredPackages.addignore.replace'.''/' ) );
                }
            }
            final SignatureChecker signatureChecker =
                new SignatureCheckernew FileInputStreama.getFile() ), ignoredPackageslogger );
            signatureChecker.setCheckJarsfalse ); // don't want to descend into jar files that have been copied to
            // the output directory as resources.
            List sourcePaths = new ArrayList();
            Iterator iterator = project.getCompileSourceRoots().iterator();
            while ( iterator.hasNext() )
            {
                String path = (Stringiterator.next();
                sourcePaths.addnew Filepath ) );
            }
            signatureChecker.setSourcePathsourcePaths );
            if (  != null )
            {
                signatureChecker.setAnnotationTypes( Arrays.asList ) );
            }
            signatureChecker.processoutputDirectory );
            if ( signatureChecker.isSignatureBroken() )
            {
                throw new EnforcerRuleException(
                    "Signature errors found. Verify them and ignore them with the proper annotation if needed." );
            }
        }
        catch ( IOException e )
        {
            throw new EnforcerRuleException"Failed to check signatures"e );
        }
        catch ( AbstractArtifactResolutionException e )
        {
            throw new EnforcerRuleException"Failed to obtain signature: " + e );
        }
        catch ( ComponentLookupException e )
        {
            throw new EnforcerRuleException"Unable to lookup a component " + e.getLocalizedMessage(), e );
        }
        catch ( ExpressionEvaluationException e )
        {
            throw new EnforcerRuleException"Unable to lookup an expression " + e.getLocalizedMessage(), e );
        }
    }
    private static Dependency findMatchingDependencySignature signatureList/*<Dependency>*/ dependencies )
    {
        Dependency match = null;
        for ( Iterator/*<Dependency>*/ iterator = dependencies.iterator(); iterator.hasNext(); )
        {
            Dependency d = (Dependencyiterator.next();
            if ( StringUtils.isEmptyd.getVersion() ) )
            {
                continue;
            }
            if ( StringUtils.equalsd.getGroupId(), signature.getGroupId() ) && StringUtils.equalsd.getArtifactId(),
                                                                                                     signature.getArtifactId() ) )
            {
                if ( "signature".equalsd.getType() ) )
                {
                    // this is a perfect match
                    match = d;
                    break;
                }
                if ( "pom".equalsd.getType() ) )
                {
                    if ( match == null || "jar".equalsmatch.getType() ) )
                    {
                        match = d;
                    }
                }
                if ( "jar".equalsd.getType() ) )
                {
                    if ( match == null )
                    {
                        match = d;
                    }
                }
            }
        }
        return match;
    }

    
List of packages defined in the application.

Parameters:
outputDirectory
logger
    private Set buildPackageListFile outputDirectoryMavenProject projectLogger logger )
        throws IOException
    {
        ClassListBuilder plb = new ClassListBuilderlogger );
        applyplboutputDirectoryprojectlogger );
        return plb.getPackages();
    }
    private void applyClassFileVisitor vFile outputDirectoryMavenProject projectLogger logger )
        throws IOException
    {
        v.processoutputDirectory );
        if (  )
        {
            PatternIncludesArtifactFilter includesFilter =  == null
                ? null
                : new PatternIncludesArtifactFilter( Arrays.asList ) );
            PatternExcludesArtifactFilter excludesFilter =  == null
                ? null
                : new PatternExcludesArtifactFilter( Arrays.asList ) );
            logger.debug"Building list of classes from dependencies" );
            for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
            {
                Artifact artifact = (Artifacti.next();
                if ( !artifact.getArtifactHandler().isAddedToClasspath() ) {
                    logger.debug"Skipping artifact " + artifactIdartifact )
                                        + " as it is not added to the classpath." );
                    continue;
                }
                if ( !( ..equalsartifact.getScope() ) || ..equals(
                    artifact.getScope() ) || ..equalsartifact.getScope() ) ) )
                {
                    logger.debug"Skipping artifact " + artifactIdartifact )
                                        + " as it is not on the compile classpath." );
                    continue;
                }
                if ( includesFilter != null && !includesFilter.includeartifact ) )
                {
                    logger.debug"Skipping classes in artifact " + artifactIdartifact )
                                        + " as it does not match include rules." );
                    continue;
                }
                if ( excludesFilter != null && !excludesFilter.includeartifact ) )
                {
                    logger.debug"Skipping classes in artifact " + artifactIdartifact )
                                        + " as it does matches exclude rules." );
                    continue;
                }
                logger.debug"Adding classes in artifact " + artifactIdartifact ) +
                                    " to the ignores" );
                v.processartifact.getFile() );
            }
        }
    }
    public boolean isCacheable()
    {
        return false;
    }
    public boolean isResultValidEnforcerRule enforcerRule )
    {
        return false;
    }
    public String getCacheId()
    {
        return getClass().getName() + new Random().nextLong();
    }
    private static String artifactIdArtifact artifact )
    {
        return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType() + (
            artifact.getClassifier() != null ? ":" + artifact.getClassifier() : "" ) + ":" + artifact.getBaseVersion();
    }
New to GrepCode? Check out our FAQ X