Search in sources :

Example 41 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class ArchetypeCatalogGenerator method generateGroupFileContent.

@Override
public Transfer generateGroupFileContent(final Group group, final List<ArtifactStore> members, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    if (!canProcess(path)) {
        return null;
    }
    final Transfer target = fileManager.getTransfer(group, path);
    if (!target.exists()) {
        String toMergePath = path;
        if (!path.endsWith(ArchetypeCatalogMerger.CATALOG_NAME)) {
            toMergePath = normalize(normalize(parentPath(toMergePath)), ArchetypeCatalogMerger.CATALOG_NAME);
        }
        final List<Transfer> sources = fileManager.retrieveAllRaw(members, toMergePath, new EventMetadata());
        final byte[] merged = merger.merge(sources, group, toMergePath);
        if (merged != null) {
            try (OutputStream fos = target.openOutputStream(TransferOperation.GENERATE, true, eventMetadata)) {
                fos.write(merged);
            } catch (final IOException e) {
                throw new IndyWorkflowException("Failed to write merged archetype catalog to: {}.\nError: {}", e, target, e.getMessage());
            }
        }
    }
    if (target.exists()) {
        return target;
    }
    return null;
}
Also used : OutputStream(java.io.OutputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 42 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class MetadataStoreListener method onMetadataFileForceDelete.

/**
 * Indy normally does not handle FileDeletionEvent when the cached metadata files were deleted due to store
 * enable/disable/delete, etc. Lately we add a force-deletion for group/remote-repo cache files. This requires to
 * delete affected group metadata files and clear ISPN cache too. We use this method for just this case, i.e.,
 * only if CHECK_CACHE_ONLY is true.
 */
public void onMetadataFileForceDelete(@Observes final FileDeletionEvent event) {
    EventMetadata eventMetadata = event.getEventMetadata();
    if (!Boolean.TRUE.equals(eventMetadata.get(CHECK_CACHE_ONLY))) {
        return;
    }
    logger.trace("Got file-delete event: {}", event);
    Transfer transfer = event.getTransfer();
    String path = transfer.getPath();
    if (!path.endsWith(METADATA_NAME)) {
        logger.trace("Not {} , path: {}", METADATA_NAME, path);
        return;
    }
    Location loc = transfer.getLocation();
    if (!(loc instanceof KeyedLocation)) {
        logger.trace("Ignore FileDeletionEvent, not a KeyedLocation, location: {}", loc);
        return;
    }
    KeyedLocation keyedLocation = (KeyedLocation) loc;
    StoreKey storeKey = keyedLocation.getKey();
    try {
        ArtifactStore store = storeManager.getArtifactStore(storeKey);
        metadataGenerator.clearAllMerged(store, path);
    } catch (IndyDataException e) {
        logger.error("Handle FileDeletionEvent failed", e);
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Transfer(org.commonjava.maven.galley.model.Transfer) StoreKey(org.commonjava.indy.model.core.StoreKey) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location)

Example 43 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class FileChangeTrackingDecoratorTest method decorateDelete.

@Test
public void decorateDelete() throws IOException {
    init(1);
    SimpleLocation loc = new SimpleLocation("foo", "http://foo.bar");
    ConcreteResource res = new ConcreteResource(loc, "path/to/foo.txt");
    Transfer tx = cp.getTransfer(res);
    byte[] data = "This is a test".getBytes();
    try (OutputStream os = tx.openOutputStream(TransferOperation.UPLOAD)) {
        os.write(data);
    }
    tx.delete();
    verifyLogFileCount(2);
}
Also used : OutputStream(java.io.OutputStream) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) Test(org.junit.Test)

Example 44 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class IndexingContentManagerDecorator method getTransferFromConstituents.

/**
 * Recursively fetching the transfer from group constituents, and only
 * indexing the transfer for first found repo and its parent groups.
 */
private Transfer getTransferFromConstituents(Collection<StoreKey> constituents, ConcreteResource resource, String path, ArtifactStore parentStore, TransferSupplier<Transfer> transferSupplier) {
    List<StoreKey> members = new ArrayList<>(constituents);
    Transfer transfer = null;
    for (StoreKey memberKey : members) {
        try {
            transfer = transferSupplier.get(memberKey);
        } catch (IndyWorkflowException e) {
            logger.error(String.format("Failed to retrieve() for member path: %s:%s. Reason: %s", memberKey, path, e.getMessage()), e);
        }
        if (exists(transfer)) {
            nfc.clearMissing(resource);
            if (indexCfg.isEnabled()) {
                logger.debug("Got transfer from constituent: {} (will index)", transfer);
                indexManager.indexTransferIn(transfer, parentStore.getKey());
            }
            return transfer;
        }
    }
    return transfer;
}
Also used : IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArrayList(java.util.ArrayList) Transfer(org.commonjava.maven.galley.model.Transfer) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 45 with Transfer

use of org.commonjava.maven.galley.model.Transfer in project indy by Commonjava.

the class IndexingContentManagerDecorator method retrieve.

@Override
public Transfer retrieve(final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    if (!indexCfg.isEnabled()) {
        return delegate.retrieve(store, path, eventMetadata);
    }
    if (store == null) {
        return null;
    }
    logger.trace("Looking for indexed path: {} in: {}", path, store.getKey());
    Transfer transfer = getIndexedTransfer(store.getKey(), null, path, TransferOperation.DOWNLOAD, eventMetadata);
    if (transfer != null) {
        logger.debug("Found indexed transfer: {}. Returning.", transfer);
        return transfer;
    } else if (isAuthoritativelyMissing(store)) {
        logger.debug("Not found indexed transfer: {} and authoritative index switched on. Considering not found and return null.", transfer);
        return null;
    }
    StoreType type = store.getKey().getType();
    if (StoreType.group == type) {
        ConcreteResource resource = new ConcreteResource(LocationUtils.toLocation(store), path);
        if (nfc.isMissing(resource)) {
            logger.debug("{} is marked as missing. Returning null.", resource);
            return null;
        }
        logger.debug("No group index hits. Devolving to member store indexes.");
        KeyedLocation location = LocationUtils.toLocation(store);
        SpecialPathInfo specialPathInfo = specialPathManager.getSpecialPathInfo(location, path, store.getPackageType());
        if (specialPathInfo == null || !specialPathInfo.isMergable()) {
            if (PathMaskChecker.checkMask(store, path)) {
                transfer = getTransferFromConstituents(((Group) store).getConstituents(), resource, path, store, memberKey -> {
                    try {
                        ArtifactStore member = storeDataManager.getArtifactStore(memberKey);
                        if (member == null) {
                            logger.trace("Cannot find store for key: {}", memberKey);
                        } else {
                            return retrieve(member, path, eventMetadata);
                        }
                    } catch (IndyDataException e) {
                        logger.error(String.format("Failed to lookup store: %s (in membership of: %s). Reason: %s", memberKey, store.getKey(), e.getMessage()), e);
                    }
                    return null;
                });
                nfcForGroup(store, transfer, resource);
                return transfer;
            } else {
                return null;
            }
        } else {
            logger.debug("Merged content. Delegating to main content manager for: {} in: {}", path, store);
            transfer = delegate.retrieve(store, path, eventMetadata);
            if (!exists(transfer)) {
                Boolean metadataGenerated = (Boolean) eventMetadata.get(GROUP_METADATA_GENERATED);
                Boolean metadataExists = (Boolean) eventMetadata.get(GROUP_METADATA_EXISTS);
                if (Boolean.TRUE.equals(metadataGenerated) || Boolean.TRUE.equals(metadataExists)) {
                    // metadata generated/exists but missing due to membership change, not add to nfc so next req can retry
                    ;
                } else // don't track NFC for hosted repos
                {
                    nfc.addMissing(resource);
                }
            }
            return transfer;
        }
    }
    logger.trace("Delegating retrieve call for concrete repository: {}/{}", store, path);
    transfer = delegate.retrieve(store, path, eventMetadata);
    if (exists(transfer) && indexCfg.isEnabled()) {
        logger.debug("Got transfer from delegate: {} (will index)", transfer);
        indexManager.indexTransferIn(transfer, store.getKey());
    }
    logger.debug("Returning transfer: {}", transfer);
    return transfer;
}
Also used : StoreType(org.commonjava.indy.model.core.StoreType) Delegate(javax.decorator.Delegate) Arrays(java.util.Arrays) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) LoggerFactory(org.slf4j.LoggerFactory) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) Group(org.commonjava.indy.model.core.Group) ArrayList(java.util.ArrayList) GROUP_METADATA_GENERATED(org.commonjava.indy.core.content.group.GroupMergeHelper.GROUP_METADATA_GENERATED) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Transfer(org.commonjava.maven.galley.model.Transfer) Measure(org.commonjava.o11yphant.metrics.annotation.Measure) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) TransferOperation(org.commonjava.maven.galley.model.TransferOperation) Any(javax.enterprise.inject.Any) Decorator(javax.decorator.Decorator) StoreKey(org.commonjava.indy.model.core.StoreKey) GROUP_METADATA_EXISTS(org.commonjava.indy.core.content.group.GroupMergeHelper.GROUP_METADATA_EXISTS) LocationUtils(org.commonjava.indy.util.LocationUtils) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Logger(org.slf4j.Logger) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Executor(java.util.concurrent.Executor) PathMaskChecker(org.commonjava.indy.core.content.PathMaskChecker) Collection(java.util.Collection) StoreType(org.commonjava.indy.model.core.StoreType) Set(java.util.Set) SpecialPathManager(org.commonjava.maven.galley.spi.io.SpecialPathManager) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) ContentManager(org.commonjava.indy.content.ContentManager) Objects(java.util.Objects) List(java.util.List) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) ContentIndexConfig(org.commonjava.indy.content.index.conf.ContentIndexConfig) IndyDataException(org.commonjava.indy.data.IndyDataException) StoreDataManager(org.commonjava.indy.data.StoreDataManager) InputStream(java.io.InputStream) IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) SpecialPathInfo(org.commonjava.maven.galley.model.SpecialPathInfo) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

Aggregations

Transfer (org.commonjava.maven.galley.model.Transfer)218 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)89 Test (org.junit.Test)80 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)64 IOException (java.io.IOException)63 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)59 InputStream (java.io.InputStream)43 StoreKey (org.commonjava.indy.model.core.StoreKey)43 Logger (org.slf4j.Logger)34 ArrayList (java.util.ArrayList)33 OutputStream (java.io.OutputStream)31 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)31 HostedRepository (org.commonjava.indy.model.core.HostedRepository)29 IndyDataException (org.commonjava.indy.data.IndyDataException)28 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)28 Group (org.commonjava.indy.model.core.Group)26 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)23 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)21 TransferException (org.commonjava.maven.galley.TransferException)20 Location (org.commonjava.maven.galley.model.Location)19