In the last two entries, i gave an rough introduction on how to add OSGi support to GraniteDS.
After several days, i made some efforts on the GraniteDS OSGi bundle, finally i got a simple implementation (also i had contributed it to
http://www.graniteds.org).
In this entry, i would like to give a short example on HOW-TO use this GraniteDS OSGi bundle.
the non-official GraniteDS OSGi bundle can be downloaded from
org.granite.osgi_1.2.0.v200812012334_beta.zip, and also both the src and binary of the graniteds-osgi-example can be downloaded from
org.granite.osgi.example_src_1.0.0.v200812100107.zip and
org.granite.osgi.example_bin_1.0.0.v200812100107.zip
Step1. download the zip files, unzip and install them in the osgi container, make sure start
org.granite.osgi bundle before
org.granite.osgi.example. the granite DataService
in the example bundle will be scanned and reigstered by
org.granite.osgi bundle
.
Step2.
Open a browser and access http://localhost:{port}/web/GranitedsSample.html
,
click button Test1, you will get Hello :)-->gembin
click button Test2, you will get Just test===>gembin
Now we try to stop example bundle, then dataservice will be removed by org.granite.osgi bundle
if click button Test1,we will get an error, because the service is not available now.
Also we will get an exception StackTrace printed in the osgi console, because
org.granite.messaging.service.ServiceFactory.getFactoryInstance will be NULL, for this version i didn't handle this
but it will not affact the usage.
HOW-TO Write a Granite OSGi DataService
1. write a service as following:
the source code of for Test1
1 package org.granite.osgi.example;
2
3 import org.granite.messaging.service.annotations.RemoteDestination;
4
5 @RemoteDestination(id = "helloService", service = "granite-service", channel = "my-graniteamf", scope = "session")
6 public class HelloService {
7
8 public String sayHello(String user) {
9 return "Hello :)--> " + user;
10 }
11
12 }
the source code of for Test2
1 package org.granite.osgi.example.test;
2
3 import org.granite.messaging.service.annotations.RemoteDestination;
4
5 @RemoteDestination(id = "testService", source = "org.granite.osgi.example.test.TestService")
6 public interface ITestService {
7 public String test(String str);
8 }
9
1 package org.granite.osgi.example.test;
2 public class TestService implements ITestService {
3
4 public String test(String str) {
5 return "Just test===>" + str;
6 }
7
8 }
2.Export the service packages in the MANIFEST.MF
Export-Package: org.granite.osgi.example,org.granite.osgi.example.test
3.Create an XML config file for service lookup (i.e. named
granite-osgi.xml under directory GraniteDS-INF, any place any name as you want)
<graniteds>
<services>
<!-- any valid granite dataService in the package org.granite.osgi.example and it's subpackages will be registered-->
<service packages="org.granite.osgi.example.*" />
</services>
</graniteds>
4.Add GraniteDS Property key in the MANIFEST.MF
GraniteDS-Service: GraniteDS-INF/granite-osgi.xml
5.Register static resources stuff in the Activator for Flex part
HttpContext commonContext = httpService.createDefaultHttpContext();
//register the resources
httpService.registerResources("/web", "/WebResources", commonContext);
put all your resources under
folder WebResources.
6.Make a bundle and install it, that is.
javax.xml bundle and osgi event service is required for granite-osgi bundle,
for me, i use eclipse equinox as osgi container.
I hope you will like to play with it.