public class DefaultEnvironment extends PropertySourcePropertyResolver implements Environment
The default implementation of the Environment
interface. Configures a named environment.
PropertySourcePropertyResolver.PropertyCatalog
Modifier and Type | Field and Description |
---|---|
protected java.util.List<PropertySource> |
refreshablePropertySources |
protected ClassPathResourceLoader |
resourceLoader |
catalog, conversionService, nonGenerated, propertyPlaceholderResolver, propertySources, rawCatalog
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
SHARED
Constructor and Description |
---|
DefaultEnvironment(ApplicationContextConfiguration configuration)
Construct a new environment for the given configuration.
|
Modifier and Type | Method and Description |
---|---|
Environment |
addConfigurationExcludes(java.lang.String... names)
Exclude configurations by name.
|
Environment |
addConfigurationIncludes(java.lang.String... names)
Exclude configurations by name.
|
<S,T> Environment |
addConverter(java.lang.Class<S> sourceType,
java.lang.Class<T> targetType,
java.util.function.Function<S,T> typeConverter)
Adds a type converter.
|
<S,T> Environment |
addConverter(java.lang.Class<S> sourceType,
java.lang.Class<T> targetType,
TypeConverter<S,T> typeConverter)
Adds a type converter.
|
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 a
PropertySource 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> boolean |
canConvert(java.lang.Class<S> sourceType,
java.lang.Class<T> targetType)
Return whether the given source type is convertible to the given target type.
|
<T> java.util.Optional<T> |
convert(java.lang.Object object,
java.lang.Class<T> targetType,
ConversionContext context)
Attempts to convert the given object to the given target type.
|
protected ClassPathAnnotationScanner |
createAnnotationScanner(java.lang.ClassLoader classLoader)
Creates the default annotation scanner.
|
ResourceLoader |
forBase(java.lang.String basePath)
Constructs a new resource loader designed to load
resources from the given path.
|
java.util.Set<java.lang.String> |
getActiveNames()
Should respect the order as provided.
|
java.lang.ClassLoader |
getClassLoader() |
java.util.Collection<java.lang.String> |
getPackages() |
PropertyPlaceholderResolver |
getPlaceholderResolver() |
java.util.Collection<PropertySourceLoader> |
getPropertySourceLoaders()
Obtains the
PropertySourceLoader instances. |
protected java.lang.String |
getPropertySourceRootName() |
java.util.Collection<PropertySource> |
getPropertySources() |
java.util.Optional<java.net.URL> |
getResource(java.lang.String path)
Obtains the URL to a given resource.
|
java.util.Optional<java.io.InputStream> |
getResourceAsStream(java.lang.String path)
Obtains a resource as a stream.
|
java.util.stream.Stream<java.net.URL> |
getResources(java.lang.String path)
Obtains all resources with the given name.
|
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 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 of
PropertySource 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) |
containsProperties, containsProperty, getAllProperties, getProperties, getProperty, getPropertyEntries, processPropertySource, resetCaches, resolveEntriesForKey, resolveEntriesForKey, resolveSubMap, resolveSubMap, resolveSubProperties
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addPackage
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, getRequiredProperty, nameOf
get, get, get
convert, convert, convert, convertRequired, convertRequired
protected final ClassPathResourceLoader resourceLoader
protected final java.util.List<PropertySource> refreshablePropertySources
public DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration)
configuration
- The configurationpublic boolean isPresent(java.lang.String className)
Environment
isPresent
in interface Environment
className
- The class namepublic PropertyPlaceholderResolver getPlaceholderResolver()
getPlaceholderResolver
in interface Environment
public java.util.stream.Stream<java.lang.Class> scan(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Environment
scan
in interface Environment
annotation
- The annotation to scanpublic java.util.stream.Stream<java.lang.Class> scan(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.String... packages)
Environment
scan
in interface Environment
annotation
- The annotation to scanpackages
- The packages to scanpublic java.lang.ClassLoader getClassLoader()
getClassLoader
in interface Environment
public boolean isActive(BeanConfiguration configuration)
Environment
isActive
in interface Environment
configuration
- The configurationpublic DefaultEnvironment addPropertySource(PropertySource propertySource)
PropertySourcePropertyResolver
PropertySource
to this resolver.addPropertySource
in interface Environment
addPropertySource
in class PropertySourcePropertyResolver
propertySource
- The PropertySource
to addPropertySourcePropertyResolver
public Environment removePropertySource(PropertySource propertySource)
Environment
removePropertySource
in interface Environment
propertySource
- The property sourcepublic DefaultEnvironment addPropertySource(java.lang.String name, java.util.Map<java.lang.String,? super java.lang.Object> values)
PropertySourcePropertyResolver
addPropertySource
in interface Environment
addPropertySource
in class PropertySourcePropertyResolver
name
- The name of the property sourcevalues
- The valuespublic Environment addPackage(java.lang.String pkg)
Environment
addPackage
in interface Environment
pkg
- The package to addpublic Environment addConfigurationExcludes(@Nullable java.lang.String... names)
Environment
addConfigurationExcludes
in interface Environment
names
- The names of the configurationpublic Environment addConfigurationIncludes(java.lang.String... names)
Environment
addConfigurationIncludes
in interface Environment
names
- The names of the configurationpublic java.util.Collection<java.lang.String> getPackages()
getPackages
in interface Environment
public java.util.Set<java.lang.String> getActiveNames()
Environment
getActiveNames
in interface Environment
public java.util.Collection<PropertySource> getPropertySources()
getPropertySources
in interface Environment
public Environment start()
LifeCycle
start
in interface LifeCycle<Environment>
public boolean isRunning()
isRunning
in interface LifeCycle<Environment>
public Environment stop()
LifeCycle
stop
in interface LifeCycle<Environment>
public java.util.Map<java.lang.String,java.lang.Object> refreshAndDiff()
Environment
PropertySource
instances and return a diff of the changes.refreshAndDiff
in interface Environment
public <T> java.util.Optional<T> convert(java.lang.Object object, java.lang.Class<T> targetType, ConversionContext context)
ConversionService
Optional
is returned.convert
in interface ConversionService<Environment>
T
- The generic typeobject
- The object to converttargetType
- The target typecontext
- The conversion contextpublic <S,T> boolean canConvert(java.lang.Class<S> sourceType, java.lang.Class<T> targetType)
ConversionService
canConvert
in interface ConversionService<Environment>
S
- The generic source typeT
- The target source typesourceType
- The source typetargetType
- The target typepublic <S,T> Environment addConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, TypeConverter<S,T> typeConverter)
ConversionService
addConverter
in interface ConversionService<Environment>
S
- The source generic typeT
- The target generic typesourceType
- The source typetargetType
- The target typetypeConverter
- The type converterpublic <S,T> Environment addConverter(java.lang.Class<S> sourceType, java.lang.Class<T> targetType, java.util.function.Function<S,T> typeConverter)
ConversionService
addConverter
in interface ConversionService<Environment>
S
- The source generic typeT
- The target generic typesourceType
- The source typetargetType
- The target typetypeConverter
- The type converterpublic java.util.Optional<java.io.InputStream> getResourceAsStream(java.lang.String path)
ResourceLoader
getResourceAsStream
in interface ResourceLoader
path
- The pathpublic java.util.Optional<java.net.URL> getResource(java.lang.String path)
ResourceLoader
getResource
in interface ResourceLoader
path
- The pathpublic java.util.stream.Stream<java.net.URL> getResources(java.lang.String path)
ResourceLoader
getResources
in interface ResourceLoader
path
- The name of the resourcepublic boolean supportsPrefix(java.lang.String path)
supportsPrefix
in interface ResourceLoader
path
- The path to a resource including a prefix
appended by a colon. Ex (classpath:, file:)public ResourceLoader forBase(java.lang.String basePath)
ResourceLoader
forBase
in interface ResourceLoader
basePath
- The path to load resourcesResourceLoader
protected boolean shouldDeduceEnvironments()
protected ClassPathAnnotationScanner createAnnotationScanner(java.lang.ClassLoader classLoader)
classLoader
- The class loaderprotected java.lang.String getPropertySourceRootName()
protected void readPropertySources(java.lang.String name)
name
- The name to read property sourcesprotected java.lang.String readPropertySourceListKeyFromEnvironment()
protected java.util.List<PropertySource> readPropertySourceListFromFiles(java.lang.String files)
files
- The comma separated list of filesprotected java.util.List<PropertySource> readPropertySourceList(java.lang.String name)
name
- The name to resolver property sourcesprotected void addDefaultPropertySources(java.util.List<PropertySource> propertySources)
propertySources
- The list of property sourcesprotected SoftServiceLoader<PropertySourceLoader> readPropertySourceLoaders()
SoftServiceLoader
public java.util.Collection<PropertySourceLoader> getPropertySourceLoaders()
PropertySourceLoader
instances.getPropertySourceLoaders
in interface Environment
PropertySourceLoader