All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.context.DefaultApplicationContextBuilder Maven / Gradle / Ivy

There is a newer version: 4.8.7
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * 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
 *
 * https://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 io.micronaut.context;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.context.env.CommandLinePropertySource;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.PropertySource;
import io.micronaut.context.env.SystemPropertiesPropertySource;
import io.micronaut.core.cli.CommandLine;
import io.micronaut.core.io.scan.ClassPathResourceLoader;
import io.micronaut.core.util.StringUtils;

import java.lang.annotation.Annotation;
import java.util.*;

/**
 * Implementation of {@link ApplicationContextBuilder}.
 *
 * @author graemerocher
 * @since 1.0
 */
public class DefaultApplicationContextBuilder implements ApplicationContextBuilder, ApplicationContextConfiguration {
    private List singletons = new ArrayList<>();
    private List environments = new ArrayList<>();
    private List defaultEnvironments = new ArrayList<>();
    private List packages = new ArrayList<>();
    private Map properties = new LinkedHashMap<>();
    private List propertySources = new ArrayList<>();
    private Collection configurationIncludes = new HashSet<>();
    private Collection configurationExcludes = new HashSet<>();
    private Boolean deduceEnvironments = null;
    private ClassLoader classLoader = getClass().getClassLoader();
    private boolean envPropertySource = true;
    private List envVarIncludes = new ArrayList<>();
    private List envVarExcludes = new ArrayList<>();
    private String[] args = new String[0];
    private Set> eagerInitAnnotated = new HashSet<>(3);
    private String[] overrideConfigLocations;
    private boolean banner = true;

    /**
     * Default constructor.
     */
    protected DefaultApplicationContextBuilder() {
    }

    @NonNull
    @Override
    public ApplicationContextBuilder eagerInitAnnotated(Class... annotations) {
        if (annotations != null) {
            eagerInitAnnotated.addAll(Arrays.asList(annotations));
        }
        return this;
    }

    @NonNull
    @Override
    public ApplicationContextBuilder overrideConfigLocations(String... configLocations) {
        overrideConfigLocations = configLocations;
        return this;
    }

    @Override
    public @Nullable  List getOverrideConfigLocations() {
        return overrideConfigLocations == null ? null : Arrays.asList(overrideConfigLocations);
    }

    @Override
    public boolean isBannerEnabled() {
        return banner;
    }

    @Override
    public Set> getEagerInitAnnotated() {
        return Collections.unmodifiableSet(eagerInitAnnotated);
    }

    @Override
    public @NonNull ApplicationContextBuilder singletons(Object... beans) {
        if (beans != null) {
            singletons.addAll(Arrays.asList(beans));
        }
        return this;
    }

    @Override
    public @NonNull ClassPathResourceLoader getResourceLoader() {
        if (classLoader != null) {
            return ClassPathResourceLoader.defaultLoader(classLoader);
        } else {
            return ClassPathResourceLoader.defaultLoader(getClass().getClassLoader());
        }
    }

    @NonNull
    @Override
    public ClassLoader getClassLoader() {
        return this.classLoader;
    }

    @Override
    public @NonNull ApplicationContextBuilder deduceEnvironment(@Nullable Boolean deduceEnvironments) {
        this.deduceEnvironments = deduceEnvironments;
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder environments(@Nullable String... environments) {
        if (environments != null) {
            this.environments.addAll(Arrays.asList(environments));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder defaultEnvironments(@Nullable String... environments) {
        if (environments != null) {
            this.defaultEnvironments.addAll(Arrays.asList(environments));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder packages(@Nullable String... packages) {
        if (packages != null) {
            this.packages.addAll(Arrays.asList(packages));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder properties(@Nullable Map properties) {
        if (properties != null) {
            this.properties.putAll(properties);
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder propertySources(@Nullable PropertySource... propertySources) {
        if (propertySources != null) {
            this.propertySources.addAll(Arrays.asList(propertySources));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder environmentPropertySource(boolean environmentPropertySource) {
        this.envPropertySource = environmentPropertySource;
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder environmentVariableIncludes(@Nullable String... environmentVariables) {
        if (environmentVariables != null) {
            this.envVarIncludes.addAll(Arrays.asList(environmentVariables));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder environmentVariableExcludes(@Nullable String... environmentVariables) {
        if (environmentVariables != null) {
            this.envVarExcludes.addAll(Arrays.asList(environmentVariables));
        }
        return this;
    }

    @Override
    public Optional getDeduceEnvironments() {
        return Optional.ofNullable(deduceEnvironments);
    }

    @Override
    public @NonNull List getEnvironments() {
        return environments;
    }

    @Override
    public @NonNull List getDefaultEnvironments() {
        return defaultEnvironments;
    }

    @Override
    public boolean isEnvironmentPropertySource() {
        return envPropertySource;
    }

    @Override
    public @Nullable List getEnvironmentVariableIncludes() {
        return envVarIncludes.isEmpty() ? null : envVarIncludes;
    }

    @Override
    public @Nullable List getEnvironmentVariableExcludes() {
        return envVarExcludes.isEmpty() ? null : envVarExcludes;
    }

    @Override
    public @NonNull ApplicationContextBuilder mainClass(Class mainClass) {
        if (mainClass != null) {
            if (this.classLoader == null) {
                this.classLoader = mainClass.getClassLoader();
            }
            String name = mainClass.getPackage().getName();
            if (StringUtils.isNotEmpty(name)) {
                packages(name);
            }
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder classLoader(ClassLoader classLoader) {
        if (classLoader != null) {
            this.classLoader = classLoader;
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder args(@Nullable String... args) {
        if (args != null) {
            this.args = args;
        }
        return this;
    }

    @Override
    @SuppressWarnings("MagicNumber")
    public @NonNull ApplicationContext build() {
        ApplicationContext applicationContext = newApplicationContext();
        Environment environment = applicationContext.getEnvironment();
        if (!packages.isEmpty()) {
            for (String aPackage : packages) {
                environment.addPackage(aPackage);
            }
        }
        if (!properties.isEmpty()) {
            PropertySource contextProperties = PropertySource.of(PropertySource.CONTEXT, properties, SystemPropertiesPropertySource.POSITION + 100);
            environment.addPropertySource(contextProperties);
        }
        if (args.length > 0) {
            CommandLine commandLine = CommandLine.parse(args);
            environment.addPropertySource(new CommandLinePropertySource(commandLine));
        }
        if (!propertySources.isEmpty()) {
            for (PropertySource propertySource : propertySources) {
                environment.addPropertySource(propertySource);
            }
        }
        if (!singletons.isEmpty()) {
            for (Object singleton : singletons) {
                applicationContext.registerSingleton(singleton);
            }
        }

        if (!configurationIncludes.isEmpty()) {
            environment.addConfigurationIncludes(configurationIncludes.toArray(StringUtils.EMPTY_STRING_ARRAY));
        }
        if (!configurationExcludes.isEmpty()) {
            environment.addConfigurationExcludes(configurationExcludes.toArray(StringUtils.EMPTY_STRING_ARRAY));
        }

        return applicationContext;
    }

    /**
     * Creates the {@link ApplicationContext} instance.
     * @return The application context
     * @since 2.0
     */
    @NonNull
    protected ApplicationContext newApplicationContext() {
        return new DefaultApplicationContext(
            this
        );
    }

    /**
     * Allow customizing the configurations that will be loaded.
     *
     * @param configurations The configurations to include
     * @return This application
     */
    @Override
    public @NonNull ApplicationContextBuilder include(@Nullable String... configurations) {
        if (configurations != null) {
            this.configurationIncludes.addAll(Arrays.asList(configurations));
        }
        return this;
    }

    /**
     * Allow customizing the configurations that will be loaded.
     *
     * @param configurations The configurations to exclude
     * @return This application
     */
    @Override
    public @NonNull ApplicationContextBuilder exclude(@Nullable String... configurations) {
        if (configurations != null) {
            this.configurationExcludes.addAll(Arrays.asList(configurations));
        }
        return this;
    }

    @Override
    public @NonNull ApplicationContextBuilder banner(boolean isEnabled) {
        this.banner = isEnabled;
        return this;
    }
}