Search in sources :

Example 96 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class StoreContentListener method processAllPaths.

private void processAllPaths(final Iterable<ArtifactStore> stores, Predicate<? super String> pathFilter, boolean deleteOriginPath) {
    StreamSupport.stream(stores.spliterator(), true).forEach((store) -> {
        final StoreKey key = store.getKey();
        Set<String> paths = listPaths(key, pathFilter);
        if (!paths.isEmpty()) {
            Set<Group> groups = null;
            try {
                groups = storeDataManager.query().packageType(key.getPackageType()).getGroupsAffectedBy(key);
            } catch (IndyDataException e) {
                e.printStackTrace();
            }
            clearPaths(paths, key, groups, deleteOriginPath);
        }
    });
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 97 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class MergedFileUploadListener method reMergeUploaded.

// NOTE: Disabling @Observes on this because I'm pretty sure the ContentManager is handling it now.
public void reMergeUploaded(/*@Observes*/
final FileEvent event) {
    if (event instanceof FileAccessEvent) {
        return;
    }
    final String path = event.getTransfer().getPath();
    final StoreKey key = getKey(event);
    if (!path.endsWith(MavenMetadataMerger.METADATA_NAME) && !path.endsWith(ArchetypeCatalogMerger.CATALOG_NAME)) {
        return;
    }
    try {
        final Set<Group> groups = dataManager.query().getGroupsContaining(key);
        if (groups != null) {
            for (final Group group : groups) {
                try {
                    reMerge(group, path);
                } catch (final IOException e) {
                    logger.error(String.format("Failed to delete: %s from group: %s. Error: %s", path, group, e.getMessage()), e);
                }
            }
        }
    } catch (final IndyDataException e) {
        logger.warn("Failed to regenerate maven-metadata.xml for groups after deployment to: {}" + "\nCannot retrieve associated groups: {}", e, key, e.getMessage());
    }
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) Group(org.commonjava.indy.model.core.Group) IOException(java.io.IOException) FileAccessEvent(org.commonjava.maven.galley.event.FileAccessEvent) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 98 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class ArchetypeCatalogMerger method merge.

@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
    final ArchetypeCatalog master = new ArchetypeCatalog();
    final ArchetypeCatalogXpp3Reader reader = new ArchetypeCatalogXpp3Reader();
    final FileReader fr = null;
    InputStream stream = null;
    boolean merged = false;
    final Set<String> seen = new HashSet<String>();
    for (final Transfer src : sources) {
        try {
            stream = src.openInputStream();
            final ArchetypeCatalog catalog = reader.read(stream, false);
            for (final Archetype arch : catalog.getArchetypes()) {
                final String key = arch.getGroupId() + ":" + arch.getArtifactId() + ":" + arch.getVersion();
                if (seen.add(key)) {
                    master.addArchetype(arch);
                }
            }
            merged = true;
        } catch (final IOException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot read archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } catch (final XmlPullParserException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot parse archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } finally {
            closeQuietly(fr);
        }
    }
    if (merged) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new ArchetypeCatalogXpp3Writer().write(baos, master);
            return baos.toByteArray();
        } catch (final IOException e) {
            logger.error(String.format("Cannot write consolidated archetype catalog: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
        }
    }
    return null;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) InputStream(java.io.InputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StoreKey(org.commonjava.indy.model.core.StoreKey) ArchetypeCatalogXpp3Reader(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader) Transfer(org.commonjava.maven.galley.model.Transfer) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) HashSet(java.util.HashSet)

Example 99 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class MavenMetadataMerger method merge.

@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
    final Metadata master = new Metadata();
    master.setVersioning(new Versioning());
    final MetadataXpp3Reader reader = new MetadataXpp3Reader();
    final FileReader fr = null;
    InputStream stream = null;
    boolean merged = false;
    Transfer snapshotProvider = null;
    for (final Transfer src : sources) {
        if (!src.exists()) {
            continue;
        }
        try {
            stream = src.openInputStream();
            String content = IOUtils.toString(stream);
            logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
            // there is a lot of junk in here to make up for Metadata's anemic merge() method.
            final Metadata md = reader.read(new StringReader(content), false);
            if (md.getGroupId() != null) {
                master.setGroupId(md.getGroupId());
            }
            if (md.getArtifactId() != null) {
                master.setArtifactId(md.getArtifactId());
            }
            if (md.getVersion() != null) {
                master.setVersion(md.getVersion());
            }
            master.merge(md);
            Versioning versioning = master.getVersioning();
            Versioning mdVersioning = md.getVersioning();
            // FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
            if (versioning.getSnapshot() == null && mdVersioning != null) {
                logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
                snapshotProvider = src;
                versioning.setSnapshot(mdVersioning.getSnapshot());
                final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
                boolean added = false;
                for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
                    if (!snapshotVersions.contains(snap)) {
                        snapshotVersions.add(snap);
                        added = true;
                    }
                }
                if (added) {
                    Collections.sort(snapshotVersions, new SnapshotVersionComparator());
                }
            } else {
                logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
            }
            merged = true;
        } catch (final IOException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot read metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } catch (final XmlPullParserException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot parse metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } finally {
            closeQuietly(fr);
            closeQuietly(stream);
        }
    }
    Versioning versioning = master.getVersioning();
    if (versioning != null && versioning.getVersions() != null) {
        if (metadataProviders != null) {
            for (MavenMetadataProvider provider : metadataProviders) {
                try {
                    Metadata toMerge = provider.getMetadata(group.getKey(), path);
                    if (toMerge != null) {
                        merged = master.merge(toMerge) || merged;
                    }
                } catch (IndyWorkflowException e) {
                    logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
                }
            }
        }
        List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
        Collections.sort(versionObjects);
        versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
        if (versionObjects.size() > 0) {
            String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
            versioning.setLatest(latest);
            versioning.setRelease(latest);
        }
    }
    if (merged) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new MetadataXpp3Writer().write(baos, master);
            return baos.toByteArray();
        } catch (final IOException e) {
            logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
        }
    }
    return null;
}
Also used : MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) InputStream(java.io.InputStream) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.slf4j.Logger) StoreKey(org.commonjava.indy.model.core.StoreKey) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) StringReader(java.io.StringReader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion)

Example 100 with StoreKey

use of org.commonjava.indy.model.core.StoreKey in project indy by Commonjava.

the class PromotionManager method rollbackPathsPromote.

/**
     * Attempt to rollbackPathsPromote a previously failing {@link PathsPromoteResult}. This is meant to handle cases where an unrecoverable error
     * occurs on the server side, and promotion can NOT proceed afterward. All paths in the completed paths set are deleted from the target, if
     * possible. The output {@link PathsPromoteResult} contains the previous content, with any successfully removed target paths moved back from the
     * completed-paths list to the pending-paths list. If an error occurs during rollbackPathsPromote, the error field will be set...otherwise, it will be null.
     *
     * @param result The result to rollbackPathsPromote
     *
     * @return The same result, with any successful deletions moved from the completed to pending paths list, and the error cleared (or set to a
     * new error)
     *
     * @throws PromotionException
     * @throws IndyWorkflowException
     */
public PathsPromoteResult rollbackPathsPromote(final PathsPromoteResult result) throws PromotionException, IndyWorkflowException {
    StoreKey targetKey = result.getRequest().getTarget();
    ReentrantLock lock;
    synchronized (byPathTargetLocks) {
        lock = byPathTargetLocks.get(targetKey);
        if (lock == null) {
            lock = new ReentrantLock();
            byPathTargetLocks.put(targetKey, lock);
        }
    }
    final List<Transfer> contents = getTransfersForPaths(targetKey, result.getCompletedPaths());
    final Set<String> completed = result.getCompletedPaths();
    final Set<String> skipped = result.getSkippedPaths();
    if (completed == null || completed.isEmpty()) {
        result.setError(null);
        return result;
    }
    Set<String> pending = result.getPendingPaths();
    pending = pending == null ? new HashSet<String>() : new HashSet<String>(pending);
    String error = null;
    final boolean copyToSource = result.getRequest().isPurgeSource();
    ArtifactStore source = null;
    try {
        source = storeManager.getArtifactStore(result.getRequest().getSource());
    } catch (final IndyDataException e) {
        error = String.format("Failed to retrieve artifact store: %s. Reason: %s", result.getRequest().getSource(), e.getMessage());
        logger.error(error, e);
    }
    boolean locked = false;
    try {
        if (error == null) {
            locked = lock.tryLock(config.getLockTimeoutSeconds(), TimeUnit.SECONDS);
            if (!locked) {
                error = String.format("Failed to acquire promotion lock on target: %s in %d seconds.", targetKey, config.getLockTimeoutSeconds());
                logger.warn(error);
            }
        }
        if (error == null) {
            for (final Transfer transfer : contents) {
                if (transfer != null && transfer.exists()) {
                    InputStream stream = null;
                    try {
                        if (copyToSource) {
                            stream = transfer.openInputStream(true);
                            final String path = transfer.getPath();
                            contentManager.store(source, path, stream, TransferOperation.UPLOAD, new EventMetadata());
                            stream.close();
                        }
                        transfer.delete(true);
                        completed.remove(transfer.getPath());
                        pending.add(transfer.getPath());
                    } catch (final IOException e) {
                        error = String.format("Failed to rollback path promotion of: %s from: %s. Reason: %s", transfer, result.getRequest().getSource(), e.getMessage());
                        logger.error(error, e);
                    } finally {
                        closeQuietly(stream);
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        error = String.format("Interrupted waiting for promotion lock on target: %s", targetKey);
        logger.warn(error);
    } finally {
        if (locked) {
            lock.unlock();
        }
    }
    return new PathsPromoteResult(result.getRequest(), pending, completed, skipped, error, new ValidationResult());
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) InputStream(java.io.InputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) IOException(java.io.IOException) ValidationResult(org.commonjava.indy.promote.model.ValidationResult) StoreKey(org.commonjava.indy.model.core.StoreKey) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) Transfer(org.commonjava.maven.galley.model.Transfer) HashSet(java.util.HashSet)

Aggregations

StoreKey (org.commonjava.indy.model.core.StoreKey)186 Test (org.junit.Test)92 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)40 StoreType (org.commonjava.indy.model.core.StoreType)39 InputStream (java.io.InputStream)33 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)32 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)31 IndyDataException (org.commonjava.indy.data.IndyDataException)30 Group (org.commonjava.indy.model.core.Group)29 Transfer (org.commonjava.maven.galley.model.Transfer)27 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)24 Response (javax.ws.rs.core.Response)23 IOException (java.io.IOException)22 Logger (org.slf4j.Logger)21 ApiOperation (io.swagger.annotations.ApiOperation)20 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)20 ArrayList (java.util.ArrayList)19 Path (javax.ws.rs.Path)19 ApiResponse (io.swagger.annotations.ApiResponse)18 IndyObjectMapper (org.commonjava.indy.model.core.io.IndyObjectMapper)18