1 package org.apache.maven.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.maven.MavenConstants;
26 import org.apache.maven.MavenUtils;
27 import org.apache.maven.project.Dependency;
28 import org.apache.maven.util.MD5Sum;
29 import org.apache.maven.verifier.ChecksumVerificationException;
30 import org.codehaus.plexus.util.FileUtils;
31
32 /**
33 * Base class from which all artifact subclasses are derived.
34 *
35 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
36 */
37 public abstract class AbstractArtifact
38 implements Artifact
39 {
40 /** logger for output */
41 private final static Log LOGGER = LogFactory.getLog( AbstractArtifact.class );
42
43 /** Platform specific file separator used for file system operations. */
44 protected static final String fs = File.separator;
45
46 /** Dependency this artifact is based on. */
47 private Dependency dependency;
48
49 /** Path to artifact. */
50 private String path;
51
52 /** Override type. */
53 private String overrideType = null;
54
55 /**
56 * Default constructor.
57 * @param dependency the dependency the artifact is based on
58 */
59 public AbstractArtifact( Dependency dependency )
60 {
61 this.dependency = dependency;
62 }
63
64 /** @see Artifact#setDependency */
65 public void setDependency( Dependency dependency )
66 {
67 this.dependency = dependency;
68 }
69
70 /** @see Artifact#getDependency */
71 public Dependency getDependency()
72 {
73 return dependency;
74 }
75
76 /** @see Artifact#setPath */
77 public void setPath( String repositoryPath )
78 {
79 this.path = repositoryPath;
80 }
81
82 /** @see Artifact#getPath */
83 public String getPath()
84 {
85 if ( path == null )
86 {
87 return generatePath();
88 }
89
90 return path;
91 }
92
93 public String generatePath()
94 {
95 return fs + getDependency().getArtifactDirectory() + fs + getDependency().getTypeDirectory() + fs
96 + getDependency().getArtifact();
97 }
98
99 /** @see Artifact#getUrlPath */
100 public String getUrlPath()
101 {
102 return '/' + getDependency().getArtifactDirectory() + '/' + getDependency().getTypeDirectory() + '/'
103 + getDependency().getArtifact();
104 }
105
106 /** @see Artifact#getChecksumUrl */
107 public String getChecksumUrl()
108 {
109 return getUrlPath() + ".md5";
110 }
111
112 /**
113 * Get the name of the artifact from the underlying dependency.
114 *
115 * @return The name of the underlying dependency.
116 */
117 public String getName()
118 {
119 return getDependency().getArtifact();
120 }
121
122 /** @see Artifact#exists */
123 public boolean exists()
124 {
125 return getFile().exists();
126 }
127
128 /** @see Artifact#isSnapshot */
129 public boolean isSnapshot()
130 {
131 if ( getDependency().getVersion() != null )
132 {
133 return ( getDependency().getVersion().indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) > 0 )
134 || ( getDependency().getArtifact().indexOf( MavenConstants.SNAPSHOT_SIGNIFIER ) > 0 );
135 }
136 else
137 {
138 return false;
139 }
140 }
141
142 /** @see Artifact#getFile */
143 public File getFile()
144 {
145 return new File( getPath() );
146 }
147
148 /**
149 * C H E C K S U M V E R I F I C A T I O N
150 * @throws ChecksumVerificationException when the checksum differs
151 */
152 public void verify()
153 throws ChecksumVerificationException
154 {
155 File checksumFile = new File( getFile() + ".md5" );
156
157
158
159
160
161 if ( !checksumFile.exists() )
162 {
163 return;
164 }
165
166 String actualChecksum;
167 MD5Sum md5;
168
169 try
170 {
171 md5 = new MD5Sum();
172 md5.setFile( getFile() );
173 md5.execute();
174
175 actualChecksum = FileUtils.fileRead( checksumFile.getCanonicalPath() );
176 actualChecksum = actualChecksum.substring( 0, actualChecksum.length() - 1 );
177 }
178 catch ( Exception e )
179 {
180 return;
181 }
182
183 LOGGER.debug( "Actual checksum: '" + actualChecksum + "'" );
184 LOGGER.debug( "MD5 checksum: '" + md5.getChecksum() + "'" );
185
186 if ( !actualChecksum.equals( md5.getChecksum() ) )
187 {
188 throw new ChecksumVerificationException( MavenUtils.getMessage( "checksum.verification.error", getPath() ) );
189 }
190 }
191
192 public String getOverrideType()
193 {
194 return overrideType;
195 }
196
197 public void setOverrideType( String overrideType )
198 {
199 this.overrideType = overrideType;
200 }
201
202
203
204
205 public String getDescription()
206 {
207 StringBuffer buffer = new StringBuffer();
208 buffer.append( getDependency().getGroupId() );
209 buffer.append( ":" );
210 buffer.append( getDependency().getArtifactId() );
211 buffer.append( ":" );
212 buffer.append( getDependency().getVersion() );
213 buffer.append( ":" );
214 buffer.append( getDependency().getType() );
215 return buffer.toString();
216 }
217
218 }