Package io.micronaut.context.env
Class DefaultEnvironment
- java.lang.Object
-
- io.micronaut.context.env.PropertySourcePropertyResolver
-
- io.micronaut.context.env.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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
PropertySourcePropertyResolver.PropertyCatalog
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<PropertySource>
refreshablePropertySources
protected io.micronaut.core.io.scan.ClassPathResourceLoader
resourceLoader
-
Fields inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
catalog, conversionService, log, nonGenerated, propertyPlaceholderResolver, propertySources, rawCatalog
-
Fields inherited from interface io.micronaut.context.env.Environment
AMAZON_EC2, ANDROID, AZURE, BARE_METAL, BOOTSTRAP_CONTEXT_PROPERTY, BOOTSTRAP_NAME, BOOTSTRAP_NAME_PROPERTY, CLI, CLOUD, CLOUD_FOUNDRY, CLOUD_PLATFORM_PROPERTY, DEDUCE_ENVIRONMENT_ENV, DEDUCE_ENVIRONMENT_PROPERTY, DEFAULT_NAME, DEVELOPMENT, DIGITAL_OCEAN, ENVIRONMENTS_ENV, ENVIRONMENTS_PROPERTY, FUNCTION, GAE, GOOGLE_COMPUTE, HEROKU, HOSTNAME, IBM, KUBERNETES, MICRONAUT, ORACLE_CLOUD, PROPERTY_SOURCES_KEY, TEST
-
-
Constructor Summary
Constructors Constructor Description DefaultEnvironment(ApplicationContextConfiguration configuration)
Construct a new environment for the given configuration.DefaultEnvironment(ApplicationContextConfiguration configuration, boolean logEnabled)
Construct a new environment for the given configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Environment
addConfigurationExcludes(java.lang.String... names)
Exclude configurations by name.Environment
addConfigurationIncludes(java.lang.String... names)
Exclude configurations by name.<S,T>
EnvironmentaddConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, io.micronaut.core.convert.TypeConverter<S,T> typeConverter)
<S,T>
EnvironmentaddConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, java.util.function.Function<S,T> typeConverter)
protected void
addDefaultPropertySources(java.util.List<PropertySource> propertySources)
Adds default property sources.Environment
addPackage(java.lang.String pkg)
Add an application package.DefaultEnvironment
addPropertySource(PropertySource propertySource)
Add aPropertySource
to this resolver.DefaultEnvironment
addPropertySource(java.lang.String name, java.util.Map<java.lang.String,? super java.lang.Object> values)
Add a property source for the given map.<S,T>
booleancanConvert(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
void
close()
Delegates toLifeCycle.stop()
.<T> java.util.Optional<T>
convert(java.lang.Object object, java.lang.Class<T> targetType, io.micronaut.core.convert.ConversionContext context)
protected io.micronaut.core.io.scan.BeanIntrospectionScanner
createAnnotationScanner(java.lang.ClassLoader classLoader)
Creates the default annotation scanner.io.micronaut.core.io.ResourceLoader
forBase(java.lang.String basePath)
java.util.Set<java.lang.String>
getActiveNames()
Should respect the order as provided.java.lang.ClassLoader
getClassLoader()
protected java.util.List<PropertySource>
getConstantPropertySources()
java.util.Collection<java.lang.String>
getPackages()
PropertyPlaceholderResolver
getPlaceholderResolver()
java.util.Collection<PropertySourceLoader>
getPropertySourceLoaders()
Obtains thePropertySourceLoader
instances.protected java.lang.String
getPropertySourceRootName()
java.util.Collection<PropertySource>
getPropertySources()
java.util.Optional<java.net.URL>
getResource(java.lang.String path)
java.util.Optional<java.io.InputStream>
getResourceAsStream(java.lang.String path)
java.util.stream.Stream<java.net.URL>
getResources(java.lang.String path)
boolean
isActive(BeanConfiguration configuration)
Whether the current environment includes the given configuration.boolean
isPresent(java.lang.String className)
Check whether the given class is present within this environment.boolean
isRunning()
protected java.util.List<PropertySource>
readPropertySourceList(java.lang.String name)
protected java.util.List<PropertySource>
readPropertySourceListFromFiles(java.lang.String files)
Resolve the property sources for files passed via system property and system env.protected java.lang.String
readPropertySourceListKeyFromEnvironment()
Reads the value of MICRONAUT_CONFIG_FILES environment variable.protected io.micronaut.core.io.service.SoftServiceLoader<PropertySourceLoader>
readPropertySourceLoaders()
protected void
readPropertySources(java.lang.String name)
java.util.Map<java.lang.String,java.lang.Object>
refreshAndDiff()
Refresh the environment from the list ofPropertySource
instances and return a diff of the changes.Environment
removePropertySource(PropertySource propertySource)
Removes a property source from this environment.java.util.stream.Stream<java.lang.Class<?>>
scan(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Scan the current environment for classes annotated with the given annotation.java.util.stream.Stream<java.lang.Class<?>>
scan(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String... packages)
Scan the current environment for classes annotated with the given annotation.protected boolean
shouldDeduceEnvironments()
Environment
start()
Starts the lifecyle component.Environment
stop()
Stops the life cycle component.boolean
supportsPrefix(java.lang.String path)
-
Methods inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
containsProperties, containsProperty, getAllProperties, getProperties, getProperty, getPropertyEntries, isLogEnabled, processPropertySource, resetCaches, resolveEntriesForKey, resolveEntriesForKey, resolveSubMap, resolveSubMap, resolveSubProperties, setLogEnabled
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.micronaut.core.convert.ConversionService
convert, convert, convert, convertRequired, convertRequired
-
Methods inherited from interface io.micronaut.context.env.Environment
addPackage
-
-
-
-
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 configurationlogEnabled
- 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 interfaceEnvironment
- Parameters:
className
- The class name- Returns:
- True if it is
-
getPlaceholderResolver
public PropertyPlaceholderResolver getPlaceholderResolver()
- Specified by:
getPlaceholderResolver
in interfaceEnvironment
- Returns:
- The placeholder resolver
-
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 interfaceEnvironment
- 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 interfaceEnvironment
- Parameters:
annotation
- The annotation to scanpackages
- The packages to scan- Returns:
- The classes
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Specified by:
getClassLoader
in interfaceEnvironment
- 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 interfaceEnvironment
- Parameters:
configuration
- The configuration- Returns:
- True if it does
-
addPropertySource
public DefaultEnvironment addPropertySource(PropertySource propertySource)
Description copied from class:PropertySourcePropertyResolver
Add aPropertySource
to this resolver.- Specified by:
addPropertySource
in interfaceEnvironment
- Overrides:
addPropertySource
in classPropertySourcePropertyResolver
- Parameters:
propertySource
- ThePropertySource
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 interfaceEnvironment
- Parameters:
propertySource
- The property source- Returns:
- This environment
-
addPropertySource
public DefaultEnvironment addPropertySource(java.lang.String name, java.util.Map<java.lang.String,? super java.lang.Object> values)
Description copied from class:PropertySourcePropertyResolver
Add a property source for the given map.- Specified by:
addPropertySource
in interfaceEnvironment
- Overrides:
addPropertySource
in classPropertySourcePropertyResolver
- Parameters:
name
- The name of the property sourcevalues
- The values- Returns:
- This environment
-
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 interfaceEnvironment
- 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 interfaceEnvironment
- 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 interfaceEnvironment
- Parameters:
names
- The names of the configuration- Returns:
- This environment
-
getPackages
public java.util.Collection<java.lang.String> getPackages()
- Specified by:
getPackages
in interfaceEnvironment
- 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 interfaceEnvironment
- Returns:
- The active environment names
-
getPropertySources
public java.util.Collection<PropertySource> getPropertySources()
- Specified by:
getPropertySources
in interfaceEnvironment
- Returns:
- The active property sources
-
start
public Environment start()
Description copied from interface:LifeCycle
Starts the lifecyle component.- Specified by:
start
in interfaceLifeCycle<Environment>
- Returns:
- This lifecycle component
-
isRunning
public boolean isRunning()
- Specified by:
isRunning
in interfaceLifeCycle<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 interfaceLifeCycle<Environment>
- Returns:
- This lifecycle component
-
refreshAndDiff
public java.util.Map<java.lang.String,java.lang.Object> refreshAndDiff()
Description copied from interface:Environment
Refresh the environment from the list ofPropertySource
instances and return a diff of the changes.- Specified by:
refreshAndDiff
in interfaceEnvironment
- 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 interfaceio.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 interfaceio.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 interfaceio.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 interfaceio.micronaut.core.convert.ConversionService<Environment>
-
getResourceAsStream
public java.util.Optional<java.io.InputStream> getResourceAsStream(java.lang.String path)
- Specified by:
getResourceAsStream
in interfaceio.micronaut.core.io.ResourceLoader
-
getResource
public java.util.Optional<java.net.URL> getResource(java.lang.String path)
- Specified by:
getResource
in interfaceio.micronaut.core.io.ResourceLoader
-
getResources
public java.util.stream.Stream<java.net.URL> getResources(java.lang.String path)
- Specified by:
getResources
in interfaceio.micronaut.core.io.ResourceLoader
-
supportsPrefix
public boolean supportsPrefix(java.lang.String path)
- Specified by:
supportsPrefix
in interfaceio.micronaut.core.io.ResourceLoader
-
forBase
public io.micronaut.core.io.ResourceLoader forBase(java.lang.String basePath)
- Specified by:
forBase
in interfaceio.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
-
getPropertySourceLoaders
public java.util.Collection<PropertySourceLoader> getPropertySourceLoaders()
Obtains thePropertySourceLoader
instances.- Specified by:
getPropertySourceLoaders
in interfaceEnvironment
- Returns:
- A collection of
PropertySourceLoader
-
close
public void close()
Description copied from interface:LifeCycle
Delegates toLifeCycle.stop()
.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceLifeCycle<Environment>
- Overrides:
close
in classPropertySourcePropertyResolver
-
-