<commit_before>/**
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.runtime.core.internal;

import com.speedment.common.injector.InjectBundle;
import com.speedment.common.injector.Injector;
import com.speedment.common.injector.InjectorBuilder;
import com.speedment.common.injector.exception.CyclicReferenceException;
import static com.speedment.common.injector.execution.ExecutionBuilder.started;
import static com.speedment.common.invariant.NullUtil.requireNonNulls;
import com.speedment.common.logger.Level;
import com.speedment.common.logger.Logger;
import com.speedment.common.logger.LoggerManager;
import com.speedment.runtime.config.Dbms;
import com.speedment.runtime.config.Document;
import com.speedment.runtime.config.Project;
import com.speedment.runtime.config.Schema;
import com.speedment.runtime.config.trait.HasEnabled;
import com.speedment.runtime.config.trait.HasName;
import com.speedment.runtime.config.util.DocumentDbUtil;
import com.speedment.runtime.core.ApplicationBuilder;
import com.speedment.runtime.core.ApplicationMetadata;
import com.speedment.runtime.core.RuntimeBundle;
import com.speedment.runtime.core.Speedment;
import com.speedment.runtime.core.component.DbmsHandlerComponent;
import com.speedment.runtime.core.component.InfoComponent;
import com.speedment.runtime.core.component.PasswordComponent;
import com.speedment.runtime.core.component.ProjectComponent;
import com.speedment.runtime.core.db.DbmsMetadataHandler;
import com.speedment.runtime.core.db.DbmsType;
import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.core.manager.Manager;
import com.speedment.runtime.core.util.DatabaseUtil;
import java.sql.SQLException;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This abstract class is implemented by classes that can build a
 * {@link Speedment} application.
 *
 * @param <APP> the type that is being built
 * @param <BUILDER> the (self) type of the AbstractApplicationBuilder
 *
 * @author <NAME>
 * @author <NAME>
 * @since 2.0.0
 */
public abstract class AbstractApplicationBuilder<
        APP extends Speedment, 
        BUILDER extends AbstractApplicationBuilder<APP, BUILDER>
> implements ApplicationBuilder<APP, BUILDER> {

    private final static Logger LOGGER = LoggerManager.getLogger(
        LogType.APPLICATION_BUILDER.getLoggerName());
    
    private final InjectorBuilder injectorBuilder;

    private boolean skipCheckDatabaseConnectivity;
    private boolean skipValidateRuntimeConfig;
    private boolean skipLogoPrintout;
    
    protected AbstractApplicationBuilder(
        Class<? extends APP> applicationImplClass,
        Class<? extends ApplicationMetadata> metadataClass) {

        this(Injector.builder()
            .withBundle(RuntimeBundle.class)
            .withComponent(applicationImplClass)
            .withComponent(metadataClass)
        );
    }
    
    protected AbstractApplicationBuilder(
        ClassLoader classLoader,
        Class<? extends APP> applicationImplClass,
        Class<? extends ApplicationMetadata> metadataClass) {

        this(Injector.builder(classLoader)
            .withBundle(RuntimeBundle.class)
            .withComponent(applicationImplClass)
            .withComponent(metadataClass)
        );
    }

    protected AbstractApplicationBuilder(InjectorBuilder injectorBuilder) {
        this.injectorBuilder = requireNonNull(injectorBuilder);
        this.skipCheckDatabaseConnectivity = false;
        this.skipValidateRuntimeConfig     = false;
    }

    @Override
    public <C extends Document & HasEnabled> BUILDER with(
            Class<C> type, String name, BiConsumer<Injector, C> consumer) {
        
        requireNonNulls(type, name, consumer);
        
        injectorBuilder.before(started(ProjectComponent.class)
            .withStateInitialized(Injector.class)
            .withExecute((projComp, injector) -> 
                DocumentDbUtil.traverseOver(projComp.getProject(), type)
                    .filter(doc -> HasName.of(doc).getName().equals(name))
                    .forEach(doc -> consumer.accept(injector, doc))
            )
        );
        
        return self();
    }

    @Override
    public <C extends Document & HasEnabled> BUILDER with(
            Class<C> type, BiConsumer<Injector, C> consumer) {
        
        requireNonNulls(type, consumer);
        
        injectorBuilder.before(started(ProjectComponent.class)
            .withStateInitialized(Injector.class)
            .withExecute((projComp, injector) -> 
                DocumentDbUtil.traverseOver(projComp.getProject(), type)
                    .forEach(doc -> consumer.accept(injector, doc))<commit_msg>runtime-core: Fix bug where named config isn't applied<commit_after>/**
 *
 * Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
 *
 * 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 com.speedment.runtime.core.internal;

import com.speedment.common.injector.InjectBundle;
import com.speedment.common.injector.Injector;
import com.speedment.common.injector.InjectorBuilder;
import com.speedment.common.injector.exception.CyclicReferenceException;
import static com.speedment.common.injector.execution.ExecutionBuilder.started;
import static com.speedment.common.invariant.NullUtil.requireNonNulls;
import com.speedment.common.logger.Level;
import com.speedment.common.logger.Logger;
import com.speedment.common.logger.LoggerManager;
import com.speedment.runtime.config.Dbms;
import com.speedment.runtime.config.Document;
import com.speedment.runtime.config.Project;
import com.speedment.runtime.config.Schema;
import com.speedment.runtime.config.trait.HasEnabled;
import com.speedment.runtime.config.trait.HasName;
import com.speedment.runtime.config.util.DocumentDbUtil;
import com.speedment.runtime.config.util.DocumentUtil;
import static com.speedment.runtime.config.util.DocumentUtil.Name.DATABASE_NAME;
import com.speedment.runtime.core.ApplicationBuilder;
import com.speedment.runtime.core.ApplicationMetadata;
import com.speedment.runtime.core.RuntimeBundle;
import com.speedment.runtime.core.Speedment;
import com.speedment.runtime.core.component.DbmsHandlerComponent;
import com.speedment.runtime.core.component.InfoComponent;
import com.speedment.runtime.core.component.PasswordComponent;
import com.speedment.runtime.core.component.ProjectComponent;
import com.speedment.runtime.core.db.DbmsMetadataHandler;
import com.speedment.runtime.core.db.DbmsType;
import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.core.manager.Manager;
import com.speedment.runtime.core.util.DatabaseUtil;
import java.sql.SQLException;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This abstract class is implemented by classes that can build a
 * {@link Speedment} application.
 *
 * @param <APP> the type that is being built
 * @param <BUILDER> the (self) type of the AbstractApplicationBuilder
 *
 * @author <NAME>
 * @author <NAME>
 * @since 2.0.0
 */
public abstract class AbstractApplicationBuilder<
        APP extends Speedment, 
        BUILDER extends AbstractApplicationBuilder<APP, BUILDER>
> implements ApplicationBuilder<APP, BUILDER> {

    private final static Logger LOGGER = LoggerManager.getLogger(
        LogType.APPLICATION_BUILDER.getLoggerName());
    
    private final InjectorBuilder injectorBuilder;

    private boolean skipCheckDatabaseConnectivity;
    private boolean skipValidateRuntimeConfig;
    private boolean skipLogoPrintout;
    
    protected AbstractApplicationBuilder(
        Class<? extends APP> applicationImplClass,
        Class<? extends ApplicationMetadata> metadataClass) {

        this(Injector.builder()
            .withBundle(RuntimeBundle.class)
            .withComponent(applicationImplClass)
            .withComponent(metadataClass)
        );
    }
    
    protected AbstractApplicationBuilder(
        ClassLoader classLoader,
        Class<? extends APP> applicationImplClass,
        Class<? extends ApplicationMetadata> metadataClass) {

        this(Injector.builder(classLoader)
            .withBundle(RuntimeBundle.class)
            .withComponent(applicationImplClass)
            .withComponent(metadataClass)
        );
    }

    protected AbstractApplicationBuilder(InjectorBuilder injectorBuilder) {
        this.injectorBuilder = requireNonNull(injectorBuilder);
        this.skipCheckDatabaseConnectivity = false;
        this.skipValidateRuntimeConfig     = false;
    }

    @Override
    public <C extends Document & HasEnabled> BUILDER with(
            Class<C> type, String name, BiConsumer<Injector, C> consumer) {
        
        requireNonNulls(type, name, consumer);

        injectorBuilder.before(started(ProjectComponent.class)
            .withStateInitialized(Injector.class)
            .withExecute((projComp, injector) -> 
                DocumentDbUtil.traverseOver(projComp.getProject(), type)
                    .filter(doc -> DocumentUtil.relativeName(
                        HasName.of(doc), 
                        Dbms.class, 
                        DATABASE_NAME
                    ).equals(name))
                    .forEach(doc -> consumer.accept(injector, doc))
            )
        );
        
        return self();
    }

    @Override
    public <C extends Document & HasEnabled> BUILDER with(
            Class<C> type, BiConsumer<Injector, C> consumer) {
        
        requireNonNulls(type, consumer);
        
        injectorBuilder.before(started(ProjectComponent.class)
            .withStateInitialized(Injector.class)
            .withExecute((projComp, injector) -> 
                DocumentDbUtil.traverseOver(projComp.getProject(), type)
                    .forEach(doc -> consumer.accept(injector, doc))
{'fraction_non_alphanumeric': 0.08999376280851822, 'fraction_numerical': 0.002673082063619353, 'mean_word_length': 3.5312878482034717, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 47, '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': '1799551', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2953, 'n_words': 671}