<commit_before>package org.apache.maven.archiva.configuration;

/*
 * 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.
 */

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Reader;
import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Writer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Load and store the configuration. No synchronization is used, but it is unnecessary as the old configuration object
 * can continue to be used.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
 * @todo would be good to monitor the store file for changes
 * @todo support other implementations than XML file
 * @plexus.component
 */
public class DefaultConfigurationStore
    extends AbstractLogEnabled
    implements ConfigurationStore
{
    /**
     * @plexus.configuration default-value="${configuration.store.file}"
     */
    private File file;

    /**
     * The cached configuration.
     */
    private Configuration configuration;

    /**
     * List of listeners to configuration changes.
     */
    private List/*<ConfigurationChangeListener>*/ listeners = new LinkedList();

    public Configuration getConfigurationFromStore()
        throws ConfigurationStoreException
    {
        if ( configuration == null )
        {
            ConfigurationXpp3Reader reader = new ConfigurationXpp3Reader();

            if ( file == null )
            {
                file = new File( System.getProperty( "user.home" ), "/.m2/archiva.xml" );

                // migration for those with the old file
                if ( !file.exists() )
                {
                    File file = new File( System.getProperty( "user.home" ), "/.m2/repository-manager.xml" );
                    if ( file.exists() )
                    {
                        getLogger().info( "Migrating " + file + " to " + this.file );
                        file.renameTo( this.file );
                    }
                }
            }

            FileReader fileReader;
            try
            {
                fileReader = new FileReader( file );
            }
            catch ( FileNotFoundException e )
            {
                getLogger().warn( "Configuration file: " + file + " not found. Using defaults." );
                configuration = new Configuration();
                return configuration;
            }

            getLogger().info( "Reading configuration from " + file );
            try
            {
                configuration = reader.read( fileReader, false );
                sanitizeConfiguration( configuration );
            }
            catch ( IOException e )
            {
                throw new ConfigurationStoreException( e.getMessage(), e );
            }
            catch ( XmlPullParserException e )
            {
                throw new ConfigurationStoreException( e.getMessage(), e );
            }
            finally
            {
                IOUtils.closeQuietly( fileReader );
            }
        }
        return configuration;
    }

    /**
     * Perform any Upgrades and Adjustments needed to bring configuration up to the
     * current configuration format.
     *
     * @param config the configuration to upgrade and adjust.
     */
    private void sanitizeConfiguration( Configuration config )
    {
        Iterator it = config.getRepositories().iterator();
        while ( it.hasNext() )
        {
            RepositoryConfiguration repo = (RepositoryConfiguration) it.next();

            // Ensure that the repo.urlName is set.
            if ( StringUtils.isEmpty( repo.getUrlName() ) )
            {
                repo.setUrlName( repo.getId() );
            }
        }
    }

    public void storeConfiguration( Configuration configuration )
        throws ConfigurationStoreException, InvalidConfigurationException, ConfigurationChangeException
    {
        for ( Iterator i = listeners.iterator(); i.hasNext(); )
        {
            ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();

            listener.notifyOfConfigurationChange( configuration );
        }

        ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();

        getLogger().info( "Writing configuration to " + file );
        FileWriter fileWriter = null;
        try
        {
            file.getParentFile().mkdirs();

            fileWriter = new FileWriter( file );
            writer.write( fileWriter, configuration );
        }
        catch ( IOException e )
        {
            throw new ConfigurationStoreException( e.getMessage(), e );
        }
        finally
        {
            IOUtils.closeQuietly( fileWriter );
        }<commit_msg>Fix NPE when file is a root file<commit_after>package org.apache.maven.archiva.configuration;

/*
 * 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.
 */

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Reader;
import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Writer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Load and store the configuration. No synchronization is used, but it is unnecessary as the old configuration object
 * can continue to be used.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
 * @todo would be good to monitor the store file for changes
 * @todo support other implementations than XML file
 * @plexus.component
 */
public class DefaultConfigurationStore
    extends AbstractLogEnabled
    implements ConfigurationStore
{
    /**
     * @plexus.configuration default-value="${configuration.store.file}"
     */
    private File file;

    /**
     * The cached configuration.
     */
    private Configuration configuration;

    /**
     * List of listeners to configuration changes.
     */
    private List/*<ConfigurationChangeListener>*/ listeners = new LinkedList();

    public Configuration getConfigurationFromStore()
        throws ConfigurationStoreException
    {
        if ( configuration == null )
        {
            ConfigurationXpp3Reader reader = new ConfigurationXpp3Reader();

            if ( file == null )
            {
                file = new File( System.getProperty( "user.home" ), "/.m2/archiva.xml" );

                // migration for those with the old file
                if ( !file.exists() )
                {
                    File file = new File( System.getProperty( "user.home" ), "/.m2/repository-manager.xml" );
                    if ( file.exists() )
                    {
                        getLogger().info( "Migrating " + file + " to " + this.file );
                        file.renameTo( this.file );
                    }
                }
            }

            FileReader fileReader;
            try
            {
                fileReader = new FileReader( file );
            }
            catch ( FileNotFoundException e )
            {
                getLogger().warn( "Configuration file: " + file + " not found. Using defaults." );
                configuration = new Configuration();
                return configuration;
            }

            getLogger().info( "Reading configuration from " + file );
            try
            {
                configuration = reader.read( fileReader, false );
                sanitizeConfiguration( configuration );
            }
            catch ( IOException e )
            {
                throw new ConfigurationStoreException( e.getMessage(), e );
            }
            catch ( XmlPullParserException e )
            {
                throw new ConfigurationStoreException( e.getMessage(), e );
            }
            finally
            {
                IOUtils.closeQuietly( fileReader );
            }
        }
        return configuration;
    }

    /**
     * Perform any Upgrades and Adjustments needed to bring configuration up to the
     * current configuration format.
     *
     * @param config the configuration to upgrade and adjust.
     */
    private void sanitizeConfiguration( Configuration config )
    {
        Iterator it = config.getRepositories().iterator();
        while ( it.hasNext() )
        {
            RepositoryConfiguration repo = (RepositoryConfiguration) it.next();

            // Ensure that the repo.urlName is set.
            if ( StringUtils.isEmpty( repo.getUrlName() ) )
            {
                repo.setUrlName( repo.getId() );
            }
        }
    }

    public void storeConfiguration( Configuration configuration )
        throws ConfigurationStoreException, InvalidConfigurationException, ConfigurationChangeException
    {
        for ( Iterator i = listeners.iterator(); i.hasNext(); )
        {
            ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();

            listener.notifyOfConfigurationChange( configuration );
        }

        ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();

        getLogger().info( "Writing configuration to " + file );
        FileWriter fileWriter = null;
        try
        {
            //does file directory exist ?
            if ( file.getParentFile() != null && !file.getParentFile().exists() )
            {
                file.getParentFile().mkdirs();
            }

            fileWriter = new FileWriter( file );
            writer.write( fileWriter, configuration );
        }
        catch ( IOException e )
        {
            throw new ConfigurationStoreException( e.getMessage(), e );
        }
        finally
        {
            IOUtils.closeQuietly( fileWriter );
        }
{'fraction_non_alphanumeric': 0.08572848504928353, 'fraction_numerical': 0.0023192247163091196, 'mean_word_length': 2.0848237097598363, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '5941626', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2846, 'n_words': 897}