Class DefaultEnvironment

All Implemented Interfaces:
Environment, LifeCycle<Environment>, ConversionService, MutableConversionService, ResourceLoader, PropertyResolver, ValueResolver<String>, Closeable, AutoCloseable

public class DefaultEnvironment extends PropertySourcePropertyResolver implements Environment

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

Since:
1.0
Author:
Graeme Rocher, rvanderwerf
  • Field Details

  • Constructor Details

    • DefaultEnvironment

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

      public DefaultEnvironment(@NonNull @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 Details

    • isPresent

      public boolean isPresent(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
    • getPlaceholderResolver

      public PropertyPlaceholderResolver getPlaceholderResolver()
      Specified by:
      getPlaceholderResolver in interface Environment
      Returns:
      The placeholder resolver
    • scan

      public Stream<Class<?>> scan(Class<? extends 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 Stream<Class<?>> scan(Class<? extends Annotation> annotation, 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 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
    • addPropertySource

      public DefaultEnvironment addPropertySource(PropertySource propertySource)
      Description copied from class: PropertySourcePropertyResolver
      Add a PropertySource to this resolver.
      Specified by:
      addPropertySource in interface Environment
      Overrides:
      addPropertySource in class PropertySourcePropertyResolver
      Parameters:
      propertySource - The PropertySource to add
      Returns:
      This PropertySourcePropertyResolver
    • removePropertySource

      public Environment removePropertySource(PropertySource propertySource)
      Description copied from interface: Environment
      Removes a property source from this environment.
      Specified by:
      removePropertySource in interface Environment
      Parameters:
      propertySource - The property source
      Returns:
      This environment
    • addPropertySource

      public DefaultEnvironment addPropertySource(String name, Map<String,? super Object> values)
      Description copied from class: PropertySourcePropertyResolver
      Add a property source for the given map.
      Specified by:
      addPropertySource in interface Environment
      Overrides:
      addPropertySource in class PropertySourcePropertyResolver
      Parameters:
      name - The name of the property source
      values - The values
      Returns:
      This environment
    • addPackage

      public Environment addPackage(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 @Nullable 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(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 Collection<String> getPackages()
      Specified by:
      getPackages in interface Environment
      Returns:
      The application packages
    • getActiveNames

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

      public Collection<PropertySource> getPropertySources()
      Specified by:
      getPropertySources in interface Environment
      Returns:
      The active property sources
    • start

      public Environment start()
      Description copied from interface: LifeCycle
      Starts the lifecyle component.
      Specified by:
      start in interface LifeCycle<Environment>
      Returns:
      This lifecycle component
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface LifeCycle<Environment>
      Returns:
      Whether the component is running
    • stop

      public Environment stop()
      Description copied from interface: LifeCycle
      Stops the life cycle component.
      Specified by:
      stop in interface LifeCycle<Environment>
      Returns:
      This lifecycle component
    • refreshAndDiff

      public Map<String,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> Optional<T> convert(Object object, Class<T> targetType, ConversionContext context)
      Description copied from interface: ConversionService
      Attempts to convert the given object to the given target type. If conversion fails or is not possible an empty Optional is returned.
      Specified by:
      convert in interface ConversionService
      Type Parameters:
      T - The generic type
      Parameters:
      object - The object to convert
      targetType - The target type
      context - The conversion context
      Returns:
      The optional
    • canConvert

      public <S, T> boolean canConvert(Class<S> sourceType, Class<T> targetType)
      Description copied from interface: ConversionService
      Return whether the given source type is convertible to the given target type.
      Specified by:
      canConvert in interface ConversionService
      Type Parameters:
      S - The generic source type
      T - The target source type
      Parameters:
      sourceType - The source type
      targetType - The target type
      Returns:
      True if it can be converted
    • addConverter

      public <S, T> void addConverter(Class<S> sourceType, Class<T> targetType, TypeConverter<S,T> typeConverter)
      Description copied from interface: MutableConversionService
      Adds a type converter.
      Specified by:
      addConverter in interface MutableConversionService
      Type Parameters:
      S - The source generic type
      T - The target generic type
      Parameters:
      sourceType - The source type
      targetType - The target type
      typeConverter - The type converter
    • addConverter

      public <S, T> void addConverter(Class<S> sourceType, Class<T> targetType, Function<S,T> typeConverter)
      Description copied from interface: MutableConversionService
      Adds a type converter.
      Specified by:
      addConverter in interface MutableConversionService
      Type Parameters:
      S - The source generic type
      T - The target generic type
      Parameters:
      sourceType - The source type
      targetType - The target type
      typeConverter - The type converter
    • getResourceAsStream

      public Optional<InputStream> getResourceAsStream(String path)
      Description copied from interface: ResourceLoader
      Obtains a resource as a stream.
      Specified by:
      getResourceAsStream in interface ResourceLoader
      Parameters:
      path - The path
      Returns:
      An optional resource
    • getResource

      public Optional<URL> getResource(String path)
      Description copied from interface: ResourceLoader
      Obtains the URL to a given resource.
      Specified by:
      getResource in interface ResourceLoader
      Parameters:
      path - The path
      Returns:
      An optional resource
    • getResources

      public Stream<URL> getResources(String path)
      Description copied from interface: ResourceLoader
      Obtains all resources with the given name.
      Specified by:
      getResources in interface ResourceLoader
      Parameters:
      path - The name of the resource
      Returns:
      A stream of URLs
    • supportsPrefix

      public boolean supportsPrefix(String path)
      Specified by:
      supportsPrefix in interface ResourceLoader
      Parameters:
      path - The path to a resource including a prefix appended by a colon. Ex (classpath:, file:)
      Returns:
      Whether the given resource loader supports the prefix
    • forBase

      public ResourceLoader forBase(String basePath)
      Description copied from interface: ResourceLoader
      Constructs a new resource loader designed to load resources from the given path. Requested resources will be loaded within the context of the given path.
      Specified by:
      forBase in interface ResourceLoader
      Parameters:
      basePath - The path to load resources
      Returns:
      The new ResourceLoader
    • shouldDeduceEnvironments

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

      protected boolean shouldDeduceCloudEnvironment()
      Returns:
      Whether cloud environment should be deduced based on environment variable, system property or configuration
    • createAnnotationScanner

      protected BeanIntrospectionScanner createAnnotationScanner(ClassLoader classLoader)
      Creates the default annotation scanner.
      Parameters:
      classLoader - The class loader
      Returns:
      The scanner
    • getPropertySourceRootName

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

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

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

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

      protected List<PropertySource> readPropertySourceListFromFiles(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 List<PropertySource> readPropertySourceList(String name)
      Parameters:
      name - The name to resolver property sources
      Returns:
      The list of property sources
    • addDefaultPropertySources

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

      protected SoftServiceLoader<PropertySourceLoader> readPropertySourceLoaders()
      Returns:
      Loaded properties as a SoftServiceLoader
    • getPropertySourceLoaders

      public Collection<PropertySourceLoader> getPropertySourceLoaders()
      Obtains the PropertySourceLoader instances.
      Specified by:
      getPropertySourceLoaders in interface Environment
      Returns:
      A collection of PropertySourceLoader
    • close

      public void close()
      Description copied from interface: LifeCycle
      Delegates to LifeCycle.stop().
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface LifeCycle<Environment>
      Overrides:
      close in class PropertySourcePropertyResolver