Class DefaultEnvironment

  • All Implemented Interfaces:
    Environment, LifeCycle<Environment>, io.micronaut.core.convert.ConversionService<Environment>, io.micronaut.core.io.ResourceLoader, io.micronaut.core.value.PropertyResolver, io.micronaut.core.value.ValueResolver<java.lang.String>, java.io.Closeable, java.lang.AutoCloseable

    public class DefaultEnvironment
    extends PropertySourcePropertyResolver
    implements Environment

    The default implementation of the Environment interface. Configures a named environment.

    Since:
    1.0
    • Field Detail

      • resourceLoader

        protected final io.micronaut.core.io.scan.ClassPathResourceLoader resourceLoader
      • refreshablePropertySources

        protected final java.util.List<PropertySource> refreshablePropertySources
    • Constructor Detail

      • DefaultEnvironment

        public DefaultEnvironment​(@NonNull
                                  ApplicationContextConfiguration configuration)
        Construct a new environment for the given configuration.
        Parameters:
        configuration - The configuration
      • DefaultEnvironment

        public DefaultEnvironment​(@NonNull
                                  ApplicationContextConfiguration configuration,
                                  boolean logEnabled)
        Construct a new environment for the given configuration.
        Parameters:
        configuration - The configuration
        logEnabled - flag to enable or disable logger
    • Method Detail

      • isPresent

        public boolean isPresent​(java.lang.String className)
        Description copied from interface: Environment
        Check whether the given class is present within this environment.
        Specified by:
        isPresent in interface Environment
        Parameters:
        className - The class name
        Returns:
        True if it is
      • scan

        public java.util.stream.Stream<java.lang.Class<?>> scan​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Description copied from interface: Environment
        Scan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.
        Specified by:
        scan in interface Environment
        Parameters:
        annotation - The annotation to scan
        Returns:
        The classes
      • scan

        public java.util.stream.Stream<java.lang.Class<?>> scan​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                                                                java.lang.String... packages)
        Description copied from interface: Environment
        Scan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.
        Specified by:
        scan in interface Environment
        Parameters:
        annotation - The annotation to scan
        packages - The packages to scan
        Returns:
        The classes
      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
        Specified by:
        getClassLoader in interface Environment
        Returns:
        The class loader for the environment
      • isActive

        public boolean isActive​(BeanConfiguration configuration)
        Description copied from interface: Environment
        Whether the current environment includes the given configuration.
        Specified by:
        isActive in interface Environment
        Parameters:
        configuration - The configuration
        Returns:
        True if it does
      • addPackage

        public Environment addPackage​(java.lang.String pkg)
        Description copied from interface: Environment
        Add an application package. Application packages are candidates for scanning for tools that need it (such as JPA or GORM).
        Specified by:
        addPackage in interface Environment
        Parameters:
        pkg - The package to add
        Returns:
        This environment
      • addConfigurationExcludes

        public Environment addConfigurationExcludes​(@Nullable
                                                    java.lang.String... names)
        Description copied from interface: Environment
        Exclude configurations by name.
        Specified by:
        addConfigurationExcludes in interface Environment
        Parameters:
        names - The names of the configuration
        Returns:
        This environment
      • addConfigurationIncludes

        public Environment addConfigurationIncludes​(java.lang.String... names)
        Description copied from interface: Environment
        Exclude configurations by name.
        Specified by:
        addConfigurationIncludes in interface Environment
        Parameters:
        names - The names of the configuration
        Returns:
        This environment
      • getPackages

        public java.util.Collection<java.lang.String> getPackages()
        Specified by:
        getPackages in interface Environment
        Returns:
        The application packages
      • getActiveNames

        public java.util.Set<java.lang.String> getActiveNames()
        Description copied from interface: Environment
        Should respect the order as provided.
        Specified by:
        getActiveNames in interface Environment
        Returns:
        The active environment names
      • refreshAndDiff

        public java.util.Map<java.lang.String,​java.lang.Object> refreshAndDiff()
        Description copied from interface: Environment
        Refresh the environment from the list of PropertySource instances and return a diff of the changes.
        Specified by:
        refreshAndDiff in interface Environment
        Returns:
        The values that changed
      • convert

        public <T> java.util.Optional<T> convert​(java.lang.Object object,
                                                 java.lang.Class<T> targetType,
                                                 io.micronaut.core.convert.ConversionContext context)
        Specified by:
        convert in interface io.micronaut.core.convert.ConversionService<Environment>
      • canConvert

        public <S,​T> boolean canConvert​(java.lang.Class<S> sourceType,
                                              java.lang.Class<T> targetType)
        Specified by:
        canConvert in interface io.micronaut.core.convert.ConversionService<Environment>
      • addConverter

        public <S,​T> Environment addConverter​(java.lang.Class<S> sourceType,
                                                    java.lang.Class<T> targetType,
                                                    io.micronaut.core.convert.TypeConverter<S,​T> typeConverter)
        Specified by:
        addConverter in interface io.micronaut.core.convert.ConversionService<Environment>
      • addConverter

        public <S,​T> Environment addConverter​(java.lang.Class<S> sourceType,
                                                    java.lang.Class<T> targetType,
                                                    java.util.function.Function<S,​T> typeConverter)
        Specified by:
        addConverter in interface io.micronaut.core.convert.ConversionService<Environment>
      • getResourceAsStream

        public java.util.Optional<java.io.InputStream> getResourceAsStream​(java.lang.String path)
        Specified by:
        getResourceAsStream in interface io.micronaut.core.io.ResourceLoader
      • getResource

        public java.util.Optional<java.net.URL> getResource​(java.lang.String path)
        Specified by:
        getResource in interface io.micronaut.core.io.ResourceLoader
      • getResources

        public java.util.stream.Stream<java.net.URL> getResources​(java.lang.String path)
        Specified by:
        getResources in interface io.micronaut.core.io.ResourceLoader
      • supportsPrefix

        public boolean supportsPrefix​(java.lang.String path)
        Specified by:
        supportsPrefix in interface io.micronaut.core.io.ResourceLoader
      • forBase

        public io.micronaut.core.io.ResourceLoader forBase​(java.lang.String basePath)
        Specified by:
        forBase in interface io.micronaut.core.io.ResourceLoader
      • shouldDeduceEnvironments

        protected boolean shouldDeduceEnvironments()
        Returns:
        Whether environment names and packages should be deduced
      • createAnnotationScanner

        protected io.micronaut.core.io.scan.BeanIntrospectionScanner createAnnotationScanner​(java.lang.ClassLoader classLoader)
        Creates the default annotation scanner.
        Parameters:
        classLoader - The class loader
        Returns:
        The scanner
      • getPropertySourceRootName

        protected java.lang.String getPropertySourceRootName()
        Returns:
        The property source root name
      • readPropertySources

        protected void readPropertySources​(java.lang.String name)
        Parameters:
        name - The name to read property sources
      • getConstantPropertySources

        protected java.util.List<PropertySource> getConstantPropertySources()
        Returns:
        Property sources created at build time
      • readPropertySourceListKeyFromEnvironment

        protected java.lang.String readPropertySourceListKeyFromEnvironment()
        Reads the value of MICRONAUT_CONFIG_FILES environment variable.
        Returns:
        The comma-separated list of files
      • readPropertySourceListFromFiles

        protected java.util.List<PropertySource> readPropertySourceListFromFiles​(java.lang.String files)
        Resolve the property sources for files passed via system property and system env.
        Parameters:
        files - The comma separated list of files
        Returns:
        The list of property sources for each file
      • readPropertySourceList

        protected java.util.List<PropertySource> readPropertySourceList​(java.lang.String name)
        Parameters:
        name - The name to resolver property sources
        Returns:
        The list of property sources
      • addDefaultPropertySources

        protected void addDefaultPropertySources​(java.util.List<PropertySource> propertySources)
        Adds default property sources.
        Parameters:
        propertySources - The list of property sources
      • readPropertySourceLoaders

        protected io.micronaut.core.io.service.SoftServiceLoader<PropertySourceLoader> readPropertySourceLoaders()
        Returns:
        Loaded properties as a SoftServiceLoader