In last entry(Adding OSGi support to graniteds v1.1.0), i talk about the OSGifition
of Graniteds. Now i would like to add dynamic registration of Granite DataService by using annotation (@RemoteDestination).
Step1:
Define two constants:
public static final String TOPIC_GDS_ADD_SERVICE ="org/granite/ds/add/serviceClass";
public static final String SERVICE_CLASS="serviceClass";
Then,we modify the org.granite.config.flex.ServicesConfig.java
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
public class ServicesConfig implements ScannedItemHandler,EventHandler{
//
//
public void handleEvent(Event event) {
if(event.getTopic().equals(TOPIC_GDS_ADD_SERVICE)){
Class c=(Class) event.getProperty(SERVICE_CLASS);
if(c!=null)
handleClass(c);//this method is already provided by this Class, we just call it
else
System.out.println("Class NOT Found!!");
}
}
}
Step2:
in the AMFServiceAdaptor.java I mentioned in the last entry
BundleContext context;
public AMFServiceAdaptor(BundleContext context){
this.context=context;
}
public void init(ServletConfig config) {
try {
super.init(config);
graniteConfig = GraniteConfig.loadConfig(getServletContext());
servicesConfig = ServicesConfig.loadConfig(getServletContext());
Dictionary<String, Object> properties = new Hashtable<String, Object>();
String[] topics = new String[] { TOPIC_GDS_ADD_SERVICE};
properties.put(EventConstants.EVENT_TOPIC, topics);
context.registerService(EventHandler.class.getName(), servicesConfig, properties);
} catch (ServletException e) {
e.printStackTrace();
}
}
Step3:
In the Activator we add:
Activator.java
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put(SERVICE_CLASS, TestService.class);
ServiceReference eventAdminRef = context.getServiceReference(EventAdmin.class.getName());
if (eventAdminRef != null){
EventAdmin admin=context.getService(eventAdminRef);
if(admin!=null){
admin.sendEvent(new Event(TOPIC_GDS_ADD_SERVICE,properties));
}
TestService.java
1 package net.blogjava.gembin.domain.services.test;
2
3 import org.granite.messaging.service.annotations.RemoteDestination;
4
5
6 @RemoteDestination(id="testService", service="granite-service",channel="my-graniteamf", scope="session")
7 public class TestService {
8
9 public void test(){
10 System.out.println("test it!!!");
11 }
12
13 }
Step4:
Flex Part
[Bindable]
private var srv:RemoteObject = null;
import mx.messaging.ChannelSet;
import mx.messaging.config.ServerConfig;
import mx.rpc.remoting.mxml.Operation;
private function testSend():void{
srv = new RemoteObject();
srv.endpoint = ServerConfig.getChannel("my-graniteamf").endpoint;
srv.destination = "testService";
srv.channelSet = new ChannelSet();
srv.channelSet.addChannel(ServerConfig.getChannel("my-graniteamf"));
var operation:Operation = new Operation();
operation.name = "test";
operation.addEventListener(FaultEvent.FAULT,handleFault);
operation.addEventListener(ResultEvent.RESULT,handleRs);
srv.operations = {test: operation};
srv.test();
}
private function handleFault(e:FaultEvent):void{
Alert.show(e.toString());
}
private function handleRs(e:ResultEvent):void{
Alert.show(e.toString());
}
<mx:Button id="Test" label="TestIt" click="testSend();"/>
Screen shot
Conclusion
from this example, you can see it's very easy to add osgi dynamic feathure support in
Graniteds.
Enjoy it!!!
Next entry
Example for GraniteDS v1.2.0 OSGi bundle