SeamFramework.orgCommunity Documentation
You can now use CDI in an OSGi environment with Weld-OSGi and bean bundles. But Weld-OSGi also provide numerous solution helping you make your multi bundles OSGi application using CDI way.
In this chapter you will see:
How to publish your CDI beans as OSGi services
How to consume these new services in regular OSGi bundles
How to match a auto published qualified CDI bean and a propertied OSGi service
How to inject OSGi services in bean bundle
CDI beans can be seen like services, with an interface defining the service contract and one or many implementations performing the service. So Weld-OSGi allows to easily publish your CDI beans from bean bundles as OSGi services. To do so you just to put an annotation on your bean implementation classes, avoiding the whole OSGi publishing process.
The main difference comes from that auto published services are proxied CDI bean instances so:
Auto published services are contextual and then might be share between
bundle (e.g with an auto published ApplicationScope
annotated CDI bean)
Furthermore the auto published service and the injected CDI instance may be the same in the same scope (i.e share their state)
Auto published services might be decorated or intercepted by the providing bean bundle
Modify the hello-world-multilingual
bean bundle to auto publish the
HelloWorld
services as OSGi services. It will be the
hello-world-provider
bean bundle.
Update the com.sample.impl.HelloWorldImpl*.java
implementation
classes
...
@Language("*")
@Publish (1)
public class HelloWorld* implements HelloWorld {
...
}
Simply
put the Publish
annotation on the implementation classes (1) and that is it ! Every time Weld-OSGi finds a CDI bean
with the Publish
annotation, it registers it as a new OSGi service.
Your project should now looks like that:
hello-world-provider pom.xml - src - main - java - com.sample App.java - api HelloWorld.java Language.java Presentation.java - impl HelloWorldEnglish.java HelloWorldFrench.java HelloWorldGerman.java PresentationInterceptor.java - resources - META-INF beans.xml hello-world-provider.bnd
Try your new hello-world-provider
bean bundle in the OSGi environment:
Build your project using Maven: mvn clean install
Copy the generated hello-world-provider-1.0.jar
file to the
Felix home bundle
directory and remove the old
hello-world-multilingual-1.0.jar
file.
Update the Felix configuration file to auto install the bean bundle
felix.auto.install.1= file:bundle/weld-osgi-core-api-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-spi-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-mandatory-1.0-SNAPSHOT.jar \
file:bundle/hello-world-provider-1.0.jar
Start the Felix framework and test your bean bundle
____________________________ Welcome to Apache Felix Gogo g! lb START LEVEL 1 ID|State |Level|Name 0|Active | 0|System Bundle (3.2.2) 1|Active | 1|Apache Felix Bundle Repository (1.6.2) 2|Active | 1|Apache Felix Gogo Command (0.8.0) 3|Active | 1|Apache Felix Gogo Runtime (0.8.0) 4|Active | 1|Apache Felix Gogo Shell (0.8.0) 5|Active | 1|Weld-OSGi :: Core :: Extension Impl (1.0.0.SNAPSHOT) 6|Active | 1|Weld-OSGi :: Implementation :: Weld Integration (1.0.0.SNAPSHOT) 7|Resolved | 1|Weld-OSGi :: Core :: Extension API (1.0.0.SNAPSHOT) 8|Resolved | 1|Weld-OSGi :: Core :: Integration API (1.0.0.SNAPSHOT) 9|Resolved | 1|Weld-OSGi :: Core :: Mandatory (1.0.0.SNAPSHOT) 10|Installed | 1|hello-world-provider (1.0.0) g! start 10 g! Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider stop 10 g! Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt!
Nothing
has visibly changed, but Weld-OSGi has published three new OSGi services. So
the Publish
annotation do not alter the regular CDI behavior in
bean bundle.
In the next section you will see these new OSGi service in action by consuming them in a second bundle. But before that you will what options Weld-OSGi give when you auto publish OSGi service.
The Publish
annotation allows to things:
Modify the service rank of the auto published OSGi service
@Publish(rank = 1) public class MyServiceImpl implements MyService { }
Provide the list of contracts that the service fulfills
@Publish(contracts = {ItfA.class, ItfB.class, AbsA.class}) public class MyServiceImpl extends AbsA implements MyService, ItfA, ItfB { }
Every given class may be assignable to the service implementation type. It allows to publish a service with both its interface types, superclass type and own type.
Weld-OSGi auto-published services get their types from the following algorithm:
If a (nonempty) contract list is provided with the
Publish
annotation the service is registered for
all these types.
Else if the implementation class possesses a (nonempty) list of non-blacklisted interfaces the service is registered for all these interface types.The blacklist is described below.
Else if Weld-OSGi the implementation class possesses a non-blacklisted superclass the service is registered for this superclass type.
Last if the implementation class has neither contract nor non-blacklisted interface or superclass, the service is register with is the implementation class type.
Weld-OSGi provides a type blacklist in order to filter auto-published OSGi service allowed type. TODO ?
Create a new regular OSGi bundle that will consume the auto published services of the
hello-world-provider
bean bundle. It will be the
hello-world-consumer
bundle.
You need to write the entry point of your bundle (i.e the activator class of the
bundle). That is the com.sample.Activator.java
main
class
package com.sample; import com.sample.api.HelloWorld; (1) import org.osgi.framework.BundleActivator; (2) import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; public class Activator implements BundleActivator { HelloWorld helloWorld; @Override public void start(BundleContext context) throws Exception { (3) ServiceReference helloWorldReference = context.getServiceReference(HelloWorld.class.getName()); (4) helloWorld = (HelloWorld)context.getService(helloWorldReference); helloWorld.sayHello(); (5) } @Override public void stop(BundleContext context) throws Exception { (6) helloWorld.sayGoodbye(); (7) } }
You
import your service interface (1) and the OSGi
dependencies (2). You ask the OSGi environment for the
HelloWorld
service (4). Then you greet
(5) and say goodbye (7) to the World at the start (3) and
stop (6) of your bundle.
You should also do a quick update of the
com.sample.impl.PresentationInterceptor.java
interceptor
... public class PresentationInterceptor { @AroundInvoke public Object present(InvocationContext ctx) throws Exception { ... if(lang.equals("FRENCH")) { System.out.println("Je suis le bundle hello-world-provider"); return null; } else if(lang.equals("GERMAN")) { System.out.println("Ich bin das bundle hello-world-provider"); return null; } } } System.out.println("I am the bundle hello-world-provider"); return null; } }
Your bean bundle may present itself right.
Finally you write the configuration files of your bundle:
The pom.xml
Maven configuration
file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample</groupId> <artifactId>hello-world-consumer</artifactId> <version>1.0</version> <packaging>bundle</packaging> (1) <dependencies> (2) <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>hello-world-provider</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> (3) <extensions>true</extensions> <configuration> <instructions> <_include>-target/classes/META-INF/${project.artifactId}.bnd</_include> (4) </instructions> </configuration> </plugin> </plugins> </build> </project>
You
setup the dependencies for the com.sample.App.java
main class
(2). You say that Maven may build an
OSGi bundle (1) using the
maven-bundle-plugin
(3) with the
META-INF/hello-world.bnd
(4) configuration file.
The META-INF\hello-world-consumer.bnd
OSGi configuration
file
# Let bnd handle the MANIFEST.MF generation # Just precise that this bundle as an activator Bundle-Activator com.sample.Activator
The
bnd tool will generate the OSGi MANIFEST.MF
configuration file
just fine but you need to precise that there is an activator class.
You do not add the META-INF\beans.xml
CDI marker file since
you want a regular OSGi bundle (i.e a bundle not managed by
Weld-OSGi).
BundleActivator
class start
and stop
methods are called when the bundle becomes active (start
) or
inactive (stop
). It works for both regular bundles and bean
bundles. But it do not ensure the availability of CDI usage in bean
bundle.
BundleContainerEvents
events listening method are called when the
bean bundle Weld container has initialized
(BundleContainerEvents.BundleContainerInitialized
) or has
shutdown (BundleContainerEvents.BundleContainerShutdown
). It works
only for bean bundles and ensure the availability of CDI usage.
BundleContainerEvents.BundleContainerInitialized
event listening
method always occurs after BundleActivator
class start
method and BundleContainerEvents.BundleContainerShutdown
event
listening method always occurs after BundleActivator
class
stop
method in a bean bundle.
Your project should now looks like that:
hello-world-consumer pom.xml - src - main - java - com.sample Activator.java - resources - META-INF hello-world-consumer.bnd
Try your new hello-world-consumer
bean bundle in the OSGi environment:
Build your project using Maven: mvn clean install
Copy the generated hello-world-consumer-1.0.jar
file to the
Felix home bundle
directory (keep the
hello-world-provider-1.0.jar
file).
Update the Felix configuration file to auto install the bean bundle
felix.auto.install.1= file:bundle/weld-osgi-core-api-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-spi-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-mandatory-1.0-SNAPSHOT.jar \
file:bundle/hello-world-provider-1.0.jar \
file:bundle/hello-world-consumer-1.0.jar
Start the Felix framework
____________________________
Welcome to Apache Felix Gogo
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (3.2.2)
1|Active | 1|Apache Felix Bundle Repository (1.6.2)
2|Active | 1|Apache Felix Gogo Command (0.8.0)
3|Active | 1|Apache Felix Gogo Runtime (0.8.0)
4|Active | 1|Apache Felix Gogo Shell (0.8.0)
5|Active | 1|Weld-OSGi :: Core :: Extension Impl (1.0.0.SNAPSHOT)
6|Active | 1|Weld-OSGi :: Implementation :: Weld Integration (1.0.0.SNAPSHOT)
7|Resolved | 1|Weld-OSGi :: Core :: Extension API (1.0.0.SNAPSHOT)
8|Resolved | 1|Weld-OSGi :: Core :: Integration API (1.0.0.SNAPSHOT)
9|Resolved | 1|Weld-OSGi :: Core :: Mandatory (1.0.0.SNAPSHOT)
10|Installed | 1|hello-world-provider (1.0.0)
11|Installed | 1|hello-world-consumer (1.0.0)
g!
Start the provider bundle
g! start 10 g! Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider
It
greets the World and auto publish the HelloWorld
services. Now
start the consumer
bundle
start 11 Hallo Welt! Ich bin das bundle hello-world-multilingual g!
It greets the World too, but seems it bit confused ! Everything is explained below. Stop it
g! stop 11 Auf Wiedersehen Welt!
It says goodbye. Finally stop the provider bundle
g! stop 10 g! Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt!
It keeps the same behavior, the consumer bundle has no impact on it.
Your bundle speaks a random language because you did not precise what language it should speak! You will see in the next section how to do it. OSGi service lookup mechanism just picks the first matching service implementation it finds (with its own internal magic), giving you a "random" language.
Your bundle presents itself as the the
hello-world-provider
bundle because the presentation
occurs in the interceptor class of the hello-world-provider
bundle. Indeed the obtained service implementation is a CDI bean from
the hello-world-provider
bundle and so it is
intercepted.
You may be careful when you use CDI bean both for bean bundle internal injection and for OSGi service auto-publication because it will be the same instance. Interceptor and decorator will act on auto published services even if they are consumed in other bundles, and in the same scope every service instances and injected instances will share the same state.
Now you need to decide what language your consumer bundle will speak. To do so you cannot use CDI qualifier like in provider bundle because you are using OSGi mechanisms to obtain the service instance. Fortunately Weld-OSGi provides a binding between CDI service qualification and OSGi service properties.
A CDI qualifier will generate an OSGi service property for each of its valued element (an element with a default value is always considered valued) following these rules:
A valued element generate a property with this template:
decapitalized_qualifier_name.decapitalized_element_name=element_value.toString()
@MyQualifier(lang="EN", country="US")
will generate:
(myqualifier.lang=EN) (myqualifier.country=US)
A non valued element with a default value generate a property with this template:
decapitalized_qualifier_name.decapitalized_element_name=element_default_value.toString()
@MyQualifier(lang="EN")
will generate:
(myqualifier.lang=EN) (myqualifier.country=US) //admitting US is the default value for the element country
A non valued element with no default value generate a property with this template:
decapitalized_qualifier_name.decapitalized_element_name=*
@MyQualifier(lang="EN")
will generate:
(myqualifier.lang=EN) (myqualifier.country=*) //admitting there is no default value for the element country
A qualifier with no element generate a property with this template:
decapitalized_qualifier_name=*
@MyQualifier()
will generate:
(myqualifier=*)
Some qualifiers follow a specific processing:
OSGiService
qualifier will not generate any
service property
Required
qualifier will not generate any
service property
Default
qualifier will not generate any
service property
Any
qualifier will not generate any service
property
Filter
and Properties
qualifiers
processing is described below
Using these rules, modify your hello-world-consumer
bundle in order to
make it speak the three languages like the hello-world-provider
bean
bundle. It will be the hello-world-consumer-multilingual
bundle.
All the work happens in the com.sample.Activator.java
main
class
package com.sample; import com.sample.api.HelloWorld; (1) import org.osgi.framework.BundleActivator; (2) import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; public class Activator implements BundleActivator { HelloWorld helloWorldEnglish; HelloWorld helloWorldFrench; HelloWorld helloWorldGerman; @Override public void start(BundleContext context) throws Exception { (3) ServiceReference helloWorldEnglishReference = context.getServiceReferences(HelloWorld.class.getName(),"(language.value=ENGLISH)")[0]; (4) ServiceReference helloWorldFrenchReference = context.getServiceReferences(HelloWorld.class.getName(),"(language.value=FRENCH)")[0]; ServiceReference helloWorldGermanReference = context.getServiceReferences(HelloWorld.class.getName(),"(language.value=GERMAN)")[0]; helloWorldEnglish = (HelloWorld)context.getService(helloWorldEnglishReference); helloWorldFrench = (HelloWorld)context.getService(helloWorldFrenchReference); helloWorldGerman = (HelloWorld)context.getService(helloWorldGermanReference); helloWorldEnglish.sayHello(); (5) helloWorldFrench.sayHello(); helloWorldGerman.sayHello(); } @Override public void stop(BundleContext context) throws Exception { (6) helloWorldEnglish.sayGoodbye(); (7) helloWorldFrench.sayGoodbye(); helloWorldGerman.sayGoodbye(); } }
You
import your service interface (1) and the OSGi
dependencies (2). You ask the OSGi environment for the
HelloWorld
services specifying the correct filter (4). Then you greet (5) and say goodbye
(7) to the World at the start (3) and stop (6) of your bundle.
Your project should now looks like that:
hello-world-consumer-multilingual pom.xml - src - main - java - com.sample Activator.java - resources - META-INF hello-world-consumer-multilingual.bnd
Try your new hello-world-consumer-multilingual
bean bundle in the OSGi environment:
Build your project using Maven: mvn clean install
Copy the generated hello-world-consumer-multilingual-1.0.jar
file to the Felix home bundle
directory and remove the old
hello-world-consumer-1.0.jar
file (keep the
hello-world-provider-1.0.jar
file).
Update the Felix configuration file to auto install the bean bundle
felix.auto.install.1= file:bundle/weld-osgi-core-api-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-spi-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-mandatory-1.0-SNAPSHOT.jar \
file:bundle/hello-world-provider-1.0.jar \
file:bundle/hello-world-consumer-multilingual-1.0.jar
Start the Felix framework
____________________________
Welcome to Apache Felix Gogo
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (3.2.2)
1|Active | 1|Apache Felix Bundle Repository (1.6.2)
2|Active | 1|Apache Felix Gogo Command (0.8.0)
3|Active | 1|Apache Felix Gogo Runtime (0.8.0)
4|Active | 1|Apache Felix Gogo Shell (0.8.0)
5|Active | 1|Weld-OSGi :: Core :: Extension Impl (1.0.0.SNAPSHOT)
6|Active | 1|Weld-OSGi :: Implementation :: Weld Integration (1.0.0.SNAPSHOT)
7|Resolved | 1|Weld-OSGi :: Core :: Extension API (1.0.0.SNAPSHOT)
8|Resolved | 1|Weld-OSGi :: Core :: Integration API (1.0.0.SNAPSHOT)
9|Resolved | 1|Weld-OSGi :: Core :: Mandatory (1.0.0.SNAPSHOT)
10|Installed | 1|hello-world-provider (1.0.0)
11|Installed | 1|hello-world-consumer-multilingual (1.0.0)
g!
Start the provider bundle
g! start 10 g! Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider
Everything works fine. Now start the consumer bundle
start 11 Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider g!
It is multilingual ! Stop everything
g! stop 11 Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt! g! stop 10 g! Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt!
The goodbye part is good too.
You can now use auto published service from a bean bundle in any other bundle. It opens CDI features to regular OSGi bundles and ensure the compatibility of Weld-OSGi with old OSGi application.
But the OSGi looking process for services is still a complicated, in the next section you will see how you can use CDI programming in order to get your OSGi services with Weld-OSGi.
Create a new bean bundle that will use the auto published services by injection. It
will be the hello-world-consumer2-multilingual
bean bundle.
The entry point of your bean bundle, the com.sample.App.java
main
class
package com.sample; import com.sample.api.HelloWorld; import com.sample.api.Language; import org.osgi.cdi.api.extension.Service; import org.osgi.cdi.api.extension.annotation.OSGiService; import org.osgi.cdi.api.extension.events.BundleContainerEvents; import javax.enterprise.event.Observes; import javax.enterprise.util.AnnotationLiteral; import javax.inject.Inject; public class App { @Inject (1) @OSGiService HelloWorld helloWorld; @Inject (2) Service<HelloWorld> helloWorldService; @Inject (3) @OSGiService @Language("ENGLISH") HelloWorld helloWorldEnglish; @Inject Service<HelloWorld> helloWorldServiceEnglish; @Inject @OSGiService @Language("FRENCH") HelloWorld helloWorldFrench; @Inject Service<HelloWorld> helloWorldServiceFrench; @Inject @OSGiService @Language("GERMAN") HelloWorld helloWorldGerman; @Inject Service<HelloWorld> helloWorldServiceGerman; HelloWorld helloWorld2; HelloWorld helloWorldEnglish2; HelloWorld helloWorldFrench2; HelloWorld helloWorldGerman2; public void onStartup(@Observes BundleContainerEvents.BundleContainerInitialized event) { (4) helloWorld2 = helloWorldService.get(); (5) helloWorldEnglish2 = helloWorldServiceEnglish.select(new LanguageAnnotationEnglish()).get(); (6) helloWorldFrench2 = helloWorldServiceFrench.select("(language.value=FRENCH)").get(); (7) helloWorldGerman2 = helloWorldServiceGerman.select("(language.value=GERMAN)").get(); helloWorld.sayHello(); (8) helloWorld2.sayHello(); helloWorldEnglish.sayHello(); helloWorldEnglish2.sayHello(); helloWorldFrench.sayHello(); helloWorldFrench2.sayHello(); helloWorldGerman.sayHello(); helloWorldGerman2.sayHello(); for (HelloWorld service : helloWorldService) { (9) service.sayHello(); } } public void onShutdown(@Observes BundleContainerEvents.BundleContainerShutdown event) { (10) helloWorld.sayGoodbye(); (11) helloWorld2.sayGoodbye(); helloWorldEnglish.sayGoodbye(); helloWorldEnglish2.sayGoodbye(); helloWorldFrench.sayGoodbye(); helloWorldFrench2.sayGoodbye(); helloWorldGerman.sayGoodbye(); helloWorldGerman2.sayGoodbye(); for (HelloWorld service : helloWorldService) { service.sayGoodbye(); } } private class LanguageAnnotationEnglish extends AnnotationLiteral<Language> implements Language { @Override public String value() { return "ENGLISH"; } } }
A lot of things to discuss here. There is two ways to get an OSGi service injected:
You put the OSGiService
annotation on a regular injection
point (1)
You get a Service<T>
injected with T
the
service type (2), then you get the service
(5)
If you want to choose the implementation:
Qualify the OSGiService
annotated injection with CDI
qualifiers (3)
Select the instance with the injected Service<T> select
method and a set of CDI qualifier annotation (6) or an OSGi service filter (7)
Iterate through the implementation of an injected
Service<T>
(9)
Finally you say hello (8) and goodbye (11) when its right (4) (10) in all languages with all technique.
Same old configuration files:
The pom.xml
Maven configuration
file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sample</groupId> <artifactId>hello-world-consumer2-multilingual</artifactId> <version>1.0</version> <packaging>bundle</packaging> <dependencies> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>1.0-SP4</version> </dependency> <dependency> <groupId>org.osgi.cdi</groupId> <artifactId>weld-osgi-core-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>hello-world-provider</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <_include>-target/classes/META-INF/${project.artifactId}.bnd</_include> </instructions> </configuration> </plugin> </plugins> </build> </project>
The META-INF\hello-world-consumer2-multilingual.bnd
OSGi
configuration
file
# Let bnd handle the MANIFEST.MF generation
The META-INF\beans.xml
CDI marker
file:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>
Your project should now looks like that:
hello-world-consumer2-multilingual pom.xml - src - main - java - com.sample App.java - resources - META-INF beans.xml hello-world-consumer2-multilingual.bnd
Try your new hello-world-consumer2-multilingual
bean bundle in the OSGi environment:
Build your project using Maven: mvn clean install
Copy the generated hello-world-consumer2-multilingual.jar
file to the Felix home bundle
directory (keep the
hello-world-provider-1.0.jar
and
hello-world-consumer-multilingual.jar
files).
Update the Felix configuration file to auto install the bean bundle
felix.auto.install.1= file:bundle/weld-osgi-core-api-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-spi-1.0-SNAPSHOT.jar \
file:bundle/weld-osgi-core-mandatory-1.0-SNAPSHOT.jar \
file:bundle/hello-world-provider-1.0.jar \
file:bundle/hello-world-consumer-multilingual-1.0.jar \
file:bundle/hello-world-consumer2-multilingual-1.0.jar
Start the Felix framework and test everything out:
____________________________ Welcome to Apache Felix Gogo g! lb START LEVEL 1 ID|State |Level|Name 0|Active | 0|System Bundle (3.2.2) 1|Active | 1|Apache Felix Bundle Repository (1.6.2) 2|Active | 1|Apache Felix Gogo Command (0.8.0) 3|Active | 1|Apache Felix Gogo Runtime (0.8.0) 4|Active | 1|Apache Felix Gogo Shell (0.8.0) 5|Active | 1|Weld-OSGi :: Core :: Extension Impl (1.0.0.SNAPSHOT) 6|Active | 1|Weld-OSGi :: Implementation :: Weld Integration (1.0.0.SNAPSHOT) 7|Resolved | 1|Weld-OSGi :: Core :: Extension API (1.0.0.SNAPSHOT) 8|Resolved | 1|Weld-OSGi :: Core :: Integration API (1.0.0.SNAPSHOT) 9|Resolved | 1|Weld-OSGi :: Core :: Mandatory (1.0.0.SNAPSHOT) 10|Installed | 1|hello-world-provider (1.0.0) 11|Installed | 1|hello-world-consumer-multilingual (1.0.0) 12|Installed | 1|hello-world-consumer2-multilingual (1.0.0) g! start 10 Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider g! start 11 Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider g! start 12 Hallo Welt! Ich bin das bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hello World! I am the bundle hello-world-provider Hello World! I am the bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider Bonjour le Monde ! Je suis le bundle hello-world-provider Hello World! I am the bundle hello-world-provider Hallo Welt! Ich bin das bundle hello-world-provider g! stop 12 Auf Wiedersehen Welt! Au revoir le Monde ! Goodbye World! Goodbye World! Au revoir le Monde ! Au revoir le Monde ! Auf Wiedersehen Welt! Auf Wiedersehen Welt! Au revoir le Monde ! Goodbye World! Auf Wiedersehen Welt! g! stop 11 Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt! g! stop 10 Goodbye World! Au revoir le Monde ! Auf Wiedersehen Welt! g! lb START LEVEL 1 ID|State |Level|Name 0|Active | 0|System Bundle (3.2.2) 1|Active | 1|Apache Felix Bundle Repository (1.6.2) 2|Active | 1|Apache Felix Gogo Command (0.8.0) 3|Active | 1|Apache Felix Gogo Runtime (0.8.0) 4|Active | 1|Apache Felix Gogo Shell (0.8.0) 5|Active | 1|Weld-OSGi :: Core :: Extension Impl (1.0.0.SNAPSHOT) 6|Active | 1|Weld-OSGi :: Implementation :: Weld Integration (1.0.0.SNAPSHOT) 7|Resolved | 1|Weld-OSGi :: Core :: Extension API (1.0.0.SNAPSHOT) 8|Resolved | 1|Weld-OSGi :: Core :: Integration API (1.0.0.SNAPSHOT) 9|Resolved | 1|Weld-OSGi :: Core :: Mandatory (1.0.0.SNAPSHOT) 10|Resolved | 1|hello-world-provider (1.0.0) 11|Resolved | 1|hello-world-consumer-multilingual (1.0.0) 12|Resolved | 1|hello-world-consumer2-multilingual (1.0.0) g!
It becomes a bit cacaphonic but everything should be here.
You know now how to use the OSGi service layer using Weld-OSGi. Howover you may refer to the specification to specific usage and niceties. In the next chapters you will see what other things Weld-OSGi can do, helping you using OSGi framework for your application:
Event management and inter-bundle communication
OSGi framework utility