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 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(String... names)
Exclude configurations by name.
|
Environment |
addConfigurationIncludes(String... names)
Exclude configurations by name.
|
<S,T> Environment |
addConverter(Class<S> sourceType,
Class<T> targetType,
Function<S,T> typeConverter)
Adds a type converter.
|
<S,T> Environment |
addConverter(Class<S> sourceType,
Class<T> targetType,
TypeConverter<S,T> typeConverter)
Adds a type converter.
|
protected void |
addDefaultPropertySources(List<PropertySource> propertySources)
Adds default property sources.
|
Environment |
addPackage(String pkg)
Add an application package.
|
DefaultEnvironment |
addPropertySource(PropertySource propertySource)
Add a
PropertySource to this resolver. |
DefaultEnvironment |
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 to
LifeCycle.stop() . |
<T> Optional<T> |
convert(Object object,
Class<T> targetType,
ConversionContext context)
Attempts to convert the given object to the given target type.
|
protected BeanIntrospectionScanner |
createAnnotationScanner(ClassLoader classLoader)
Creates the default annotation scanner.
|
ResourceLoader |
forBase(String basePath)
Constructs a new resource loader designed to load
resources from the given path.
|
Set<String> |
getActiveNames()
Should respect the order as provided.
|
ClassLoader |
getClassLoader() |
protected List<PropertySource> |
getConstantPropertySources() |
Collection<String> |
getPackages() |
PropertyPlaceholderResolver |
getPlaceholderResolver() |
Collection<PropertySourceLoader> |
getPropertySourceLoaders()
Obtains the
PropertySourceLoader instances. |
protected String |
getPropertySourceRootName() |
Collection<PropertySource> |
getPropertySources() |
Optional<URL> |
getResource(String path)
Obtains the URL to a given resource.
|
Optional<InputStream> |
getResourceAsStream(String path)
Obtains a resource as a stream.
|
Stream<URL> |
getResources(String path)
Obtains all resources with the given name.
|
boolean |
isActive(BeanConfiguration configuration)
Whether the current environment includes the given configuration.
|
boolean |
isPresent(String className)
Check whether the given class is present within this environment.
|
boolean |
isRunning() |
protected List<PropertySource> |
readPropertySourceList(String name) |
protected List<PropertySource> |
readPropertySourceListFromFiles(String files)
Resolve the property sources for files passed via system property and system env.
|
protected String |
readPropertySourceListKeyFromEnvironment()
Reads the value of MICRONAUT_CONFIG_FILES environment variable.
|
protected SoftServiceLoader<PropertySourceLoader> |
readPropertySourceLoaders() |
protected void |
readPropertySources(String name) |
Map<String,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.
|
Stream<Class<?>> |
scan(Class<? extends Annotation> annotation)
Scan the current environment for classes annotated with the given annotation.
|
Stream<Class<?>> |
scan(Class<? extends Annotation> annotation,
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(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 List<PropertySource> refreshablePropertySources
public DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration)
configuration
- The configurationpublic boolean isPresent(String className)
Environment
isPresent
in interface Environment
className
- The class namepublic PropertyPlaceholderResolver getPlaceholderResolver()
getPlaceholderResolver
in interface Environment
public Stream<Class<?>> scan(Class<? extends Annotation> annotation)
Environment
scan
in interface Environment
annotation
- The annotation to scanpublic Stream<Class<?>> scan(Class<? extends Annotation> annotation, String... packages)
Environment
scan
in interface Environment
annotation
- The annotation to scanpackages
- The packages to scanpublic 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(String name, Map<String,? super Object> values)
PropertySourcePropertyResolver
addPropertySource
in interface Environment
addPropertySource
in class PropertySourcePropertyResolver
name
- The name of the property sourcevalues
- The valuespublic Environment addPackage(String pkg)
Environment
addPackage
in interface Environment
pkg
- The package to addpublic Environment addConfigurationExcludes(@Nullable String... names)
Environment
addConfigurationExcludes
in interface Environment
names
- The names of the configurationpublic Environment addConfigurationIncludes(String... names)
Environment
addConfigurationIncludes
in interface Environment
names
- The names of the configurationpublic Collection<String> getPackages()
getPackages
in interface Environment
public Set<String> getActiveNames()
Environment
getActiveNames
in interface Environment
public 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 Map<String,Object> refreshAndDiff()
Environment
PropertySource
instances and return a diff of the changes.refreshAndDiff
in interface Environment
public <T> Optional<T> convert(Object object, 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(Class<S> sourceType, 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(Class<S> sourceType, 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(Class<S> sourceType, Class<T> targetType, 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 Optional<InputStream> getResourceAsStream(String path)
ResourceLoader
getResourceAsStream
in interface ResourceLoader
path
- The pathpublic Optional<URL> getResource(String path)
ResourceLoader
getResource
in interface ResourceLoader
path
- The pathpublic Stream<URL> getResources(String path)
ResourceLoader
getResources
in interface ResourceLoader
path
- The name of the resourcepublic boolean supportsPrefix(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(String basePath)
ResourceLoader
forBase
in interface ResourceLoader
basePath
- The path to load resourcesResourceLoader
protected boolean shouldDeduceEnvironments()
protected BeanIntrospectionScanner createAnnotationScanner(ClassLoader classLoader)
classLoader
- The class loaderprotected String getPropertySourceRootName()
protected void readPropertySources(String name)
name
- The name to read property sourcesprotected List<PropertySource> getConstantPropertySources()
protected String readPropertySourceListKeyFromEnvironment()
protected List<PropertySource> readPropertySourceListFromFiles(String files)
files
- The comma separated list of filesprotected List<PropertySource> readPropertySourceList(String name)
name
- The name to resolver property sourcesprotected void addDefaultPropertySources(List<PropertySource> propertySources)
propertySources
- The list of property sourcesprotected SoftServiceLoader<PropertySourceLoader> readPropertySourceLoaders()
SoftServiceLoader
public Collection<PropertySourceLoader> getPropertySourceLoaders()
PropertySourceLoader
instances.getPropertySourceLoaders
in interface Environment
PropertySourceLoader
public void close()
LifeCycle
LifeCycle.stop()
.close
in interface LifeCycle<Environment>
close
in interface Closeable
close
in interface AutoCloseable
close
in class PropertySourcePropertyResolver