1 package org.apache.maven.archiva.repository.content;
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.archiva.metadata.repository.storage.RepositoryPathTranslator;
23 import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
24 import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.common.utils.VersionUtil;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.model.ProjectReference;
30 import org.apache.maven.archiva.model.VersionedReference;
31 import org.apache.maven.archiva.repository.layout.LayoutException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.ApplicationContext;
36 import javax.annotation.PostConstruct;
37 import javax.inject.Inject;
38 import java.util.ArrayList;
39 import java.util.List;
42 * AbstractDefaultRepositoryContent - common methods for working with default (maven 2) layout.
46 public abstract class AbstractDefaultRepositoryContent
48 protected Logger log = LoggerFactory.getLogger( getClass() );
50 public static final String MAVEN_METADATA = "maven-metadata.xml";
52 protected static final char PATH_SEPARATOR = '/';
54 protected static final char GROUP_SEPARATOR = '.';
56 protected static final char ARTIFACT_SEPARATOR = '-';
58 private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator();
60 private PathParser defaultPathParser = new DefaultPathParser();
63 * plexus.requirement role="org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider"
66 protected List<? extends ArtifactMappingProvider> artifactMappingProviders;
69 //protected ApplicationContext applicationContext;
72 protected void initialize()
74 //artifactMappingProviders = new ArrayList<ArtifactMappingProvider>(
75 // applicationContext.getBeansOfType( ArtifactMappingProvider.class ).values() );
78 public ArtifactReference toArtifactReference( String path )
79 throws LayoutException
81 return defaultPathParser.toArtifactReference( path );
84 public String toMetadataPath( ProjectReference reference )
86 StringBuilder path = new StringBuilder();
88 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
89 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
90 path.append( MAVEN_METADATA );
92 return path.toString();
95 public String toMetadataPath( VersionedReference reference )
97 StringBuilder path = new StringBuilder();
99 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
100 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
101 if ( reference.getVersion() != null )
103 // add the version only if it is present
104 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
106 path.append( MAVEN_METADATA );
108 return path.toString();
111 public String toPath( ArchivaArtifact reference )
113 if ( reference == null )
115 throw new IllegalArgumentException( "ArchivaArtifact cannot be null" );
118 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
119 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
120 reference.getClassifier(), reference.getType() );
123 public String toPath( ArtifactReference reference )
125 if ( reference == null )
127 throw new IllegalArgumentException( "Artifact reference cannot be null" );
130 String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() );
131 return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(),
132 reference.getClassifier(), reference.getType() );
135 private String formatAsDirectory( String directory )
137 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
140 private String toPath( String groupId, String artifactId, String baseVersion, String version, String classifier,
143 if ( baseVersion != null )
145 return pathTranslator.toPath( groupId, artifactId, baseVersion,
146 constructId( artifactId, version, classifier, type ) );
150 return pathTranslator.toPath( groupId, artifactId );
154 // TODO: move into the Maven Artifact facet when refactoring away the caller - the caller will need to have access
155 // to the facet or filename (for the original ID)
156 private String constructId( String artifactId, String version, String classifier, String type )
159 for ( ArtifactMappingProvider provider : artifactMappingProviders )
161 ext = provider.mapTypeToExtension( type );
172 StringBuilder id = new StringBuilder();
173 if ( ( version != null ) && ( type != null ) )
175 id.append( artifactId ).append( ARTIFACT_SEPARATOR ).append( version );
177 if ( StringUtils.isNotBlank( classifier ) )
179 id.append( ARTIFACT_SEPARATOR ).append( classifier );
182 id.append( "." ).append( ext );
184 return id.toString();