In my last post I talked about using Resource Bundles to hold settings for your application that the user can modify. These resources are an excellent replacement for the configuration documents or profile documents in traditional domino development. The questions I received over and over again is "How/Can I access those from Java, and can I set that up in some way for the user to work with?"
Well, the two questions go hand in hand. Yes, you can access them from Java! You can read and write to those property files. While I won't go into detail on how you can create a managed bean, and then connect it to your XPage to manage the properties, I will show you how you can load the properties file into your bean, access, and update, then save those properties back to the file.
First off, accessing the properties file. This example assumes you are using the plaintext key=value properties file, not an XML resource. You can use an XML file as well, but the syntax is different.
Ok her's the java code you'll need to access the properties file:
import com.ibm.xsp.context.FacesContextEx;
import java.util.Properties;
...
// Pull from resource file the list of servers we need to catalog...
Properties application;
application = new Properties();
application.load(FacesContextEx.getCurrentInstance().getExternalContext().getResourceAsStream("WEB-INF/application.properties"));
Here, we create a new java.util.Properties object called application, and load the resource file by using the FacesContextEx (from com.ibm.xsp.context.FacesContextEx) object to load the file from the NSF's virtual file system.
Once you have the properties file loaded, you can reference an property value using the getProperty() method, like so:
String servers = application.getProperty("browsableServersNrpc");
As you might have guessed, since there is a getProperty() method for the Properties object, there is also a setProperty method.