refactoring towards similar pattern of artifact metadata

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@264967 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-08-31 08:11:47 +00:00
parent 8201bb9d18
commit c991b41571
6 changed files with 112 additions and 313 deletions

View File

@ -22,19 +22,14 @@ import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* @todo try to crop all, particularly plugin stuff
* @todo check caching?
*/
public class DefaultRepositoryMetadataManager
extends AbstractLogEnabled
implements RepositoryMetadataManager
@ -42,94 +37,68 @@ public class DefaultRepositoryMetadataManager
// component requirement
private WagonManager wagonManager;
// only resolve repository metadata once per session...
private Map cachedMetadata = new HashMap();
/**
* @todo very primitve. Probably we can cache artifacts themselves in a central location, as well as reset the flag over time in a long running process.
*/
private Set cachedMetadata = new HashSet();
public void resolveLocally( RepositoryMetadata metadata, ArtifactRepository local )
public void resolve( RepositoryMetadata metadata, List repositories, ArtifactRepository local )
throws RepositoryMetadataManagementException
{
resolve( metadata, null, local );
}
public void resolve( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local )
throws RepositoryMetadataManagementException
{
File metadataFile = (File) cachedMetadata.get( metadata.getRepositoryPath() );
if ( metadataFile == null )
boolean alreadyResolved = alreadyResolved( metadata );
if ( !alreadyResolved )
{
metadataFile = constructLocalRepositoryFile( metadata, local );
if ( !metadataFile.exists() && remote != null )
for ( Iterator i = repositories.iterator(); i.hasNext(); )
{
try
ArtifactRepository repository = (ArtifactRepository) i.next();
// TODO: replace with a more general repository update mechanism like artifact metadata uses
// (Actually, this should now supersede artifact metadata...)
File metadataFile = new File( local.getBasedir(), local.pathOfRepositoryMetadata( metadata ) );
if ( !metadataFile.exists() )
{
try
{
wagonManager.getRepositoryMetadata( metadata, remote, metadataFile );
try
{
wagonManager.getRepositoryMetadata( metadata, repository, metadataFile );
}
catch ( ResourceDoesNotExistException e )
{
if ( !metadataFile.exists() )
{
throw new RepositoryMetadataManagementException( metadata,
"Remote repository metadata not found.",
e );
}
else
{
String message = "Cannot find " + metadata +
" in remote repository - Using local copy.";
getLogger().info( message );
getLogger().debug( message, e );
}
}
}
catch ( ResourceDoesNotExistException e )
catch ( TransferFailedException e )
{
if ( !metadataFile.exists() )
{
throw new RepositoryMetadataManagementException( metadata,
"Remote repository metadata not found.",
e );
}
else
{
String message = "Cannot find " + metadata + " in remote repository - Using local copy.";
getLogger().info( message );
getLogger().debug( message, e );
}
throw new RepositoryMetadataManagementException( metadata,
"Failed to download repository metadata.", e );
}
}
catch ( TransferFailedException e )
else
{
throw new RepositoryMetadataManagementException( metadata,
"Failed to download repository metadata.", e );
}
}
else
{
getLogger().info( "Using local copy of " + metadata + " from: " + metadataFile );
}
if ( metadataFile.exists() )
{
if ( !verifyFileNotEmpty( metadataFile ) )
{
throw new InvalidRepositoryMetadataException( metadata, "Metadata located in file: " +
metadataFile + " appears to be corrupt (file is empty). DOWNLOAD FAILED." );
getLogger().info( "Using local copy of " + metadata + " from: " + metadataFile );
}
cachedMetadata.put( metadata.getRepositoryPath(), metadataFile );
cachedMetadata.add( metadata.getRepositoryPath() );
}
}
}
private boolean verifyFileNotEmpty( File metadataFile )
{
InputStream verifyInputStream = null;
try
{
verifyInputStream = new FileInputStream( metadataFile );
return verifyInputStream.available() > 0;
}
catch ( IOException e )
{
return false;
}
finally
{
IOUtil.close( verifyInputStream );
}
}
public void deploy( File source, RepositoryMetadata metadata, ArtifactRepository remote )
throws RepositoryMetadataManagementException
{
@ -147,7 +116,7 @@ public class DefaultRepositoryMetadataManager
public void install( File source, RepositoryMetadata metadata, ArtifactRepository local )
throws RepositoryMetadataManagementException
{
File metadataFile = constructLocalRepositoryFile( metadata, local );
File metadataFile = new File( local.getBasedir(), local.pathOfRepositoryMetadata( metadata ) );
try
{
@ -167,24 +136,8 @@ public class DefaultRepositoryMetadataManager
}
public void purgeLocalCopy( RepositoryMetadata metadata, ArtifactRepository local )
throws RepositoryMetadataManagementException
private boolean alreadyResolved( RepositoryMetadata metadata )
{
File metadataFile = constructLocalRepositoryFile( metadata, local );
if ( metadataFile.exists() )
{
if ( !metadataFile.delete() )
{
throw new RepositoryMetadataManagementException( metadata,
"Failed to purge local copy from: " + metadataFile );
}
}
return cachedMetadata.contains( metadata.getRepositoryPath() );
}
private static File constructLocalRepositoryFile( RepositoryMetadata metadata, ArtifactRepository local )
{
return new File( local.getBasedir(), local.pathOfRepositoryMetadata( metadata ) );
}
}

View File

@ -1,33 +0,0 @@
package org.apache.maven.artifact.repository.metadata;
/*
* Copyright 2001-2005 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.
*/
public class InvalidRepositoryMetadataException
extends RepositoryMetadataManagementException
{
public InvalidRepositoryMetadataException( RepositoryMetadata metadata, String message, Throwable cause )
{
super( metadata, message, cause );
}
public InvalidRepositoryMetadataException( RepositoryMetadata metadata, String message )
{
super( metadata, message );
}
}

View File

@ -16,8 +16,6 @@ package org.apache.maven.artifact.repository.metadata;
* limitations under the License.
*/
import java.io.File;
public class PluginMappingMetadata
implements RepositoryMetadata
{
@ -25,11 +23,6 @@ public class PluginMappingMetadata
private final String groupId;
/**
* @todo REMOVE!
*/
private File metadataFile;
public PluginMappingMetadata( String groupId )
{
this.groupId = groupId;

View File

@ -19,13 +19,12 @@ package org.apache.maven.artifact.repository.metadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import java.io.File;
import java.util.List;
public interface RepositoryMetadataManager
{
void resolveLocally( RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException;
void resolve( RepositoryMetadata repositoryMetadata, ArtifactRepository remote, ArtifactRepository local )
void resolve( RepositoryMetadata repositoryMetadata, List repositories, ArtifactRepository local )
throws RepositoryMetadataManagementException;
void deploy( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository remote )
@ -34,6 +33,4 @@ public interface RepositoryMetadataManager
void install( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException;
void purgeLocalCopy( RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException;
}

View File

@ -17,7 +17,6 @@ package org.apache.maven.plugin;
*/
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.InvalidRepositoryMetadataException;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.PluginMappingMetadata;
@ -51,98 +50,23 @@ public class DefaultPluginMappingManager
{
protected RepositoryMetadataManager repositoryMetadataManager;
private List mappings = new ArrayList();
private boolean refreshed;
private Map pluginDefinitionsByPrefix;
public void clear()
{
this.mappings = null;
clearCache();
}
private void clearCache()
{
this.pluginDefinitionsByPrefix = null;
}
private Map pluginDefinitionsByPrefix = new HashMap();
public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories,
ArtifactRepository localRepository )
throws RepositoryMetadataManagementException
{
if ( pluginDefinitionsByPrefix == null )
// if not found, try from the remote repository
if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) )
{
// firstly, search the local repository
getLogger().info( "Searching repository for plugin with prefix: \'" + pluginPrefix + "\'." );
loadPluginMappings( groupIds, pluginRepositories, localRepository );
calculatePluginDefinitionsByPrefix();
// if not found, try from the remote repository
if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) && !refreshed )
{
getLogger().info(
"Refreshing plugin mapping metadata; looking for plugin with prefix: \'" + pluginPrefix + "\'." );
refreshPluginMappingManager( pluginRepositories, localRepository );
refreshed = true;
}
calculatePluginDefinitionsByPrefix();
}
return (org.apache.maven.model.Plugin) pluginDefinitionsByPrefix.get( pluginPrefix );
}
private void calculatePluginDefinitionsByPrefix()
{
pluginDefinitionsByPrefix = new HashMap();
for ( Iterator it = mappings.iterator(); it.hasNext(); )
{
Metadata pluginMap = (Metadata) it.next();
String groupId = pluginMap.getGroupId();
for ( Iterator pluginIterator = pluginMap.getPlugins().iterator(); pluginIterator.hasNext(); )
{
Plugin mapping = (Plugin) pluginIterator.next();
String prefix = mapping.getPrefix();
String artifactId = mapping.getArtifactId();
org.apache.maven.model.Plugin plugin = new org.apache.maven.model.Plugin();
plugin.setGroupId( groupId );
plugin.setArtifactId( artifactId );
pluginDefinitionsByPrefix.put( prefix, plugin );
}
}
}
private void refreshPluginMappingManager( List pluginRepositories, ArtifactRepository localRepository )
throws RepositoryMetadataManagementException
{
List groupIds = new ArrayList();
for ( Iterator it = mappings.iterator(); it.hasNext(); )
{
Metadata map = (Metadata) it.next();
String groupId = map.getGroupId();
groupIds.add( groupId );
repositoryMetadataManager.purgeLocalCopy( new PluginMappingMetadata( groupId ), localRepository );
}
loadPluginMappings( groupIds, pluginRepositories, localRepository );
}
private void loadPluginMappings( List groupIds, List pluginRepositories, ArtifactRepository localRepository )
{
List pluginGroupIds = new ArrayList( groupIds );
@ -159,15 +83,7 @@ public class DefaultPluginMappingManager
try
{
File mappingFile = resolveMappingMetadata( repositoryMetadataManager, groupId, pluginRepositories,
localRepository );
Metadata pluginMap = readPluginMap( mappingFile );
if ( pluginMap != null )
{
mappings.add( pluginMap );
}
loadPluginMappings( groupId, pluginRepositories, localRepository );
}
catch ( RepositoryMetadataManagementException e )
{
@ -175,93 +91,76 @@ public class DefaultPluginMappingManager
getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e );
}
clearCache();
}
}
private static Metadata readPluginMap( File mappingFile )
throws RepositoryMetadataManagementException
{
Metadata result = null;
if ( mappingFile.exists() )
{
Reader fileReader = null;
try
{
fileReader = new FileReader( mappingFile );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
result = mappingReader.read( fileReader );
}
catch ( FileNotFoundException e )
{
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile,
e );
}
catch ( IOException e )
{
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile,
e );
}
catch ( XmlPullParserException e )
{
throw new RepositoryMetadataManagementException( "Cannot parse plugin mappings from: " + mappingFile,
e );
}
finally
{
IOUtil.close( fileReader );
}
}
return result;
}
private static File resolveMappingMetadata( RepositoryMetadataManager repositoryMetadataManager, String groupId,
List pluginRepositories, ArtifactRepository localRepository )
private void loadPluginMappings( String groupId, List pluginRepositories, ArtifactRepository localRepository )
throws RepositoryMetadataManagementException
{
PluginMappingMetadata metadata = new PluginMappingMetadata( groupId );
RepositoryMetadataManagementException repositoryException = null;
// TOOD: aggregate the results of this instead
repositoryMetadataManager.resolve( metadata, pluginRepositories, localRepository );
for ( Iterator repoIterator = pluginRepositories.iterator(); repoIterator.hasNext(); )
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfRepositoryMetadata( metadata ) );
if ( metadataFile.exists() )
{
ArtifactRepository repository = (ArtifactRepository) repoIterator.next();
Metadata pluginMap = readMetadata( metadataFile );
try
if ( pluginMap != null )
{
repositoryMetadataManager.resolve( metadata, repository, localRepository );
// reset this to keep it from getting in the way when we succeed but not on first repo...
repositoryException = null;
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfRepositoryMetadata( metadata ) );
if ( metadataFile.exists() )
for ( Iterator pluginIterator = pluginMap.getPlugins().iterator(); pluginIterator.hasNext(); )
{
return metadataFile;
Plugin mapping = (Plugin) pluginIterator.next();
String prefix = mapping.getPrefix();
String artifactId = mapping.getArtifactId();
org.apache.maven.model.Plugin plugin = new org.apache.maven.model.Plugin();
plugin.setGroupId( groupId );
plugin.setArtifactId( artifactId );
pluginDefinitionsByPrefix.put( prefix, plugin );
}
}
catch ( InvalidRepositoryMetadataException e )
{
repositoryMetadataManager.purgeLocalCopy( metadata, localRepository );
}
catch ( RepositoryMetadataManagementException e )
{
repositoryException = e;
}
}
}
if ( repositoryException != null )
private static Metadata readMetadata( File mappingFile )
throws RepositoryMetadataManagementException
{
Metadata result;
Reader fileReader = null;
try
{
throw repositoryException;
}
fileReader = new FileReader( mappingFile );
throw new RepositoryMetadataManagementException( "No repository metadata found" );
MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
result = mappingReader.read( fileReader );
}
catch ( FileNotFoundException e )
{
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, e );
}
catch ( IOException e )
{
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, e );
}
catch ( XmlPullParserException e )
{
throw new RepositoryMetadataManagementException( "Cannot parse plugin mappings from: " + mappingFile, e );
}
finally
{
IOUtil.close( fileReader );
}
return result;
}
}

View File

@ -16,7 +16,6 @@ package org.apache.maven.plugin.plugin.metadata;
* limitations under the License.
*/
import org.apache.maven.artifact.repository.metadata.InvalidRepositoryMetadataException;
import org.apache.maven.artifact.repository.metadata.PluginMappingMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManagementException;
@ -39,15 +38,6 @@ public class PluginMappingInstallMojo
try
{
try
{
getRepositoryMetadataManager().resolveLocally( metadata, getLocalRepository() );
}
catch ( InvalidRepositoryMetadataException e )
{
getRepositoryMetadataManager().purgeLocalCopy( metadata, getLocalRepository() );
}
File metadataFile = updatePluginMap( metadata );
if ( metadataFile != null )