1 package org.apache.maven.archiva.repository.layout;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.model.ArchivaArtifact;
24 import org.apache.maven.archiva.model.ArtifactReference;
25 import org.apache.maven.archiva.model.ProjectReference;
26 import org.apache.maven.archiva.model.VersionedReference;
27 import org.apache.maven.archiva.repository.content.LegacyArtifactExtensionMapping;
29 import java.util.HashMap;
33 * LegacyBidirectionalRepositoryLayout - the layout mechanism for use by Maven 1.x repositories.
35 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
38 * @plexus.component role-hint="legacy"
40 public class LegacyBidirectionalRepositoryLayout
41 implements BidirectionalRepositoryLayout
43 private static final String MAVEN_METADATA = "maven-metadata.xml";
45 private static final String PATH_SEPARATOR = "/";
47 private LegacyArtifactExtensionMapping extensionMapper = new LegacyArtifactExtensionMapping();
49 private Map typeToDirectoryMap;
51 public LegacyBidirectionalRepositoryLayout()
53 typeToDirectoryMap = new HashMap();
54 typeToDirectoryMap.put( "ejb-client", "ejb" );
55 typeToDirectoryMap.put( "distribution-tgz", "distribution" );
56 typeToDirectoryMap.put( "distribution-zip", "distribution" );
64 public String toPath( ArchivaArtifact artifact )
66 return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
67 artifact.getClassifier(), artifact.getType() );
70 public String toPath( ProjectReference reference )
72 StringBuffer path = new StringBuffer();
74 path.append( reference.getGroupId() ).append( PATH_SEPARATOR );
75 path.append( getDirectory( null, "jar" ) ).append( PATH_SEPARATOR );
76 path.append( MAVEN_METADATA );
78 return path.toString();
81 public String toPath( VersionedReference reference )
83 // NOTE: A legacy repository cannot contain a versioned reference to the metadata.
84 StringBuffer path = new StringBuffer();
86 path.append( reference.getGroupId() ).append( PATH_SEPARATOR );
87 path.append( getDirectory( null, "jar" ) ).append( PATH_SEPARATOR );
88 path.append( MAVEN_METADATA );
90 return path.toString();
93 public String toPath( ArtifactReference reference )
95 return toPath( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), reference
96 .getClassifier(), reference.getType() );
99 private String toPath( String groupId, String artifactId, String version, String classifier, String type )
101 StringBuffer path = new StringBuffer();
103 path.append( groupId ).append( PATH_SEPARATOR );
104 path.append( getDirectory( classifier, type ) ).append( PATH_SEPARATOR );
106 if ( version != null )
108 path.append( artifactId ).append( '-' ).append( version );
110 if ( StringUtils.isNotBlank( classifier ) )
112 path.append( '-' ).append( classifier );
115 path.append( '.' ).append( extensionMapper.getExtension( type ) );
118 return path.toString();
121 private String getDirectory( String classifier, String type )
123 // Special Cases involving type + classifier
124 if ( "jar".equals( type ) && StringUtils.isNotBlank( classifier ) )
126 if ( "sources".equals( classifier ) )
128 return "source.jars";
131 if ( "javadoc".equals( classifier ) )
133 return "javadoc.jars";
137 // Special Cases involving only type.
138 String dirname = (String) typeToDirectoryMap.get( type );
140 if ( dirname != null )
142 return dirname + "s";
151 public String groupId;
153 public String pathType;
157 public FilenameParts fileParts;
160 private PathReferences toPathReferences( String path, boolean parseFilename )
161 throws LayoutException
163 PathReferences prefs = new PathReferences();
165 String normalizedPath = StringUtils.replace( path, "\\", "/" );
167 String pathParts[] = StringUtils.split( normalizedPath, '/' );
169 /* Always 3 parts. (Never more or less)
171 * path = "commons-lang/jars/commons-lang-2.1.jar"
172 * path[0] = "commons-lang"; // The Group ID
173 * path[1] = "jars"; // The Directory Type
174 * path[2] = "commons-lang-2.1.jar"; // The Filename.
177 if ( pathParts.length != 3 )
179 // Illegal Path Parts Length.
180 throw new LayoutException( "Invalid number of parts to the path [" + path
181 + "] to construct an ArchivaArtifact from. (Required to be 3 parts)" );
185 prefs.groupId = pathParts[0];
187 // The Expected Type.
188 prefs.pathType = pathParts[1];
193 String filename = pathParts[2];
195 prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
197 prefs.type = extensionMapper.getType( prefs.pathType, filename );
200 if ( StringUtils.isEmpty( prefs.fileParts.extension ) )
202 throw new LayoutException( "Invalid artifact, no extension." );
205 if ( !prefs.type.equals( prefs.fileParts.extension ) )
207 throw new LayoutException( "Invalid artifact, mismatch on extension <" + prefs.fileParts.extension
208 + "> and expected layout specified type <" + prefs.type
209 + "> (mapped from actual path provided type <" + prefs.pathType + ">)" );
216 public ProjectReference toProjectReference( String path )
217 throws LayoutException
219 throw new LayoutException( "Cannot parse legacy paths to a Project Reference." );
222 public ArchivaArtifact toArtifact( String path )
223 throws LayoutException
225 PathReferences pathrefs = toPathReferences( path, true );
227 ArchivaArtifact artifact = new ArchivaArtifact( pathrefs.groupId, pathrefs.fileParts.artifactId,
228 pathrefs.fileParts.version, pathrefs.fileParts.classifier,
234 public ArtifactReference toArtifactReference( String path )
235 throws LayoutException
237 PathReferences pathrefs = toPathReferences( path, true );
239 ArtifactReference reference = new ArtifactReference();
241 reference.setGroupId( pathrefs.groupId );
242 reference.setArtifactId( pathrefs.fileParts.artifactId );
243 reference.setVersion( pathrefs.fileParts.version );
244 reference.setClassifier( pathrefs.fileParts.classifier );
245 reference.setType( pathrefs.type );
250 public VersionedReference toVersionedReference( String path )
251 throws LayoutException