How to get property in iPOJO

96 Views Asked by At

I have a simple component as follows:

@Component (name="Test")
@Instantiate
public class Test {
    @Property(name="foo", value="my-instance-2")
    String buffer = "abcbuffer";
    public Test() {
        System.out.println("test running");
    }
}

i use the "instance iTest" i have the result

g! instance iTest
instance name="iTest" state="valid" bundle="31" component.type="Test"
        handler name="org.apache.felix.ipojo:properties" state="valid"
                property name="foo" value="abcbuffer"
        handler name="org.apache.felix.ipojo:callback" state="valid"
        handler name="org.apache.felix.ipojo:architecture" state="valid"
        object name="test.Test@637a91a"

How to i get "buffer" property via "introspection" from another component? i want to get the "abcbuffer" value.

Thanks,

3

There are 3 best solutions below

0
HNT On

In order to respond this question, i do as follows:

for (Factory factory : factories) {
        if (factory.getName().equals("Test")) { //Test is default name of a component name
            InstanceManager im = (InstanceManager) factory.getInstances().get(0);

            String buffer = (Strig) im.getFieldValue("buffer");

            System.out.println(buffer);
    }

}
0
HNT On

And to update dynamically the buffer value, we use:

            ComponentInstance ci = (ComponentInstance) factory.getInstances().get(0);
            Properties props = new Properties();
            String newbuffer = new String("newValue");
            props.put("buffer", newbuffer);
            ci.reconfigure(props);
0
colin aygalinc On

All iPOJO component are wrapped with an architecture handler. You can track this architecture handler,through the Architecture service, and use it to introspect the component. It is currently what the instance command do if I remember.