java.lang.String in jndi default context with Apache Geronimo - How?

2k Views Asked by At

In a servlet I do the following:

  Context context = new InitialContext();
  value = (String) context.lookup("java:comp/env/propertyName");

On an Apache Geronimo instance (WAS CE 2.1) how do i associate a value with the key propertyName?

In Websphere AS 6 i can configure these properties for JNDI lookup under the "Name Space Bindings" page in the management console, but for the life of me I can find no way to do this in community edition on the web.

2

There are 2 best solutions below

0
On

It is possible to put your properties in a file and make the name and location of the file a resource-ref of type URL in web.xml. The value of the resource is set in geronimo-web.xml.

Your web.xml will have the following entry:

<resource-ref>
    <res-ref-name>configFileName</res-ref-name>
    <res-type>java.net.URL</res-type>
</resource-ref>

In geronimo-web.xml you define the value for the configFileName

<name:resource-ref>
    <name:ref-name>configFileName</name:ref-name>
    <name:url>file:///etc/myConfigFile</name:url>
</name:resource-ref>

In java you have the following code to lookup the value:

initialContext = new InitialContext();
URL url = (URL) initialContext.lookup("java:comp/env/configFileName");
String configFileName = url.getPath();

Then you have to open the file and read whatever value is in there.

The result of all this is that you have the properties in a file on the filesystem. It will not be overwritten if you redeploy your application.

0
On

One possibility is to add the properties to your web.xml file (in the WEB-INF directory), using one or more <env-entry> tags. For example, something like the following:

<env-entry>
   <description>My string property</descriptor>
   <env-entry-name>propertyName</env-entry-name>
   <env-entry-type>java.lang.String</env-entry-type>
   <env-entry-value>Your string goes here</env-entry-value>
</env-entry>

Each env-entry tag declares a new environment variable that you can then access from the java:comp/env context.

Once you add the necessary env-entry's you can use code similar to what you already posted to access these values. Mind you, I don't have Geronimo installed, so I don't know if there is any additional configuration that needs to be done in order to make this work.