ResourceResolver Configuration with multiple basenames

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ResourceResolver Configuration with multiple basenames

jjamsa

Hi,

 

In Griffon 1.x there was a place in Config.groovy where one could configure a list of basenames to use for resource resolution.

 

How does accomplish the same thing in Griffon 2.x?   In the guide it says “The default basename may be changed to some other value, or additional basenames may be specified too; it’s just a matter of configuring a Module override”.   How does one specify additional basenames without overriding the default (which is ‘resources’)?

 

Jon Jamsa


This e-mail, including attachments, may include confidential and/or
proprietary information, and may be used only by the person or entity
to which it is addressed. If the reader of this e-mail is not the intended
recipient or his or her authorized agent, the reader is hereby notified
that any dissemination, distribution or copying of this e-mail is
prohibited. If you have received this e-mail in error, please notify the
sender by replying to this message and delete this e-mail immediately.

Reply | Threaded
Open this post in threaded view
|

Re: ResourceResolver Configuration with multiple basenames

aalmiray
Administrator
Hi Jon,

You must supply an override for the following binding

        bind(ResourceResolver.class)
            .withClassifier(named("applicationResourceResolver"))
            .toProvider(new ResourceResolverProvider("resources"))
            .asSingleton();

which is defined in the core module http://griffon-framework.org/guide/2.7.0/#_core

Now, this `ResourceResolver` is bound to a single basename. You can create a custom `Provider` that can take multiple basenames as inputs, using a `org.codehaus.griffon.runtime.core.resources.CompositeResourceResolver`. What's important is that you override the matching binding, iow, make sure that your binding defines

        bind(ResourceResolver.class)
            .withClassifier(named("applicationResourceResolver"))
            /* your type goes here */
 

One possible solution would be to do the following

public class MyResourceResolverProvider implements Provider<ResourceResolver> {
    @Inject @Named("basename1")
    private ResourceResolver resourceResolver1;

    @Inject @Named("basename2")
    private ResourceResolver resourceResolver2;

    @Inject
    private ResourceResolverDecoratorFactory resourceResolverDecoratorFactory;

    @Override
    public ResourceResolver get() {
        requireNonNull(resourceResolver1, "Argument 'resourceResolver1' must not be null");
        requireNonNull(resourceResolver2, "Argument 'resourceResolver2' must not be null");
        CompositeResourceResolver compositeResolver = new CompositeResourceResolver(Arrays.asList(resourceResolver1, resourceResolver2));
        return resourceResolverDecoratorFactory.create(compositeResolver);
    }
}

This requires the following bindings

        bind(ResourceResolver.class)
            .withClassifier(named("applicationResourceResolver"))
            .toProvider(MyResourceResolverProvider.class)
            .asSingleton();

        bind(ResourceResolver.class)
            .withClassifier(named("basename1"))
            .toProvider(new ResourceResolverProvider("basename1"))
            .asSingleton();

        bind(ResourceResolver.class)
            .withClassifier(named("basename2"))
            .toProvider(new ResourceResolverProvider("basename2"))
            .asSingleton();

Cheers,
Andres