Please, consider the following piece of a typical VMWare configuration file (*.vmx):
memsize = "2048"
MemTrimRate = "-1"
mks.enable3d = "TRUE"
nvram = "Windows Server 2003 Standard Edition.nvram"
pciBridge0.pciSlotNumber = "17"
pciBridge0.present = "TRUE"
pciBridge4.functions = "8"
pciBridge4.pciSlotNumber = "18"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge5.pciSlotNumber = "19"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge6.pciSlotNumber = "20"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
pciBridge7.pciSlotNumber = "32"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
replay.filename = ""
replay.supported = "FALSE"
roamingVM.exitBehavior = "go"
By observing this configuration, one can imagine a PciBridge java bean type with the following signature:
class PciBridge
{
public int pciSlotNumber; // or public int getPciSlotNumber(){...} and public void setPciSlotNumber(int v){...}
public boolean present; // or get/is/set methods
public int functions; // or get/set methods
public String virtualDev; // or get/set methods
}
Moreover, the configuration manager responsible for reading the vmx files might expose the following method:
public <T> List<T> getObjects(final String prop, Class<T> clazz);
And then given the aforementioned configuration, invoking getObjects("pciBridge", PciBridge.class) would return a list of all the PciBridge objects specified in the configuration – the total of 5 in our case.
How do I implement this functionality? Of course, I have seen the same pattern in several different products, so I figure there should be something ready out there to implement this functionality.
Any ideas?
Thanks.
EDIT
Correction – I do not claim that VMWare utilizes the java properties file format (the double quotes are redundant), but the spirit is the same. Besides, there are proper Java applications utilizing the same pattern.
I am posting my own solution. The code depends on http://commons.apache.org/beanutils/ to reflect on the beans and on http://commons.apache.org/configuration/ to manage the property based configuration (because it supports property references using the ${} syntax).