001    package org.apache.archiva.converter.legacy;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.archiva.admin.model.beans.ManagedRepository;
023    import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
024    import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
025    import org.apache.archiva.consumers.AbstractMonitoredConsumer;
026    import org.apache.archiva.consumers.ConsumerException;
027    import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
028    import org.apache.archiva.converter.artifact.ArtifactConversionException;
029    import org.apache.archiva.converter.artifact.ArtifactConverter;
030    import org.apache.archiva.model.ArtifactReference;
031    import org.apache.archiva.repository.ManagedRepositoryContent;
032    import org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent;
033    import org.apache.archiva.repository.layout.LayoutException;
034    import org.apache.maven.artifact.Artifact;
035    import org.apache.maven.artifact.factory.ArtifactFactory;
036    import org.apache.maven.artifact.repository.ArtifactRepository;
037    import org.slf4j.Logger;
038    import org.slf4j.LoggerFactory;
039    import org.springframework.context.annotation.Scope;
040    import org.springframework.stereotype.Service;
041    
042    import javax.inject.Inject;
043    import java.util.ArrayList;
044    import java.util.Date;
045    import java.util.List;
046    
047    /**
048     * LegacyConverterArtifactConsumer - convert artifacts as they are found
049     * into the destination repository.
050     *
051     *
052     */
053    @Service( "knownRepositoryContentConsumer#artifact-legacy-to-default-converter" )
054    @Scope( "prototype" )
055    public class LegacyConverterArtifactConsumer
056        extends AbstractMonitoredConsumer
057        implements KnownRepositoryContentConsumer
058    {
059        private Logger log = LoggerFactory.getLogger( LegacyConverterArtifactConsumer.class );
060    
061        /**
062         *
063         */
064        @Inject
065        private ArtifactConverter artifactConverter;
066    
067        /**
068         *
069         */
070        private ArtifactFactory artifactFactory;
071    
072        private ManagedRepositoryContent managedRepository;
073    
074        private ArtifactRepository destinationRepository;
075    
076        private List<String> includes;
077    
078        private List<String> excludes;
079    
080        @Inject
081        public LegacyConverterArtifactConsumer( PlexusSisuBridge plexusSisuBridge )
082            throws PlexusSisuBridgeException
083        {
084            includes = new ArrayList<String>( 3 );
085            includes.add( "**/*.jar" );
086            includes.add( "**/*.ear" );
087            includes.add( "**/*.war" );
088            artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
089        }
090    
091        public void beginScan( ManagedRepository repository, Date whenGathered )
092            throws ConsumerException
093        {
094            this.managedRepository = new ManagedDefaultRepositoryContent();
095            this.managedRepository.setRepository( repository );
096        }
097    
098        public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
099            throws ConsumerException
100        {
101            beginScan( repository, whenGathered );
102        }
103    
104        public void completeScan()
105        {
106            // no op
107        }
108    
109        public void completeScan( boolean executeOnEntireRepo )
110        {
111            completeScan();
112        }
113    
114        public List<String> getExcludes()
115        {
116            return excludes;
117        }
118    
119        public List<String> getIncludes()
120        {
121            return includes;
122        }
123    
124        public void processFile( String path )
125            throws ConsumerException
126        {
127            try
128            {
129                ArtifactReference reference = managedRepository.toArtifactReference( path );
130                Artifact artifact = artifactFactory.createArtifact( reference.getGroupId(), reference.getArtifactId(),
131                                                                    reference.getVersion(), reference.getClassifier(),
132                                                                    reference.getType() );
133                artifactConverter.convert( artifact, destinationRepository );
134            }
135            catch ( LayoutException e )
136            {
137                log.warn( "Unable to convert artifact: " + path + " : " + e.getMessage(), e );
138            }
139            catch ( ArtifactConversionException e )
140            {
141                log.warn( "Unable to convert artifact: " + path + " : " + e.getMessage(), e );
142            }
143        }
144    
145        public void processFile( String path, boolean executeOnEntireRepo )
146            throws Exception
147        {
148            processFile( path );
149        }
150    
151        public String getDescription()
152        {
153            return "Legacy Artifact to Default Artifact Converter";
154        }
155    
156        public String getId()
157        {
158            return "artifact-legacy-to-default-converter";
159        }
160    
161        public boolean isPermanent()
162        {
163            return false;
164        }
165    
166        public void setExcludes( List<String> excludes )
167        {
168            this.excludes = excludes;
169        }
170    
171        public void setIncludes( List<String> includes )
172        {
173            this.includes = includes;
174        }
175    
176        public ArtifactRepository getDestinationRepository()
177        {
178            return destinationRepository;
179        }
180    
181        public void setDestinationRepository( ArtifactRepository destinationRepository )
182        {
183            this.destinationRepository = destinationRepository;
184        }
185    }