How to handle variable number of context parameters with JNDI

400 views Asked by At

I have a web application that needs to be able to read N number of keys (Strings) out of JNDI at runtime:

Context ctx = new InitialContext();
String[] values = (String[])ctx.lookup("KEYS");

for(String value : values) {
    // Do something with the key's value...
}

This will run on Tomcat, and so I need to be able to store these keys as Context/Parameter elements inside context.xml like so:

<Context>
    <Parameter name="key1" value="value1" override="false" />
    <Parameter name="key2" value="value2" override="false" />
    <Parameter name="key3" value="value3" override="false" />
    ...etc.
</Context>

The problem is, every environment that I deploy this application to (DEV, QA, DEMO, LIVE, etc.) will have a different number of keys. For instance, DEV might only have 1 key (that is, just 1 Context/Parameter element). LIVE might have 20.

How do I accommodate for this in the Java code? The code can't change across environments, so I need a way to load any number of keys via JNDI, using the same Java code, just different Context/Parameters inside context.xml. Any ideas? Thanks in advance!

1

There are 1 answers

1
Prabhakaran Ramaswamy On
Context ctx = new InitialContext();
Enumeration e = ctx.getInitParameterNames();  
while (e.hasMoreElements()) {  
     String key = (String)e.nextElement();  
     String value = getInitParameter(key);  
     out.println("   " + key + " = " + value);  
}