This project has retired. For details please refer to its Attic page.
LegacyConverterArtifactConsumer xref
View Javadoc
1   package org.apache.archiva.converter.legacy;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24  import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
26  import org.apache.archiva.consumers.ConsumerException;
27  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28  import org.apache.archiva.converter.artifact.ArtifactConversionException;
29  import org.apache.archiva.converter.artifact.ArtifactConverter;
30  import org.apache.archiva.model.ArtifactReference;
31  import org.apache.archiva.repository.ManagedRepositoryContent;
32  import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
33  import org.apache.archiva.repository.layout.LayoutException;
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.factory.ArtifactFactory;
36  import org.apache.maven.artifact.repository.ArtifactRepository;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.springframework.context.annotation.Scope;
40  import org.springframework.stereotype.Service;
41  
42  import javax.inject.Inject;
43  import javax.inject.Named;
44  import java.util.ArrayList;
45  import java.util.Date;
46  import java.util.List;
47  
48  /**
49   * LegacyConverterArtifactConsumer - convert artifacts as they are found
50   * into the destination repository.
51   *
52   *
53   */
54  @Service( "knownRepositoryContentConsumer#artifact-legacy-to-default-converter" )
55  @Scope( "prototype" )
56  public class LegacyConverterArtifactConsumer
57      extends AbstractMonitoredConsumer
58      implements KnownRepositoryContentConsumer
59  {
60      private Logger log = LoggerFactory.getLogger( LegacyConverterArtifactConsumer.class );
61  
62      @Inject
63      @Named("artifactConverter#legacy-to-default")
64      private ArtifactConverter artifactConverter;
65  
66      private ArtifactFactory artifactFactory;
67  
68      private ManagedRepositoryContent managedRepository;
69  
70      private ArtifactRepository destinationRepository;
71  
72      private List<String> includes;
73  
74      private List<String> excludes;
75  
76      @Inject
77      public LegacyConverterArtifactConsumer( PlexusSisuBridge plexusSisuBridge )
78          throws PlexusSisuBridgeException
79      {
80          includes = new ArrayList<>( 3 );
81          includes.add( "**/*.jar" );
82          includes.add( "**/*.ear" );
83          includes.add( "**/*.war" );
84          artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
85      }
86  
87      @Override
88      public void beginScan( ManagedRepository repository, Date whenGathered )
89          throws ConsumerException
90      {
91          this.managedRepository = new ManagedDefaultRepositoryContent();
92          this.managedRepository.setRepository( repository );
93      }
94  
95      @Override
96      public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
97          throws ConsumerException
98      {
99          beginScan( repository, whenGathered );
100     }
101 
102     @Override
103     public void completeScan()
104     {
105         // no op
106     }
107 
108     @Override
109     public void completeScan( boolean executeOnEntireRepo )
110     {
111         completeScan();
112     }
113 
114     @Override
115     public List<String> getExcludes()
116     {
117         return excludes;
118     }
119 
120     @Override
121     public List<String> getIncludes()
122     {
123         return includes;
124     }
125 
126     @Override
127     public void processFile( String path )
128         throws ConsumerException
129     {
130         try
131         {
132             ArtifactReference reference = managedRepository.toArtifactReference( path );
133             Artifact artifact = artifactFactory.createArtifact( reference.getGroupId(), reference.getArtifactId(),
134                                                                 reference.getVersion(), reference.getClassifier(),
135                                                                 reference.getType() );
136             artifactConverter.convert( artifact, destinationRepository );
137         }
138         catch ( LayoutException e )
139         {
140             log.warn( "Unable to convert artifact: {} : {}",path , e.getMessage(), e );
141         }
142         catch ( ArtifactConversionException e )
143         {
144             log.warn( "Unable to convert artifact: {} : {}",path , e.getMessage(), e );
145         }
146     }
147 
148     @Override
149     public void processFile( String path, boolean executeOnEntireRepo )
150         throws Exception
151     {
152         processFile( path );
153     }
154 
155     @Override
156     public String getDescription()
157     {
158         return "Legacy Artifact to Default Artifact Converter";
159     }
160 
161     @Override
162     public String getId()
163     {
164         return "artifact-legacy-to-default-converter";
165     }
166 
167     public void setExcludes( List<String> excludes )
168     {
169         this.excludes = excludes;
170     }
171 
172     public void setIncludes( List<String> includes )
173     {
174         this.includes = includes;
175     }
176 
177     public ArtifactRepository getDestinationRepository()
178     {
179         return destinationRepository;
180     }
181 
182     public void setDestinationRepository( ArtifactRepository destinationRepository )
183     {
184         this.destinationRepository = destinationRepository;
185     }
186 }