In fact I found such message before:
Configuring and looking up a simple String via JNDI in WebSphere
I want to do the "clean" way (http://www.ibm.com/developerworks/websphere/library/techarticles/0611_totapally/0611_totapally.html). Maybe I am simply too green on WebSphere, my mind is totally messed up by the article because it is showing a more complicated example.
Can someone give me a brief steps on how to configure WebSphere so that I can bind a String value in JNDI? Thanks so much
Well, assuming that what you want to store is a "configuration" element, I'd actually take the time and implement it using Resource Environment Providers as it's a more flexible, and obviously more portable, approach.
In order to bind a String using JNDI, use "String Namespace Bindings": http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.nd.doc/info/ae/ae/unam_rstring_namespace.html
Edited (to add details about ResEnv):
The factory class is a class that you write, not IBM. Here's an example for one:
public class MyFactory implements javax.naming.ObjectFactory {
@Override
public Object getObjectInstance(Object object, Name name, Context context, Hashtable env) throws NamingException {
MyConfig cfg = new MyConfig();
for (Enumeration e = ((Reference) object).getAll(); e.hasMoreElements();) {
RefAddr addr = (RefAddr) e.nextElement();
String key = addr.getType();
Object value = addr.getContent();
cfg.put(key, value);
}
}
}
(We're assuming that MyConfig
is a simple class with get(String)
and put(String, String)
methods)
Next, create a new Resource Environment Provider in WebSphere. For a Referenceable, put the fully-qualified class name of MyFactory as the "factory class" and the fully-qualified class name of MyConfig as the "class".
Next, add a Resource Environment entry. Give it a JNDI name, say myresources/test
. Select the Referenceable from the dropdown box.
Click "Custom Properties", and add key-value pairs there.
Save the WebSphere configuration, restart your server and try again. Issue a JNDI lookup to myresources/test
, and you'll get a MyConfig
instance with all the custom properties you placed there.
Let us know how it went. Good luck!
I posted some JNDI configuration tips along with their pros and cons. It's a bit dated but should still apply.