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.io.FileUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.ConfigurationNames;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
28 import org.apache.maven.archiva.consumers.ConsumerException;
29 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
30 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
31 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
32 import org.apache.maven.archiva.indexer.RepositoryIndexException;
33 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.ArtifactReference;
36 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
37 import org.apache.maven.archiva.repository.RepositoryContentFactory;
38 import org.apache.maven.archiva.repository.RepositoryException;
39 import org.apache.maven.archiva.repository.layout.LayoutException;
40 import org.apache.maven.archiva.repository.metadata.MetadataTools;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
42 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
43 import org.codehaus.plexus.registry.Registry;
44 import org.codehaus.plexus.registry.RegistryListener;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 import java.io.IOException;
50 import java.util.ArrayList;
51 import java.util.Date;
52 import java.util.List;
55 * IndexContentConsumer - generic full file content indexing consumer.
58 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
59 * role-hint="index-content"
60 * instantiation-strategy="per-lookup"
62 public class IndexContentConsumer
63 extends AbstractMonitoredConsumer
64 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
66 private Logger log = LoggerFactory.getLogger( IndexContentConsumer.class );
68 private static final String READ_CONTENT = "read_content";
70 private static final String INDEX_ERROR = "indexing_error";
73 * @plexus.configuration default-value="index-content"
78 * @plexus.configuration default-value="Text and XML file contents indexing"
80 private String description;
85 private ArchivaConfiguration configuration;
90 private FileTypes filetypes;
95 private RepositoryContentFactory repositoryFactory;
98 * @plexus.requirement role-hint="lucene"
100 private RepositoryContentIndexFactory indexFactory;
102 private List<String> includes = new ArrayList<String>();
104 private RepositoryContentIndex index;
106 private ManagedRepositoryContent repository;
108 private File repositoryDir;
110 public String getId()
115 public String getDescription()
117 return this.description;
120 public boolean isPermanent()
125 public List<String> getExcludes()
130 public List<String> getIncludes()
132 return this.includes;
135 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
136 throws ConsumerException
140 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
141 this.repositoryDir = new File( repository.getRepoRoot() );
142 this.index = indexFactory.createFileContentIndex( repository.getRepository() );
144 catch ( RepositoryException e )
146 throw new ConsumerException( "Unable to start IndexContentConsumer: " + e.getMessage(), e );
150 public void processFile( String path )
151 throws ConsumerException
153 if ( path.endsWith( "/" + MetadataTools.MAVEN_METADATA ) )
155 log.debug( "File is a metadata file. Not indexing." );
159 FileContentRecord record = new FileContentRecord();
162 File file = new File( repositoryDir, path );
163 record.setRepositoryId( this.repository.getId() );
164 record.setFilename( path );
165 record.setContents( FileUtils.readFileToString( file, null ) );
167 // Test for possible artifact reference syntax.
170 ArtifactReference ref = repository.toArtifactReference( path );
171 ArchivaArtifact artifact = new ArchivaArtifact( ref );
172 artifact.getModel().setRepositoryId( repository.getId() );
173 record.setArtifact( artifact );
175 catch ( LayoutException e )
180 index.modifyRecord( record );
182 catch ( IOException e )
184 triggerConsumerError( READ_CONTENT, "Unable to read file contents: " + e.getMessage() );
186 catch ( RepositoryIndexException e )
188 triggerConsumerError( INDEX_ERROR, "Unable to index file contents: " + e.getMessage() );
192 public void completeScan()
197 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
199 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
205 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
210 private void initIncludes()
214 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
217 public void initialize()
218 throws InitializationException
220 configuration.addChangeListener( this );