concept ipojo in category osgi

appears as: iPOJO, iPOJO, The iPOJO
OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

In this chapter and the next, we’ll look at three different OSGi-based component frameworks: Declarative Services, Blueprint Container, and iPOJO. You’ll re-create the example paint program using each of these component frameworks. For each, you’ll have the components shown in figure 11.3, where each component is packaged in its own bundle (although this isn’t strictly necessary).

In the last chapter, we introduced you to component-oriented programming and how it relates to OSGi. We also introduced a lightweight component framework defined by the OSGi Alliance, called Declarative Services, which you used to recreate your paint program. Declarative Services is just one possible component framework for OSGi. In this chapter, we’ll introduce you to two more: Blueprint and iPOJO. These component frameworks provide more advanced capabilities than Declarative Services.

iPOJO uses byte-code manipulation to instrument component class files. This instrumentation inserts hooks into the component class file so it can be externally managed. Although iPOJO also supports execution-time byte-code instrumentation, the simplest way to get it done is with a build-time step to process your components. To achieve this, iPOJO integrates with Ant, Maven, and Eclipse. As an example, here’s the Ant task for the circle bundle:

<taskdef name="ipojo"
         classname="org.apache.felix.ipojo.task.iPojoTask"
         classpath="${lib}/felix/org.apache.felix.ipojo.ant-1.6.0.jar" />
<ipojo input="${dist}/${ant.project.name}-${version}.jar"
       metadata = "OSGI-INF/circle.xml"/>

Upon completion of this build step, iPOJO has instrumented the byte code of any components contained in the referenced bundle. The details of how iPOJO instruments the component byte code aren’t as important; but for the curious, iPOJO instruments all components in a single, generic way to enable intercepting member field and method accesses. All functionality provided by the iPOJO component framework (providing services, requiring services, and so on) is provided by handlers using these interception hooks. The hooks themselves don’t change the component behavior; the modified component classes behave the same as before and can still be use without iPOJO, although they now have some code dependencies on it.

12.2.5. Instantiating components with iPOJO

At this point, you’ve seen how to define an iPOJO component using Java annotations, and we’ve looked into component lifecycle issues; but, surprisingly, nothing you’ve learned so far creates any component instances. Unlike Declarative Services and Blueprint, where component definitions are typically treated as configured component instances, iPOJO always treats a component definition as a type definition. The distinction is the same as between a class (type) and an object (instance).

An iPOJO component description defines a template for creating component instances; but creating an instance requires an extra step, much like using new in Java to create an object. How do you accomplish this in iPOJO? There are four possibilities: