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
Environmentinterface. 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>refreshablePropertySourcesprotected io.micronaut.core.io.scan.ClassPathResourceLoaderresourceLoader-
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 EnvironmentaddConfigurationExcludes(java.lang.String... names)Exclude configurations by name.EnvironmentaddConfigurationIncludes(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 voidaddDefaultPropertySources(java.util.List<PropertySource> propertySources)Adds default property sources.EnvironmentaddPackage(java.lang.String pkg)Add an application package.DefaultEnvironmentaddPropertySource(PropertySource propertySource)Add aPropertySourceto this resolver.DefaultEnvironmentaddPropertySource(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)voidclose()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.BeanIntrospectionScannercreateAnnotationScanner(java.lang.ClassLoader classLoader)Creates the default annotation scanner.io.micronaut.core.io.ResourceLoaderforBase(java.lang.String basePath)java.util.Set<java.lang.String>getActiveNames()Should respect the order as provided.java.lang.ClassLoadergetClassLoader()protected java.util.List<PropertySource>getConstantPropertySources()java.util.Collection<java.lang.String>getPackages()PropertyPlaceholderResolvergetPlaceholderResolver()java.util.Collection<PropertySourceLoader>getPropertySourceLoaders()Obtains thePropertySourceLoaderinstances.protected java.lang.StringgetPropertySourceRootName()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)booleanisActive(BeanConfiguration configuration)Whether the current environment includes the given configuration.booleanisPresent(java.lang.String className)Check whether the given class is present within this environment.booleanisRunning()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.StringreadPropertySourceListKeyFromEnvironment()Reads the value of MICRONAUT_CONFIG_FILES environment variable.protected io.micronaut.core.io.service.SoftServiceLoader<PropertySourceLoader>readPropertySourceLoaders()protected voidreadPropertySources(java.lang.String name)java.util.Map<java.lang.String,java.lang.Object>refreshAndDiff()Refresh the environment from the list ofPropertySourceinstances and return a diff of the changes.EnvironmentremovePropertySource(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 booleanshouldDeduceEnvironments()Environmentstart()Starts the lifecyle component.Environmentstop()Stops the life cycle component.booleansupportsPrefix(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:EnvironmentCheck whether the given class is present within this environment.- Specified by:
isPresentin interfaceEnvironment- Parameters:
className- The class name- Returns:
- True if it is
-
getPlaceholderResolver
public PropertyPlaceholderResolver getPlaceholderResolver()
- Specified by:
getPlaceholderResolverin 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:EnvironmentScan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.- Specified by:
scanin 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:EnvironmentScan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.- Specified by:
scanin interfaceEnvironment- Parameters:
annotation- The annotation to scanpackages- The packages to scan- Returns:
- The classes
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfaceEnvironment- Returns:
- The class loader for the environment
-
isActive
public boolean isActive(BeanConfiguration configuration)
Description copied from interface:EnvironmentWhether the current environment includes the given configuration.- Specified by:
isActivein interfaceEnvironment- Parameters:
configuration- The configuration- Returns:
- True if it does
-
addPropertySource
public DefaultEnvironment addPropertySource(PropertySource propertySource)
Description copied from class:PropertySourcePropertyResolverAdd aPropertySourceto this resolver.- Specified by:
addPropertySourcein interfaceEnvironment- Overrides:
addPropertySourcein classPropertySourcePropertyResolver- Parameters:
propertySource- ThePropertySourceto add- Returns:
- This
PropertySourcePropertyResolver
-
removePropertySource
public Environment removePropertySource(PropertySource propertySource)
Description copied from interface:EnvironmentRemoves a property source from this environment.- Specified by:
removePropertySourcein 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:PropertySourcePropertyResolverAdd a property source for the given map.- Specified by:
addPropertySourcein interfaceEnvironment- Overrides:
addPropertySourcein 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:EnvironmentAdd an application package. Application packages are candidates for scanning for tools that need it (such as JPA or GORM).- Specified by:
addPackagein interfaceEnvironment- Parameters:
pkg- The package to add- Returns:
- This environment
-
addConfigurationExcludes
public Environment addConfigurationExcludes(@Nullable java.lang.String... names)
Description copied from interface:EnvironmentExclude configurations by name.- Specified by:
addConfigurationExcludesin interfaceEnvironment- Parameters:
names- The names of the configuration- Returns:
- This environment
-
addConfigurationIncludes
public Environment addConfigurationIncludes(java.lang.String... names)
Description copied from interface:EnvironmentExclude configurations by name.- Specified by:
addConfigurationIncludesin interfaceEnvironment- Parameters:
names- The names of the configuration- Returns:
- This environment
-
getPackages
public java.util.Collection<java.lang.String> getPackages()
- Specified by:
getPackagesin interfaceEnvironment- Returns:
- The application packages
-
getActiveNames
public java.util.Set<java.lang.String> getActiveNames()
Description copied from interface:EnvironmentShould respect the order as provided.- Specified by:
getActiveNamesin interfaceEnvironment- Returns:
- The active environment names
-
getPropertySources
public java.util.Collection<PropertySource> getPropertySources()
- Specified by:
getPropertySourcesin interfaceEnvironment- Returns:
- The active property sources
-
start
public Environment start()
Description copied from interface:LifeCycleStarts the lifecyle component.- Specified by:
startin interfaceLifeCycle<Environment>- Returns:
- This lifecycle component
-
isRunning
public boolean isRunning()
- Specified by:
isRunningin interfaceLifeCycle<Environment>- Returns:
- Whether the component is running
-
stop
public Environment stop()
Description copied from interface:LifeCycleStops the life cycle component.- Specified by:
stopin interfaceLifeCycle<Environment>- Returns:
- This lifecycle component
-
refreshAndDiff
public java.util.Map<java.lang.String,java.lang.Object> refreshAndDiff()
Description copied from interface:EnvironmentRefresh the environment from the list ofPropertySourceinstances and return a diff of the changes.- Specified by:
refreshAndDiffin 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:
convertin interfaceio.micronaut.core.convert.ConversionService<Environment>
-
canConvert
public <S,T> boolean canConvert(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)- Specified by:
canConvertin 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:
addConverterin 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:
addConverterin interfaceio.micronaut.core.convert.ConversionService<Environment>
-
getResourceAsStream
public java.util.Optional<java.io.InputStream> getResourceAsStream(java.lang.String path)
- Specified by:
getResourceAsStreamin interfaceio.micronaut.core.io.ResourceLoader
-
getResource
public java.util.Optional<java.net.URL> getResource(java.lang.String path)
- Specified by:
getResourcein interfaceio.micronaut.core.io.ResourceLoader
-
getResources
public java.util.stream.Stream<java.net.URL> getResources(java.lang.String path)
- Specified by:
getResourcesin interfaceio.micronaut.core.io.ResourceLoader
-
supportsPrefix
public boolean supportsPrefix(java.lang.String path)
- Specified by:
supportsPrefixin interfaceio.micronaut.core.io.ResourceLoader
-
forBase
public io.micronaut.core.io.ResourceLoader forBase(java.lang.String basePath)
- Specified by:
forBasein 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 thePropertySourceLoaderinstances.- Specified by:
getPropertySourceLoadersin interfaceEnvironment- Returns:
- A collection of
PropertySourceLoader
-
close
public void close()
Description copied from interface:LifeCycleDelegates toLifeCycle.stop().- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceLifeCycle<Environment>- Overrides:
closein classPropertySourcePropertyResolver
-
-