001package org.apache.archiva.indexer.maven; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.common.filelock.DefaultFileLockManager; 023import org.apache.archiva.common.filelock.FileLockManager; 024import org.apache.archiva.indexer.ArchivaIndexingContext; 025import org.apache.archiva.repository.Repository; 026import org.apache.archiva.repository.storage.FilesystemStorage; 027import org.apache.archiva.repository.storage.StorageAsset; 028import org.apache.maven.index.context.IndexingContext; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032import java.io.IOException; 033import java.net.URI; 034import java.nio.file.Files; 035import java.nio.file.NoSuchFileException; 036import java.nio.file.Path; 037import java.sql.Date; 038import java.time.ZonedDateTime; 039import java.util.Set; 040import java.util.concurrent.atomic.AtomicBoolean; 041 042/** 043 * Maven implementation of index context 044 */ 045public class MavenIndexContext implements ArchivaIndexingContext { 046 047 private static final Logger log = LoggerFactory.getLogger(ArchivaIndexingContext.class); 048 049 050 private AtomicBoolean openStatus = new AtomicBoolean(false); 051 private IndexingContext delegate; 052 private Repository repository; 053 private StorageAsset dir = null; 054 055 protected MavenIndexContext(Repository repository, IndexingContext delegate) { 056 this.delegate = delegate; 057 this.repository = repository; 058 this.openStatus.set(true); 059 060 } 061 062 @Override 063 public String getId() { 064 return delegate.getId(); 065 } 066 067 @Override 068 public Repository getRepository() { 069 return repository; 070 } 071 072 @Override 073 public StorageAsset getPath() { 074 if (dir==null) { 075 StorageAsset repositoryDirAsset = repository.getAsset(""); 076 Path repositoryDir = repositoryDirAsset.getFilePath().toAbsolutePath(); 077 Path indexDir = delegate.getIndexDirectoryFile().toPath(); 078 if (indexDir.startsWith(repositoryDir)) { 079 dir = repository.getAsset(repositoryDir.relativize(indexDir).toString()); 080 } else { 081 try { 082 FilesystemStorage storage = new FilesystemStorage(indexDir, new DefaultFileLockManager()); 083 dir = storage.getAsset(""); 084 } catch (IOException e) { 085 log.error("Error occured while creating storage for index dir"); 086 } 087 } 088 } 089 return dir; 090 } 091 092 @Override 093 public boolean isEmpty() throws IOException { 094 return Files.list(delegate.getIndexDirectoryFile().toPath()).count()==0; 095 } 096 097 @Override 098 public void commit() throws IOException { 099 delegate.commit(); 100 } 101 102 @Override 103 public void rollback() throws IOException { 104 delegate.rollback(); 105 } 106 107 @Override 108 public void optimize() throws IOException { 109 delegate.optimize(); 110 } 111 112 @Override 113 public void close(boolean deleteFiles) throws IOException { 114 if (openStatus.compareAndSet(true,false)) { 115 try { 116 delegate.close(deleteFiles); 117 } catch (NoSuchFileException e) { 118 // Ignore missing directory 119 } 120 } 121 } 122 123 @Override 124 public void close() throws IOException { 125 if (openStatus.compareAndSet(true,false)) { 126 try { 127 delegate.close(false); 128 } catch (NoSuchFileException e) { 129 // Ignore missing directory 130 } 131 } 132 } 133 134 @Override 135 public boolean isOpen() { 136 return openStatus.get(); 137 } 138 139 @Override 140 public void purge() throws IOException { 141 delegate.purge(); 142 } 143 144 @Override 145 public boolean supports(Class<?> clazz) { 146 return IndexingContext.class.equals(clazz); 147 } 148 149 @SuppressWarnings( "unchecked" ) 150 @Override 151 public <T> T getBaseContext(Class<T> clazz) throws UnsupportedOperationException { 152 if (IndexingContext.class.equals(clazz)) { 153 return (T) delegate; 154 } else { 155 throw new UnsupportedOperationException("The class "+clazz+" is not supported by the maven indexer"); 156 } 157 } 158 159 @Override 160 public Set<String> getGroups() throws IOException { 161 return delegate.getAllGroups(); 162 } 163 164 @Override 165 public void updateTimestamp(boolean save) throws IOException { 166 delegate.updateTimestamp(save); 167 } 168 169 @Override 170 public void updateTimestamp(boolean save, ZonedDateTime time) throws IOException { 171 delegate.updateTimestamp(save, Date.from(time.toInstant())); 172 } 173 174 175}