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.repository.content.ArtifactExtensionMapping;
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 PATH_SEPARATOR = "/";
45 private ArtifactExtensionMapping extensionMapper = new LegacyArtifactExtensionMapping();
47 private Map typeToDirectoryMap;
49 public LegacyBidirectionalRepositoryLayout()
51 typeToDirectoryMap = new HashMap();
52 typeToDirectoryMap.put( "ejb-client", "ejb" );
53 typeToDirectoryMap.put( "distribution-tgz", "distribution" );
54 typeToDirectoryMap.put( "distribution-zip", "distribution" );
62 public String toPath( ArchivaArtifact reference )
64 return toPath( reference.getGroupId(), reference.getArtifactId(), reference
65 .getVersion(), reference.getClassifier(), reference.getType() );
68 public String toPath( ProjectReference reference )
71 return toPath( reference.getGroupId(), reference.getArtifactId(), null, null, "metadata-xml" );
74 public String toPath( ArtifactReference artifact )
76 return toPath( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getClassifier(),
80 private String toPath( String groupId, String artifactId, String version, String classifier, String type )
82 StringBuffer path = new StringBuffer();
84 path.append( groupId ).append( PATH_SEPARATOR );
85 path.append( getDirectory( classifier, type ) ).append( PATH_SEPARATOR );
87 if ( version != null )
89 path.append( artifactId ).append( '-' ).append( version );
91 if ( StringUtils.isNotBlank( classifier ) )
93 path.append( '-' ).append( classifier );
96 path.append( '.' ).append( extensionMapper.getExtension( type ) );
99 return path.toString();
102 private String getDirectory( String classifier, String type )
104 // Special Cases involving classifiers and type.
105 if ( "jar".equals( type ) && "sources".equals( classifier ) )
107 return "javadoc.jars";
110 // Special Cases involving only type.
111 String dirname = (String) typeToDirectoryMap.get( type );
113 if ( dirname != null )
115 return dirname + "s";
122 public ArchivaArtifact toArtifact( String path )
123 throws LayoutException
125 String normalizedPath = StringUtils.replace( path, "\\", "/" );
127 String pathParts[] = StringUtils.split( normalizedPath, '/' );
129 /* Always 3 parts. (Never more or less)
131 * path = "commons-lang/jars/commons-lang-2.1.jar"
132 * path[0] = "commons-lang"; // The Group ID
133 * path[1] = "jars"; // The Directory Type
134 * path[2] = "commons-lang-2.1.jar"; // The Filename.
137 if ( pathParts.length != 3 )
139 // Illegal Path Parts Length.
140 throw new LayoutException( "Invalid number of parts to the path [" + path
141 + "] to construct an ArchivaArtifact from. (Required to be 3 parts)" );
145 String groupId = pathParts[0];
147 // The Expected Type.
148 String expectedType = pathParts[1];
151 String filename = pathParts[2];
153 FilenameParts fileParts = RepositoryLayoutUtils.splitFilename( filename, null );
155 String type = extensionMapper.getType( filename );
157 ArchivaArtifact artifact = new ArchivaArtifact( groupId, fileParts.artifactId, fileParts.version,
158 fileParts.classifier, type );
161 if ( StringUtils.isEmpty( fileParts.extension ) )
163 throw new LayoutException( "Invalid artifact, no extension." );
166 if ( !expectedType.equals( fileParts.extension + "s" ) )
168 throw new LayoutException( "Invalid artifact, extension and layout specified type mismatch." );