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.discovery.ArtifactDiscoverer;
28 import org.apache.maven.repository.discovery.MetadataDiscoverer;
29 import org.apache.maven.repository.indexing.ArtifactRepositoryIndex;
30 import org.apache.maven.repository.indexing.MetadataRepositoryIndex;
31 import org.apache.maven.repository.indexing.PomRepositoryIndex;
32 import org.apache.maven.repository.indexing.RepositoryIndexException;
33 import org.apache.maven.repository.indexing.RepositoryIndexingFactory;
34 import org.apache.maven.repository.manager.web.job.Configuration;
35 import org.codehaus.plexus.logging.AbstractLogEnabled;
38 import java.net.MalformedURLException;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Properties;
44 * This is the class that executes the discoverer and indexer.
46 * @plexus.component role="org.apache.maven.repository.manager.web.execution.DiscovererExecution"
48 public class DiscovererExecution
49 extends AbstractLogEnabled
54 private Configuration config;
57 * @plexus.requirement role-hint="default"
59 private ArtifactDiscoverer defaultArtifactDiscoverer;
62 * @plexus.requirement role-hint="legacy"
64 private ArtifactDiscoverer legacyArtifactDiscoverer;
67 * @plexus.requirement role-hint="default"
69 private MetadataDiscoverer defaultMetadataDiscoverer;
74 private RepositoryIndexingFactory indexFactory;
79 private ArtifactRepositoryFactory repoFactory;
81 private ArtifactRepositoryLayout layout;
83 private String indexPath;
85 private String blacklistedPatterns;
87 private boolean includeSnapshots;
89 private boolean convertSnapshots;
91 private ArtifactRepository defaultRepository;
94 * Executes discoverer and indexer if an index does not exist yet
96 * @throws MalformedURLException
97 * @throws RepositoryIndexException
99 public void executeDiscovererIfIndexDoesNotExist()
100 throws MalformedURLException, RepositoryIndexException
102 Properties props = config.getProperties();
103 indexPath = props.getProperty( "index.path" );
105 File indexDir = new File( indexPath );
106 boolean isExisting = false;
108 if ( IndexReader.indexExists( indexDir ) )
120 * Method that executes the discoverer and indexer
122 public void executeDiscoverer()
123 throws MalformedURLException, RepositoryIndexException
125 Properties props = config.getProperties();
126 indexPath = props.getProperty( "index.path" );
127 layout = config.getLayout();
128 blacklistedPatterns = props.getProperty( "blacklist.patterns" );
129 includeSnapshots = Boolean.valueOf( props.getProperty( "include.snapshots" ) ).booleanValue();
130 convertSnapshots = Boolean.valueOf( props.getProperty( "convert.snapshots" ) ).booleanValue();
134 defaultRepository = getDefaultRepository();
136 catch ( MalformedURLException me )
138 getLogger().error( me.getMessage() );
141 getLogger().info( "[DiscovererExecution] Started discovery and indexing.." );
142 if ( "default".equals( props.getProperty( "layout" ) ) )
144 executeDiscovererInDefaultRepo();
146 else if ( "legacy".equals( props.getProperty( "layout" ) ) )
148 executeDiscovererInLegacyRepo();
150 getLogger().info( "[DiscovererExecution] Finished discovery and indexing." );
154 * Method that discovers and indexes artifacts, poms and metadata in a default
155 * m2 repository structure.
157 * @throws MalformedURLException
158 * @throws RepositoryIndexException
159 * @todo why is this any different from legacy? [!]
161 protected void executeDiscovererInDefaultRepo()
162 throws MalformedURLException, RepositoryIndexException
165 defaultArtifactDiscoverer.discoverArtifacts( defaultRepository, blacklistedPatterns, includeSnapshots );
166 indexArtifact( artifacts, indexPath, defaultRepository );
168 List models = defaultArtifactDiscoverer.discoverStandalonePoms( defaultRepository, blacklistedPatterns,
170 indexPom( models, indexPath, defaultRepository );
172 List metadataList = defaultMetadataDiscoverer.discoverMetadata( new File( defaultRepository
173 .getBasedir() ), blacklistedPatterns );
174 indexMetadata( metadataList, indexPath, new File( defaultRepository.getBasedir() ) );
178 * Method that discovers and indexes artifacts in a legacy type repository
180 * @throws RepositoryIndexException
182 protected void executeDiscovererInLegacyRepo()
183 throws RepositoryIndexException
186 legacyArtifactDiscoverer.discoverArtifacts( defaultRepository, blacklistedPatterns, includeSnapshots );
187 indexArtifact( artifacts, indexPath, defaultRepository );
191 * Index the artifacts in the list
193 * @param artifacts the artifacts to be indexed
194 * @param indexPath the path to the index file
195 * @param repository the repository where the artifacts are located
197 protected void indexArtifact( List artifacts, String indexPath, ArtifactRepository repository )
198 throws RepositoryIndexException
200 ArtifactRepositoryIndex artifactIndex = indexFactory.createArtifactRepositoryIndex( indexPath, repository );
201 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
203 Artifact artifact = (Artifact) iter.next();
204 artifactIndex.indexArtifact( artifact );
206 if ( artifactIndex.isOpen() )
208 artifactIndex.optimize();
209 artifactIndex.close();
215 * Index the metadata in the list
217 * @param metadataList the metadata to be indexed
218 * @param indexPath the path to the index file
219 * @param repositoryBase the repository where the metadata are located
221 protected void indexMetadata( List metadataList, String indexPath, File repositoryBase )
222 throws RepositoryIndexException, MalformedURLException
224 String repoDir = repositoryBase.toURL().toString();
225 ArtifactRepository repository = repoFactory
226 .createArtifactRepository( "repository", repoDir, layout, null, null );
228 MetadataRepositoryIndex metadataIndex = indexFactory.createMetadataRepositoryIndex( indexPath, repository );
229 for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )
231 RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();
232 metadataIndex.index( repoMetadata );
234 if ( metadataIndex.isOpen() )
236 metadataIndex.optimize();
237 metadataIndex.close();
243 * Index the poms in the list
245 * @param models list of poms that will be indexed
246 * @param indexPath the path to the index
247 * @param repository the artifact repository where the poms were discovered
249 protected void indexPom( List models, String indexPath, ArtifactRepository repository )
250 throws RepositoryIndexException
252 PomRepositoryIndex pomIndex = indexFactory.createPomRepositoryIndex( indexPath, repository );
253 for ( Iterator iter = models.iterator(); iter.hasNext(); )
255 Model model = (Model) iter.next();
256 pomIndex.indexPom( model );
258 if ( pomIndex.isOpen() )
267 * Method that creates the artifact repository
269 * @return an ArtifactRepository instance
270 * @throws java.net.MalformedURLException
272 protected ArtifactRepository getDefaultRepository()
273 throws MalformedURLException
275 File repositoryDirectory = new File( config.getRepositoryDirectory() );
276 String repoDir = repositoryDirectory.toURL().toString();
277 ArtifactRepositoryFactory repoFactory = new DefaultArtifactRepositoryFactory();
279 return repoFactory.createArtifactRepository( "test", repoDir, config.getLayout(), null, null );
283 * Method that sets the configuration object
287 public void setConfiguration( Configuration config )
289 this.config = config;
293 * Returns the cofiguration
295 * @return a Configuration object that contains the configuration values
297 public Configuration getConfiguration()