<commit_before>/*-
 * Copyright (c) 2014-2016 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.resolver.impl;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.fedoraproject.xmvn.artifact.Artifact;
import org.fedoraproject.xmvn.config.Configurator;
import org.fedoraproject.xmvn.config.ResolverSettings;
import org.fedoraproject.xmvn.metadata.ArtifactMetadata;
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;
import org.fedoraproject.xmvn.resolver.Resolver;
import org.fedoraproject.xmvn.utils.AtomicFileCounter;

/**
 * Default implementation of XMvn {@code Resolver} interface.
 * <p>
 * <strong>WARNING</strong>: This class is part of internal implementation of XMvn and it is marked as public only for
 * technical reasons. This class is not part of XMvn API. Client code using XMvn should <strong>not</strong> reference
 * it directly.
 * 
 * @author <NAME>
 */
@Named
@Singleton
public class DefaultResolver
    implements Resolver
{
    private final Logger logger = LoggerFactory.getLogger( DefaultResolver.class );

    private final MetadataResolver metadataResolver;

    private static final RpmDb RPMDB = new RpmDb();

    private final Resolver localRepoResolver;

    private final EffectivePomGenerator pomGenerator;

    private final CacheManager cacheManager;

    private final MockAgent mockAgent;

    private final AtomicFileCounter counter;

    @Inject
    public DefaultResolver( @Named( "local-repo" ) Resolver localRepoResolver, Configurator configurator )
    {
        this.localRepoResolver = localRepoResolver;

        ResolverSettings settings = configurator.getConfiguration().getResolverSettings();
        metadataResolver = new MetadataResolver( settings.getMetadataRepositories() );
        pomGenerator = new EffectivePomGenerator();
        cacheManager = new CacheManager();
        mockAgent = new MockAgent();

        String bisectCounterPath = System.getProperty( "xmvn.bisect.counter" );
        counter = Strings.isNullOrEmpty( bisectCounterPath ) ? null : new AtomicFileCounter( bisectCounterPath );
    }

    @Override
    public ResolutionResult resolve( ResolutionRequest request )
    {
        if ( counter != null && counter.tryDecrement() > 0 )
            return new DefaultResolutionResult();

        Properties properties = new Properties();
        properties.putAll( System.getProperties() );

        ResolutionResult localRepoResult = localRepoResolver.resolve( request );
        if ( localRepoResult.getArtifactPath() != null )
            return localRepoResult;

        Artifact artifact = request.getArtifact();
        logger.debug( "Trying to resolve artifact {}", artifact );

        String compatVersion;
        ArtifactMetadata metadata = metadataResolver.resolveArtifactMetadata( artifact );

        if ( metadata == null )
        {
            metadata = metadataResolver.resolveArtifactMetadata( artifact.setVersion( Artifact.DEFAULT_VERSION ) );
            compatVersion = null;
        }
        else
        {<commit_msg>[refactor] Rename counter to bisectCounter<commit_after>/*-
 * Copyright (c) 2014-2016 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.resolver.impl;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.fedoraproject.xmvn.artifact.Artifact;
import org.fedoraproject.xmvn.config.Configurator;
import org.fedoraproject.xmvn.config.ResolverSettings;
import org.fedoraproject.xmvn.metadata.ArtifactMetadata;
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;
import org.fedoraproject.xmvn.resolver.Resolver;
import org.fedoraproject.xmvn.utils.AtomicFileCounter;

/**
 * Default implementation of XMvn {@code Resolver} interface.
 * <p>
 * <strong>WARNING</strong>: This class is part of internal implementation of XMvn and it is marked as public only for
 * technical reasons. This class is not part of XMvn API. Client code using XMvn should <strong>not</strong> reference
 * it directly.
 * 
 * @author <NAME>
 */
@Named
@Singleton
public class DefaultResolver
    implements Resolver
{
    private final Logger logger = LoggerFactory.getLogger( DefaultResolver.class );

    private final MetadataResolver metadataResolver;

    private static final RpmDb RPMDB = new RpmDb();

    private final Resolver localRepoResolver;

    private final EffectivePomGenerator pomGenerator;

    private final CacheManager cacheManager;

    private final MockAgent mockAgent;

    private final AtomicFileCounter bisectCounter;

    @Inject
    public DefaultResolver( @Named( "local-repo" ) Resolver localRepoResolver, Configurator configurator )
    {
        this.localRepoResolver = localRepoResolver;

        ResolverSettings settings = configurator.getConfiguration().getResolverSettings();
        metadataResolver = new MetadataResolver( settings.getMetadataRepositories() );
        pomGenerator = new EffectivePomGenerator();
        cacheManager = new CacheManager();
        mockAgent = new MockAgent();

        String bisectCounterPath = System.getProperty( "xmvn.bisect.counter" );
        bisectCounter = Strings.isNullOrEmpty( bisectCounterPath ) ? null : new AtomicFileCounter( bisectCounterPath );
    }

    @Override
    public ResolutionResult resolve( ResolutionRequest request )
    {
        if ( bisectCounter != null && bisectCounter.tryDecrement() > 0 )
            return new DefaultResolutionResult();

        Properties properties = new Properties();
        properties.putAll( System.getProperties() );

        ResolutionResult localRepoResult = localRepoResolver.resolve( request );
        if ( localRepoResult.getArtifactPath() != null )
            return localRepoResult;

        Artifact artifact = request.getArtifact();
        logger.debug( "Trying to resolve artifact {}", artifact );

        String compatVersion;
        ArtifactMetadata metadata = metadataResolver.resolveArtifactMetadata( artifact );

        if ( metadata == null )
        {
            metadata = metadataResolver.resolveArtifactMetadata( artifact.setVersion( Artifact.DEFAULT_VERSION ) );
            compatVersion = null;
        }
        else
        {
{'fraction_non_alphanumeric': 0.08288060644346178, 'fraction_numerical': 0.003790271636133923, 'mean_word_length': 3.76580373269115, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2374198', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2113, 'n_words': 620}