views:

90

answers:

1

Hi, I have a structs action object instance that loads a variable from a properties file.I want it to happen only the first time the action is called, so in further executions its read from memory. Any hints ? Thanks.

A: 

At first glance I see at least two ways of doing this:

1 - read the value from the properties file and store it as a static field in your action class. A static initializer should do for loading the bundle and reading the value. When your Action class is loaded by the JVM you will get your value and later just use it from there.

public class YourAction extends Action {
  private static String value;
  static {
    // value = load code here
  }
  ...
}

Since you don't have access to Struts capabilities when this is loaded I think you will have to go for something like the following to read your value:

ResourceBundle.getBundle("com/some/package/bundle").getString("some_key")

2 - have your value set on the Action instance and loaded with your constructor and later just use it:

public class YourAction extends Action {
  private String value;
  public YourAction() {
    super();
    //value = load code here
  }
  ...
}

This will work because Struts uses one Action instance to serve all requests, so all requests will see your value (i.e. Struts Action classes are not thread safe, they behave like servlets).

dpb