<commit_before>/*-
 * Copyright (c) 2013-2014 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.xmvn.connector.ivy;

import static org.fedoraproject.xmvn.connector.ivy.IvyResolver.LazyDeployerProvider.deployer;
import static org.fedoraproject.xmvn.connector.ivy.IvyResolver.LazyResolverProvider.resolver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;

import org.apache.ivy.core.cache.ArtifactOrigin;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.DefaultArtifact;
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ArtifactDownloadReport;
import org.apache.ivy.core.report.DownloadReport;
import org.apache.ivy.core.report.DownloadStatus;
import org.apache.ivy.core.report.MetadataArtifactDownloadReport;
import org.apache.ivy.core.resolve.DownloadOptions;
import org.apache.ivy.core.resolve.ResolveData;
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
import org.apache.ivy.plugins.parser.ModuleDescriptorParser;
import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorParser;
import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
import org.apache.ivy.plugins.repository.Resource;
import org.apache.ivy.plugins.repository.file.FileRepository;
import org.apache.ivy.plugins.repository.file.FileResource;
import org.apache.ivy.plugins.resolver.AbstractResolver;
import org.apache.ivy.plugins.resolver.util.ResolvedResource;

import org.fedoraproject.xmvn.deployer.Deployer;
import org.fedoraproject.xmvn.deployer.DeploymentRequest;
import org.fedoraproject.xmvn.deployer.DeploymentResult;
import org.fedoraproject.xmvn.locator.IsolatedDeployer;
import org.fedoraproject.xmvn.locator.IsolatedResolver;
import org.fedoraproject.xmvn.locator.IsolatedXMvnServiceLocator;
import org.fedoraproject.xmvn.locator.XMvnHomeClassLoader;
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;
import org.fedoraproject.xmvn.resolver.Resolver;

/**
 * Resolve and publish Ivy artifacts by delegating most tasks to XMvn.
 * 
 * @author <NAME>
 */
public class IvyResolver
    extends AbstractResolver
{
    static class LazyLocatorProvider
    {
        static final IsolatedXMvnServiceLocator locator;

        static
        {
            @SuppressWarnings( "resource" ) XMvnHomeClassLoader realm =
                new XMvnHomeClassLoader( LazyLocatorProvider.class.getClassLoader() );
            realm.addJarDirectory( realm.getHome().resolve( "lib" ).resolve( "ivy" ) );
            locator = new IsolatedXMvnServiceLocator( realm );
        }
    }

    static class LazyResolverProvider
    {
        static final Resolver resolver = new IsolatedResolver( LazyLocatorProvider.locator );
    }

    static class LazyDeployerProvider
    {
        static final Deployer deployer = new IsolatedDeployer( LazyLocatorProvider.locator );
    }

    public IvyResolver()
    {
        setName( "XMvn" );
    }

    private static org.eclipse.aether.artifact.Artifact ivy2aether( ModuleRevisionId revision, String extension )
    {
        return new org.eclipse.aether.artifact.DefaultArtifact( revision.getOrganisation(), revision.getName(),
                                                                revision.getExtraAttribute( "classifier" ), extension,
                                                                revision.getRevision() );
    }

    private static org.eclipse.aether.artifact.Artifact ivy2aether( org.apache.ivy.core.module.descriptor.Artifact artifact )
    {
        return ivy2aether( artifact.getModuleRevisionId(), artifact.getExt() );
    }

    private static String resolvedVersion( ResolutionResult resolutionResult )
    {
        String version = resolutionResult.getCompatVersion();
        return version != null ? version : "SYSTEM";
    }

    private String resolveModuleVersion( ModuleDescriptor module )
    {
        for ( Artifact artifact : module.getAllArtifacts() )
        {
            ResolutionRequest request = new ResolutionRequest( ivy2aether( artifact ) );
            ResolutionResult result = resolver.resolve( request );
            if ( result.getArtifactFile() != null )
                return resolvedVersion( result );
        }

        return null;
    }<commit_msg>Map Ivy artifact types to Aether classifiers<commit_after>/*-
 * Copyright (c) 2013-2014 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fedoraproject.xmvn.connector.ivy;

import static org.fedoraproject.xmvn.connector.ivy.IvyResolver.LazyDeployerProvider.deployer;
import static org.fedoraproject.xmvn.connector.ivy.IvyResolver.LazyResolverProvider.resolver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;

import org.apache.ivy.core.cache.ArtifactOrigin;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.descriptor.DefaultArtifact;
import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ArtifactDownloadReport;
import org.apache.ivy.core.report.DownloadReport;
import org.apache.ivy.core.report.DownloadStatus;
import org.apache.ivy.core.report.MetadataArtifactDownloadReport;
import org.apache.ivy.core.resolve.DownloadOptions;
import org.apache.ivy.core.resolve.ResolveData;
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
import org.apache.ivy.plugins.parser.ModuleDescriptorParser;
import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorParser;
import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
import org.apache.ivy.plugins.repository.Resource;
import org.apache.ivy.plugins.repository.file.FileRepository;
import org.apache.ivy.plugins.repository.file.FileResource;
import org.apache.ivy.plugins.resolver.AbstractResolver;
import org.apache.ivy.plugins.resolver.util.ResolvedResource;

import org.fedoraproject.xmvn.deployer.Deployer;
import org.fedoraproject.xmvn.deployer.DeploymentRequest;
import org.fedoraproject.xmvn.deployer.DeploymentResult;
import org.fedoraproject.xmvn.locator.IsolatedDeployer;
import org.fedoraproject.xmvn.locator.IsolatedResolver;
import org.fedoraproject.xmvn.locator.IsolatedXMvnServiceLocator;
import org.fedoraproject.xmvn.locator.XMvnHomeClassLoader;
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;
import org.fedoraproject.xmvn.resolver.Resolver;

/**
 * Resolve and publish Ivy artifacts by delegating most tasks to XMvn.
 * 
 * @author <NAME>
 */
public class IvyResolver
    extends AbstractResolver
{
    static class LazyLocatorProvider
    {
        static final IsolatedXMvnServiceLocator locator;

        static
        {
            @SuppressWarnings( "resource" ) XMvnHomeClassLoader realm =
                new XMvnHomeClassLoader( LazyLocatorProvider.class.getClassLoader() );
            realm.addJarDirectory( realm.getHome().resolve( "lib" ).resolve( "ivy" ) );
            locator = new IsolatedXMvnServiceLocator( realm );
        }
    }

    static class LazyResolverProvider
    {
        static final Resolver resolver = new IsolatedResolver( LazyLocatorProvider.locator );
    }

    static class LazyDeployerProvider
    {
        static final Deployer deployer = new IsolatedDeployer( LazyLocatorProvider.locator );
    }

    public IvyResolver()
    {
        setName( "XMvn" );
    }

    private static org.eclipse.aether.artifact.Artifact ivy2aether( ModuleRevisionId revision, String extension )
    {
        String groupId = revision.getOrganisation();
        String artifactId = revision.getName();
        String classifier = revision.getExtraAttribute( "classifier" );
        String version = revision.getRevision();

        return new org.eclipse.aether.artifact.DefaultArtifact( groupId, artifactId, classifier, extension, version );
    }

    private static org.eclipse.aether.artifact.Artifact ivy2aether( org.apache.ivy.core.module.descriptor.Artifact artifact )
    {
        ModuleRevisionId revision = artifact.getModuleRevisionId();

        String groupId = revision.getOrganisation();
        String artifactId = revision.getName();
        String extension = artifact.getExt();
        String classifier = revision.getExtraAttribute( "classifier" );
        String version = revision.getRevision();
        String type = artifact.getType();

        if ( classifier == null || classifier.isEmpty() )
        {
            if ( extension.equals( "jar" ) && type.equals( "source" ) )
                classifier = "sources";
            else if ( extension.equals( "jar" ) && type.equals( "javadoc" ) )
                classifier = "javadoc";
        }

        return new org.eclipse.aether.artifact.DefaultArtifact( groupId, artifactId, classifier, extension, version );
    }

    private static String resolvedVersion( ResolutionResult resolutionResult )
    {
        String version = resolutionResult.getCompatVersion();
        return version != null ? version : "SYSTEM";
    }

    private String resolveModuleVersion( ModuleDescriptor module )
    {
        for ( Artifact artifact : module.getAllArtifacts() )
        {
            ResolutionRequest request = new ResolutionRequest( ivy2aether( artifact ) );
            ResolutionResult result = resolver.resolve( request );
            if ( result.getArtifactFile() != null )
                return resolvedVersion( result );
        }

        return null;
    }
{'fraction_non_alphanumeric': 0.09195715323166304, 'fraction_numerical': 0.0029956427015250544, 'mean_word_length': 3.660321489001692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004099', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3120, 'n_words': 661}