Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • How a component can provide two services
  • How to attach a service property to provided service
  • How a service property can be dynamically updated by component code
  • How to configure instances
  • How a service dependency can filter providers
  • How creating instance for a component not contained in the same bundle
  • How configuring instances
  • How to use the lifecycle controller handler

The sources of this tutorial is available here. This tutorial used the iPOJO Eclipse plugin.

...

When deployed this bundle looks for the required factory. If not available it waits for it apparition. When this bundle is stopped, all instances are destroyed.

Using the lifecycle controller

Sometimes you want to invalidate your instance from your code (to unregister a service...). That's possible with the lifecycle controller handler.
Imagine the popcorn vendor with a corn stock. At each time he sells popcorn, its stock decreases. When the stock reach 0, it can no more sell popcorn (so, the vendor service need to be withdrawed).
The following implementation uses a field to control the lifecycle.

Code Block

public class PopCornVendor implements VendorService {
    
    private int m_corn_stock = 5;
    private boolean m_can_sell = true;
    
    public String getName() {
        return "Eat my pop corn !"; 
    }

    public String sell() {
        m_corn_stock = m_corn_stock - 1;
        if (m_corn_stock == 0 && m_can_sell) {
            m_can_sell = false;
        }
        return "popcorn"; 
    }
}

When the field is set to false, the instance is invalidated (the vendor service is no more available). To configure the controller, just use following metadata :

Code Block
xml
xml

<ipojo>
<component classname="org.apache.felix.ipojo.example.vendor.popcorn.PopCornVendor" name="popcorn" factory="false" architecture="true">
	<provides/>
	<controller field="m_can_sell"/>
</component>

<instance component="popcorn"/>
</ipojo>

The instance can be re-validated if the field is set to true.

Conclusion

This small tutorial has presented some iPOJO features. If you have comments or questions, do not hesitate to send me an email to clement.escoffier@gmail.com.