SeamFramework.orgCommunity Documentation

Chapter 7. Resource Loading

7.1. Extending the Resource Loader

Solder provides an extensible, injectable resource loader. The resource loader can provide URLs or managed input streams. By default the resource loader will look at the classpath, and the servlet context if available.

If the resource name is known at development time, the resource can be injected, either as a URL or an InputStream:

   @Inject

   @Resource("WEB-INF/beans.xml")
   URL beansXml;
   @Inject
   @Resource("WEB-INF/web.xml")
   InputStream webXml;

If the resource name is not known, the ResourceProvider can be injected, and the resource looked up dynamically:

   @Inject

   void readXml(ResourceProvider provider, String fileName) {
      InputStream is = provider.loadResourceStream(fileName);
   }

If you need access to all resources under a given name known to the resource loader (as opposed to first resource loaded), you can inject a collection of resources:

   @Inject

   @Resource("WEB-INF/beans.xml")
   Collection<URL> beansXmls;
   @Inject
   @Resource("WEB-INF/web.xml")
   Collection<InputStream> webXmls;

Tip

Any input stream injected, or created directly by the ResourceProvider is managed, and will be automatically closed when the bean declaring the injection point of the resource or provider is destroyed.

If the resource is a Properties bundle, you can also inject it as a set of Properties:

   @Inject

   @Resource("META-INF/aws.properties")
   Properties awsProperties;

If you want to load resources from another location, you can provide an additional resource loader. First, create the resource loader implementation:

class MyResourceLoader implements ResourceLoader {

   ...
}

And then register it as a service by placing the fully qualified class name of the implementation in a file called META-INF/services/org.jboss.solder.resourceLoader.ResourceLoader.