From 4ab7501ffbf4162815dc43c424e4af4311f6a5d3 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Gonzalez Date: Wed, 23 Feb 2005 20:57:34 +0000 Subject: [PATCH] Added checksum validator repository tool git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163387 13f79535-47bb-0310-9956-ffa450edef68 --- maven-repository-tools/pom.xml | 5 + .../maven/repository/ChecksumValidator.java | 70 +++++++++ .../maven/repository/RepositoryTools.java | 138 ++++++++++++++++++ .../maven/repository/RepositoryToolsTest.java | 62 ++++++++ 4 files changed, 275 insertions(+) create mode 100644 maven-repository-tools/src/main/java/org/apache/maven/repository/ChecksumValidator.java create mode 100644 maven-repository-tools/src/main/java/org/apache/maven/repository/RepositoryTools.java create mode 100644 maven-repository-tools/src/test/java/org/apache/maven/repository/RepositoryToolsTest.java diff --git a/maven-repository-tools/pom.xml b/maven-repository-tools/pom.xml index 978260f222..acdd6dffe5 100644 --- a/maven-repository-tools/pom.xml +++ b/maven-repository-tools/pom.xml @@ -36,5 +36,10 @@ plexus 0.17-SNAPSHOT + + maven + maven-artifact + 2.0-SNAPSHOT + diff --git a/maven-repository-tools/src/main/java/org/apache/maven/repository/ChecksumValidator.java b/maven-repository-tools/src/main/java/org/apache/maven/repository/ChecksumValidator.java new file mode 100644 index 0000000000..ea9eeff4ec --- /dev/null +++ b/maven-repository-tools/src/main/java/org/apache/maven/repository/ChecksumValidator.java @@ -0,0 +1,70 @@ +package org.apache.maven.repository; + +/* + * Copyright 2001-2004 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.File; +import java.util.Iterator; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; + +/** + * Prints all artifacts without checksum file + * + * @todo generate checksums for those files without it + * + * @author Carlos Sanchez + * @version $Id$ + */ + +public class ChecksumValidator +{ + + public static void main( String[] args ) + { + if ( args.length != 1 ) + { + System.out.println( "Usage: " + ChecksumValidator.class.getName() + " path.to.local.repository" ); + + return; + } + + ArtifactRepository localRepository = new ArtifactRepository(); + + String path = args[0]; + + File f = new File( path ); + + localRepository.setUrl( "file://" + f.getPath() ); + + List artifacts = RepositoryTools.getAllArtifacts( localRepository ); + + Iterator it = artifacts.iterator(); + + while ( it.hasNext() ) + { + Artifact artifact = (Artifact) it.next(); + + if ( !artifact.getChecksumFile().exists() ) + { + System.out.println( artifact ); + } + } + } + +} diff --git a/maven-repository-tools/src/main/java/org/apache/maven/repository/RepositoryTools.java b/maven-repository-tools/src/main/java/org/apache/maven/repository/RepositoryTools.java new file mode 100644 index 0000000000..85ea1fee71 --- /dev/null +++ b/maven-repository-tools/src/main/java/org/apache/maven/repository/RepositoryTools.java @@ -0,0 +1,138 @@ +package org.apache.maven.repository; + +/* + * Copyright 2001-2004 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.File; +import java.io.FilenameFilter; +import java.util.LinkedList; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.repository.ArtifactRepository; + +/** + * Tools to help repository management + * + * @author Carlos Sanchez + * @version $Id$ + */ + +public class RepositoryTools +{ + + /** + * Files with these extensions will be ignored + */ + private static final String[] IGNORE_EXTENSIONS = { "md5" }; + + /** + * Get all artifacts present in a file repository + * + * @todo make this layout independent + * + * @param artifactRepository + * all files in this repository basedir will be returned as + * artifacts + * @return all the artifacts available in the repository + */ + public static List getAllArtifacts( ArtifactRepository artifactRepository ) + { + List list = new LinkedList(); + + File basedir = new File( artifactRepository.getBasedir() ); + + File[] groups = basedir.listFiles(); + + for ( int i = 0; (groups != null) && (i < groups.length); i++ ) + { + File[] types = groups[i].listFiles(); + + for ( int j = 0; (types != null) && (j < types.length); j++ ) + { + + File[] artifacts = types[j].listFiles( new FilenameFilter() { + + public boolean accept( File dir, String name ) + { + int x = name.lastIndexOf( "." ); + + String extension = name.substring( x, name.length() ); + + for ( int y = 0; y < IGNORE_EXTENSIONS.length; y++ ) + { + + if ( extension.equals( IGNORE_EXTENSIONS[y] ) ) + + return false; + } + + return true; + } + } ); + + for ( int k = 0; (artifacts != null) && (k < artifacts.length); k++ ) + { + Artifact artifact = getArtifact( groups[i].getName(), types[j].getName().substring( 0, + types[j].getName().length() - 1 ), artifacts[k] ); + + if ( artifact != null ) + + list.add( artifact ); + } + } + } + + return list; + } + + /** + * Construct an artifact object from a file + * + * @todo make this layout independent + * + * @param groupId + * @param type + * @param artifactFile + * artifactId, version and path will be get from this file name + * @return + */ + public static Artifact getArtifact( String groupId, String type, File artifactFile ) + { + String name = artifactFile.getName(); + + int i = name.lastIndexOf( "-" ); + + int j = name.lastIndexOf( "." ); + + if ( (i < 0) || (j < 0) ) + { + return null; + } + + String version = name.substring( i + 1, j ); + + String artifactId = name.substring( 0, i ); + + Artifact artifact = new DefaultArtifact( groupId, artifactId, version, type ); + + artifact.setPath( artifactFile.getPath() ); + + return artifact; + } + +} diff --git a/maven-repository-tools/src/test/java/org/apache/maven/repository/RepositoryToolsTest.java b/maven-repository-tools/src/test/java/org/apache/maven/repository/RepositoryToolsTest.java new file mode 100644 index 0000000000..e48b0118ca --- /dev/null +++ b/maven-repository-tools/src/test/java/org/apache/maven/repository/RepositoryToolsTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2004 Carlos Sanchez. + * + * 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. + */ +package org.apache.maven.repository; + +import java.io.File; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactComponentTestCase; +import org.apache.maven.repository.RepositoryTools; + +/** + * @author Carlos Sanchez + * @version $Revision$ + */ +public class RepositoryToolsTest + extends ArtifactComponentTestCase +{ + + protected void setUp() throws Exception + { + super.setUp(); + } + + protected String component() + { + return "repositorytools"; + } + + public void testGetAllArtifacts() throws Exception + { + Artifact artifact = createLocalArtifact( "a", "1.0" ); + List artifacts = RepositoryTools.getAllArtifacts( localRepository() ); + assertEquals( 1, artifacts.size() ); + assertEquals( artifact, artifacts.get( 0 ) ); + } + + public void testGetArtifact() + { + File artifactFile = new File( "whatever/maven/jars/maven-meeper-1.0.jar" ); + Artifact artifact = RepositoryTools.getArtifact( "maven", "jar", artifactFile ); + assertEquals( "maven", artifact.getGroupId() ); + assertEquals( "maven-meeper", artifact.getArtifactId() ); + assertEquals( "1.0", artifact.getVersion() ); + assertEquals( "jar", artifact.getType() ); + assertNotNull( artifact.getPath() ); + } + +}