package org.apache.maven.repository;
/* ====================================================================
* 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 java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.MavenConstants;
import org.apache.maven.MavenUtils;
import org.apache.maven.project.Dependency;
import org.apache.maven.util.MD5Sum;
import org.apache.maven.verifier.ChecksumVerificationException;
import org.codehaus.plexus.util.FileUtils;
/**
* Base class from which all artifact subclasses are derived.
*
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
*/
public abstract class AbstractArtifact
implements Artifact
{
/** logger for output */
private final static Log LOGGER = LogFactory.getLog( AbstractArtifact.class );
/** Platform specific file separator used for file system operations. */
protected static final String fs = File.separator;
/** Dependency this artifact is based on. */
private Dependency dependency;
/** Path to artifact. */
private String path;
/** Override type. */
private String overrideType = null;
/**
* Default constructor.
* @param dependency the dependency the artifact is based on
*/
public AbstractArtifact( Dependency dependency )
{
this.dependency = dependency;
}
/** @see Artifact#setDependency */
public void setDependency( Dependency dependency )
{
this.dependency = dependency;
}
/** @see Artifact#getDependency */
public Dependency getDependency()
{
return dependency;
}
/** @see Artifact#setPath */
public void setPath( String repositoryPath )
{
this.path = repositoryPath;
}
/** @see Artifact#getPath */
public String getPath()
{
if ( path == null )
{
return generatePath();
}
return path;
}
public String generatePath()
{
return fs + getDependency().getArtifactDirectory() + fs + getDependency().getTypeDirectory() + fs
+ getDependency().getArtifact();
}
/** @see Artifact#getUrlPath */
public String getUrlPath()
{
return '/' + getDependency().getArtifactDirectory() + '/' + getDependency().getTypeDirectory() + '/'
+ getDependency().getArtifact();
}
/** @see Artifact#getChecksumUrl */
public String getChecksumUrl()
{
return getUrlPath() + ".md5";
}
/**
* Get the name of the artifact from the underlying dependency.
*
* @return The name of the underlying dependency.
*/
public String getName()
{
return getDependency().getArtifact();
}
/** @see Artifact#exists */
public boolean exists()
{
return getFile().exists();
}
/** @see Artifact#isSnapshot */
public boolean isSnapshot()
{
if ( getDependency().getVersion() != null )
{
return ( getDependency().getVersion().indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) > 0 )
|| ( getDependency().getArtifact().indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) > 0 );
}
else
{
return false;
}
}
/** @see Artifact#getFile */
public File getFile()
{
return new File( getPath() );
}
/**
* C H E C K S U M V E R I F I C A T I O N
* @throws ChecksumVerificationException when the checksum differs
*/
public void verify()
throws ChecksumVerificationException
{
File checksumFile = new File( getFile() + ".md5" );
// The central repository is not fully populated with md5
// checksum files yet so we'll be a little lax right now but
// hopefully in the future we can differentiate or specify
// repositories whose artifacts must undergo verification.
if ( !checksumFile.exists() )
{
return;
}
String actualChecksum;
MD5Sum md5;
try
{
md5 = new MD5Sum();
md5.setFile( getFile() );
md5.execute();
actualChecksum = FileUtils.fileRead( checksumFile.getCanonicalPath() );
actualChecksum = actualChecksum.substring( 0, actualChecksum.length() - 1 );
}
catch ( Exception e )
{
return;
}
LOGGER.debug( "Actual checksum: '" + actualChecksum + "'" );
LOGGER.debug( "MD5 checksum: '" + md5.getChecksum() + "'" );
if ( !actualChecksum.equals( md5.getChecksum() ) )
{
throw new ChecksumVerificationException( MavenUtils.getMessage( "checksum.verification.error", getPath() ) );
}
}
public String getOverrideType()
{
return overrideType;
}
public void setOverrideType( String overrideType )
{
this.overrideType = overrideType;
}
/* (non-Javadoc)
* @see org.apache.maven.repository.Artifact#getDescription()
*/
public String getDescription()
{
StringBuffer buffer = new StringBuffer();
buffer.append( getDependency().getGroupId() );
buffer.append( ":" );
buffer.append( getDependency().getArtifactId() );
buffer.append( ":" );
buffer.append( getDependency().getVersion() );
buffer.append( ":" );
buffer.append( getDependency().getType() );
return buffer.toString();
}
}