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.apache.maven.wagon.TransferFailedException;
import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.util.HashSet;
import java.util.HashMap; import java.util.Iterator;
import java.util.Map; import java.util.List;
import java.util.Set;
/**
* @todo try to crop all, particularly plugin stuff
* @todo check caching?
*/
public class DefaultRepositoryMetadataManager public class DefaultRepositoryMetadataManager
extends AbstractLogEnabled extends AbstractLogEnabled
implements RepositoryMetadataManager implements RepositoryMetadataManager
@ -42,31 +37,32 @@ public class DefaultRepositoryMetadataManager
// component requirement // component requirement
private WagonManager wagonManager; 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 throws RepositoryMetadataManagementException
{ {
resolve( metadata, null, local ); boolean alreadyResolved = alreadyResolved( metadata );
} if ( !alreadyResolved )
public void resolve( RepositoryMetadata metadata, ArtifactRepository remote, ArtifactRepository local )
throws RepositoryMetadataManagementException
{ {
File metadataFile = (File) cachedMetadata.get( metadata.getRepositoryPath() ); for ( Iterator i = repositories.iterator(); i.hasNext(); )
if ( metadataFile == null )
{ {
metadataFile = constructLocalRepositoryFile( metadata, local ); ArtifactRepository repository = (ArtifactRepository) i.next();
if ( !metadataFile.exists() && remote != null ) // 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 try
{ {
try try
{ {
wagonManager.getRepositoryMetadata( metadata, remote, metadataFile ); wagonManager.getRepositoryMetadata( metadata, repository, metadataFile );
} }
catch ( ResourceDoesNotExistException e ) catch ( ResourceDoesNotExistException e )
{ {
@ -78,7 +74,8 @@ public class DefaultRepositoryMetadataManager
} }
else else
{ {
String message = "Cannot find " + metadata + " in remote repository - Using local copy."; String message = "Cannot find " + metadata +
" in remote repository - Using local copy.";
getLogger().info( message ); getLogger().info( message );
@ -97,36 +94,8 @@ public class DefaultRepositoryMetadataManager
getLogger().info( "Using local copy of " + metadata + " from: " + metadataFile ); getLogger().info( "Using local copy of " + metadata + " from: " + metadataFile );
} }
if ( metadataFile.exists() ) cachedMetadata.add( metadata.getRepositoryPath() );
{
if ( !verifyFileNotEmpty( metadataFile ) )
{
throw new InvalidRepositoryMetadataException( metadata, "Metadata located in file: " +
metadataFile + " appears to be corrupt (file is empty). DOWNLOAD FAILED." );
} }
cachedMetadata.put( metadata.getRepositoryPath(), metadataFile );
}
}
}
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 );
} }
} }
@ -147,7 +116,7 @@ public class DefaultRepositoryMetadataManager
public void install( File source, RepositoryMetadata metadata, ArtifactRepository local ) public void install( File source, RepositoryMetadata metadata, ArtifactRepository local )
throws RepositoryMetadataManagementException throws RepositoryMetadataManagementException
{ {
File metadataFile = constructLocalRepositoryFile( metadata, local ); File metadataFile = new File( local.getBasedir(), local.pathOfRepositoryMetadata( metadata ) );
try try
{ {
@ -167,24 +136,8 @@ public class DefaultRepositoryMetadataManager
} }
public void purgeLocalCopy( RepositoryMetadata metadata, ArtifactRepository local ) private boolean alreadyResolved( RepositoryMetadata metadata )
throws RepositoryMetadataManagementException
{ {
File metadataFile = constructLocalRepositoryFile( metadata, local ); return cachedMetadata.contains( metadata.getRepositoryPath() );
if ( metadataFile.exists() )
{
if ( !metadataFile.delete() )
{
throw new RepositoryMetadataManagementException( metadata,
"Failed to purge local copy from: " + metadataFile );
} }
} }
}
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. * limitations under the License.
*/ */
import java.io.File;
public class PluginMappingMetadata public class PluginMappingMetadata
implements RepositoryMetadata implements RepositoryMetadata
{ {
@ -25,11 +23,6 @@ public class PluginMappingMetadata
private final String groupId; private final String groupId;
/**
* @todo REMOVE!
*/
private File metadataFile;
public PluginMappingMetadata( String groupId ) public PluginMappingMetadata( String groupId )
{ {
this.groupId = 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 org.apache.maven.artifact.repository.ArtifactRepository;
import java.io.File; import java.io.File;
import java.util.List;
public interface RepositoryMetadataManager 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; throws RepositoryMetadataManagementException;
void deploy( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository remote ) void deploy( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository remote )
@ -34,6 +33,4 @@ public interface RepositoryMetadataManager
void install( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository local ) void install( File source, RepositoryMetadata repositoryMetadata, ArtifactRepository local )
throws RepositoryMetadataManagementException; 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.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.InvalidRepositoryMetadataException;
import org.apache.maven.artifact.repository.metadata.Metadata; import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.Plugin; import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.PluginMappingMetadata; import org.apache.maven.artifact.repository.metadata.PluginMappingMetadata;
@ -51,98 +50,23 @@ public class DefaultPluginMappingManager
{ {
protected RepositoryMetadataManager repositoryMetadataManager; protected RepositoryMetadataManager repositoryMetadataManager;
private List mappings = new ArrayList(); private Map pluginDefinitionsByPrefix = new HashMap();
private boolean refreshed;
private Map pluginDefinitionsByPrefix;
public void clear()
{
this.mappings = null;
clearCache();
}
private void clearCache()
{
this.pluginDefinitionsByPrefix = null;
}
public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories, public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories,
ArtifactRepository localRepository ) ArtifactRepository localRepository )
throws RepositoryMetadataManagementException throws RepositoryMetadataManagementException
{ {
if ( pluginDefinitionsByPrefix == null )
{
// firstly, search the local repository
loadPluginMappings( groupIds, pluginRepositories, localRepository );
calculatePluginDefinitionsByPrefix();
// if not found, try from the remote repository // if not found, try from the remote repository
if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) && !refreshed ) if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) )
{ {
getLogger().info( getLogger().info( "Searching repository for plugin with prefix: \'" + pluginPrefix + "\'." );
"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 ); loadPluginMappings( groupIds, pluginRepositories, localRepository );
} }
return (org.apache.maven.model.Plugin) pluginDefinitionsByPrefix.get( pluginPrefix );
}
private void loadPluginMappings( List groupIds, List pluginRepositories, ArtifactRepository localRepository ) private void loadPluginMappings( List groupIds, List pluginRepositories, ArtifactRepository localRepository )
{ {
List pluginGroupIds = new ArrayList( groupIds ); List pluginGroupIds = new ArrayList( groupIds );
@ -159,15 +83,7 @@ public class DefaultPluginMappingManager
try try
{ {
File mappingFile = resolveMappingMetadata( repositoryMetadataManager, groupId, pluginRepositories, loadPluginMappings( groupId, pluginRepositories, localRepository );
localRepository );
Metadata pluginMap = readPluginMap( mappingFile );
if ( pluginMap != null )
{
mappings.add( pluginMap );
}
} }
catch ( RepositoryMetadataManagementException e ) catch ( RepositoryMetadataManagementException e )
{ {
@ -175,18 +91,51 @@ public class DefaultPluginMappingManager
getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e ); getLogger().debug( "Error resolving plugin-mapping metadata for groupId: " + groupId + ".", e );
} }
clearCache();
} }
} }
private static Metadata readPluginMap( File mappingFile ) private void loadPluginMappings( String groupId, List pluginRepositories, ArtifactRepository localRepository )
throws RepositoryMetadataManagementException throws RepositoryMetadataManagementException
{ {
Metadata result = null; PluginMappingMetadata metadata = new PluginMappingMetadata( groupId );
if ( mappingFile.exists() ) // TOOD: aggregate the results of this instead
repositoryMetadataManager.resolve( metadata, pluginRepositories, localRepository );
File metadataFile = new File( localRepository.getBasedir(),
localRepository.pathOfRepositoryMetadata( metadata ) );
if ( metadataFile.exists() )
{ {
Metadata pluginMap = readMetadata( metadataFile );
if ( pluginMap != null )
{
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 static Metadata readMetadata( File mappingFile )
throws RepositoryMetadataManagementException
{
Metadata result;
Reader fileReader = null; Reader fileReader = null;
try try
{ {
@ -198,70 +147,20 @@ public class DefaultPluginMappingManager
} }
catch ( FileNotFoundException e ) catch ( FileNotFoundException e )
{ {
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, e );
e );
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, throw new RepositoryMetadataManagementException( "Cannot read plugin mappings from: " + mappingFile, e );
e );
} }
catch ( XmlPullParserException e ) catch ( XmlPullParserException e )
{ {
throw new RepositoryMetadataManagementException( "Cannot parse plugin mappings from: " + mappingFile, throw new RepositoryMetadataManagementException( "Cannot parse plugin mappings from: " + mappingFile, e );
e );
} }
finally finally
{ {
IOUtil.close( fileReader ); IOUtil.close( fileReader );
} }
}
return result; return result;
} }
private static File resolveMappingMetadata( RepositoryMetadataManager repositoryMetadataManager, String groupId,
List pluginRepositories, ArtifactRepository localRepository )
throws RepositoryMetadataManagementException
{
PluginMappingMetadata metadata = new PluginMappingMetadata( groupId );
RepositoryMetadataManagementException repositoryException = null;
for ( Iterator repoIterator = pluginRepositories.iterator(); repoIterator.hasNext(); )
{
ArtifactRepository repository = (ArtifactRepository) repoIterator.next();
try
{
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() )
{
return metadataFile;
}
}
catch ( InvalidRepositoryMetadataException e )
{
repositoryMetadataManager.purgeLocalCopy( metadata, localRepository );
}
catch ( RepositoryMetadataManagementException e )
{
repositoryException = e;
}
}
if ( repositoryException != null )
{
throw repositoryException;
}
throw new RepositoryMetadataManagementException( "No repository metadata found" );
}
} }

View File

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