h1. WARNING : this project is no longer updated
the project has moved to "https://github.com/weld/core":https://github.com/weld/core on the master (environments/osgi) branch. This project is only used for wiki and issues tracker about Weld-OSGi.
If you want to use and hack Weld-OSGi, please fork "https://github.com/weld/core":https://github.com/weld/core
You can find actual documentation at : "http://mathieuancelin.github.com/weld-osgi/":http://mathieuancelin.github.com/weld-osgi/
h1. Weld OSGi integration
This project aims to provide an OSGi integration for Weld. This project will take full advantage of OSGi like frameworks such as iPojo, Blueprint, SCR, Peaberry, etc ...
h3. How does it works
!https://github.com/mathieuancelin/weld-osgi/raw/master/images/diagrams.png!
h3. How to use it
First of all, you will need to install and start the Weld/OSGi bundle in your OSGi environment. Then you can write a standard OSGi bundle with a valid manifest (You don't need to write an activator or anything else). To enable Weld for this bundle, you need to create a beans.xml file in the META-INF folder.
public void onStartup(@Observes BundleContainerInitialized event) {}
public void onShutdown(@Observes BundleContainerShutdown event) {}
@Publish
@ApplicationScoped
public class MyServiceImpl implements MyService {
@Overrides
public void doSomething() { ... }
}
// direct injection
@Inject @OSGiService MyService service;
...
service.doSomething();
...
// provider injection
@Inject Service myService;
...
myService.get().doSomething();
you can also filter service with :
@Inject @OSGiService @Filter("(&(lang=EN)(country=US))") MyService service;
...
@Inject @Filter("(&(lang=EN)(country=US))") Service service;
...
@Inject Service myServices;
...
// print number of available services
logger.log(myServices.size());
...
for (MyService myService : myServices) {
myService.doSomething();
}
you can also filter services with :
@Inject @Filter("(&(lang=EN)(country=US))") Service myServices;
...
@Inject @OSGiService @Required MyService service;
...
@Inject @Required Service myService;
...
public void validate(@Observes Valid event) {
// start application here
}
...
public void invalidate(@Observes Invalid event) {
// stop application here
}
public void bindBundle(@Observes BundleInstalled event) {}
public void unbindBundle(@Observes BundleUninstalled event) {}
...
you can also filter event for specific bundles :
public void bindService(@Observes @BundleName("com.sample.gui") BundleInstalled event) {}
public void bindService(@Observes @BundleVersion("4.2.1") BundleInstalled event) {}
public void bindService(@Observes @BundleName("com.sample.gui") @BundleVersion("4.2.1") BundleInstalled event) {}
public void bindService(@Observes ServiceArrival event) {}
public void unbindService(@Observes ServiceDeparture event) {}
public void changeService(@Observes ServiceChange event) {}
...
you can also filter event for specific services types :
public void bindService(@Observes @Specification(MyService.class) ServiceArrival event) {}
or filter with native OSGi filters;
public void bindService(@Observes @Specification(MyService.class) @Filter("(&(lang=EN)(country=US))") ServiceArrival event) {}
@Inject Instance
@Inject Registration registeredServices;
...
logger.log(registeredServices.size());
...
for (Registration registredService : registeredServices) {
registredService.unregister();
}
@Inject Event event;
...
event.fire(new InterBundleEvent("Hello bundles));
In another bundle :
// listen to all InterBundleEvent even from your own bundle
public void listenAllEvents(@Observes InterBundleEvent event) {}
...
// listen to all InterBundleEvent from other bundles
public void listenAllEventsFromOtherBundles(@Observes @Sent InterBundleEvent event) {}
...
// listen to all InterBundleEvent containing String object from other bundles
public void listenStringEventsFromOtherBundles(
@Observes @Sent @Specification(String.class) InterBundleEvent event) {}
@Inject Bundle bundle;
@Inject BundleContext context;
@Inject @BundleHeaders Map metadata;
You can also inject only one specific metadata with :
@Inject @BundleHeader("Bundle-SymbolicName") String symbolicName;
@Inject @BundleDataFile("test.txt") File file;
public class MyApp implements BundleActivator {
private WeldEmbedded weld;
@Override
public void start(BundleContext bc) throws Exception {
weld = WeldEmbedded.startFor(bc);
...
MyService service = weld.instance().select(MyService.class).get();
}
@Override
public void stop(BundleContext bc) throws Exception {
weld.shutdown();
}
}
Just be careful here, to make this exemple work, the Weld/OSGi bundle must not be started. You also won't be able to use certain features such as inter bundle events.
h3. Play with samples
This project provide a small sample project that shows how to use Weld integration with OSGi. The sample is actually a small shapes drawing software in which you can add new shapes.
To use the sample, first build the entire Weld OSGi project with :
mvn clean install
then launch the sample with :
./run-sample-container.sh
this script will launch an Apache Felix container with the right bundles within. A swing window should be displayed :
!https://github.com/mathieuancelin/weld-osgi/raw/master/images/sample1.png!
You can play with it by clicking on the red shape and drawing circles in the white area.
Now if you want more shape, you need to find out the id of the shape bundle. In your Felix terminal type the "ps" command. You should get something like that :
> ps
...
[ 4] [Installed ] [ 1] Weld OSGi :: Samples :: Paint :: Triangle (1.0.0.SNAPSHOT)
[ 5] [Installed ] [ 1] Weld OSGi :: Samples :: Paint :: Square (1.0.0.SNAPSHOT)
...
Now you can start the "triangle bundle" by typing in the Felix terminal :
start 4
!https://github.com/mathieuancelin/weld-osgi/raw/master/images/sample2.png!
and the "square bundle" :
start 5
!https://github.com/mathieuancelin/weld-osgi/raw/master/images/sample3.png!
you can also stop bundles. When a bundle is stopped, existing shapes of this bundle are deleted :
stop 4
!https://github.com/mathieuancelin/weld-osgi/raw/master/images/sample4.png!
Other samples are available in the sample container (calculator, webapp), don't hesitate to try them :)