You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MavenIndexContext.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package org.apache.archiva.indexer.maven;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.filelock.DefaultFileLockManager;
  21. import org.apache.archiva.common.filelock.FileLockManager;
  22. import org.apache.archiva.indexer.ArchivaIndexingContext;
  23. import org.apache.archiva.repository.Repository;
  24. import org.apache.archiva.repository.storage.FilesystemStorage;
  25. import org.apache.archiva.repository.storage.StorageAsset;
  26. import org.apache.maven.index.context.IndexingContext;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import java.io.IOException;
  30. import java.net.URI;
  31. import java.nio.file.Files;
  32. import java.nio.file.NoSuchFileException;
  33. import java.nio.file.Path;
  34. import java.sql.Date;
  35. import java.time.ZonedDateTime;
  36. import java.util.Set;
  37. /**
  38. * Maven implementation of index context
  39. */
  40. public class MavenIndexContext implements ArchivaIndexingContext {
  41. private static final Logger log = LoggerFactory.getLogger(ArchivaIndexingContext.class);
  42. private IndexingContext delegate;
  43. private Repository repository;
  44. private StorageAsset dir = null;
  45. protected MavenIndexContext(Repository repository, IndexingContext delegate) {
  46. this.delegate = delegate;
  47. this.repository = repository;
  48. }
  49. @Override
  50. public String getId() {
  51. return delegate.getId();
  52. }
  53. @Override
  54. public Repository getRepository() {
  55. return repository;
  56. }
  57. @Override
  58. public StorageAsset getPath() {
  59. if (dir==null) {
  60. StorageAsset repositoryDirAsset = repository.getAsset("");
  61. Path repositoryDir = repositoryDirAsset.getFilePath().toAbsolutePath();
  62. Path indexDir = delegate.getIndexDirectoryFile().toPath();
  63. if (indexDir.startsWith(repositoryDir)) {
  64. dir = repository.getAsset(repositoryDir.relativize(indexDir).toString());
  65. } else {
  66. try {
  67. FilesystemStorage storage = new FilesystemStorage(indexDir, new DefaultFileLockManager());
  68. dir = storage.getAsset("");
  69. } catch (IOException e) {
  70. log.error("Error occured while creating storage for index dir");
  71. }
  72. }
  73. }
  74. return dir;
  75. }
  76. @Override
  77. public boolean isEmpty() throws IOException {
  78. return Files.list(delegate.getIndexDirectoryFile().toPath()).count()==0;
  79. }
  80. @Override
  81. public void commit() throws IOException {
  82. delegate.commit();
  83. }
  84. @Override
  85. public void rollback() throws IOException {
  86. delegate.rollback();
  87. }
  88. @Override
  89. public void optimize() throws IOException {
  90. delegate.optimize();
  91. }
  92. @Override
  93. public void close(boolean deleteFiles) throws IOException {
  94. try {
  95. delegate.close(deleteFiles);
  96. } catch (NoSuchFileException e) {
  97. // Ignore missing directory
  98. }
  99. }
  100. @Override
  101. public void close() throws IOException {
  102. try {
  103. delegate.close(false);
  104. } catch (NoSuchFileException e) {
  105. // Ignore missing directory
  106. }
  107. }
  108. @Override
  109. public void purge() throws IOException {
  110. delegate.purge();
  111. }
  112. @Override
  113. public boolean supports(Class<?> clazz) {
  114. return IndexingContext.class.equals(clazz);
  115. }
  116. @SuppressWarnings( "unchecked" )
  117. @Override
  118. public <T> T getBaseContext(Class<T> clazz) throws UnsupportedOperationException {
  119. if (IndexingContext.class.equals(clazz)) {
  120. return (T) delegate;
  121. } else {
  122. throw new UnsupportedOperationException("The class "+clazz+" is not supported by the maven indexer");
  123. }
  124. }
  125. @Override
  126. public Set<String> getGroups() throws IOException {
  127. return delegate.getAllGroups();
  128. }
  129. @Override
  130. public void updateTimestamp(boolean save) throws IOException {
  131. delegate.updateTimestamp(save);
  132. }
  133. @Override
  134. public void updateTimestamp(boolean save, ZonedDateTime time) throws IOException {
  135. delegate.updateTimestamp(save, Date.from(time.toInstant()));
  136. }
  137. }