<commit_before>package org.apache.maven.continuum.updater;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import org.apache.maven.continuum.updater.exception.UpdaterException;
import org.apache.maven.continuum.updater.model.UpdaterModel;
import org.apache.maven.continuum.updater.model.Version;
import org.apache.maven.continuum.updater.model.io.xpp3.ContinuumUpdaterXpp3Reader;
import org.apache.maven.continuum.updater.util.WagonManager;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.FileUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

/**
 * @plexus.component
 *   role="org.apache.maven.continuum.updater.UpdaterManager"
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class DefaultUpdaterManager
    extends AbstractLogEnabled
    implements UpdaterManager, Contextualizable
{
    /**
     * @plexus.requirement
     *   role="org.apache.maven.continuum.updater.Updater"
     */
    private Map updaters;

    /**
     * @plexus.requirement
     *   role="org.apache.maven.continuum.updater.util.WagonManager"
     */
    private WagonManager wagonManager;

    private PlexusContainer container;

    private File newContinuumFile;

    public void execute( String userVersion, File continuumHome )
        throws UpdaterException
    {
        InputStream confAsStream;

        confAsStream = DefaultUpdaterManager.class.getClassLoader().getResourceAsStream( "updater.xml" );

        InputStreamReader reader = new InputStreamReader( confAsStream );

        ContinuumUpdaterXpp3Reader xpp3Reader = new ContinuumUpdaterXpp3Reader();

        UpdaterModel model = null;

        try
        {
            model = xpp3Reader.read( reader );
        }
        catch ( Exception e )
        {
            throw new UpdaterException( "Can't find updater configuration file.", e );
        }

        boolean versionFounded = false;

        newContinuumFile = downloadContinuum( model, continuumHome );

        for( Iterator i = model.getVersions().iterator(); i.hasNext(); )
        {
            Version v = (Version) i.next();

            if ( v.getName().equals( userVersion ) )
            {
                versionFounded = true;
            }

            if ( versionFounded && v.getComponentRole() != null )
            {
                update( userVersion, v, continuumHome );
            }
        }

        if ( !versionFounded )
        {
            getLogger().warn( "There are no updater for your version(" + userVersion + ")\n" + getAvailableVersions( model ) );
        }
    }

    private String getAvailableVersions( UpdaterModel model )
    {
        StringBuffer sb = new StringBuffer();

        for( Iterator i = model.getVersions().iterator(); i.hasNext(); )
        {
            Version v = (Version) i.next();

            if ( v.getComponentRole() != null )
            {
                if ( sb.length() != 0 )
                {
                    sb.append( "\n" );
                }

                sb.append( v.getName() );
            }
        }

        return sb.toString();
    }

    private File downloadContinuum( UpdaterModel model, File continuumHome )
        throws UpdaterException
    {
        File downloadedFile = null;

        try
        {
            String latestUpdaterComponentRole = ( (Version) model.getVersions().get( model.getVersions().size() - 2 ) )
                .getComponentRole();

            Updater latestUpdater = (Updater) updaters.get( latestUpdaterComponentRole );

            URL url = new URL( latestUpdater.getReleaseUrl() );

            String filename = url.getFile().substring( url.getFile().lastIndexOf( "/" ) + 1 );

            downloadedFile = new File( continuumHome.getParentFile(), filename );

            getLogger().info( "==> Download " + url + " in " + downloadedFile.getAbsolutePath() );

            if ( downloadedFile.exists() )
            {
                downloadedFile.delete();
            }

            wagonManager.getFile( url, downloadedFile, "warn" );
        }
        catch ( Exception e )
        {
            throw new UpdaterException( "Can't download the new release", e );
        }

        return downloadedFile;
    }

    private void update( String userVersion, Version version, File continuumHome )
        throws UpdaterException
    {
        getLogger().info( "************************************************************************" );
        getLogger().info( "Update version " + version.getName() + " with component " + version.getComponentRole() );
        getLogger().info( "************************************************************************" );

        Updater updater = (Updater) updaters.get( version.getComponentRole() );

        if ( updater == null )
        {
            throw new UpdaterException( "Updater " + version.getComponentRole() + " doesn't exist." );
        }

        try
        {
            if ( userVersion.equals( version.getName() ) )
            {
                backup( continuumHome, userVersion );

                updateContinuumFiles( continuumHome );
            }

            getLogger().info( "==> Update database" );

            updater.updateDatabase();
        }
        catch( UpdaterException e )
        {
            e.printStackTrace();

            restore( continuumHome, userVersion );

            System.exit( 1 );
        }
    }

    private void backup( File continuumHome, String userVersion )
        throws UpdaterException
    {
        File backupDir = new File( continuumHome.getParentFile(), "continuum-" + userVersion );

        getLogger().info( "==> Backup Continuum " + userVersion + " to " + backupDir.getAbsolutePath() );

        try
        {
            FileUtils.deleteDirectory( backupDir);

            FileUtils.copyDirectoryStructure( continuumHome, backupDir);
        }
        catch( IOException e )
        {
            throw new UpdaterException( "Can't create a continuum backup.", e );
        }
    }

    private void restore( File continuumHome, String userVersion )
        throws UpdaterException
    {
        getLogger().info( "==> Restore Continuum " + userVersion );

        try
        {
            FileUtils.cleanDirectory( continuumHome );

            FileUtils.copyDirectoryStructure( new File( continuumHome.getParentFile(), "continuum-" + userVersion ),
                                              continuumHome );
        }
        catch( IOException e )
        {
            throw new UpdaterException( "Can't restore continuum.", e );
        }
    }

    private void updateContinuumFiles( File continuumHome )
        throws UpdaterException
    {
        getLogger().info( "==> Update Continuum files." );

        try
        {
            FileUtils.cleanDirectory( continuumHome );

            // Extract new continuum version
            UnArchiver unArchiver = (UnArchiver) container.lookup( UnArchiver.ROLE,
                                                                   FileUtils.extension( newContinuumFile.getName() ) );

            unArchiver.setSourceFile( newContinuumFile );
<commit_msg>download file for all versions<commit_after>package org.apache.maven.continuum.updater;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * 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.
 */

import org.apache.maven.continuum.updater.exception.UpdaterException;
import org.apache.maven.continuum.updater.model.UpdaterModel;
import org.apache.maven.continuum.updater.model.Version;
import org.apache.maven.continuum.updater.model.io.xpp3.ContinuumUpdaterXpp3Reader;
import org.apache.maven.continuum.updater.util.WagonManager;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.FileUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.File;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

/**
 * @plexus.component
 *   role="org.apache.maven.continuum.updater.UpdaterManager"
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class DefaultUpdaterManager
    extends AbstractLogEnabled
    implements UpdaterManager, Contextualizable
{
    /**
     * @plexus.requirement
     *   role="org.apache.maven.continuum.updater.Updater"
     */
    private Map updaters;

    /**
     * @plexus.requirement
     *   role="org.apache.maven.continuum.updater.util.WagonManager"
     */
    private WagonManager wagonManager;

    private PlexusContainer container;

    public void execute( String userVersion, File continuumHome )
        throws UpdaterException
    {
        InputStream confAsStream;

        confAsStream = DefaultUpdaterManager.class.getClassLoader().getResourceAsStream( "updater.xml" );

        InputStreamReader reader = new InputStreamReader( confAsStream );

        ContinuumUpdaterXpp3Reader xpp3Reader = new ContinuumUpdaterXpp3Reader();

        UpdaterModel model = null;

        try
        {
            model = xpp3Reader.read( reader );
        }
        catch ( Exception e )
        {
            throw new UpdaterException( "Can't find updater configuration file.", e );
        }

        boolean versionFounded = false;

        for( Iterator i = model.getVersions().iterator(); i.hasNext(); )
        {
            Version v = (Version) i.next();

            if ( v.getName().equals( userVersion ) )
            {
                versionFounded = true;
            }

            if ( versionFounded && v.getComponentRole() != null )
            {
                update( userVersion, v, continuumHome, model );
            }
        }

        if ( !versionFounded )
        {
            getLogger().warn( "There are no updater for your version(" + userVersion + ")\n" + getAvailableVersions( model ) );
        }
    }

    private String getAvailableVersions( UpdaterModel model )
    {
        StringBuffer sb = new StringBuffer();

        for( Iterator i = model.getVersions().iterator(); i.hasNext(); )
        {
            Version v = (Version) i.next();

            if ( v.getComponentRole() != null )
            {
                if ( sb.length() != 0 )
                {
                    sb.append( "\n" );
                }

                sb.append( v.getName() );
            }
        }

        return sb.toString();
    }

    private File downloadContinuum( UpdaterModel model, File continuumHome )
        throws UpdaterException
    {
        File downloadedFile = null;

        try
        {
            String latestUpdaterComponentRole = ( (Version) model.getVersions().get( model.getVersions().size() - 2 ) )
                .getComponentRole();

            Updater latestUpdater = (Updater) updaters.get( latestUpdaterComponentRole );

            URL url = new URL( latestUpdater.getReleaseUrl() );

            String filename = url.getFile().substring( url.getFile().lastIndexOf( "/" ) + 1 );

            downloadedFile = new File( continuumHome.getParentFile(), filename );

            getLogger().info( "==> Download " + url + " in " + downloadedFile.getAbsolutePath() );

            if ( downloadedFile.exists() )
            {
                downloadedFile.delete();
            }

            wagonManager.getFile( url, downloadedFile, "warn" );
        }
        catch ( Exception e )
        {
            throw new UpdaterException( "Can't download the new release", e );
        }

        return downloadedFile;
    }

    private void update( String userVersion, Version version, File continuumHome, UpdaterModel model )
        throws UpdaterException
    {
        getLogger().info( "************************************************************************" );
        getLogger().info( "Update version " + version.getName() + " with component " + version.getComponentRole() );
        getLogger().info( "************************************************************************" );

        Updater updater = (Updater) updaters.get( version.getComponentRole() );

        if ( updater == null )
        {
            throw new UpdaterException( "Updater " + version.getComponentRole() + " doesn't exist." );
        }

        try
        {
            if ( userVersion.equals( version.getName() ) )
            {
                backup( continuumHome, userVersion );
            }

            updateContinuumFiles( continuumHome, model );

            getLogger().info( "==> Update database" );

            updater.updateDatabase();
        }
        catch( UpdaterException e )
        {
            e.printStackTrace();

            restore( continuumHome, userVersion );

            System.exit( 1 );
        }
    }

    private void backup( File continuumHome, String userVersion )
        throws UpdaterException
    {
        File backupDir = new File( continuumHome.getParentFile(), "continuum-" + userVersion );

        getLogger().info( "==> Backup Continuum " + userVersion + " to " + backupDir.getAbsolutePath() );

        try
        {
            FileUtils.deleteDirectory( backupDir);

            FileUtils.copyDirectoryStructure( continuumHome, backupDir);
        }
        catch( IOException e )
        {
            throw new UpdaterException( "Can't create a continuum backup.", e );
        }
    }

    private void restore( File continuumHome, String userVersion )
        throws UpdaterException
    {
        getLogger().info( "==> Restore Continuum " + userVersion );

        try
        {
            FileUtils.cleanDirectory( continuumHome );

            FileUtils.copyDirectoryStructure( new File( continuumHome.getParentFile(), "continuum-" + userVersion ),
                                              continuumHome );
        }
        catch( IOException e )
        {
            throw new UpdaterException( "Can't restore continuum.", e );
        }
    }

    private void updateContinuumFiles( File continuumHome, UpdaterModel model )
        throws UpdaterException
    {
        File newContinuumFile = downloadContinuum( model, continuumHome );

        getLogger().info( "==> Update Continuum files." );

        try
        {
            FileUtils.cleanDirectory( continuumHome );

            // Extract new continuum version
            UnArchiver unArchiver = (UnArchiver) container.lookup( UnArchiver.ROLE,
                                                                   FileUtils.extension( newContinuumFile.getName() ) );

            unArchiver.setSourceFile( newContinuumFile );

{'fraction_non_alphanumeric': 0.11724853515625, 'fraction_numerical': 0.002685546875, 'mean_word_length': 2.2343071456770627, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4235168', 'n_tokens_mistral': 4387, 'n_tokens_neox': 4230, 'n_words': 998}