1 package org.apache.maven.repository.manager.web.execution;
4 * Copyright 2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.DefaultArtifactRepositoryFactory;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
26 import org.apache.maven.model.Model;
27 import org.apache.maven.repository.configuration.Configuration;
28 import org.apache.maven.repository.discovery.ArtifactDiscoverer;
29 import org.apache.maven.repository.discovery.MetadataDiscoverer;
30 import org.apache.maven.repository.indexing.ArtifactRepositoryIndex;
31 import org.apache.maven.repository.indexing.MetadataRepositoryIndex;
32 import org.apache.maven.repository.indexing.PomRepositoryIndex;
33 import org.apache.maven.repository.indexing.RepositoryIndexException;
34 import org.apache.maven.repository.indexing.RepositoryIndexingFactory;
35 import org.codehaus.plexus.logging.AbstractLogEnabled;
38 import java.net.MalformedURLException;
39 import java.util.Iterator;
40 import java.util.List;
44 * This is the class that executes the discoverer and indexer.
46 * @plexus.component role="org.apache.maven.repository.manager.web.execution.DiscovererExecution"
47 * @todo note that a legacy repository will fail due to lack of metadata discoverer
49 public class DiscovererExecution
50 extends AbstractLogEnabled
54 * @plexus.requirement role="org.apache.maven.repository.discovery.ArtifactDiscoverer"
56 private Map artifactDiscoverers;
59 * @plexus.requirement role="org.apache.maven.repository.discovery.MetadataDiscoverer"
61 private Map metadataDiscoverers;
66 private RepositoryIndexingFactory indexFactory;
71 private ArtifactRepositoryFactory repoFactory;
74 * @plexus.requirement role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
76 private Map repositoryLayouts;
79 * Executes discoverer and indexer if an index does not exist yet
82 * @throws MalformedURLException
83 * @throws RepositoryIndexException
85 public void executeDiscovererIfIndexDoesNotExist( File indexDir )
86 throws MalformedURLException, RepositoryIndexException
88 boolean isExisting = false;
90 if ( IndexReader.indexExists( indexDir ) )
102 * Method that executes the discoverer and indexer
104 public void executeDiscoverer()
105 throws MalformedURLException, RepositoryIndexException
107 Configuration configuration = new Configuration(); // TODO!
108 File indexPath = new File( configuration.getIndexPath() );
109 String blacklistedPatterns = configuration.getDiscoveryBlackListPatterns();
110 boolean includeSnapshots = configuration.isDiscoverSnapshots();
112 ArtifactRepository defaultRepository = getDefaultRepository( configuration );
114 getLogger().info( "[DiscovererExecution] Started discovery and indexing.." );
115 String layoutProperty = configuration.getRepositoryLayout();
116 ArtifactDiscoverer discoverer = (ArtifactDiscoverer) artifactDiscoverers.get( layoutProperty );
117 List artifacts = discoverer.discoverArtifacts( defaultRepository, blacklistedPatterns, includeSnapshots );
118 indexArtifact( artifacts, indexPath, defaultRepository );
120 List models = discoverer.discoverStandalonePoms( defaultRepository, blacklistedPatterns, includeSnapshots );
121 indexPom( models, indexPath, defaultRepository );
123 MetadataDiscoverer metadataDiscoverer = (MetadataDiscoverer) metadataDiscoverers.get( layoutProperty );
125 metadataDiscoverer.discoverMetadata( new File( defaultRepository.getBasedir() ), blacklistedPatterns );
126 indexMetadata( metadataList, indexPath, defaultRepository );
127 getLogger().info( "[DiscovererExecution] Finished discovery and indexing." );
131 * Index the artifacts in the list
133 * @param artifacts the artifacts to be indexed
134 * @param indexPath the path to the index file
135 * @param repository the repository where the artifacts are located
137 protected void indexArtifact( List artifacts, File indexPath, ArtifactRepository repository )
138 throws RepositoryIndexException
140 ArtifactRepositoryIndex artifactIndex = indexFactory.createArtifactRepositoryIndex( indexPath, repository );
141 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
143 Artifact artifact = (Artifact) iter.next();
144 artifactIndex.indexArtifact( artifact );
146 if ( artifactIndex.isOpen() )
148 artifactIndex.optimize();
149 artifactIndex.close();
155 * Index the metadata in the list
157 * @param metadataList the metadata to be indexed
158 * @param indexPath the path to the index file
160 protected void indexMetadata( List metadataList, File indexPath, ArtifactRepository repository )
161 throws RepositoryIndexException, MalformedURLException
163 MetadataRepositoryIndex metadataIndex = indexFactory.createMetadataRepositoryIndex( indexPath, repository );
164 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
166 RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
167 metadataIndex.index( repoMetadata );
169 if ( metadataIndex.isOpen() )
171 metadataIndex.optimize();
172 metadataIndex.close();
178 * Index the poms in the list
180 * @param models list of poms that will be indexed
181 * @param indexPath the path to the index
182 * @param repository the artifact repository where the poms were discovered
184 protected void indexPom( List models, File indexPath, ArtifactRepository repository )
185 throws RepositoryIndexException
187 PomRepositoryIndex pomIndex = indexFactory.createPomRepositoryIndex( indexPath, repository );
188 for ( Iterator iter = models.iterator(); iter.hasNext(); )
190 Model model = (Model) iter.next();
191 pomIndex.indexPom( model );
193 if ( pomIndex.isOpen() )
202 * Method that creates the artifact repository
204 * @return an ArtifactRepository instance
205 * @throws java.net.MalformedURLException
207 protected ArtifactRepository getDefaultRepository( Configuration configuration )
208 throws MalformedURLException
210 // TODO! share with general search action, should only instantiate once
211 File repositoryDirectory = new File( configuration.getRepositoryDirectory() );
212 String repoDir = repositoryDirectory.toURL().toString();
213 ArtifactRepositoryFactory repoFactory = new DefaultArtifactRepositoryFactory();
215 ArtifactRepositoryLayout layout =
216 (ArtifactRepositoryLayout) repositoryLayouts.get( configuration.getRepositoryLayout() );
217 return repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );