1 package org.apache.maven.archiva.common.consumers;
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.maven.archiva.common.artifact.builder.BuilderException;
23 import org.apache.maven.archiva.common.artifact.builder.DefaultLayoutArtifactBuilder;
24 import org.apache.maven.archiva.common.artifact.builder.LayoutArtifactBuilder;
25 import org.apache.maven.archiva.common.artifact.builder.LegacyLayoutArtifactBuilder;
26 import org.apache.maven.archiva.common.utils.BaseFile;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
31 import org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
39 * DefaultArtifactConsumer
41 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44 public abstract class GenericArtifactConsumer
45 extends AbstractConsumer
48 public abstract void processArtifact( Artifact artifact, BaseFile file );
50 private Map artifactBuilders = new HashMap();
52 private static final List includePatterns;
56 includePatterns = new ArrayList();
57 includePatterns.add( "**/*.pom" );
58 includePatterns.add( "**/*.jar" );
59 includePatterns.add( "**/*.war" );
60 includePatterns.add( "**/*.ear" );
61 includePatterns.add( "**/*.sar" );
62 includePatterns.add( "**/*.zip" );
63 includePatterns.add( "**/*.gz" );
64 includePatterns.add( "**/*.bz2" );
67 private String layoutId = "default";
69 public boolean init( ArtifactRepository repository )
71 this.artifactBuilders.clear();
72 this.artifactBuilders.put( "default", new DefaultLayoutArtifactBuilder( artifactFactory ) );
73 this.artifactBuilders.put( "legacy", new LegacyLayoutArtifactBuilder( artifactFactory ) );
75 if ( repository.getLayout() instanceof LegacyRepositoryLayout )
77 this.layoutId = "legacy";
80 return super.init( repository );
83 public List getIncludePatterns()
85 return includePatterns;
88 public boolean isEnabled()
90 ArtifactRepositoryLayout layout = repository.getLayout();
91 return ( layout instanceof DefaultRepositoryLayout ) || ( layout instanceof LegacyRepositoryLayout );
94 public void processFile( BaseFile file )
95 throws ConsumerException
97 if ( file.length() <= 0 )
99 processFileProblem( file, "File is empty." );
102 if ( !file.canRead() )
104 processFileProblem( file, "Not allowed to read file due to permission settings on file." );
109 Artifact artifact = buildArtifact( file );
111 processArtifact( artifact, file );
113 catch ( BuilderException e )
115 throw new ConsumerException( file, e.getMessage(), e );
119 private Artifact buildArtifact( BaseFile file )
120 throws BuilderException
122 LayoutArtifactBuilder builder = (LayoutArtifactBuilder) artifactBuilders.get( layoutId );
124 Artifact artifact = builder.build( file.getRelativePath() );
125 artifact.setRepository( repository );
126 artifact.setFile( file );