Search in sources :

Example 6 with DefaultFileLockManager

use of org.apache.archiva.common.filelock.DefaultFileLockManager in project archiva by apache.

the class ArchivaDavResourceFactory method buildMergedIndexDirectory.

protected StorageAsset buildMergedIndexDirectory(String activePrincipal, DavServletRequest request, RepositoryGroup repositoryGroup) throws DavException {
    try {
        final List<ManagedRepository> repositories = repositoryGroup.getRepositories();
        HttpSession session = request.getSession();
        @SuppressWarnings("unchecked") Map<String, TemporaryGroupIndex> temporaryGroupIndexMap = (Map<String, TemporaryGroupIndex>) session.getAttribute(TemporaryGroupIndexSessionCleaner.TEMPORARY_INDEX_SESSION_KEY);
        if (temporaryGroupIndexMap == null) {
            temporaryGroupIndexMap = new HashMap<>();
        }
        final String id = repositoryGroup.getId();
        TemporaryGroupIndex tmp = temporaryGroupIndexMap.get(id);
        if (tmp != null && tmp.getDirectory() != null && tmp.getDirectory().exists()) {
            if (System.currentTimeMillis() - tmp.getCreationTime() > (repositoryGroup.getMergedIndexTTL() * 60 * 1000)) {
                log.debug(MarkerFactory.getMarker("group.merged.index"), "tmp group index '{}' is too old so delete it", id);
                indexMerger.cleanTemporaryGroupIndex(tmp);
            } else {
                log.debug(MarkerFactory.getMarker("group.merged.index"), "merged index for group '{}' found in cache", id);
                return tmp.getDirectory();
            }
        }
        Set<String> authzRepos = new HashSet<String>();
        String permission = WebdavMethodUtil.getMethodPermission(request.getMethod());
        for (ManagedRepository repository : repositories) {
            try {
                if (servletAuth.isAuthorized(activePrincipal, repository.getId(), permission)) {
                    authzRepos.add(repository.getId());
                    authzRepos.addAll(this.repositorySearch.getRemoteIndexingContextIds(repository.getId()));
                }
            } catch (UnauthorizedException e) {
                // TODO: review exception handling
                log.debug("Skipping repository '{}' for user '{}': {}", repository, activePrincipal, e.getMessage());
            }
        }
        log.info("generate temporary merged index for repository group '{}' for repositories '{}'", id, authzRepos);
        IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature(IndexCreationFeature.class);
        Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath();
        if (indexPath != null) {
            Path tempRepoFile = Files.createTempDirectory("temp");
            tempRepoFile.toFile().deleteOnExit();
            FilesystemStorage storage = new FilesystemStorage(tempRepoFile, new DefaultFileLockManager());
            StorageAsset tmpAsset = storage.getRoot();
            IndexMergerRequest indexMergerRequest = new IndexMergerRequest(authzRepos, true, id, indexPath.toString(), repositoryGroup.getMergedIndexTTL()).mergedIndexDirectory(tmpAsset).temporary(true);
            MergedRemoteIndexesTaskRequest taskRequest = new MergedRemoteIndexesTaskRequest(indexMergerRequest, indexMerger);
            MergedRemoteIndexesTask job = new MergedRemoteIndexesTask(taskRequest);
            ArchivaIndexingContext indexingContext = job.execute().getIndexingContext();
            StorageAsset mergedRepoDir = indexingContext.getPath();
            TemporaryGroupIndex temporaryGroupIndex = new TemporaryGroupIndex(mergedRepoDir, indexingContext.getId(), id, // 
            repositoryGroup.getMergedIndexTTL()).setCreationTime(new Date().getTime());
            temporaryGroupIndexMap.put(id, temporaryGroupIndex);
            session.setAttribute(TemporaryGroupIndexSessionCleaner.TEMPORARY_INDEX_SESSION_KEY, temporaryGroupIndexMap);
            return mergedRepoDir;
        } else {
            log.error("Local index path for repository group {} does not exist.", repositoryGroup.getId());
            throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    } catch (RepositorySearchException e) {
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    } catch (IndexMergerException e) {
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    } catch (IOException e) {
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
    }
}
Also used : TemporaryGroupIndex(org.apache.archiva.indexer.merger.TemporaryGroupIndex) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MergedRemoteIndexesTaskRequest(org.apache.archiva.indexer.merger.base.MergedRemoteIndexesTaskRequest) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) UnauthorizedException(org.apache.archiva.redback.authorization.UnauthorizedException) IndexMergerException(org.apache.archiva.indexer.merger.IndexMergerException) HashSet(java.util.HashSet) Path(java.nio.file.Path) MergedRemoteIndexesTask(org.apache.archiva.indexer.merger.base.MergedRemoteIndexesTask) DavException(org.apache.jackrabbit.webdav.DavException) HttpSession(javax.servlet.http.HttpSession) IndexMergerRequest(org.apache.archiva.indexer.merger.IndexMergerRequest) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) IOException(java.io.IOException) ArchivaIndexingContext(org.apache.archiva.indexer.ArchivaIndexingContext) Date(java.util.Date) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with DefaultFileLockManager

use of org.apache.archiva.common.filelock.DefaultFileLockManager in project archiva by apache.

the class MetadataTransferTest method assertMetadataEquals.

private void assertMetadataEquals(String expectedMetadataXml, Path actualFile) throws Exception {
    assertNotNull("Actual File should not be null.", actualFile);
    assertTrue("Actual file exists.", Files.exists(actualFile));
    StringWriter actualContents = new StringWriter();
    FilesystemStorage fsStorage = new FilesystemStorage(actualFile.getParent(), new DefaultFileLockManager());
    StorageAsset actualFileAsset = fsStorage.getAsset(actualFile.getFileName().toString());
    ArchivaRepositoryMetadata metadata = metadataTools.getMetadataReader(null).read(actualFileAsset);
    RepositoryMetadataWriter.write(metadata, actualContents);
    Diff detailedDiff = DiffBuilder.compare(expectedMetadataXml).withTest(actualContents.toString()).checkForSimilar().build();
    if (detailedDiff.hasDifferences()) {
        for (Difference diff : detailedDiff.getDifferences()) {
            System.out.println(diff);
        }
        assertEquals(expectedMetadataXml, actualContents);
    }
// assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
}
Also used : StringWriter(java.io.StringWriter) Diff(org.xmlunit.diff.Diff) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) Difference(org.xmlunit.diff.Difference) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 8 with DefaultFileLockManager

use of org.apache.archiva.common.filelock.DefaultFileLockManager in project archiva by apache.

the class MavenRemoteRepository method newLocalInstance.

public static MavenRemoteRepository newLocalInstance(String id, String name, Path basePath) throws IOException {
    FileLockManager lockManager = new DefaultFileLockManager();
    FilesystemStorage storage = new FilesystemStorage(basePath.resolve(id), lockManager);
    return new MavenRemoteRepository(id, name, storage);
}
Also used : FileLockManager(org.apache.archiva.common.filelock.FileLockManager) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager)

Example 9 with DefaultFileLockManager

use of org.apache.archiva.common.filelock.DefaultFileLockManager in project archiva by apache.

the class BasicRepositoryGroup method newFilesystemInstance.

/**
 * Creates a filesystem based repository instance. The path is built by basePath/repository-id
 *
 * @param id The repository id
 * @param name The name of the repository
 * @param repositoryPath The path to the repository
 * @return The repository instance
 * @throws IOException
 */
public static BasicRepositoryGroup newFilesystemInstance(String id, String name, Path repositoryPath) throws IOException {
    FileLockManager lockManager = new DefaultFileLockManager();
    FilesystemStorage storage = new FilesystemStorage(repositoryPath, lockManager);
    return new BasicRepositoryGroup(id, name, storage);
}
Also used : FileLockManager(org.apache.archiva.common.filelock.FileLockManager) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager)

Example 10 with DefaultFileLockManager

use of org.apache.archiva.common.filelock.DefaultFileLockManager in project archiva by apache.

the class MavenRepositoryMapperTest method reverseMap.

@Test
void reverseMap() throws IOException, URISyntaxException, UnsupportedURIException {
    MavenRepositoryMapper mapper = new MavenRepositoryMapper();
    Path tmpDir = Files.createTempDirectory("mapper-test");
    FilesystemStorage fsStorage = new FilesystemStorage(tmpDir, new DefaultFileLockManager());
    EditableManagedRepository repository = new BasicManagedRepository(Locale.getDefault(), RepositoryType.MAVEN, "repo02", "Repo 02", fsStorage);
    repository.setDescription(Locale.getDefault(), "This is repo 02");
    repository.setBlocksRedeployment(false);
    repository.setLocation(new URI("test-path"));
    repository.setScanned(true);
    repository.setLayout("maven2");
    repository.setSchedulingDefinition("* 3,5,10 * * *");
    IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class);
    icf.setIndexPath(new URI(".indexer"));
    icf.setPackedIndexPath(new URI(".index"));
    icf.setSkipPackedIndexCreation(false);
    ArtifactCleanupFeature acf = repository.getFeature(ArtifactCleanupFeature.class);
    acf.setDeleteReleasedSnapshots(false);
    acf.setRetentionPeriod(Period.ofDays(5));
    acf.setRetentionCount(17);
    StagingRepositoryFeature srf = repository.getFeature(StagingRepositoryFeature.class);
    srf.setStageRepoNeeded(false);
    MavenManagedRepository result = mapper.reverseMap(repository);
    assertEquals("repo02", result.getId());
    assertEquals("Repo 02", result.getName());
    assertEquals("This is repo 02", result.getDescription());
    assertFalse(result.isBlocksRedeployments());
    assertEquals("test-path", result.getLocation());
    assertTrue(result.isScanned());
    assertEquals("maven2", result.getLayout());
    assertEquals("* 3,5,10 * * *", result.getSchedulingDefinition());
    assertEquals(".indexer", result.getIndexPath());
    assertEquals(".index", result.getPackedIndexPath());
    assertFalse(result.isSkipPackedIndexCreation());
    assertFalse(result.isDeleteSnapshotsOfRelease());
    assertEquals(Period.ofDays(5), result.getRetentionPeriod());
    assertEquals(17, result.getRetentionCount());
    assertFalse(result.hasStagingRepository());
}
Also used : Path(java.nio.file.Path) BasicManagedRepository(org.apache.archiva.repository.base.managed.BasicManagedRepository) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) MavenManagedRepository(org.apache.archiva.rest.api.v2.model.MavenManagedRepository) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) StagingRepositoryFeature(org.apache.archiva.repository.features.StagingRepositoryFeature) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultFileLockManager (org.apache.archiva.common.filelock.DefaultFileLockManager)42 FilesystemStorage (org.apache.archiva.repository.storage.fs.FilesystemStorage)41 Path (java.nio.file.Path)23 FileLockManager (org.apache.archiva.common.filelock.FileLockManager)14 FilesystemAsset (org.apache.archiva.repository.storage.fs.FilesystemAsset)12 IndexCreationFeature (org.apache.archiva.repository.features.IndexCreationFeature)7 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)7 BasicManagedRepository (org.apache.archiva.repository.base.managed.BasicManagedRepository)6 ArtifactContentEntry (org.apache.archiva.rest.api.model.ArtifactContentEntry)6 IOException (java.io.IOException)5 URI (java.net.URI)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 DavException (org.apache.jackrabbit.webdav.DavException)3 Before (org.junit.Before)3 Date (java.util.Date)2 MavenMetadataReader (org.apache.archiva.maven.metadata.MavenMetadataReader)2 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)2