Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Section
Column
width80%

Declaring component types

Code Block
xml
xml
titleXML
<component
    classname="my.Implementation"
    name="my-type">
</component>
Code Block
java
java
titleAnnotations
@Component(name="my-type")
public class Implementation {
  // ...
}
Center

Creating component instances

Code Block
xml
xml
titleXML only
<instance component="my-type"/>
<instance component="my.Implementation"/>
<instance component="my-type" name="my-instance"/>
<instance component="my-type" name="my-instance">
    <property name="property1" value="value1"/>
</instance>
Center

  • Instances can contains a configuration given under the key-value form. Properties can also by complex type.
  • How-to use iPOJO factories

Providing services

Code Block
xml
xml
titleXML
<component classname="my.service.implementation" name="my-service-impl">
   <provides/>
</component>
<instance name="my-service-impl"/>
Code Block
java
java
titleAnnotations
@Component
@Provides
public class Implementation implements FooService {
   ...
}
  • Only instances provides really services, so don't forget to declare an instance.
  • Published service interfaces must be implemented by your component implementation
    Center

  • Providing OSGi services

Publishing service properties

Code Block
xml
xml
titleXML
<component classname="my.service.implementation" name="my-service-impl">
  <provides>
	<property name="foo" field="m_foo" />
	<property name="bar" field="m_bar" mandatory="true" />
	<property name="baz" type="java.lang.String" /> <!-- Static property (do not change at runtime) -->
  </provides>
</component>
<instance name="my-service-impl"> <!-- The configuration has to inject value in unvalued mandatory properties -->
  <property name="bar" value="5"/>
  <property name="baz" value="my string"/>
<instance/>
Code Block
java
java
titleAnnotations
@Component
@Provides(specifications= {FooService.class, BarService.class})
public class ProvidesProperties implements FooService, BarService {
    
    @ServiceProperty(name = "foo")
    public int m_foo = 0;
    
    @ServiceProperty(name="bar", mandatory=true)
    public int m_bar;
    
// ...
}
Center

Using services with field injection

Code Block
xml
xml
titleXML
<component classname="my.consumer.Implementation">
    <requires field="fs" />
</component>
Code Block
java
java
titleAnnotations
@Component
public class Dependency {

    @Requires
    public FooService fs;
    
    //...
}
Center

Using services with method injection

Code Block
xml
xml
titleXML
<component classname="my.consumer.Implementation">
    <requires>
	<callback type="bind" method="bind" />
	<callback type="unbind" method="unbind" />
    </requires>	
</component>
Code Block
java
java
titleAnnotations
@Component
public class Dependency {

    @Unbind
    public synchronized void unbind(BazService bz) {
        //...
    }
    
    @Bind
    public synchronized void bind(BazService bz) {
        // ...
    }

  //...
}
Center

Reacting to lifecycle state changes

Immediate components

  • A POJO object (implementation object) is created as soons as the instance becomes valid
  • Instances that don't provide services becomes automatically immediate
    Code Block
    xml
    xml
    titleXML
    <component classname="my.service.implementation" name="my-service-impl" immediate="true">
       <provides/>
    </component>
    
    Code Block
    java
    java
    titleAnnotations
    @Component(immediate=true)
    @Provides
    public class Implementation implements FooService {
       ...
    }
    
    Center

  • Lifecycle Callback Handler

Lifecycle callbacks

Code Block
xml
xml
titleXML
<component classname="my.implementation" name="my-impl">
    <callback transition="validate" method="start" />
    <callback transition="invalidate" method="stop" />
</component>
Code Block
java
java
titleAnnotations
@Component
public class Implementation {
    
    @Validate
    public void start() {
        
    }
    
    @Invalidate
    public void stop() {
        
    }
}

Declaring properties

Code Block
xml
xml
titleXML
<component classname="my.Implementation" name="my-impl">
    <properties propagation="true" managedservice="MyPID">
        <property name="boo" method="setBoo" />
	<property field="m_bar" mandatory="true"/>
	<property field="m_foo" value="4"/>
    </properties>
</component>
<instance component="my-impl">
    <property name="boo" value="..."/>
    <property name="m_bar" value="..."/>
</instance>
<instance component="my-impl">
    <property name="boo" value="..."/>
    <property name="m_bar" value="..."/>
    <property name="managed.service.pid" value="AnotherPID"/>
</instance>
Code Block
java
java
titleAnnotations
@Component(managedservice="MyPID", propagation=true)
public class Implementation {
    
    @Property(name="boo")
    public void setBoo(int boo) {
        //...
    }
        
    @Property(mandatory=true)
    public int m_bar;

    @Property(value="4")
    public int m_foo;
}
Center

Using 'arch'

  • Deploy the 'arch' command bundle (available for Felix and Equinox)
  • Launch the 'arch' command in the OSGi Framework Shell
    Code Block
    arch => displays instances name & state (equivalent to arch \-instances)
    arch -instance $instance_name => displays complete information about the instance $instance_name
    arch -factories => display the list of available factories
    arch -factory $factory_name => display complete information about the factory $factory_name
    arch -handlers => list available handlers
    
  • Architecture Handler

Temporal Dependencies

  • Temporal dependencies are injected in fields. When accessing to the service, the thread waits for the service availability. If a timeout is reached, a timeout policy is executed.
  • Service objects can be injected as proxies and be given to collaborator objects.
  • Temporal dependencies are implemented as an external handlers. To use them, deploy and start the temporal dependency handler bundle.
Code Block
xml
xml
titleXML
<iPOJO xmlns:temporal="org.apache.felix.ipojo.handler.temporal">
<component
    className="my.Implementation">

    <!-- Temporal dependency configuration -->
    <temporal:requires field="mytemporal"/>
    <provides/>
</component>
</iPOJO>
Code Block
java
java
titleAnnotations
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.handler.temporal.Requires;
import org.apache.felix.ipojo.test.scenarios.annotations.service.FooService;

@Component
public class Implementation {
    
    @Requires // org.apache.felix.ipojo.handler.temporal.Requires
    private FooService mytemporal;
    
}
Center

Column
width20%
Include Page
FELIX:apache-felix-ipojo-menu
FELIX:apache-felix-ipojo-menu