1 package org.apache.archiva.indexer.maven;
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.archiva.common.utils.PathUtil;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.indexer.ArchivaIndexManager;
25 import org.apache.archiva.indexer.ArchivaIndexingContext;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.RemoteRepository;
29 import org.apache.archiva.repository.Repository;
30 import org.apache.archiva.repository.RepositoryType;
31 import org.apache.archiva.repository.features.RemoteIndexFeature;
32 import org.apache.maven.index.Indexer;
33 import org.apache.maven.index.context.IndexCreator;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
41 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.util.List;
48 * Maven implementation of index manager
50 @Service("archivaIndexManager#maven")
51 public class MavenIndexManager implements ArchivaIndexManager {
53 private static final Logger log = LoggerFactory.getLogger(MavenIndexManager.class);
56 private Indexer indexer;
59 private List<? extends IndexCreator> indexCreators;
62 private ArchivaConfiguration archivaConfiguration;
65 public void pack(ArchivaIndexingContext context) {
70 public void scan(ArchivaIndexingContext context, boolean update) {
75 public void update(ArchivaIndexingContext context, URI remoteUpdateUri, boolean fullUpdate) {
80 public void addArtifactToIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) {
85 public void removeArtifactFromIndex(ArchivaIndexingContext context, ArtifactReference artifactReference) {
90 public boolean supportsRepository(RepositoryType type) {
95 public ArchivaIndexingContext createContext(Repository remoteRepository) throws IOException {
96 IndexingContext mvnCtx = null;
97 if (remoteRepository instanceof RemoteRepository) {
98 mvnCtx = createRemoteContext((RemoteRepository) remoteRepository);
99 } else if (remoteRepository instanceof ManagedRepository) {
100 // TODO: Implement managed repository index creation
103 MavenIndexContext context = new MavenIndexContext(remoteRepository, mvnCtx);
107 private IndexingContext createRemoteContext(RemoteRepository remoteRepository) throws IOException {
108 Path appServerBase = archivaConfiguration.getAppServerBaseDir();
110 String contextKey = "remote-" + remoteRepository.getId();
112 // create remote repository path
113 Path repoDir = appServerBase.resolve( "data").resolve( "remotes" ).resolve( remoteRepository.getId() );
114 if ( !Files.exists(repoDir) )
116 Files.createDirectories(repoDir);
119 Path indexDirectory = null;
121 // is there configured indexDirectory ?
122 if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
123 RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
124 indexDirectory = PathUtil.getPathFromUri(rif.getIndexUri());
125 if (!indexDirectory.isAbsolute()) {
126 indexDirectory = repoDir.resolve(indexDirectory);
129 // if not configured use a default value
130 if (indexDirectory == null) {
131 indexDirectory = repoDir.resolve(".index");
133 if (!Files.exists(indexDirectory)) {
134 Files.createDirectories(indexDirectory);
139 return indexer.createIndexingContext(contextKey, remoteRepository.getId(), repoDir.toFile(), indexDirectory.toFile(),
140 remoteRepository.getLocation() == null ? null : remoteRepository.getLocation().toString(),
141 calculateIndexRemoteUrl(remoteRepository.getLocation(), rif),
144 } catch (IndexFormatTooOldException e) {
145 // existing index with an old lucene format so we need to delete it!!!
146 // delete it first then recreate it.
147 log.warn("the index of repository {} is too old we have to delete and recreate it", //
148 remoteRepository.getId());
149 org.apache.archiva.common.utils.FileUtils.deleteDirectory(indexDirectory);
150 return indexer.createIndexingContext(contextKey, remoteRepository.getId(), repoDir.toFile(), indexDirectory.toFile(),
151 remoteRepository.getLocation() == null ? null : remoteRepository.getLocation().toString(),
152 calculateIndexRemoteUrl(remoteRepository.getLocation(), rif),
158 throw new IOException("No remote index defined");
162 private String calculateIndexRemoteUrl(URI baseUri, RemoteIndexFeature rif) {
163 if (rif.getIndexUri()==null) {
164 return baseUri.resolve(".index").toString();
166 return baseUri.resolve(rif.getIndexUri()).toString();