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.filelock.DefaultFileLockManager;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.repository.Repository;
25 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
26 import org.apache.archiva.repository.storage.StorageAsset;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.NoSuchFileException;
34 import java.nio.file.Path;
36 import java.time.ZonedDateTime;
38 import java.util.concurrent.atomic.AtomicBoolean;
41 * Maven implementation of index context
43 public class MavenIndexContext implements ArchivaIndexingContext {
45 private static final Logger log = LoggerFactory.getLogger(ArchivaIndexingContext.class);
48 private AtomicBoolean openStatus = new AtomicBoolean(false);
49 private IndexingContext delegate;
50 private Repository repository;
51 private StorageAsset dir = null;
53 protected MavenIndexContext(Repository repository, IndexingContext delegate) {
54 this.delegate = delegate;
55 this.repository = repository;
56 this.openStatus.set(true);
61 public String getId() {
62 return delegate.getId();
66 public Repository getRepository() {
71 public StorageAsset getPath() {
73 StorageAsset repositoryDirAsset = repository.getAsset("");
74 Path repositoryDir = repositoryDirAsset.getFilePath().toAbsolutePath();
75 Path indexDir = delegate.getIndexDirectoryFile().toPath();
76 if (indexDir.startsWith(repositoryDir)) {
77 dir = repository.getAsset(repositoryDir.relativize(indexDir).toString());
80 FilesystemStorage storage = new FilesystemStorage(indexDir, new DefaultFileLockManager());
81 dir = storage.getAsset("");
82 } catch (IOException e) {
83 log.error("Error occured while creating storage for index dir");
91 public boolean isEmpty() throws IOException {
92 return Files.list(delegate.getIndexDirectoryFile().toPath()).count()==0;
96 public void commit() throws IOException {
101 public void rollback() throws IOException {
106 public void optimize() throws IOException {
111 public void close(boolean deleteFiles) throws IOException {
112 if (openStatus.compareAndSet(true,false)) {
114 delegate.close(deleteFiles);
115 } catch (NoSuchFileException e) {
116 // Ignore missing directory
122 public void close() throws IOException {
123 if (openStatus.compareAndSet(true,false)) {
125 delegate.close(false);
126 } catch (NoSuchFileException e) {
127 // Ignore missing directory
133 public boolean isOpen() {
134 return openStatus.get();
138 public void purge() throws IOException {
143 public boolean supports(Class<?> clazz) {
144 return IndexingContext.class.equals(clazz);
147 @SuppressWarnings( "unchecked" )
149 public <T> T getBaseContext(Class<T> clazz) throws UnsupportedOperationException {
150 if (IndexingContext.class.equals(clazz)) {
153 throw new UnsupportedOperationException("The class "+clazz+" is not supported by the maven indexer");
158 public Set<String> getGroups() throws IOException {
159 return delegate.getAllGroups();
163 public void updateTimestamp(boolean save) throws IOException {
164 delegate.updateTimestamp(save);
168 public void updateTimestamp(boolean save, ZonedDateTime time) throws IOException {
169 delegate.updateTimestamp(save, Date.from(time.toInstant()));