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.repository.content.ArtifactExtensionMapping;
27 import java.util.HashMap;
31 * LegacyBidirectionalRepositoryLayout - the layout mechanism for use by Maven 1.x repositories.
33 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
35 * @plexus.component role-hint="legacy"
37 public class LegacyBidirectionalRepositoryLayout
38 implements BidirectionalRepositoryLayout
40 private static final String DIR_JAVADOC = "javadoc.jars";
42 private static final String DIR_JAVA_SOURCE = "java-sources";
44 private static final String PATH_SEPARATOR = "/";
46 private Map typeToDirectoryMap;
48 public LegacyBidirectionalRepositoryLayout()
50 typeToDirectoryMap = new HashMap();
51 typeToDirectoryMap.put( "ejb-client", "ejb" );
52 typeToDirectoryMap.put( "distribution-tgz", "distribution" );
53 typeToDirectoryMap.put( "distribution-zip", "distribution" );
61 public String toPath( ArchivaArtifact artifact )
63 return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
64 artifact.getClassifier(), artifact.getType() );
67 public String toPath( ArtifactReference reference )
69 return toPath( reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), reference
70 .getClassifier(), reference.getType() );
73 private String toPath( String groupId, String artifactId, String version, String classifier, String type )
75 StringBuffer path = new StringBuffer();
77 path.append( groupId ).append( PATH_SEPARATOR );
78 path.append( getDirectory( classifier, type ) ).append( PATH_SEPARATOR );
80 if ( version != null )
82 path.append( artifactId ).append( '-' ).append( version );
84 if ( StringUtils.isNotBlank( classifier ) )
86 path.append( '-' ).append( classifier );
89 path.append( '.' ).append( ArtifactExtensionMapping.getExtension( type ) );
92 return path.toString();
95 private String getDirectory( String classifier, String type )
97 // Special Cases involving type + classifier
98 if ( "jar".equals( type ) && StringUtils.isNotBlank( classifier ) )
100 if ( "sources".equals( classifier ) )
102 return DIR_JAVA_SOURCE;
105 if ( "javadoc".equals( classifier ) )
111 // Special Cases involving only type.
112 String dirname = (String) typeToDirectoryMap.get( type );
114 if ( dirname != null )
116 return dirname + "s";
125 public String groupId;
127 public String pathType;
131 public FilenameParts fileParts;
134 private PathReferences toPathReferences( String path )
135 throws LayoutException
137 PathReferences prefs = new PathReferences();
139 String normalizedPath = StringUtils.replace( path, "\\", "/" );
141 String pathParts[] = StringUtils.split( normalizedPath, '/' );
143 /* Always 3 parts. (Never more or less)
145 * path = "commons-lang/jars/commons-lang-2.1.jar"
146 * path[0] = "commons-lang"; // The Group ID
147 * path[1] = "jars"; // The Directory Type
148 * path[2] = "commons-lang-2.1.jar"; // The Filename.
151 if ( pathParts.length != 3 )
153 // Illegal Path Parts Length.
154 throw new LayoutException( "Invalid number of parts to the path [" + path
155 + "] to construct an ArchivaArtifact from. (Required to be 3 parts)" );
159 prefs.groupId = pathParts[0];
161 // The Expected Type.
162 prefs.pathType = pathParts[1];
165 String filename = pathParts[2];
167 prefs.fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
169 String trimPathType = prefs.pathType.substring( 0, prefs.pathType.length() - 1 );
170 prefs.type = ArtifactExtensionMapping.guessTypeFromFilename( filename );
172 // Sanity Check: does it have an extension?
173 if ( StringUtils.isEmpty( prefs.fileParts.extension ) )
175 throw new LayoutException( "Invalid artifact, no extension." );
178 // Sanity Check: pathType should end in "s".
179 if ( !prefs.pathType.toLowerCase().endsWith( "s" ) )
181 throw new LayoutException( "Invalid path, the type specified in the path <" + prefs.pathType
182 + "> does not end in the letter <s>." );
185 // Sanity Check: does extension match pathType on path?
186 String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
187 String actualExtension = prefs.fileParts.extension;
189 if ( !expectedExtension.equals( actualExtension ) )
191 throw new LayoutException( "Invalid artifact, mismatch on extension <" + prefs.fileParts.extension
192 + "> and layout specified type <" + prefs.pathType + "> (which maps to extension: <"
193 + expectedExtension + ">) on path <" + path + ">" );
199 public boolean isValidPath( String path )
203 toPathReferences( path );
206 catch ( LayoutException e )
212 public ArchivaArtifact toArtifact( String path )
213 throws LayoutException
215 PathReferences pathrefs = toPathReferences( path );
217 ArchivaArtifact artifact = new ArchivaArtifact( pathrefs.groupId, pathrefs.fileParts.artifactId,
218 pathrefs.fileParts.version, pathrefs.fileParts.classifier,
224 public ArtifactReference toArtifactReference( String path )
225 throws LayoutException
227 PathReferences pathrefs = toPathReferences( path );
229 ArtifactReference reference = new ArtifactReference();
231 reference.setGroupId( pathrefs.groupId );
232 reference.setArtifactId( pathrefs.fileParts.artifactId );
233 reference.setVersion( pathrefs.fileParts.version );
234 reference.setClassifier( pathrefs.fileParts.classifier );
235 reference.setType( pathrefs.type );