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>
,ConversionService
,MutableConversionService
,ResourceLoader
,PropertyResolver
,ValueResolver<String>
,Closeable
,AutoCloseable
The default implementation of the Environment
interface. Configures a named environment.
- Since:
- 1.0
- Author:
- Graeme Rocher, rvanderwerf
-
Nested Class Summary
Nested classes/interfaces inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
PropertySourcePropertyResolver.PropertyCatalog
-
Field Summary
Modifier and TypeFieldDescriptionprotected final MutableConversionService
protected final List<PropertySource>
protected final ClassPathResourceLoader
Fields inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
catalog, conversionService, log, nonGenerated, propertyPlaceholderResolver, propertySources, rawCatalog
Fields inherited from interface io.micronaut.core.convert.ConversionService
SHARED
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_CLOUD_ENVIRONMENT_ENV, DEDUCE_CLOUD_ENVIRONMENT_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
ConstructorDescriptionDefaultEnvironment
(@NonNull ApplicationContextConfiguration configuration) Construct a new environment for the given configuration.DefaultEnvironment
(@NonNull ApplicationContextConfiguration configuration, boolean logEnabled) Construct a new environment for the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionaddConfigurationExcludes
(@Nullable String... names) Exclude configurations by name.addConfigurationIncludes
(String... names) Exclude configurations by name.<S,
T> void addConverter
(Class<S> sourceType, Class<T> targetType, TypeConverter<S, T> typeConverter) Adds a type converter.<S,
T> void addConverter
(Class<S> sourceType, Class<T> targetType, Function<S, T> typeConverter) Adds a type converter.protected void
addDefaultPropertySources
(List<PropertySource> propertySources) Adds default property sources.addPackage
(String pkg) Add an application package.addPropertySource
(PropertySource propertySource) Add aPropertySource
to this resolver.addPropertySource
(String name, Map<String, ? super Object> values) Add a property source for the given map.<S,
T> boolean canConvert
(Class<S> sourceType, Class<T> targetType) Return whether the given source type is convertible to the given target type.void
close()
Delegates toLifeCycle.stop()
.<T> Optional<T>
convert
(Object object, Class<T> targetType, ConversionContext context) Attempts to convert the given object to the given target type.<S,
T> Optional<T> convert
(S object, Class<? super S> sourceType, Class<T> targetType, ConversionContext context) Attempts to convert the given object to the given target type from the given source type.protected BeanIntrospectionScanner
createAnnotationScanner
(ClassLoader classLoader) Creates the default annotation scanner.Constructs a new resource loader designed to load resources from the given path.Should respect the order as provided.protected List<PropertySource>
Obtains thePropertySourceLoader
instances.protected String
getResource
(String path) Obtains the URL to a given resource.getResourceAsStream
(String path) Obtains a resource as a stream.getResources
(String path) Obtains all resources with the given name.boolean
isActive
(BeanConfiguration configuration) Whether the current environment includes the given configuration.boolean
Check whether the given class is present within this environment.boolean
protected List<PropertySource>
readPropertySourceList
(String name) protected List<PropertySource>
Resolve the property sources for files passed via system property and system env.protected String
Reads the value of MICRONAUT_CONFIG_FILES environment variable.protected SoftServiceLoader<PropertySourceLoader>
protected void
readPropertySources
(String name) Refresh the environment from the list ofPropertySource
instances and return a diff of the changes.removePropertySource
(PropertySource propertySource) Removes a property source from this environment.scan
(Class<? extends Annotation> annotation) Scan the current environment for classes annotated with the given annotation.scan
(Class<? extends Annotation> annotation, String... packages) Scan the current environment for classes annotated with the given annotation.protected boolean
protected boolean
start()
Starts the lifecyle component.stop()
Stops the life cycle component.boolean
supportsPrefix
(String path) Methods inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
containsProperties, containsProperty, getAllProperties, getProperties, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, processPropertySource, resetCaches, resolveEntriesForKey, resolveSubMap, resolveSubMap, resolveSubProperties
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, convert, convertRequired, convertRequired, convertRequired
Methods inherited from interface io.micronaut.context.env.Environment
addPackage
Methods inherited from interface io.micronaut.core.value.PropertyResolver
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, getRequiredProperty
Methods inherited from interface io.micronaut.core.value.ValueResolver
get, get, get
-
Field Details
-
resourceLoader
-
refreshablePropertySources
-
mutableConversionService
-
-
Constructor Details
-
DefaultEnvironment
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 configurationlogEnabled
- flag to enable or disable logger
-
-
Method Details
-
isPresent
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
- Specified by:
getPlaceholderResolver
in interfaceEnvironment
- Returns:
- The placeholder resolver
-
scan
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
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
- Specified by:
getClassLoader
in interfaceEnvironment
- Returns:
- The class loader for the environment
-
isActive
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
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
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
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
Description copied from interface:Environment
Add an application package. Application packages are candidates for scanning for tools that need it (such as JPA).- Specified by:
addPackage
in interfaceEnvironment
- Parameters:
pkg
- The package to add- Returns:
- This environment
-
addConfigurationExcludes
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
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
- Specified by:
getPackages
in interfaceEnvironment
- Returns:
- The application packages
-
getActiveNames
Description copied from interface:Environment
Should respect the order as provided.- Specified by:
getActiveNames
in interfaceEnvironment
- Returns:
- The active environment names
-
getPropertySources
- Specified by:
getPropertySources
in interfaceEnvironment
- Returns:
- The active property sources
-
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
Description copied from interface:LifeCycle
Stops the life cycle component.- Specified by:
stop
in interfaceLifeCycle<Environment>
- Returns:
- This lifecycle component
-
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
Description copied from interface:ConversionService
Attempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptional
is returned.- Specified by:
convert
in interfaceConversionService
- Type Parameters:
T
- The target type- Parameters:
object
- The object to converttargetType
- The target typecontext
- The conversion context- Returns:
- The optional
-
convert
public <S,T> Optional<T> convert(S object, Class<? super S> sourceType, Class<T> targetType, ConversionContext context) Description copied from interface:ConversionService
Attempts to convert the given object to the given target type from the given source type. If conversion fails or is not possible an emptyOptional
is returned.- Specified by:
convert
in interfaceConversionService
- Type Parameters:
S
- The source typeT
- The target type- Parameters:
object
- The object to convertsourceType
- The source typetargetType
- The target typecontext
- The conversion context- Returns:
- The optional
-
canConvert
Description copied from interface:ConversionService
Return whether the given source type is convertible to the given target type.- Specified by:
canConvert
in interfaceConversionService
- Type Parameters:
S
- The generic source typeT
- The target source type- Parameters:
sourceType
- The source typetargetType
- 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 interfaceMutableConversionService
- Type Parameters:
S
- The source generic typeT
- The target generic type- Parameters:
sourceType
- The source typetargetType
- The target typetypeConverter
- 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 interfaceMutableConversionService
- Type Parameters:
S
- The source generic typeT
- The target generic type- Parameters:
sourceType
- The source typetargetType
- The target typetypeConverter
- The type converter
-
getMutableConversionService
- Returns:
- The mutable conversion service.
-
getResourceAsStream
Description copied from interface:ResourceLoader
Obtains a resource as a stream.- Specified by:
getResourceAsStream
in interfaceResourceLoader
- Parameters:
path
- The path- Returns:
- An optional resource
-
getResource
Description copied from interface:ResourceLoader
Obtains the URL to a given resource.- Specified by:
getResource
in interfaceResourceLoader
- Parameters:
path
- The path- Returns:
- An optional resource
-
getResources
Description copied from interface:ResourceLoader
Obtains all resources with the given name.- Specified by:
getResources
in interfaceResourceLoader
- Parameters:
path
- The name of the resource- Returns:
- A stream of URLs
-
supportsPrefix
- Specified by:
supportsPrefix
in interfaceResourceLoader
- 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
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 interfaceResourceLoader
- 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
Creates the default annotation scanner.- Parameters:
classLoader
- The class loader- Returns:
- The scanner
-
getPropertySourceRootName
- Returns:
- The property source root name
-
readPropertySources
- Parameters:
name
- The name to read property sources
-
getConstantPropertySources
- Returns:
- Property sources created at build time
-
readPropertySourceListKeyFromEnvironment
Reads the value of MICRONAUT_CONFIG_FILES environment variable.- Returns:
- The comma-separated list of files
-
readPropertySourceListFromFiles
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
- Parameters:
name
- The name to resolver property sources- Returns:
- The list of property sources
-
addDefaultPropertySources
Adds default property sources.- Parameters:
propertySources
- The list of property sources
-
readPropertySourceLoaders
- Returns:
- Loaded properties as a
SoftServiceLoader
-
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceLifeCycle<Environment>
- Overrides:
close
in classPropertySourcePropertyResolver
-