That's great - you're welcome :)

However, you can cache isDevelopmentStage no matter what ProjectStage
you have. It won't change unless you redeploy your webapp.

Thus I recommend this code:

private Boolean developmentStage = null;

public boolean isDevelopmentStage()
{
    if (developmentStage == null)
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        developmentStage =
facesContext.isProjectStage(ProjectStage.Development);
    }
    return developmentStage;
}

This spares you one FacesContext.getCurrentInstance() -->
ThreadLocal.get() invocation per resource creation (in development
stage).

Regards,
Jakob

2011/3/15 Eisenträger, Tobias <[email protected]>:
> Thank you Jacob - that worked great.
>
> Here's the code for reference:
>
> Faces-config.xml:
> <application>
>        <resource-handler>
> de.example.jsf2.sample.application.CustomResourceHandler
>        </resource-handler>
> </application>
>
> CustomResourceHandler.java:
>
> package de.example.jsf2.sample.application;
>
> import javax.faces.application.ProjectStage;
> import javax.faces.application.Resource;
> import javax.faces.application.ResourceHandler;
> import javax.faces.application.ResourceHandlerWrapper;
> import javax.faces.context.FacesContext;
>
> public class CustomResourceHandler extends ResourceHandlerWrapper {
>
>        private ResourceHandler parent = null;
>        private boolean isDevelopmentStage = true;
>
>        public CustomResourceHandler(ResourceHandler parent) {
>                this.parent = parent;
>        }
>
>        @Override
>        public ResourceHandler getWrapped() {
>                return this.parent;
>        }
>
>        @Override
>        public Resource createResource(String resourceName) {
>                return 
> super.createResource(takeOutMinIfInDevelopmentmode(resourceName));
>        }
>
>        @Override
>        public Resource createResource(String resourceName, String 
> libraryName,String contentType) {
>                return 
> super.createResource(takeOutMinIfInDevelopmentmode(resourceName), 
> libraryName, contentType);
>        }
>
>        @Override
>        public Resource createResource(String resourceName, String 
> libraryName) {
>
>                return 
> super.createResource(takeOutMinIfInDevelopmentmode(resourceName), 
> libraryName);
>        }
>
>        private String takeOutMinIfInDevelopmentmode(String resourceName) {
>                if(getIsDevelopmentStage()) {
>                        if(resourceName!=null && 
> resourceName.contains(".min")) {
>                                resourceName = resourceName.replace(".min", 
> "");
>                        }
>                }
>                return resourceName;
>        }
>
>        private boolean getIsDevelopmentStage() {
>                if(isDevelopmentStage) {
>                        FacesContext fc = FacesContext.getCurrentInstance();
>                        if (fc.isProjectStage(ProjectStage.Development)) {
>                                // isDevelopmentStage; bleibt true
>                        } else {
>                                isDevelopmentStage = false; // fuehrt logik 
> bis zum naechsten neustart nicht mehr aus
>                        }
>                }
>                return isDevelopmentStage;
>        }
>
> }
>
> So now I can just write:
> @ResourceDependencies({
>    @ResourceDependency(name="jquery-ui.css",library="css"),
>    @ResourceDependency(name="jquery.min.js",library="js",target="head"),
>    @ResourceDependency(name="jquery-ui.min.js",library="js",target="head")
>  })
>  @FacesComponent("de.example.jsf2.components.Accordion")
>  public class Accordion extends UIPanel {
>        .....
> }
>
> and it will snip out the .min part when the Stage is Development. Nice 
> feature is that it will only check for the stage one time if we are in 
> Production mode.
>
> Toby
>
>> -----Ursprüngliche Nachricht-----
>> Von: [email protected] [mailto:[email protected]] Im
>> Auftrag von Jakob Korherr
>> Gesendet: Dienstag, 15. März 2011 10:59
>> An: MyFaces Discussion
>> Betreff: Re: ProjectStage and ResourceDependency
>>
>> Hi Tobias,
>>
>> You could write your own ResourceHandler (--> ResourceHandlerWrapper)
>> which wraps the createResource() methods and adjusts the filenames
>> depending on the current ProjectStage.
>>
>> Regards,
>> Jakob
>>
>> 2011/3/15 Eisenträger, Tobias <Tobias.Eisentraeger@...>:
>> > Hello list,
>> >
>> > Is it possible to have different ResourceDependencies for components
>> depending on the Project Stage?
>> >
>> > Example - when the project is in Development Stage the context param
>> javax.faces.PROJECT_STAGE is set to Development
>> >
>> > <context-param>
>> >        <param-name>javax.faces.PROJECT_STAGE</param-name>
>> >        <param-value>Development</param-value>
>> > </context-param>
>> >
>> > I want this:
>> >
>> > @ResourceDependencies({
>> >    @ResourceDependency(name="jquery-ui.css",library="css"),
>> >    @ResourceDependency(name="jquery.js",library="js",target="head"),
>> >
>>  //@ResourceDependency(name="jquery.min.js",library="js",target="head"),
>> >    @ResourceDependency(name="jquery-ui.js",library="js",target="head")
>> >    //@ResourceDependency(name="jquery-
>> ui.min.js",library="js",target="head")
>> > })
>> > @FacesComponent("de.example.jsf2.components.Accordion")
>> > public class Accordion extends UIPanel {
>> >
>> > But when the Project stage is Test or Production I want this:
>> >
>> > @ResourceDependencies({
>> >    @ResourceDependency(name="jquery-ui.css",library="css"),
>> >    //@ResourceDependency(name="jquery.js",library="js",target="head"),
>> >    @ResourceDependency(name="jquery.min.js",library="js",target="head"),
>> >    //@ResourceDependency(name="jquery-ui.js",library="js",target="head")
>> >    @ResourceDependency(name="jquery-
>> ui.min.js",library="js",target="head")
>> > })@FacesComponent("de.example.jsf2.components.Accordion")
>> > public class Accordion extends UIPanel {
>> >
>> > Maybe there is a system in the Resources similar how you can add version
>> numbers.
>> >
>> > Toby
>> >
>> >
>>
>>
>>
>> --
>> Jakob Korherr
>>
>> blog: http://www.jakobk.com
>> twitter: http://twitter.com/jakobkorherr
>> work: http://www.irian.at
>



-- 
Jakob Korherr

blog: http://www.jakobk.com
twitter: http://twitter.com/jakobkorherr
work: http://www.irian.at

Reply via email to