Tamaya Resources (Extension Module)


Tamaya Resources is an extension module. Refer to the extensions documentation for further details about modules.

Tamaya Resources defines some additional tools to locate resources in your classpath or file system based on descriptive ant-styled resource patterns. To use this module add the following dependency:


The module’s main entry point is the singleton class org.apache.tamaya.resource.ConfigResources. This class provides access to a ResourceResolver instance:

ResourceResolver resolver = ConfigResources.getResourceResolver();
public interface ResourceResolver {
    Collection<URL> getResources(Collection<String> expressions) {...}
    Collection<URL> getResources(String... expressions) {...}
    Collection<URL> getResources(ClassLoader classLoader, String... expressions){...}
    Collection<URL> getResources(ClassLoader classLoader, Collection<String> expressions);

Hereby the methods allow to resolve expressions to a collection of URLs. In case the expression is also targeting the current classpath the target ClassLoader to be used can be passed additionally.

The default implementation provides resource resolution mechanism similar to the functionality offered by Spring. So by default resources can be looked up

  • from files

  • from the classpath

  • optionally ant-styled expressions can be used.

Valid Expression Examples

There are numerous ways how a resource pattern can be defined. Following the most important variants are listed:

// explicitly searching the file system

// explicitly searching the classpath

// search both classpath and files

Summarizing the resources module provides useful functionality that helps to locate resources on the file system and in the classpath. This can be used to implement PropertySourceProvider implementations that are based on corresponding resource path patterns instead of concrete files.

Overall Usage Example

Given the functionality we can easily implement a PropertySourceProvider that reads all files from a classpath location, hereby traversing down all folders:

public class PathBasedPropertySourceProvider implements PropertySourceProvider {

    public Collection<PropertySource> getPropertySources() {
        List<PropertySource> propertySources = new ArrayList<>();
        Collection<URL> resources = Resources.getResourceResolver().getResources("META-INF/cfg/**/*.properties");
        for(URL url:resources){
            Properties props = new Properties();
            try(InputStream is = url.openStream()){
                propertySources.add(new PropertiesBasedPropertySource(url.toString(), props));
            catch(Exception e){

        return propertySources;

    private final static class PropertiesBasedPropertySource implements PropertySource {
        private String name;
        private Map<String,String> properties = new HashMap<>();

        public PropertiesBasedPropertySource(String name, Properties props) {
            this.name = name;
            props.forEach((k,v) -> this.properties.put(k.toString(), v.toString()));

        public String getName() {
            return name;

        public String get(String key) {
            return properties.get(key);

        public Map<String, String> getProperties() {
            return properties;


The ResourceResolver that is returned by the ConfigResources singleton is determined by the current ServiceContext, by default you can replace the default implementation by registering an alternate implementation with an overriding @Priority annotation added using the ServiceLoader.

Additionally a BaseResourceResolver class can be used to reduce the amount of code to be written on your own.

Last updated 2016-07-13 23:26:00 +02:00

Back to top

Version: 0.3-incubating-SNAPSHOT. Last Published: 2016-07-13.

Reflow Maven skin by Andrius Velykis.