<commit_before>/*
 *  Copyright (C) 2010 <NAME>.
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.commonjava.emb.app;

import org.apache.log4j.Logger;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.commonjava.emb.EMBException;
import org.commonjava.emb.boot.embed.EMBEmbedderBuilder;
import org.commonjava.emb.conf.EMBConfiguration;
import org.commonjava.emb.conf.EMBLibrary;
import org.commonjava.emb.conf.VersionProvider;
import org.commonjava.emb.conf.ext.ExtensionConfiguration;
import org.commonjava.emb.conf.ext.ExtensionConfigurationException;
import org.commonjava.emb.conf.loader.InstanceLibraryLoader;
import org.commonjava.emb.plexus.ComponentKey;
import org.commonjava.emb.plexus.ComponentSelector;
import org.commonjava.emb.plexus.InstanceRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class AbstractEMBApplication
    implements EMBApplication
{

    private final List<EMBLibrary> additionalLibraries = new ArrayList<EMBLibrary>();

    private final InstanceRegistry instanceRegistry = new InstanceRegistry();

    private transient boolean loaded = false;

    @SuppressWarnings( { "rawtypes", "unchecked" } )
    protected AbstractEMBApplication()
    {
        withLibrary( this );
        withComponentInstance( new ComponentKey( getClass() ), this );
    }

    protected final AbstractEMBApplication withLibrary( final EMBLibrary library )
    {
        additionalLibraries.add( library );
        return this;
    }

    private EMBEmbedderBuilder builder()
    {
        return new EMBEmbedderBuilder().withLibraryLoader( new InstanceLibraryLoader( additionalLibraries ) );
    }

    protected boolean allowLoadWithoutConfiguration()
    {
        return true;
    }

    protected boolean allowLoadWithConfiguration()
    {
        return true;
    }

    @Override
    public final EMBApplication load()
        throws EMBException
    {
        if ( !allowLoadWithoutConfiguration() )
        {
            throw new EMBException( "Cannot load application: " + getName()
                            + " without a configuration. Use the 'app.load( config )' method instead!" );
        }

        return doLoad( null );
    }

    @Override
    public final EMBApplication load( final EMBApplicationConfiguration configuration )
        throws EMBException
    {
        if ( !allowLoadWithConfiguration() )
        {
            throw new EMBException( "Cannot load application: " + getName()
                            + " with a configuration. Use the 'app.load()' method instead!" );
        }

        return doLoad( configuration );
    }

    private EMBApplication doLoad( final EMBApplicationConfiguration configuration )
        throws EMBException
    {
        if ( loaded )
        {
            return this;
        }

        final EMBEmbedderBuilder builder = builder();

        beforeLoading();
        configureBuilder( builder );
        if ( configuration != null )
        {
            configuration.configureBuilder( builder );
        }

        builder.build();
        for ( final ComponentKey<?> key : getInstanceRegistry().getInstances().keySet() )
        {
            try
            {
                builder.container().lookup( key.getRoleClass(), key.getHint() );
            }
            catch ( final ComponentLookupException e )
            {
                throw new EMBException( "Forced member-injection for registered instance: %s failed. Reason: %s", e,<commit_msg>inject the app configuration as a component.<commit_after>/*
 *  Copyright (C) 2010 <NAME>.
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *  
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.commonjava.emb.app;

import org.apache.log4j.Logger;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.commonjava.emb.EMBException;
import org.commonjava.emb.boot.embed.EMBEmbedderBuilder;
import org.commonjava.emb.conf.EMBConfiguration;
import org.commonjava.emb.conf.EMBLibrary;
import org.commonjava.emb.conf.VersionProvider;
import org.commonjava.emb.conf.ext.ExtensionConfiguration;
import org.commonjava.emb.conf.ext.ExtensionConfigurationException;
import org.commonjava.emb.conf.loader.InstanceLibraryLoader;
import org.commonjava.emb.plexus.ComponentKey;
import org.commonjava.emb.plexus.ComponentSelector;
import org.commonjava.emb.plexus.InstanceRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class AbstractEMBApplication
    implements EMBApplication
{

    private final List<EMBLibrary> additionalLibraries = new ArrayList<EMBLibrary>();

    private final InstanceRegistry instanceRegistry = new InstanceRegistry();

    private transient boolean loaded = false;

    @SuppressWarnings( { "rawtypes", "unchecked" } )
    protected AbstractEMBApplication()
    {
        withLibrary( this );
        withComponentInstance( new ComponentKey( getClass() ), this );
    }

    protected final AbstractEMBApplication withLibrary( final EMBLibrary library )
    {
        additionalLibraries.add( library );
        return this;
    }

    private EMBEmbedderBuilder builder()
    {
        return new EMBEmbedderBuilder().withLibraryLoader( new InstanceLibraryLoader( additionalLibraries ) );
    }

    protected boolean allowLoadWithoutConfiguration()
    {
        return true;
    }

    protected boolean allowLoadWithConfiguration()
    {
        return true;
    }

    @Override
    public final EMBApplication load()
        throws EMBException
    {
        if ( !allowLoadWithoutConfiguration() )
        {
            throw new EMBException( "Cannot load application: " + getName()
                            + " without a configuration. Use the 'app.load( config )' method instead!" );
        }

        return doLoad( null );
    }

    @Override
    public final EMBApplication load( final EMBApplicationConfiguration configuration )
        throws EMBException
    {
        if ( !allowLoadWithConfiguration() )
        {
            throw new EMBException( "Cannot load application: " + getName()
                            + " with a configuration. Use the 'app.load()' method instead!" );
        }

        instanceRegistry.add( configuration );
        return doLoad( configuration );
    }

    private EMBApplication doLoad( final EMBApplicationConfiguration configuration )
        throws EMBException
    {
        if ( loaded )
        {
            return this;
        }

        final EMBEmbedderBuilder builder = builder();

        beforeLoading();
        configureBuilder( builder );
        if ( configuration != null )
        {
            configuration.configureBuilder( builder );
        }

        builder.build();
        for ( final ComponentKey<?> key : getInstanceRegistry().getInstances().keySet() )
        {
            try
            {
                builder.container().lookup( key.getRoleClass(), key.getHint() );
            }
            catch ( final ComponentLookupException e )
            {
                throw new EMBException( "Forced member-injection for registered instance: %s failed. Reason: %s", e,
{'fraction_non_alphanumeric': 0.08284371327849589, 'fraction_numerical': 0.001410105757931845, 'mean_word_length': 2.7894033837934105, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5565175', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2141, 'n_words': 645}