<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 npanday.executable.impl;

import npanday.executable.*;
import npanday.executable.compiler.CompilerRequirement;
import npanday.executable.compiler.CompilerConfig;
import npanday.executable.compiler.CompilerContext;
import npanday.executable.compiler.CompilerExecutable;
import npanday.vendor.*;
import npanday.vendor.IllegalStateException;
import npanday.registry.RepositoryRegistry;
import npanday.artifact.ArtifactContext;
import npanday.PathUtil;
import npanday.InitializationException;
import npanday.PlatformUnsupportedException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.Artifact;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;

/**
 * Provides an implementation of <code>NetExecutableFactory</code>.
 *
 * @author <NAME>
 */
public class NetExecutableFactoryImpl
    implements NetExecutableFactory, LogEnabled
{

    /**
     * The capability matcher
     */
    private CapabilityMatcher capabilityMatcher;

    private ArtifactContext artifactContext;

    private RepositoryExecutableContext repositoryExecutableContext;

    private ExecutableContext executableContext;

    private CompilerContext compilerContext;

    private RepositoryRegistry repositoryRegistry;

    private VendorInfoRepository vendorInfoRepository;

    private StateMachineProcessor processor;

    /**
     * A logger for writing log messages
     */
    private Logger logger;

    /**
     * @see LogEnabled#enableLogging(org.codehaus.plexus.logging.Logger)
     */
    public void enableLogging( Logger logger )
    {
        this.logger = logger;
    }

    /**
     * @see NetExecutableFactory#getCompilerExecutableFor(npanday.executable.compiler.CompilerRequirement,
     *      npanday.executable.compiler.CompilerConfig, org.apache.maven.project.MavenProject, java.io.File)
     */
    public CompilerExecutable getCompilerExecutableFor( CompilerRequirement compilerRequirement,
                                                        CompilerConfig compilerConfig, MavenProject project,
                                                        File assemblyPath )
        throws PlatformUnsupportedException
    {
        File targetDir = PathUtil.getPrivateApplicationBaseDirectory( project );

        VendorInfo vendorInfo = VendorInfo.Factory.createDefaultVendorInfo();
        vendorInfo.setVendorVersion( compilerRequirement.getVendorVersion() );
        vendorInfo.setFrameworkVersion( compilerRequirement.getFrameworkVersion() );
        vendorInfo.setVendor( compilerRequirement.getVendor() );
        try
        {
            logger.debug("NPANDAY-066-025 - Try to find executable for vendor:" + vendorInfo + ":processor:" + processor);        
            processor.process( vendorInfo );
        }
        catch ( IllegalStateException e )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-011: Illegal State: Vendor Info = " + vendorInfo, e );
        }

        if ( vendorInfo.getVendor() == null || vendorInfo.getFrameworkVersion() == null )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-012: Missing Vendor Information: " + vendorInfo );
        }

        logger.info( "NPANDAY-066-013: Found Vendor = " + vendorInfo );
        compilerRequirement.setVendor( vendorInfo.getVendor() );
        compilerRequirement.setVendorVersion( vendorInfo.getVendorVersion() );
        compilerRequirement.setFrameworkVersion( vendorInfo.getFrameworkVersion() );

        // init does not need the executable paths to be set
        compilerContext.init( compilerRequirement, compilerConfig, project, capabilityMatcher );

        if ( assemblyPath != null )
        {
            compilerContext.getCompilerCapability().setAssemblyPath( assemblyPath.getAbsolutePath() );
        }

        List<String> executionPaths = ( compilerConfig.getExecutionPaths() == null ) ? new ArrayList<String>()
            : compilerConfig.getExecutionPaths();
        if (executionPaths == null  || executionPaths.size() == 0 )
        {
            if (vendorInfo.getExecutablePaths() != null) {
                for(File path : vendorInfo.getExecutablePaths()){
                    executionPaths.add(path.getAbsolutePath());
                }
            }


            String netDependencyId = compilerContext.getCompilerCapability().getNetDependencyId();

            if ( netDependencyId != null )
            {
                Artifact artifact = artifactContext.getArtifactByID( netDependencyId );
                if ( artifact != null )
                {
                    File artifactPath = PathUtil.getPrivateApplicationBaseFileFor( artifact,
                                                                                   compilerConfig.getLocalRepository(),
                                                                                   targetDir );
                    executionPaths.add( artifactPath.getParentFile().getAbsolutePath() );
                }
            }

            compilerConfig.setExecutionPaths( executionPaths );
        }

        try
        {
            return compilerContext.getCompilerExecutable();
        }
        catch ( ExecutionException e )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-007: Unable to find net executable", e );
        }
    }

    /**
     * @see NetExecutableFactory#getPluginLoaderFor(String, String, npanday.vendor.VendorInfo, String, java.io.File, String, java.io.File)
     */
    public NetExecutable getPluginLoaderFor( String groupId, String artifactId, VendorInfo vendorInfo,
                                             String localRepository, File parameterFile, String mojoName,
                                             File targetDir )
        throws PlatformUnsupportedException
    {
        Artifact artifact = getArtifactFor(groupId, artifactId);
        return getPluginLoaderFor(artifact, vendorInfo, localRepository, parameterFile, mojoName, targetDir );
    }

    public NetExecutable getPluginLoaderFor( Artifact artifact, VendorInfo vendorInfo, String localRepository, File parameterFile, String mojoName,
                                             File targetDir ) throws PlatformUnsupportedException {
        //AssemblyRepositoryLayout layout = new AssemblyRepositoryLayout();
        File artifactPath = PathUtil.getPrivateApplicationBaseFileFor( artifact, new File( localRepository ), targetDir );

        List<String> commands = new ArrayList<String>();
        commands.add( "parameterFile=" + parameterFile.getAbsolutePath() );
        commands.add( "assemblyFile=" + artifactPath.getAbsolutePath() );
        commands.add( "mojoName=" + mojoName );//ArtifactId = namespace

        // make sure plugin artifact is present to run with in the application base
        Artifact pluginArtifact = artifactContext.getArtifactsFor( "org.apache.npanday.plugins", "NPanday.Plugin", null,
                                                                   null ).get( 0 );
        PathUtil.getPrivateApplicationBaseFileFor( pluginArtifact, new File( localRepository ), targetDir );
<commit_msg>[NPANDAY-410] make sure NPanday.Model.Pom is now present at execution time<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 npanday.executable.impl;

import npanday.executable.*;
import npanday.executable.compiler.CompilerRequirement;
import npanday.executable.compiler.CompilerConfig;
import npanday.executable.compiler.CompilerContext;
import npanday.executable.compiler.CompilerExecutable;
import npanday.vendor.*;
import npanday.vendor.IllegalStateException;
import npanday.registry.RepositoryRegistry;
import npanday.artifact.ArtifactContext;
import npanday.PathUtil;
import npanday.InitializationException;
import npanday.PlatformUnsupportedException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.Artifact;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;

/**
 * Provides an implementation of <code>NetExecutableFactory</code>.
 *
 * @author <NAME>
 */
public class NetExecutableFactoryImpl
    implements NetExecutableFactory, LogEnabled
{

    /**
     * The capability matcher
     */
    private CapabilityMatcher capabilityMatcher;

    private ArtifactContext artifactContext;

    private RepositoryExecutableContext repositoryExecutableContext;

    private ExecutableContext executableContext;

    private CompilerContext compilerContext;

    private RepositoryRegistry repositoryRegistry;

    private VendorInfoRepository vendorInfoRepository;

    private StateMachineProcessor processor;

    /**
     * A logger for writing log messages
     */
    private Logger logger;

    /**
     * @see LogEnabled#enableLogging(org.codehaus.plexus.logging.Logger)
     */
    public void enableLogging( Logger logger )
    {
        this.logger = logger;
    }

    /**
     * @see NetExecutableFactory#getCompilerExecutableFor(npanday.executable.compiler.CompilerRequirement,
     *      npanday.executable.compiler.CompilerConfig, org.apache.maven.project.MavenProject, java.io.File)
     */
    public CompilerExecutable getCompilerExecutableFor( CompilerRequirement compilerRequirement,
                                                        CompilerConfig compilerConfig, MavenProject project,
                                                        File assemblyPath )
        throws PlatformUnsupportedException
    {
        File targetDir = PathUtil.getPrivateApplicationBaseDirectory( project );

        VendorInfo vendorInfo = VendorInfo.Factory.createDefaultVendorInfo();
        vendorInfo.setVendorVersion( compilerRequirement.getVendorVersion() );
        vendorInfo.setFrameworkVersion( compilerRequirement.getFrameworkVersion() );
        vendorInfo.setVendor( compilerRequirement.getVendor() );
        try
        {
            logger.debug("NPANDAY-066-025 - Try to find executable for vendor:" + vendorInfo + ":processor:" + processor);        
            processor.process( vendorInfo );
        }
        catch ( IllegalStateException e )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-011: Illegal State: Vendor Info = " + vendorInfo, e );
        }

        if ( vendorInfo.getVendor() == null || vendorInfo.getFrameworkVersion() == null )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-012: Missing Vendor Information: " + vendorInfo );
        }

        logger.info( "NPANDAY-066-013: Found Vendor = " + vendorInfo );
        compilerRequirement.setVendor( vendorInfo.getVendor() );
        compilerRequirement.setVendorVersion( vendorInfo.getVendorVersion() );
        compilerRequirement.setFrameworkVersion( vendorInfo.getFrameworkVersion() );

        // init does not need the executable paths to be set
        compilerContext.init( compilerRequirement, compilerConfig, project, capabilityMatcher );

        if ( assemblyPath != null )
        {
            compilerContext.getCompilerCapability().setAssemblyPath( assemblyPath.getAbsolutePath() );
        }

        List<String> executionPaths = ( compilerConfig.getExecutionPaths() == null ) ? new ArrayList<String>()
            : compilerConfig.getExecutionPaths();
        if (executionPaths == null  || executionPaths.size() == 0 )
        {
            if (vendorInfo.getExecutablePaths() != null) {
                for(File path : vendorInfo.getExecutablePaths()){
                    executionPaths.add(path.getAbsolutePath());
                }
            }


            String netDependencyId = compilerContext.getCompilerCapability().getNetDependencyId();

            if ( netDependencyId != null )
            {
                Artifact artifact = artifactContext.getArtifactByID( netDependencyId );
                if ( artifact != null )
                {
                    File artifactPath = PathUtil.getPrivateApplicationBaseFileFor( artifact,
                                                                                   compilerConfig.getLocalRepository(),
                                                                                   targetDir );
                    executionPaths.add( artifactPath.getParentFile().getAbsolutePath() );
                }
            }

            compilerConfig.setExecutionPaths( executionPaths );
        }

        try
        {
            return compilerContext.getCompilerExecutable();
        }
        catch ( ExecutionException e )
        {
            throw new PlatformUnsupportedException( "NPANDAY-066-007: Unable to find net executable", e );
        }
    }

    /**
     * @see NetExecutableFactory#getPluginLoaderFor(String, String, npanday.vendor.VendorInfo, String, java.io.File, String, java.io.File)
     */
    public NetExecutable getPluginLoaderFor( String groupId, String artifactId, VendorInfo vendorInfo,
                                             String localRepository, File parameterFile, String mojoName,
                                             File targetDir )
        throws PlatformUnsupportedException
    {
        Artifact artifact = getArtifactFor(groupId, artifactId);
        return getPluginLoaderFor(artifact, vendorInfo, localRepository, parameterFile, mojoName, targetDir );
    }

    public NetExecutable getPluginLoaderFor( Artifact artifact, VendorInfo vendorInfo, String localRepository, File parameterFile, String mojoName,
                                             File targetDir ) throws PlatformUnsupportedException {
        //AssemblyRepositoryLayout layout = new AssemblyRepositoryLayout();
        File artifactPath = PathUtil.getPrivateApplicationBaseFileFor( artifact, new File( localRepository ), targetDir );

        List<String> commands = new ArrayList<String>();
        commands.add( "parameterFile=" + parameterFile.getAbsolutePath() );
        commands.add( "assemblyFile=" + artifactPath.getAbsolutePath() );
        commands.add( "mojoName=" + mojoName );//ArtifactId = namespace

        // make sure plugin artifact is present to run with in the application base
        // TODO: can we do this transitively from the loader or the plugin artifact? Hardcoding the transitive deps here
        Artifact modelArtifact = artifactContext.getArtifactsFor( "org.apache.npanday", "NPanday.Model.Pom", null,
                                                                   null ).get( 0 );
        PathUtil.getPrivateApplicationBaseFileFor( modelArtifact, new File( localRepository ), targetDir );
        Artifact pluginArtifact = artifactContext.getArtifactsFor( "org.apache.npanday.plugins", "NPanday.Plugin", null,
                                                                   null ).get( 0 );
        PathUtil.getPrivateApplicationBaseFileFor( pluginArtifact, new File( localRepository ), targetDir );

{'fraction_non_alphanumeric': 0.07855384061017356, 'fraction_numerical': 0.004564290433007026, 'mean_word_length': 2.608234019501625, 'pattern_counts': {'":': 2, '<': 17, '<?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': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7396786', 'n_tokens_mistral': 4032, 'n_tokens_neox': 3865, 'n_words': 1006}