You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ch...@apache.org on 2008/08/02 15:23:32 UTC

svn commit: r681969 - in /servicemix/maven-plugins/checksum-maven-plugin: branches/ tags/ trunk/ trunk/src/ trunk/src/main/ trunk/src/main/java/ trunk/src/main/java/org/ trunk/src/main/java/org/apache/ trunk/src/main/java/org/apache/servicemix/ trunk/s...

Author: chirino
Date: Sat Aug  2 06:23:32 2008
New Revision: 681969

URL: http://svn.apache.org/viewvc?rev=681969&view=rev
Log:
Added the checksum plugin

Added:
    servicemix/maven-plugins/checksum-maven-plugin/branches/
    servicemix/maven-plugins/checksum-maven-plugin/tags/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/pom.xml
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/test/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/test/java/
    servicemix/maven-plugins/checksum-maven-plugin/trunk/src/test/resources/

Added: servicemix/maven-plugins/checksum-maven-plugin/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/pom.xml?rev=681969&view=auto
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/pom.xml (added)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/pom.xml Sat Aug  2 06:23:32 2008
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<!--
+
+    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.
+-->
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.servicemix.tooling</groupId>
+    <artifactId>maven-plugins-pom</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  
+  <groupId>org.apache.servicemix.tooling</groupId>
+  <artifactId>checksum-maven-plugin</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  
+  <packaging>maven-plugin</packaging>
+  <name>ServiceMix :: Checksum Plugin</name>
+  
+  <scm>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/servicemix/maven-plugins/checksum-maven-plugin/trunk</connection>
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/servicemix/maven-plugins/checksum-maven-plugin/trunk</developerConnection>
+    <url>http://svn.apache.org/viewcvs.cgi/servicemix/maven-plugins/checksum-maven-plugin/trunk</url>
+  </scm>
+  
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>2.0.9</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <version>2.0.9</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-archiver</artifactId>
+      <version>2.3</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.0.5</version>
+    </dependency>
+  </dependencies>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.4</source>
+          <target>1.4</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  
+</project>

Added: servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java?rev=681969&view=auto
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java (added)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumGeneratorMojo.java Sat Aug  2 06:23:32 2008
@@ -0,0 +1,128 @@
+/**
+ * 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.
+ */
+package org.apache.servicemix.maven.plugin.checksum;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.MojoExecutionException;
+
+/**
+ * Adds all the checksums of the artifact dependencies from that are dependencies
+ * of the current build to a checksum.txt file.
+ * 
+ * Once generated, the build can validate the checksums of the artifacts against that
+ * checksums.txt 
+ * 
+ * @goal generate-checksums
+ * @phase compile 
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 
+ */
+public class ChecksumGeneratorMojo extends ChecksumValidatorMojo {
+
+    public void execute() throws MojoExecutionException {
+        
+        LinkedHashMap checksums = new LinkedHashMap();
+        
+        boolean modified=true;
+        try { 
+            checksums = getCheckSums();
+            modified=false;
+        } catch ( MojoExecutionException e) {
+        }
+        
+        for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+            Artifact pom = getPomArtifact( artifact );
+
+            modified |= processArtifact(checksums, pom);
+            modified |= processArtifact(checksums, artifact);
+            
+        }
+        
+        if( modified ) {
+            
+            // put it back to property file format
+            Properties p = new Properties();
+
+            for (Iterator iterator = checksums.entrySet().iterator(); iterator.hasNext();) {
+                Map.Entry i = (Map.Entry)iterator.next();            
+                StringBuffer b = new StringBuffer();
+                for (Iterator iterator2 = ((List)i.getValue()).iterator(); iterator.hasNext();) {
+                    String s = (String)iterator2.next();            
+                    if( b.length()!=0 ) {
+                        b.append("|");
+                    }
+                    b.append(s);
+                }
+                p.put(i.getKey(), b.toString());
+            }
+            
+            // Store it.
+            FileOutputStream os=null;
+            try {
+                os = new FileOutputStream(this.checksums);
+                p.store(os, "");
+            } catch (Throwable e) {
+                throw new MojoExecutionException("Could not write: "+this.checksums);
+            } finally {
+                try {
+                    os.close();
+                } catch (Throwable ignore ) {
+                }
+            }
+        }
+    }
+
+    /**
+     * 
+     * @param checksums
+     * @param pom
+     * @return true if this method modified the checksums
+     * @throws MojoExecutionException
+     */
+    private boolean processArtifact(HashMap checksums, Artifact pom) throws MojoExecutionException {
+        String sum = checksum(pom.getFile());
+        List sums = (List)checksums.get(pom.getId());
+        if( sums == null ) {
+            sums = new ArrayList();
+            sums.add(sum);
+            checksums.put(pom.getId(), sums);
+            return true;
+        } else {
+            if ( !sums.contains(sum) ) {
+                sums.add(sum);
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+}

Added: servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java
URL: http://svn.apache.org/viewvc/servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java?rev=681969&view=auto
==============================================================================
--- servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java (added)
+++ servicemix/maven-plugins/checksum-maven-plugin/trunk/src/main/java/org/apache/servicemix/maven/plugin/checksum/ChecksumValidatorMojo.java Sat Aug  2 06:23:32 2008
@@ -0,0 +1,231 @@
+/**
+ * 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.
+ */
+package org.apache.servicemix.maven.plugin.checksum;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
+import org.apache.maven.artifact.resolver.ArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Validates the checksums of the dependencies of the project
+ * against the checksums.txt file.
+ * 
+ * @goal validate-checksums
+ * @phase process-classes 
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 
+ */
+public class ChecksumValidatorMojo extends AbstractMojo {
+
+    /**
+     * The maven project.
+     * 
+     * @parameter expression="${project}"
+     * @required
+     * @readonly
+     */
+    protected MavenProject project;
+
+    /**
+     * Remote repositories which will be searched for source attachments.
+     *
+     * @parameter expression="${project.remoteArtifactRepositories}"
+     * @required
+     * @readonly
+     */
+    protected List remoteArtifactRepositories;
+
+    /**
+     * Local maven repository.
+     *
+     * @parameter expression="${localRepository}"
+     * @required
+     * @readonly
+     */
+    protected ArtifactRepository localRepository;
+
+    /**
+     * Artifact factory, needed to download source jars for inclusion in classpath.
+     *
+     * @component role="org.apache.maven.artifact.factory.ArtifactFactory"
+     * @required
+     * @readonly
+     */
+    protected ArtifactFactory artifactFactory;
+
+    /**
+     * Artifact resolver, needed to download source jars for inclusion in classpath.
+     *
+     * @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
+     * @required
+     * @readonly
+     */
+    protected ArtifactResolver artifactResolver;
+    
+    
+    /**
+     * The file that holds dependency checksums.
+     * 
+     * @parameter default-value="${basedir}/checksums.txt"
+     */
+    protected File checksums;
+
+    /**
+     * The checksum algorithm used to in the checksums. 
+     * 
+     * @parameter default-value="SHA-1"
+     */
+    private String checksumAlgorithm;
+
+    public void execute() throws MojoExecutionException {
+        
+        LinkedHashMap checksums = getCheckSums();
+        
+        boolean failed = false;
+        
+        for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
+        {
+            Artifact artifact = (Artifact) it.next();
+            Artifact pom = getPomArtifact( artifact );
+            
+            
+            String sum = checksum(pom.getFile());
+            List list = (List)checksums.get(pom.getId());
+            if( list == null ) {
+                getLog().error("No checksum specified for "+pom.getId()+" in "+this.checksums+" ("+sum+")" );
+                failed = true;
+            } else if ( !list.contains(sum) && !list.contains("*") ) {
+                getLog().error("Checksum mismatch for "+pom.getId()+" in "+this.checksums+" expected one of "+list+" but was "+sum );
+                failed = true;
+            }
+        }
+        
+        if( failed ) {
+            throw new MojoExecutionException("Invalid checksums found.. see previous error messages for more details.");
+        }
+    }
+
+    protected LinkedHashMap getCheckSums() throws MojoExecutionException {
+        LinkedHashMap rc = new LinkedHashMap();
+        
+        if( !checksums.canRead() ) {
+            throw new MojoExecutionException("Cannot read checksum file: "+checksums);
+        }
+        
+        Properties sums = new Properties();
+        FileInputStream is=null;
+        try {
+            is = new FileInputStream(checksums);
+            sums.load(is);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Could not load checksum file: "+checksums);
+        } finally {
+            try {
+                is.close();
+            } catch (Throwable e) {
+            }
+        }
+        
+        for (Iterator iterator = sums.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry i = (Map.Entry)iterator.next();            
+            String value = (String)i.getValue();
+            String[] t = value.split("\\|");
+            List values = new ArrayList(t.length);
+            for (int j = 0; j < t.length; j++) {
+                values.add(t[j].toLowerCase().trim());
+            }
+            rc.put((String)i.getKey(), values);
+        }
+        
+        return rc;
+    }
+    
+    protected Artifact getPomArtifact(Artifact artifact) {
+        Artifact resolvedArtifact = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
+        try {
+            artifactResolver.resolve(resolvedArtifact, remoteArtifactRepositories, localRepository);
+        } catch (ArtifactNotFoundException e) {
+            // ignore, the jar has not been found
+        } catch (ArtifactResolutionException e) {
+            getLog().warn("Could not get pom for " + artifact);
+        }
+        if (resolvedArtifact.isResolved()) {
+            return resolvedArtifact;
+        }
+        return null;
+    }
+
+    String checksum(File file) throws MojoExecutionException {
+        try {
+            MessageDigest md = MessageDigest.getInstance(checksumAlgorithm);
+            FileInputStream is=null;
+            try {
+                is = new FileInputStream(file);
+                byte buffer[] = new byte[1024*4];
+                int c;
+                while( (c=is.read(buffer)) >= 0 ) {
+                    md.update(buffer,0, c);
+                }
+                byte[] digest = md.digest();
+                
+                return toString(digest);
+                
+            } catch (IOException e) {
+                throw new MojoExecutionException("Could read file: "+checksums);
+            } finally {
+                try {
+                    is.close();
+                } catch (Throwable e) {
+                }
+            }
+
+        } catch (NoSuchAlgorithmException e) {
+            throw new MojoExecutionException("Invalid checksum algorithm: "+checksumAlgorithm, e);
+        }
+    }
+
+    static char hexTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+    
+    static String toString(byte[] digest) {
+        StringBuilder rc = new StringBuilder(digest.length*2);
+        for (int i = 0; i < digest.length; i++) {
+            rc.append( hexTable[ ((digest[i]>>4) & 0x0F) ] ) ;
+            rc.append( hexTable[ (digest[i] & 0x0F) ] ) ;
+        }
+        return rc.toString();
+    }
+
+}