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 org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
29 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
31 import org.apache.maven.archiva.indexer.RepositoryIndexException;
32 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
33 import org.apache.maven.archiva.model.ArchivaArtifact;
34 import org.apache.maven.archiva.model.ArchivaRepository;
35 import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
36 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
39 import org.codehaus.plexus.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.List;
48 * IndexArtifactConsumer
50 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
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 static final String INDEX_ERROR = "indexing_error";
64 * @plexus.configuration default-value="index-artifact"
69 * @plexus.configuration default-value="Index the artifact checksums for Find functionality."
71 private String description;
76 private ArchivaConfiguration configuration;
79 * @plexus.requirement role="org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout"
81 private Map bidirectionalLayoutMap;
84 * @plexus.requirement role-hint="lucene"
86 private RepositoryContentIndexFactory indexFactory;
88 private Map repositoryMap = new HashMap();
90 public void beginScan()
92 /* nothing to do here */
95 public void completeScan()
97 /* nothing to do here */
100 public List getIncludedTypes()
102 return null; // TODO: define these as a list of artifacts.
105 public void processArchivaArtifact( ArchivaArtifact artifact )
106 throws ConsumerException
108 HashcodesRecord record = new HashcodesRecord();
109 record.setRepositoryId( artifact.getModel().getRepositoryId() );
110 record.setArtifact( artifact );
112 IndexedRepositoryDetails pnl = getIndexedRepositoryDetails( artifact );
113 String artifactPath = pnl.layout.toPath( artifact );
114 record.setFilename( artifactPath );
118 pnl.index.modifyRecord( record );
120 catch ( RepositoryIndexException e )
122 triggerConsumerError( INDEX_ERROR, "Unable to index hashcodes: " + e.getMessage() );
126 private IndexedRepositoryDetails getIndexedRepositoryDetails( ArchivaArtifact artifact )
128 String repoId = artifact.getModel().getRepositoryId();
129 if ( StringUtils.isBlank( repoId ) )
131 throw new IllegalStateException( "Unable to process artifact [" + artifact
132 + "] as it has no repository id associated with it." );
135 return getIndexedRepositoryDetails( repoId );
138 private IndexedRepositoryDetails getIndexedRepositoryDetails( String id )
140 return (IndexedRepositoryDetails) this.repositoryMap.get( id );
143 public String getDescription()
148 public String getId()
153 public boolean isPermanent()
158 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
160 if ( ConfigurationNames.isRepositories( propertyName ) )
166 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
171 public void initialize()
172 throws InitializationException
175 configuration.addChangeListener( this );
178 private void initRepositoryMap()
180 synchronized ( this.repositoryMap )
182 this.repositoryMap.clear();
184 Iterator it = configuration.getConfiguration().getRepositories().iterator();
185 while ( it.hasNext() )
187 RepositoryConfiguration repoconfig = (RepositoryConfiguration) it.next();
188 if ( !repoconfig.isManaged() )
193 ArchivaRepository repository = ArchivaConfigurationAdaptor.toArchivaRepository( repoconfig );
194 IndexedRepositoryDetails pnl = new IndexedRepositoryDetails();
196 pnl.path = repository.getUrl().getPath();
197 pnl.layout = (BidirectionalRepositoryLayout) this.bidirectionalLayoutMap.get( repoconfig.getLayout() );
199 pnl.index = indexFactory.createHashcodeIndex( repository );
201 this.repositoryMap.put( repoconfig.getId(), pnl );
206 class IndexedRepositoryDetails
210 public BidirectionalRepositoryLayout layout;
212 public RepositoryContentIndex index;