1 package org.apache.maven.archiva.consumers.lucene;
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 java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.ConfigurationNames;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
32 import org.apache.maven.archiva.consumers.ConsumerException;
33 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
34 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
35 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
36 import org.apache.maven.archiva.indexer.RepositoryIndexException;
37 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
38 import org.apache.maven.archiva.model.ArchivaArtifact;
39 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
40 import org.apache.maven.archiva.repository.RepositoryContentFactory;
41 import org.apache.maven.archiva.repository.RepositoryException;
42 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
43 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
44 import org.codehaus.plexus.registry.Registry;
45 import org.codehaus.plexus.registry.RegistryListener;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * IndexArtifactConsumer
53 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer"
54 * role-hint="index-artifact"
55 * instantiation-strategy="per-lookup"
57 public class IndexArtifactConsumer
58 extends AbstractMonitoredConsumer
59 implements DatabaseUnprocessedArtifactConsumer, RegistryListener, Initializable
61 private Logger log = LoggerFactory.getLogger( IndexArtifactConsumer.class );
63 private static final String INDEX_ERROR = "indexing_error";
66 * @plexus.configuration default-value="index-artifact"
71 * @plexus.configuration default-value="Index the artifact checksums for Find functionality."
73 private String description;
78 private ArchivaConfiguration configuration;
83 private RepositoryContentFactory repositoryFactory;
86 * @plexus.requirement role-hint="lucene"
88 private RepositoryContentIndexFactory indexFactory;
90 private Map<String, IndexedRepositoryDetails> repositoryMap = new HashMap<String, IndexedRepositoryDetails>();
92 public void beginScan()
94 /* nothing to do here */
97 public void completeScan()
99 /* nothing to do here */
102 public List<String> getIncludedTypes()
104 return null; // TODO: define these as a list of artifacts.
107 public void processArchivaArtifact( ArchivaArtifact artifact )
108 throws ConsumerException
110 HashcodesRecord record = new HashcodesRecord();
111 record.setRepositoryId( artifact.getModel().getRepositoryId() );
112 record.setArtifact( artifact );
114 IndexedRepositoryDetails pnl = getIndexedRepositoryDetails( artifact );
116 String artifactPath = pnl.repository.toPath( artifact );
117 record.setFilename( artifactPath );
121 pnl.index.modifyRecord( record );
123 catch ( RepositoryIndexException e )
125 triggerConsumerError( INDEX_ERROR, "Unable to index hashcodes: " + e.getMessage() );
129 private IndexedRepositoryDetails getIndexedRepositoryDetails( ArchivaArtifact artifact )
131 String repoId = artifact.getModel().getRepositoryId();
132 if ( StringUtils.isBlank( repoId ) )
134 throw new IllegalStateException(
135 "Unable to process artifact [" + artifact + "] as it has no repository id associated with it." );
138 return getIndexedRepositoryDetails( repoId );
141 private IndexedRepositoryDetails getIndexedRepositoryDetails( String id )
143 return this.repositoryMap.get( id );
146 public String getDescription()
151 public String getId()
156 public boolean isPermanent()
161 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
163 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
169 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
174 public void initialize()
175 throws InitializationException
178 configuration.addChangeListener( this );
181 private void initRepositoryMap()
183 synchronized ( this.repositoryMap )
185 this.repositoryMap.clear();
187 Iterator<ManagedRepositoryConfiguration> it = configuration.getConfiguration().getManagedRepositories().iterator();
188 while ( it.hasNext() )
190 ManagedRepositoryConfiguration repository = it.next();
194 IndexedRepositoryDetails pnl = new IndexedRepositoryDetails();
196 pnl.repository = repositoryFactory.getManagedRepositoryContent( repository.getId() );
198 pnl.index = indexFactory.createHashcodeIndex( repository );
200 this.repositoryMap.put( repository.getId(), pnl );
202 catch ( RepositoryException e )
204 log.error( "Unable to load repository content object: " + e.getMessage(), e );
210 class IndexedRepositoryDetails
212 public ManagedRepositoryContent repository;
214 public RepositoryContentIndex index;