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.2KB

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