You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Erik Fäßler <er...@uni-jena.de> on 2011/07/21 17:27:33 UTC

How to use ContextResourceSymbolProvider

  Hey all,

I've been struggling for a while with what seems to be a simple task at 
first:

"On WebApp startup, load a configuration file - located in the app's 
context, i.e. in the WEB-INF directory - and contribute its contents to 
Tapestry's SymbolSource service."

I always get in trouble by the (servlet)context being null when 
contributing. I get the ApplicationGlobals service and get the context 
from it.
I have a solution were the context is _sometimes_ null and sometimes not 
- so I just have to restart the servlet container as often enough as it 
will eventually work. Of course, this is nothing to keep it this way. In 
fact, it's quite annoying ;-)
In the end I thought that it would just not be possible to access the 
ServletContext for contributing a SymbolSource depending on it. But then 
I came about the "ContextResourceSymbolProvider" class. I would have 
thought you would use it that way:

public static void contributeSymbolSource(final 
OrderedConfiguration<SymbolProvider> configuration, 
@InjectService("ApplicationGlobals")ApplicationGlobals applicationGlobals) {
         configuration.add("SemedicoToolsSymbols", new 
ContextResourceSymbolProvider(applicationGlobals.getContext(), 
"configuration.properties"), "before:ApplicationDefaults");
     }

but this does not work as described above: At this point of application 
startup, the context (gotten by applicationGlobals.getContext() as shown 
above) is null.

So I have two questions:
1) How to use ContextResourceSymbolProvider? I currently can't imagine a 
useful employment of this class without being able to contribute it to 
Tapestry's SymbolSource.
2) Is it at all possible to read a file from the context and contribute 
it to the SymbolSource service? Goal: Being able to configure my WebApp 
from file rather than being forced to edit the code every time I change 
deployment (from test to productive for instance; I would have to change 
my local paths to the correct paths in the productive environment; other 
developers have their own local resources, etc).

Thanks for your time!

Best,

     Erik

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to use ContextResourceSymbolProvider

Posted by Erik Fäßler <er...@uni-jena.de>.
Hi again,

thank you all for your kind advice!

My responses follow:

@Josh: This "lazy" approach didn't work for me anyhow. I got just the same NPEs. This really is kind of non-deterministic as it sometimes would work and sometimes it wouldn't. I mean the whole access-the-servlet-context-while-startup-thing.

@Mark: Yes, I know the possibility to give symbol values through web.xml but it just doesn't feel right to mess with development configuration at such an important and central place.

@Mihail: I was not able to exactly replicate what you were proposing. But assume you just read resources from the class path?

In the end the last thing is what I am doing. I moved away from storing my configuration in the servlet context and just treated my configuration file as a plain java resource (that is, I moved it to the src/main/resources folder instead of WEB-INF). Then, the solution is quite short:

public static void contributeSymbolSource(
			final OrderedConfiguration<SymbolProvider> configuration) {
		configuration.add("DevSymbols", new ClasspathResourceSymbolProvider("configuration.properties"), "before:ApplicationDefaults");
}

Works like a charm for me.

Thank you again and best regards,

	Erik

Re: How to use ContextResourceSymbolProvider

Posted by Михаил Слободянюк <sl...@gmail.com>.
Hi!
I simply use this code for it:

    public static void contributeApplicationDefaults(

            MappedConfiguration<String, String> configuration) {

        ResourceBundle bundle = ResourceBundle.getBundle("Cpu");

        Enumeration<String> e = bundle.getKeys();

        while (e.hasMoreElements()) {

            String key = e.nextElement();

            configuration.add(key, bundle.getString(key));

        }
    }

ApplicationDefaults override FactoryDefaults values, and it's not possibly
breakdown library's logic, when we change SymbolSource service directly

Mihail.

Re: How to use ContextResourceSymbolProvider

Posted by Josh Canfield <jo...@gmail.com>.
You can also set them from the command line:

-Dtapestry.production-mode=false

which is mostly how I choose to do it...

On Thu, Jul 21, 2011 at 11:35 AM, Mark <ma...@xeric.net> wrote:
> On Thu, Jul 21, 2011 at 10:27 AM, Erik Fäßler <er...@uni-jena.de> wrote:
>> Goal: Being able to configure my WebApp from
>> file rather than being forced to edit the code every time I change
>> deployment (from test to productive for instance;
>
> You probably already know this, but you can set these from web.xml
> with something like this:
>
>     <context-param>
>         <param-name>tapestry.production-mode</param-name>
>         <param-value>false</param-value>
>     </context-param>
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to use ContextResourceSymbolProvider

Posted by Mark <ma...@xeric.net>.
On Thu, Jul 21, 2011 at 10:27 AM, Erik Fäßler <er...@uni-jena.de> wrote:
> Goal: Being able to configure my WebApp from
> file rather than being forced to edit the code every time I change
> deployment (from test to productive for instance;

You probably already know this, but you can set these from web.xml
with something like this:

     <context-param>
         <param-name>tapestry.production-mode</param-name>
         <param-value>false</param-value>
     </context-param>

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: How to use ContextResourceSymbolProvider

Posted by Josh Canfield <jo...@gmail.com>.
Hmm... I can't find any code that's using it. All my incantations
ended up causing recursion with the Alias service.

Here is something that works by being lazy:

 @Contribute(SymbolSource.class)
    public void
addTestResourceSymbols(OrderedConfiguration<SymbolProvider>
configuration,
                                       @InjectService("ApplicationGlobals")
                                       final ApplicationGlobals
applicationGlobals
    ) {
        configuration.add("testResources", new SymbolProvider() {
            private volatile ContextResourceSymbolProvider provider = null;

            public String valueForSymbol(String symbolName) {
                if (provider == null) {
                    synchronized (this) {
                        if (provider == null)
                            provider = new
ContextResourceSymbolProvider(applicationGlobals.getContext(),
"test.properties");
                    }
                }

                return provider.valueForSymbol(symbolName);
            }
        });
    }



On Thu, Jul 21, 2011 at 8:27 AM, Erik Fäßler <er...@uni-jena.de> wrote:
>  Hey all,
>
> I've been struggling for a while with what seems to be a simple task at
> first:
>
> "On WebApp startup, load a configuration file - located in the app's
> context, i.e. in the WEB-INF directory - and contribute its contents to
> Tapestry's SymbolSource service."
>
> I always get in trouble by the (servlet)context being null when
> contributing. I get the ApplicationGlobals service and get the context from
> it.
> I have a solution were the context is _sometimes_ null and sometimes not -
> so I just have to restart the servlet container as often enough as it will
> eventually work. Of course, this is nothing to keep it this way. In fact,
> it's quite annoying ;-)
> In the end I thought that it would just not be possible to access the
> ServletContext for contributing a SymbolSource depending on it. But then I
> came about the "ContextResourceSymbolProvider" class. I would have thought
> you would use it that way:
>
> public static void contributeSymbolSource(final
> OrderedConfiguration<SymbolProvider> configuration,
> @InjectService("ApplicationGlobals")ApplicationGlobals applicationGlobals) {
>        configuration.add("SemedicoToolsSymbols", new
> ContextResourceSymbolProvider(applicationGlobals.getContext(),
> "configuration.properties"), "before:ApplicationDefaults");
>    }
>
> but this does not work as described above: At this point of application
> startup, the context (gotten by applicationGlobals.getContext() as shown
> above) is null.
>
> So I have two questions:
> 1) How to use ContextResourceSymbolProvider? I currently can't imagine a
> useful employment of this class without being able to contribute it to
> Tapestry's SymbolSource.
> 2) Is it at all possible to read a file from the context and contribute it
> to the SymbolSource service? Goal: Being able to configure my WebApp from
> file rather than being forced to edit the code every time I change
> deployment (from test to productive for instance; I would have to change my
> local paths to the correct paths in the productive environment; other
> developers have their own local resources, etc).
>
> Thanks for your time!
>
> Best,
>
>    Erik
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org