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.FileTypes;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.maven.archiva.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
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.filecontent.FileContentRecord;
33 import org.apache.maven.archiva.model.ArchivaArtifact;
34 import org.apache.maven.archiva.model.ArtifactReference;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.archiva.repository.RepositoryContentFactory;
37 import org.apache.maven.archiva.repository.RepositoryException;
38 import org.apache.maven.archiva.repository.layout.LayoutException;
39 import org.apache.maven.archiva.repository.metadata.MetadataTools;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.codehaus.plexus.registry.Registry;
43 import org.codehaus.plexus.registry.RegistryListener;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Date;
51 import java.util.List;
54 * IndexContentConsumer - generic full file content indexing consumer.
56 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
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> propertyNameTriggers = new ArrayList<String>();
104 private List<String> includes = new ArrayList<String>();
106 private RepositoryContentIndex index;
108 private ManagedRepositoryContent repository;
110 private File repositoryDir;
112 public String getId()
117 public String getDescription()
119 return this.description;
122 public boolean isPermanent()
127 public List<String> getExcludes()
132 public List<String> getIncludes()
134 return this.includes;
137 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
138 throws ConsumerException
142 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
143 this.repositoryDir = new File( repository.getRepoRoot() );
144 this.index = indexFactory.createFileContentIndex( repository.getRepository() );
146 catch ( RepositoryException e )
148 throw new ConsumerException( "Unable to start IndexContentConsumer: " + e.getMessage(), e );
152 public void processFile( String path )
153 throws ConsumerException
155 if ( path.endsWith( "/" + MetadataTools.MAVEN_METADATA ) )
157 log.debug( "File is a metadata file. Not indexing." );
161 FileContentRecord record = new FileContentRecord();
164 File file = new File( repositoryDir, path );
165 record.setRepositoryId( this.repository.getId() );
166 record.setFilename( path );
167 record.setContents( FileUtils.readFileToString( file, null ) );
169 // Test for possible artifact reference syntax.
172 ArtifactReference ref = repository.toArtifactReference( path );
173 ArchivaArtifact artifact = new ArchivaArtifact( ref );
174 record.setArtifact( artifact );
176 catch ( LayoutException e )
181 index.modifyRecord( record );
183 catch ( IOException e )
185 triggerConsumerError( READ_CONTENT, "Unable to read file contents: " + e.getMessage() );
187 catch ( RepositoryIndexException e )
189 triggerConsumerError( INDEX_ERROR, "Unable to index file contents: " + e.getMessage() );
193 public void completeScan()
198 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
200 if ( propertyNameTriggers.contains( propertyName ) )
206 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
211 private void initIncludes()
215 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
218 public void initialize()
219 throws InitializationException
221 propertyNameTriggers = new ArrayList<String>();
222 propertyNameTriggers.add( "repositoryScanning" );
223 propertyNameTriggers.add( "fileTypes" );
224 propertyNameTriggers.add( "fileType" );
225 propertyNameTriggers.add( "patterns" );
226 propertyNameTriggers.add( "pattern" );
228 configuration.addChangeListener( this );