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.

MavenIndexContextMock.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package org.apache.archiva.repository.index.mock;
  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.indexer.ArchivaIndexingContext;
  22. import org.apache.archiva.repository.Repository;
  23. import org.apache.archiva.repository.storage.FilesystemStorage;
  24. import org.apache.archiva.repository.storage.StorageAsset;
  25. import org.apache.maven.index.context.IndexingContext;
  26. import java.io.IOException;
  27. import java.nio.file.Files;
  28. import java.nio.file.NoSuchFileException;
  29. import java.sql.Date;
  30. import java.time.ZonedDateTime;
  31. import java.util.Set;
  32. /**
  33. * Maven implementation of index context
  34. */
  35. public class MavenIndexContextMock implements ArchivaIndexingContext {
  36. private IndexingContext delegate;
  37. private Repository repository;
  38. private FilesystemStorage indexStorage;
  39. MavenIndexContextMock(Repository repository, IndexingContext delegate) throws IOException {
  40. this.delegate = delegate;
  41. this.repository = repository;
  42. indexStorage = new FilesystemStorage(delegate.getIndexDirectoryFile().toPath(), new DefaultFileLockManager());
  43. }
  44. @Override
  45. public String getId() {
  46. return delegate.getId();
  47. }
  48. @Override
  49. public Repository getRepository() {
  50. return repository;
  51. }
  52. @Override
  53. public StorageAsset getPath() {
  54. return indexStorage.getAsset("");
  55. }
  56. @Override
  57. public boolean isEmpty() throws IOException {
  58. return Files.list(delegate.getIndexDirectoryFile().toPath()).count()==0;
  59. }
  60. @Override
  61. public void commit() throws IOException {
  62. delegate.commit();
  63. }
  64. @Override
  65. public void rollback() throws IOException {
  66. delegate.rollback();
  67. }
  68. @Override
  69. public void optimize() throws IOException {
  70. delegate.optimize();
  71. }
  72. @Override
  73. public void close(boolean deleteFiles) throws IOException {
  74. try {
  75. delegate.close(deleteFiles);
  76. } catch (NoSuchFileException e) {
  77. // Ignore missing directory
  78. }
  79. }
  80. @Override
  81. public void close() throws IOException {
  82. try {
  83. delegate.close(false);
  84. } catch (NoSuchFileException e) {
  85. // Ignore missing directory
  86. }
  87. }
  88. @Override
  89. public void purge() throws IOException {
  90. delegate.purge();
  91. }
  92. @Override
  93. public boolean supports(Class<?> clazz) {
  94. return IndexingContext.class.equals(clazz);
  95. }
  96. @SuppressWarnings( "unchecked" )
  97. @Override
  98. public <T> T getBaseContext(Class<T> clazz) throws UnsupportedOperationException {
  99. if (IndexingContext.class.equals(clazz)) {
  100. return (T) delegate;
  101. } else {
  102. throw new UnsupportedOperationException("The class "+clazz+" is not supported by the maven indexer");
  103. }
  104. }
  105. @Override
  106. public Set<String> getGroups() throws IOException {
  107. return delegate.getAllGroups();
  108. }
  109. @Override
  110. public void updateTimestamp(boolean save) throws IOException {
  111. delegate.updateTimestamp(save);
  112. }
  113. @Override
  114. public void updateTimestamp(boolean save, ZonedDateTime time) throws IOException {
  115. delegate.updateTimestamp(save, Date.from(time.toInstant()));
  116. }
  117. }