Search in sources :

Example 46 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class ImpliedRepositoryDetector method initJob.

private boolean initJob(final ImplicationsJob job) {
    switch(job.event.getType()) {
        case DOWNLOAD:
        case UPLOAD:
            break;
        default:
            // we're not interested in these.
            return false;
    }
    final Transfer transfer = job.transfer;
    if (!transfer.getPath().endsWith(".pom")) {
        return false;
    }
    final Location location = transfer.getLocation();
    if (!(location instanceof KeyedLocation)) {
        return false;
    }
    final StoreKey key = ((KeyedLocation) location).getKey();
    try {
        job.store = storeManager.getArtifactStore(key);
    } catch (final IndyDataException e) {
        logger.error(String.format("Cannot retrieve artifact store for: %s. Failed to process implied repositories.", key), e);
    }
    if (job.store == null) {
        return false;
    }
    job.pathInfo = ArtifactPathInfo.parse(transfer.getPath());
    if (job.pathInfo == null) {
        return false;
    }
    try {
        logger.debug("Parsing: {}", transfer);
        job.pomView = pomReader.readLocalPom(job.pathInfo.getProjectId(), transfer, MavenPomView.ALL_PROFILES);
    } catch (final GalleyMavenException e) {
        logger.error(String.format("Cannot parse: %s from: %s. Failed to process implied repositories.", job.pathInfo.getProjectId(), transfer), e);
    }
    return job.pomView != null;
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Transfer(org.commonjava.maven.galley.model.Transfer) StoreKey(org.commonjava.indy.model.core.StoreKey) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) Location(org.commonjava.maven.galley.model.Location)

Example 47 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class ImpliedRepositoryDetector method addImpliedRepositories.

private void addImpliedRepositories(final ImplicationsJob job) {
    job.implied = new ArrayList<>();
    logger.debug("Retrieving repository/pluginRepository declarations from:\n  {}", new JoinString("\n  ", job.pomView.getDocRefStack()));
    final List<List<RepositoryView>> repoLists = Arrays.asList(job.pomView.getNonProfileRepositories(), job.pomView.getAllPluginRepositories());
    final ImpliedRepositoryCreator creator = createRepoCreator();
    if (creator == null) {
        logger.error("Cannot proceed without a valid ImpliedRepositoryCreator instance. Aborting detection.");
        return;
    }
    for (final List<RepositoryView> repos : repoLists) {
        if (repos == null || repos.isEmpty()) {
            continue;
        }
        for (final RepositoryView repo : repos) {
            final ProjectVersionRef gav = job.pathInfo.getProjectId();
            try {
                if (config.isBlacklisted(repo.getUrl())) {
                    logger.debug("Discarding blacklisted repository: {}", repo);
                    continue;
                } else if (!config.isIncludeSnapshotRepos() && !repo.isReleasesEnabled()) {
                    logger.debug("Discarding snapshot repository: {}", repo);
                    continue;
                }
            } catch (final MalformedURLException e) {
                logger.error(String.format("Cannot add implied remote repo: %s from: %s (transfer: %s). Failed to check if repository is blacklisted.", repo.getUrl(), gav, job.transfer), e);
            }
            logger.debug("Detected POM-declared repository: {}", repo);
            RemoteRepository rr = null;
            try {
                rr = storeManager.query().packageType(MAVEN_PKG_KEY).getRemoteRepositoryByUrl(repo.getUrl());
            } catch (IndyDataException e) {
                logger.error(String.format("Cannot lookup remote repositories by URL: %s. Reason: %s", e.getMessage()), e);
            }
            if (rr == null) {
                logger.debug("Creating new RemoteRepository for: {}", repo);
                rr = creator.createFrom(gav, repo, LoggerFactory.getLogger(creator.getClass()));
                if (rr == null) {
                    logger.warn("ImpliedRepositoryCreator didn't create anything for repo: {}, specified in: {}. Skipping.", repo.getId(), gav);
                    continue;
                }
                rr.setMetadata(METADATA_ORIGIN, IMPLIED_REPO_ORIGIN);
                if (!remoteValidator.isValid(rr)) {
                    logger.warn("Implied repository to: {} is invalid! Repository created was: {}", repo.getUrl(), rr);
                    continue;
                }
                final String changelog = String.format("Adding remote repository: %s (url: %s, name: %s), which is implied by the POM: %s (at: %s/%s)", repo.getId(), repo.getUrl(), repo.getName(), gav, job.transfer.getLocation().getUri(), job.transfer.getPath());
                final ChangeSummary summary = new ChangeSummary(ChangeSummary.SYSTEM_USER, changelog);
                try {
                    final boolean result = storeManager.storeArtifactStore(rr, summary, true, false, new EventMetadata().set(StoreDataManager.EVENT_ORIGIN, IMPLIED_REPOS_DETECTION).set(IMPLIED_BY_POM_TRANSFER, job.transfer));
                    logger.debug("Stored new RemoteRepository: {}. (successful? {})", rr, result);
                    job.implied.add(rr);
                } catch (final IndyDataException e) {
                    logger.error(String.format("Cannot add implied remote repo: %s from: %s (transfer: %s). Failed to store new remote repository.", repo.getUrl(), gav, job.transfer), e);
                }
            } else {
                logger.debug("Found existing RemoteRepository: {}", rr);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) RepositoryView(org.commonjava.maven.galley.maven.model.view.RepositoryView) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) ArrayList(java.util.ArrayList) List(java.util.List) ChangeSummary(org.commonjava.indy.audit.ChangeSummary)

Example 48 with IndyDataException

use of org.commonjava.indy.data.IndyDataException 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 49 with IndyDataException

use of org.commonjava.indy.data.IndyDataException in project indy by Commonjava.

the class PromotionManager method rollbackGroupPromote.

public GroupPromoteResult rollbackGroupPromote(GroupPromoteResult result, String user) throws PromotionException {
    GroupPromoteRequest request = result.getRequest();
    if (!storeManager.hasArtifactStore(request.getSource())) {
        String error = String.format("No such source/member store: %s", request.getSource());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    Group target;
    try {
        target = (Group) storeManager.getArtifactStore(request.getTargetKey());
    } catch (IndyDataException e) {
        throw new PromotionException("Cannot retrieve target group: %s. Reason: %s", e, request.getTargetGroup(), e.getMessage());
    }
    if (target == null) {
        String error = String.format("No such target group: %s.", request.getTargetGroup());
        logger.warn(error);
        return new GroupPromoteResult(request, error);
    }
    if (target.getConstituents().contains(request.getSource())) {
        // give the preUpdate event a different object to compare vs. the original group.
        target = target.copyOf();
        target.removeConstituent(request.getSource());
        try {
            final ChangeSummary changeSummary = new ChangeSummary(user, "Removing " + request.getSource() + " from membership of group: " + target.getKey());
            storeManager.storeArtifactStore(target, changeSummary, false, true, new EventMetadata());
        } catch (IndyDataException e) {
            throw new PromotionException("Failed to store group: %s with additional member: %s. Reason: %s", e, target.getKey(), request.getSource(), e.getMessage());
        }
    } else {
        return new GroupPromoteResult(request, "Group: " + target.getKey() + " does not contain member: " + request.getSource());
    }
    return new GroupPromoteResult(request);
}
Also used : IndyDataException(org.commonjava.indy.data.IndyDataException) GroupPromoteRequest(org.commonjava.indy.promote.model.GroupPromoteRequest) Group(org.commonjava.indy.model.core.Group) GroupPromoteResult(org.commonjava.indy.promote.model.GroupPromoteResult) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 50 with IndyDataException

use of org.commonjava.indy.data.IndyDataException 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

IndyDataException (org.commonjava.indy.data.IndyDataException)75 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)50 StoreKey (org.commonjava.indy.model.core.StoreKey)30 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)26 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)24 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)24 Transfer (org.commonjava.maven.galley.model.Transfer)19 ArrayList (java.util.ArrayList)17 Group (org.commonjava.indy.model.core.Group)17 IOException (java.io.IOException)16 Logger (org.slf4j.Logger)16 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)14 HashSet (java.util.HashSet)10 List (java.util.List)7 StoreType (org.commonjava.indy.model.core.StoreType)7 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)7 HostedRepository (org.commonjava.indy.model.core.HostedRepository)6 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)6 ContentGenerator (org.commonjava.indy.content.ContentGenerator)5 InputStream (java.io.InputStream)4