diff options
author | Joakim Erdfelt <joakime@apache.org> | 2007-04-03 15:21:33 +0000 |
---|---|---|
committer | Joakim Erdfelt <joakime@apache.org> | 2007-04-03 15:21:33 +0000 |
commit | 74cb10586cf7f1c415930d5ab1d59a6e44b253ee (patch) | |
tree | a6c5079ac724479adc4d6eabfb7144e38f713732 /archiva-cli/src/main/java | |
parent | 54e1d8c6083cd552d9b8d3ee2d2ced20e9a6f460 (diff) | |
download | archiva-74cb10586cf7f1c415930d5ab1d59a6e44b253ee.tar.gz archiva-74cb10586cf7f1c415930d5ab1d59a6e44b253ee.zip |
Updating branch with latest work towards database refactor.
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@525176 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-cli/src/main/java')
4 files changed, 457 insertions, 33 deletions
diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/AbstractProgressConsumer.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/AbstractProgressConsumer.java new file mode 100644 index 000000000..46500a44d --- /dev/null +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/AbstractProgressConsumer.java @@ -0,0 +1,61 @@ +package org.apache.maven.archiva.cli; + +/* + * 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. + */ + +import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.RepositoryContentConsumer; +import org.apache.maven.archiva.model.ArchivaRepository; + +/** + * AbstractProgressConsumer + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public abstract class AbstractProgressConsumer + extends AbstractMonitoredConsumer + implements RepositoryContentConsumer +{ + private int count = 0; + + public void beginScan( ArchivaRepository repository ) + throws ConsumerException + { + this.count = 0; + } + + public void processFile( String path ) + throws ConsumerException + { + count++; + if ( ( count % 1000 ) == 0 ) + { + System.out.println( "Files Processed: " + count ); + } + + } + + public void completeScan() + { + System.out.println( "Final Count of Artifacts processed by " + getId() + ": " + count ); + } + +} diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java index 26ee4db77..921bd8883 100644 --- a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java @@ -20,19 +20,32 @@ package org.apache.maven.archiva.cli; */ import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.lang.StringUtils; +import org.apache.maven.archiva.common.utils.DateUtil; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.RepositoryContentConsumer; import org.apache.maven.archiva.converter.RepositoryConversionException; import org.apache.maven.archiva.converter.legacy.LegacyRepositoryConverter; +import org.apache.maven.archiva.model.ArchivaRepository; +import org.apache.maven.archiva.model.RepositoryContentStatistics; +import org.apache.maven.archiva.repository.RepositoryException; +import org.apache.maven.archiva.repository.scanner.RepositoryScanner; import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.tools.cli.AbstractCli; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; /** @@ -47,6 +60,12 @@ public class ArchivaCli public static final char CONVERT = 'c'; + public static final char SCAN = 's'; + + public static final char CONSUMERS = 'u'; + + public static final char LIST_CONSUMERS = 'l'; + // ---------------------------------------------------------------------------- // Properties controlling Repository conversion // ---------------------------------------------------------------------------- @@ -68,11 +87,36 @@ public class ArchivaCli return "META-INF/maven/org.apache.maven.archiva/archiva-cli/pom.properties"; } + private Option createOption( char shortOpt, String longOpt, int argCount, String description ) + { + boolean hasArg = ( argCount > 0 ); + Option opt = new Option( String.valueOf( shortOpt ), hasArg, description ); + opt.setLongOpt( longOpt ); + if ( hasArg ) + { + opt.setArgs( argCount ); + } + return opt; + } + public Options buildCliOptions( Options options ) { - options.addOption( OptionBuilder.withLongOpt( "convert" ).hasArg().withDescription( - "Convert a legacy Maven 1.x repository to a Maven 2.x repository using a properties file to describe the conversion." ) - .create( CONVERT ) ); + Option convertOption = createOption( CONVERT, "convert", 1, "Convert a legacy Maven 1.x repository to a " + + "Maven 2.x repository using a properties file to describe the conversion." ); + convertOption.setArgName( "conversion.properties" ); + options.addOption( convertOption ); + + Option scanOption = createOption( SCAN, "scan", 1, "Scan the specified repository." ); + scanOption.setArgName( "repository directory" ); + options.addOption( scanOption ); + + Option consumerOption = createOption( CONSUMERS, "consumers", 1, "The consumers to use. " + + "(comma delimited. default: 'count-artifacts')" ); + consumerOption.setArgName( "consumer list" ); + options.addOption( consumerOption ); + + Option listConsumersOption = createOption( LIST_CONSUMERS, "listconsumers", 0, "List available consumers." ); + options.addOption( listConsumersOption ); return options; } @@ -80,47 +124,147 @@ public class ArchivaCli public void invokePlexusComponent( CommandLine cli, PlexusContainer plexus ) throws Exception { - LegacyRepositoryConverter legacyRepositoryConverter = - (LegacyRepositoryConverter) plexus.lookup( LegacyRepositoryConverter.ROLE ); - if ( cli.hasOption( CONVERT ) ) { - Properties p = new Properties(); + doConversion( cli, plexus ); + } + else if ( cli.hasOption( SCAN ) ) + { + doScan( cli, plexus ); + } + else if ( cli.hasOption( LIST_CONSUMERS ) ) + { + dumpAvailableConsumers( plexus ); + } + else + { + displayHelp(); + } + } - try - { - p.load( new FileInputStream( cli.getOptionValue( CONVERT ) ) ); - } - catch ( IOException e ) - { - showFatalError( "Cannot find properties file which describes the conversion.", e, true ); - } + private void doScan( CommandLine cli, PlexusContainer plexus ) + throws ConsumerException, ComponentLookupException + { + String path = cli.getOptionValue( SCAN ); - File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) ); + ArchivaRepository repo = new ArchivaRepository( "cliRepo", "Archiva CLI Provided Repo", "file://" + path ); - File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) ); + List consumerList = new ArrayList(); - System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath ); + consumerList.addAll( getConsumerList( cli, plexus ) ); - List fileExclusionPatterns = null; + RepositoryScanner scanner = new RepositoryScanner(); - String s = p.getProperty( BLACKLISTED_PATTERNS ); + try + { + RepositoryContentStatistics stats = scanner.scan( repo, consumerList, true ); - if ( s != null ) - { - fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) ); - } + SimpleDateFormat df = new SimpleDateFormat(); + System.out.println( ".\\ Scan of " + repo.getId() + " \\.__________________________________________" ); + System.out.println( " Repository URL : " + repo.getUrl() ); + System.out.println( " Repository Name : " + repo.getModel().getName() ); + System.out.println( " Repository Layout : " + repo.getModel().getLayoutName() ); + System.out.println( " Duration : " + DateUtil.getDuration( stats.getDuration() ) ); + System.out.println( " When Gathered : " + df.format( stats.getWhenGathered() ) ); + System.out.println( " Total File Count : " + stats.getTotalFileCount() ); + System.out.println( " New File Count : " + stats.getNewFileCount() ); + System.out.println( "______________________________________________________________" ); + } + catch ( RepositoryException e ) + { + e.printStackTrace( System.err ); + } + } - try - { - legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath, - fileExclusionPatterns, - true ); - } - catch ( RepositoryConversionException e ) + private Collection getConsumerList( CommandLine cli, PlexusContainer plexus ) + throws ComponentLookupException, ConsumerException + { + String specifiedConsumers = "count-artifacts"; + + if ( cli.hasOption( CONSUMERS ) ) + { + specifiedConsumers = cli.getOptionValue( CONSUMERS ); + } + + List consumerList = new ArrayList(); + + Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class ); + + String consumerArray[] = StringUtils.split( specifiedConsumers, ',' ); + + for ( int i = 0; i < consumerArray.length; i++ ) + { + String specifiedConsumer = consumerArray[i]; + if ( !availableConsumers.containsKey( specifiedConsumer ) ) { - showFatalError( "Error converting repository.", e, true ); + System.err.println( "Specified consumer [" + specifiedConsumer + "] not found." ); + dumpAvailableConsumers( plexus ); + System.exit( 1 ); } + + consumerList.add( availableConsumers.get( specifiedConsumer ) ); + } + + return consumerList; + } + + private void dumpAvailableConsumers( PlexusContainer plexus ) + throws ComponentLookupException + { + Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class ); + + System.out.println( ".\\ Available Consumer List \\.______________________________" ); + + for ( Iterator iter = availableConsumers.entrySet().iterator(); iter.hasNext(); ) + { + Map.Entry entry = (Map.Entry) iter.next(); + String consumerHint = (String) entry.getKey(); + RepositoryContentConsumer consumer = (RepositoryContentConsumer) entry.getValue(); + System.out.println( " " + consumerHint + ": " + consumer.getDescription() + " (" + + consumer.getClass().getName() + ")" ); + } + } + + private void doConversion( CommandLine cli, PlexusContainer plexus ) + throws ComponentLookupException + { + LegacyRepositoryConverter legacyRepositoryConverter = (LegacyRepositoryConverter) plexus + .lookup( LegacyRepositoryConverter.ROLE ); + + Properties p = new Properties(); + + try + { + p.load( new FileInputStream( cli.getOptionValue( CONVERT ) ) ); + } + catch ( IOException e ) + { + showFatalError( "Cannot find properties file which describes the conversion.", e, true ); + } + + File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) ); + + File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) ); + + System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath ); + + List fileExclusionPatterns = null; + + String s = p.getProperty( BLACKLISTED_PATTERNS ); + + if ( s != null ) + { + fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) ); + } + + try + { + legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath, + fileExclusionPatterns, true ); + } + catch ( RepositoryConversionException e ) + { + showFatalError( "Error converting repository.", e, true ); } } } diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java new file mode 100644 index 000000000..c4583427b --- /dev/null +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java @@ -0,0 +1,95 @@ +package org.apache.maven.archiva.cli; + +/* + * 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. + */ + +import org.apache.maven.archiva.consumers.RepositoryContentConsumer; + +import java.util.ArrayList; +import java.util.List; + +/** + * ArtifactCountConsumer + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role="org.apache.maven.archiva.consumers.RepositoryContentConsumer" + * role-hint="count-artifacts" + * instantiation-strategy="per-lookup" + */ +public class ArtifactCountConsumer + extends AbstractProgressConsumer + implements RepositoryContentConsumer +{ + /** + * @plexus.configuration default-value="count-artifacts" + */ + private String id; + + /** + * @plexus.configuration default-value="Count Artifacts" + */ + private String description; + + private List includes; + + public ArtifactCountConsumer() + { + includes = new ArrayList(); + includes.add( "**/*.pom" ); + includes.add( "**/*.jar" ); + includes.add( "**/*.war" ); + includes.add( "**/*.ear" ); + includes.add( "**/*.sar" ); + includes.add( "**/*.car" ); + includes.add( "**/*.mar" ); + includes.add( "**/*.dtd" ); + includes.add( "**/*.tld" ); + includes.add( "**/*.gz" ); + includes.add( "**/*.bz2" ); + includes.add( "**/*.zip" ); + } + + public String getDescription() + { + return description; + } + + public String getId() + { + return id; + } + + public boolean isPermanent() + { + return false; + } + + public List getExcludes() + { + return null; + } + + public List getIncludes() + { + return includes; + } + +} diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java new file mode 100644 index 000000000..7832ff5be --- /dev/null +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java @@ -0,0 +1,124 @@ +package org.apache.maven.archiva.cli; + +/* + * 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. + */ + +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.RepositoryContentConsumer; +import org.apache.maven.archiva.model.ArchivaProjectModel; +import org.apache.maven.archiva.model.ArchivaRepository; +import org.apache.maven.archiva.repository.project.ProjectModel400Reader; +import org.apache.maven.archiva.repository.project.ProjectModelException; +import org.apache.maven.archiva.repository.project.ProjectModelReader; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * ProjectReaderConsumer + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role="org.apache.maven.archiva.consumers.RepositoryContentConsumer" + * role-hint="read-poms" + * instantiation-strategy="per-lookup" + */ +public class ProjectReaderConsumer + extends AbstractProgressConsumer + implements RepositoryContentConsumer +{ + /** + * @plexus.configuration default-value="read-poms" + */ + private String id; + + /** + * @plexus.configuration default-value="Read POMs and report anomolies." + */ + private String description; + + private ProjectModelReader reader; + + private ArchivaRepository repo; + + private List includes; + + public ProjectReaderConsumer() + { + reader = new ProjectModel400Reader(); + + includes = new ArrayList(); + includes.add( "**/*.pom" ); + } + + public String getDescription() + { + return description; + } + + public String getId() + { + return id; + } + + public boolean isPermanent() + { + return false; + } + + public List getExcludes() + { + return null; + } + + public List getIncludes() + { + return includes; + } + + public void beginScan( ArchivaRepository repository ) + throws ConsumerException + { + super.beginScan( repository ); + this.repo = repository; + } + + public void processFile( String path ) + throws ConsumerException + { + super.processFile( path ); + + File pomFile = new File( repo.getUrl().getPath(), path ); + try + { + ArchivaProjectModel model = reader.read( pomFile ); + if ( model == null ) + { + System.err.println( "Got null model on " + pomFile ); + } + } + catch ( ProjectModelException e ) + { + System.err.println( "Unable to process: " + pomFile ); + e.printStackTrace( System.out ); + } + } +} |