1 | |
package org.apache.archiva.consumers.lucene; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.List; |
25 | |
|
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.database.updater.DatabaseCleanupConsumer; |
30 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
31 | |
import org.apache.maven.archiva.repository.ManagedRepositoryContent; |
32 | |
import org.apache.maven.archiva.repository.RepositoryContentFactory; |
33 | |
import org.apache.maven.archiva.repository.RepositoryException; |
34 | |
import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; |
35 | |
import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; |
36 | |
import org.apache.maven.archiva.scheduled.tasks.TaskCreator; |
37 | |
import org.codehaus.plexus.taskqueue.TaskQueueException; |
38 | |
import org.slf4j.Logger; |
39 | |
import org.slf4j.LoggerFactory; |
40 | |
import org.sonatype.nexus.index.context.IndexingContext; |
41 | |
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class LuceneCleanupRemoveIndexedConsumer |
50 | |
extends AbstractMonitoredConsumer |
51 | |
implements DatabaseCleanupConsumer |
52 | |
{ |
53 | 0 | private static final Logger log = LoggerFactory.getLogger( LuceneCleanupRemoveIndexedConsumer.class ); |
54 | |
|
55 | |
private RepositoryContentFactory repoFactory; |
56 | |
|
57 | |
private ArchivaTaskScheduler scheduler; |
58 | |
|
59 | |
public LuceneCleanupRemoveIndexedConsumer( RepositoryContentFactory repoFactory, ArchivaTaskScheduler scheduler ) |
60 | 0 | { |
61 | 0 | this.repoFactory = repoFactory; |
62 | 0 | this.scheduler = scheduler; |
63 | 0 | } |
64 | |
|
65 | |
public void beginScan() |
66 | |
{ |
67 | 0 | } |
68 | |
|
69 | |
public void completeScan() |
70 | |
{ |
71 | 0 | } |
72 | |
|
73 | |
public List<String> getIncludedTypes() |
74 | |
{ |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | |
public void processArchivaArtifact( ArchivaArtifact artifact ) |
79 | |
throws ConsumerException |
80 | |
{ |
81 | 0 | ManagedRepositoryContent repoContent = null; |
82 | |
|
83 | |
try |
84 | |
{ |
85 | 0 | repoContent = repoFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() ); |
86 | |
} |
87 | 0 | catch ( RepositoryException e ) |
88 | |
{ |
89 | 0 | throw new ConsumerException( "Can't run index cleanup consumer: " + e.getMessage() ); |
90 | 0 | } |
91 | |
|
92 | 0 | ManagedRepositoryConfiguration repository = repoContent.getRepository(); |
93 | |
|
94 | 0 | IndexingContext context = null; |
95 | |
try |
96 | |
{ |
97 | 0 | File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) ); |
98 | |
|
99 | 0 | if ( !artifactFile.exists() ) |
100 | |
{ |
101 | 0 | context = TaskCreator.createContext( repository ); |
102 | |
|
103 | 0 | ArtifactIndexingTask task = |
104 | |
TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.DELETE, |
105 | |
context ); |
106 | |
|
107 | 0 | log.debug( "Queueing indexing task '" + task + "' to remove the artifact from the index." ); |
108 | 0 | scheduler.queueIndexingTask( task ); |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | task = |
113 | |
TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.FINISH, |
114 | |
context ); |
115 | 0 | log.debug( "Queueing indexing task + '" + task + "' to finish indexing." ); |
116 | 0 | scheduler.queueIndexingTask( task ); |
117 | |
} |
118 | |
|
119 | |
} |
120 | 0 | catch ( TaskQueueException e ) |
121 | |
{ |
122 | 0 | throw new ConsumerException( e.getMessage() ); |
123 | |
} |
124 | 0 | catch ( IOException e ) |
125 | |
{ |
126 | 0 | throw new ConsumerException( e.getMessage(), e ); |
127 | |
} |
128 | 0 | catch ( UnsupportedExistingLuceneIndexException e ) |
129 | |
{ |
130 | 0 | throw new ConsumerException( e.getMessage(), e ); |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | |
public String getDescription() |
135 | |
{ |
136 | 0 | return "Remove indexed content if not present on filesystem."; |
137 | |
} |
138 | |
|
139 | |
public String getId() |
140 | |
{ |
141 | 0 | return "not-present-remove-indexed"; |
142 | |
} |
143 | |
|
144 | |
public boolean isPermanent() |
145 | |
{ |
146 | 0 | return false; |
147 | |
} |
148 | |
|
149 | |
public void setRepositoryContentFactory( RepositoryContentFactory repoFactory ) |
150 | |
{ |
151 | 0 | this.repoFactory = repoFactory; |
152 | 0 | } |
153 | |
} |