1 package org.apache.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.maven.archiva.configuration.ManagedRepositoryConfiguration;
23 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
24 import org.apache.maven.archiva.consumers.ConsumerException;
25 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
26 import org.apache.maven.archiva.model.ArchivaArtifact;
27 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
28 import org.apache.maven.archiva.repository.RepositoryContentFactory;
29 import org.apache.maven.archiva.repository.RepositoryException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.sonatype.nexus.index.ArtifactContext;
33 import org.sonatype.nexus.index.ArtifactContextProducer;
34 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
35 import org.sonatype.nexus.index.NexusIndexer;
36 import org.sonatype.nexus.index.context.DefaultIndexingContext;
37 import org.sonatype.nexus.index.context.IndexingContext;
38 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
39 import org.sonatype.nexus.index.IndexerEngine;
42 import java.io.IOException;
43 import java.util.List;
46 * LuceneCleanupRemoveIndexedConsumer
50 public class LuceneCleanupRemoveIndexedConsumer
51 extends AbstractMonitoredConsumer
52 implements DatabaseCleanupConsumer
54 private static final Logger log = LoggerFactory.getLogger( LuceneCleanupRemoveIndexedConsumer.class );
56 private RepositoryContentFactory repoFactory;
58 private ArtifactContextProducer artifactContextProducer;
60 private IndexingContext context;
62 private IndexerEngine indexerEngine;
64 //TODO - deng - use indexerEngine to remove documents instead of directly using the IndexingContext!
66 public LuceneCleanupRemoveIndexedConsumer( RepositoryContentFactory repoFactory, IndexerEngine indexerEngine )
68 this.repoFactory = repoFactory;
69 this.indexerEngine = indexerEngine;
70 this.artifactContextProducer = new DefaultArtifactContextProducer();
73 public void beginScan()
77 public void completeScan()
79 /*synchronized( indexerEngine )
83 //context.getIndexWriter().close();
85 //indexerEngine.endIndexing( context );
86 //indexer.removeIndexingContext( context, false );
88 catch ( IOException e )
90 log.error( e.getMessage() );
95 public List<String> getIncludedTypes()
97 // TODO Auto-generated method stub
101 public void processArchivaArtifact( ArchivaArtifact artifact )
102 throws ConsumerException
104 //synchronized( context )
106 // TODO - deng - block this if there is the nexus indexer consumer is executing?
107 ManagedRepositoryContent repoContent = null;
112 repoFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() );
114 catch ( RepositoryException e )
116 throw new ConsumerException( "Can't run index cleanup consumer: " + e.getMessage() );
119 ManagedRepositoryConfiguration repository = repoContent.getRepository();
120 String indexDir = repository.getIndexDir();
121 File managedRepository = new File( repository.getLocation() );
122 File indexDirectory = null;
124 if ( indexDir != null && !"".equals( indexDir ) )
126 indexDirectory = new File( repository.getIndexDir() );
130 indexDirectory = new File( managedRepository, ".indexer" );
136 new DefaultIndexingContext( repository.getId(), repository.getId(), managedRepository,
137 indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
139 // indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
140 // indexDirectory, null, null, NexusIndexer.FULL_INDEX );
141 context.setSearchable( repository.isScanned() );
143 catch ( UnsupportedExistingLuceneIndexException e )
145 log.warn( "Unsupported index format.", e );
148 catch ( IOException e )
150 log.warn( "Unable to open index at " + indexDirectory.getAbsoluteFile(), e );
156 File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) );
158 if ( !artifactFile.exists() )
160 ArtifactContext artifactContext =
161 artifactContextProducer.getArtifactContext( context, artifactFile );
163 if ( artifactContext != null )
165 //indexerEngine.remove( context, artifactContext );
167 indexerEngine.remove( context, artifactContext );
169 context.close( false );
170 // hack for deleting documents - indexer engine's remove(...) isn't working for me
171 //removeDocuments( artifactContext );
175 catch ( IOException e )
177 log.error( "Unable to open index at " + indexDirectory.getAbsoluteFile(), e );
182 /* private void removeDocuments( ArtifactContext ac )
185 synchronized( indexerEngine )
187 IndexWriter w = context.getIndexWriter();
189 ArtifactInfo ai = ac.getArtifactInfo();
190 String uinfo = AbstractIndexCreator.getGAV( ai.groupId, ai.artifactId, ai.version, ai.classifier, ai.packaging );
192 Document doc = new Document();
193 doc.add( new Field( ArtifactInfo.DELETED, uinfo, Field.Store.YES, Field.Index.NO ) );
194 doc.add( new Field( ArtifactInfo.LAST_MODIFIED, Long.toString( System.currentTimeMillis() ), Field.Store.YES,
197 w.addDocument( doc );
199 w.deleteDocuments( new Term( ArtifactInfo.UINFO, uinfo ) );
203 context.updateTimestamp();
207 public String getDescription()
209 return "Remove indexed content if not present on filesystem.";
212 public String getId()
214 return "not-present-remove-indexed";
217 public boolean isPermanent()
222 public void setRepositoryContentFactory( RepositoryContentFactory repoFactory )
224 this.repoFactory = repoFactory;
227 public void setArtifactContextProducer( ArtifactContextProducer artifactContextProducer )
229 this.artifactContextProducer = artifactContextProducer;