This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes getProperty() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getProperty() " Watch "getProperty() " New topic
Author

getProperty()

kacuak
Greenhorn

Joined: Mar 24, 2004
Posts: 9
Hi, these are part of my project's source code (which is not written by me).
The output of execution of the code is
sTemp = false
sTemp = false
RNIF.validateMsgs : false
Outgoing messages are NOT being validated.
As far as I understand is, because of getProperty() method inside getRNIFValidate() in RNIABProps.java return false, so end up the validateMsgs() in RNIF.java also return false.
Question is:
What is the action of getProperty() method which is in Java.util.Properties?
I don't understand it even after I read the documentation.
can someone explain to me..?
//RNIF.java
if (RNIF.validateMsgs()) {
.....
} else {
System.out.println("RNIF.validateMsgs : " + RNIF.validateMsgs());
System.out.println("Outgoing messages are NOT being validated.");
}
//RNIF.java
public static boolean validateMsgs() {
RNIABProps props = RNIABProps.getInstance();
String sTemp = props.getRNIFValidate();
System.out.println("sTemp = " + sTemp);
if ((sTemp != null) && (sTemp.toUpperCase().equals("FALSE"))) {
return false;
} else {
return true;
}
}

//RNIABProps.java
public static RNIABProps getInstance() {
if (props == null) {
props = new RNIABProps();
}
return props;
}

//RNIABprobs.java
public String getRNIFValidate() {
return getProperty(RNIAB_RNIF_VALIDATE);
}
[ April 21, 2004: Message edited by: Soo Chin Yeoh ]
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Hi,
The workings of your code would seem to rely heavily on the existence of <props> in your different classes. As you haven't given that code it's a little difficult to say what is going wrong.
With regard to the Properties class:
String getProperty(String s);
The Properties object will contain whatever properties ( key/value pair) you store in it with setProperty(). So if you have instantiated a Properties object, called setProperty("A Key","A Value"), then your Hashtable contains one key/value pair.
If you call String s=getProperty("A Key"); then s will contain "A Value".
If you call String s=getProperty("Another Key") then s will contain null.
However, suppose that you created your object using the one parameter constructor which you would pass in another Properties object to. This would create an object with a second set of Properties stored in the defaults field. This set of Properties should contain a set of default key/value pairs.
If you call String s=getProperty("Another Key"), then your object does not contain this as a key. The method will then look in the defaults field for this key and return the appropriate value assuming that it is there. As defaults may also contain its own set of defaults and so on recursively, the method will only stop when it finds the key or the defaults field is null.
Regards
Kenny
kacuak
Greenhorn

Joined: Mar 24, 2004
Posts: 9
Your explanation is clear and good, What I understand is I have to set a value for Properties using setProperty() other wise the default value should be null, right?
The value of getProperty(RNIF_VALIDATE) I get from my project code is false...
This is a RNIABConstant.java file.......
with the content as below
does this value related with getProperty() value?
public interface RNIABConstants {
.....
// property constants
public final static String RNIAB_HOME = "rniab.home";
public final static String RNIAB_VAULT = "rniab.vault";
public final static String RNIAB_MANIP = "rniab.manip";
public final static String RNIAB_PIPSPECS = "rniab.pipspecs";
public final static String RNIAB_TEMPLATES = "rniab.templates";
public final static String RNIAB_DTD = "rniab.dtds";
public final static String RNIAB_PROTOCOLS = "rniab.protocols";
public final static String RNIAB_TEST = "rniab.test";
public final static String RNIAB_BLANKREC = "rniab.blankrec";
public final static String RNIAB_RNIF_VALIDATE = "rniab.rnifvalidate";
public final static String RNIAB_SIGVALIDATE = "rniab.sigvalidate";
......
}
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Hi,

//RNIABprobs.java
public String getRNIFValidate() {
return getProperty(RNIAB_RNIF_VALIDATE);

Your constants interface (as far as I remember this is not a recommended approach but never mind) contains a value for the given constant. So the above method should be looking in the Hashtable your Properties instance inherits from for the value associated with this key. The code you give for RNIABprobs.java doesn't show where this Properties object is coming from. Is RNIABprobs extending Properties? Does is contain a Properties object?
RNIABConstants simply provides a set of possible keys. The Properties object which getProperty() references is where the action is.
You could use the propertyNames() method of Properties which will return an Enumeration of all the keys, including those in the defaults.
Regards
Kenny
 
Don't get me started about those stupid light bulbs.
 
subject: getProperty()