views:

27

answers:

2

I run webapp on Jetty. The configuration for the app come from file that lives on the same server where Jetty is running. Inside the app I rely on the system property to obtain path to the file so I can parse it. E.g.

final String loc = System.getProperty(FACTORY);

Now I can start jetty with D switch to provide $FACTORY on the command line but I rather put it in jetty.xml if I can. I know there is <SystemProperty /> tag but that seems to just provide system value that already exists for the <Set/> tag. Can someone give me example how this can be achieved? (If it can be achieved)

A: 

To configure a web application it is better to avoid system properties and to use JNDI instead.

Recently I posted an example on how to accomplish that with Jetty.

vanje
Thanks but I still want to know if it can be done and how. Frankly if it set from jetty.xml and not in the system itself I don't see why it is bad
DroidIn.net
The benefit is portability. If you ever will deploy your web app into another container then you can be sure, that there is a way to set JNDI paramaters. As far as I know the `SystemProperty` tag in jetty.xml is only for reading a system property. Here is the Jetty syntax reference: http://docs.codehaus.org/display/JETTY/Syntax+Reference#SyntaxReference-SystemProperty
vanje
The only location I know to set a system property is either the configuration file for the Windows Jetty service or the appropriate Unix shell script.
vanje
There's no question of portability if the parameter set in the config file in platform independend fashion (e.g. as XML tag). I'm not relying on system property to be set. But thank you for your suggestion.
DroidIn.net
A: 

I'm going to accept @vanje answer since it got me thinking into right direction. Here's what I ended up using:

  1. Create jetty-web.xml outside of your WAR distro (no you don't want to package it with WAR if you want to configure the app from "outside")
  2. Place jetty-web.xml alongside of jetty.xml
  3. I needed just a single parameter so I ended up with the following:

jetty-web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"    
     "http://jetty.mortbay.org/configure.dtd"&gt;
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <New class="org.mortbay.jetty.plus.naming.EnvEntry">
        <Arg>myOwnParam</Arg>
        <Arg type="java.lang.String">//some/path/to/the/file</Arg>
    </New>
</Configure>

Java snippet

    InitialContext c = new InitialContext();
    EnvEntry env = (EnvEntry)
         c.lookup("org.mortbay.jetty.plus.naming.EnvEntry/myOwnParam");
    final String myString = (String) env.getObjectToBind();

The biggest gotcha for me here was that I was trying to get myString from the defaul env which didn't work until I realized that JNDI was using local context. This is OK for me but will break portability if you try to move WAR on say Tomcat. If someone can post an example how this can be saved into default context that would be greatOwnParam

DroidIn.net