I am now researching on the Opendaylight project (an open-source SDN controller project leaded by Cisco), and discovered that the project uses resources from apache.felix.dm package to dynamically manage the service dependencies on equinox (an OSGi framework) during runtime.
To understand the mechanism of dm, I have traced the code in ComponentImpl.java under apache.felix.dm package. What I understand now is:
- DependencyManager will invoke init() method after all the service dependencies are satisfied.
- the start() method will be invoked after init() method but before the services provided by the class are being registered on OSGi framework.
The invoking mechanism written in code is provided as follow (they are both located in ComponentImpl.java):
The method to invoke init():
private void activateService(State state) {
String init;
synchronized (this) {
init = m_callbackInit;
}
// service activation logic, first we initialize the service instance itself
// meaning it is created if necessary and the bundle context is set
initService();
// now is the time to configure the service, meaning all required
// dependencies will be set and any callbacks called
configureService(state);
// flag that our instance has been created
m_isInstantiated = true;
// then we invoke the init callback so the service can further initialize
// itself
invoke(init);
// see if any of this caused further state changes
calculateStateChanges();
}
The method to invoke start():
private void bindService(State state) {
String start;
synchronized (this) {
start = m_callbackStart;
}
// configure service with extra-dependencies which might have been added from init() method.
configureServiceWithExtraDependencies(state);
// inform the state listeners we're starting
stateListenersStarting();
// invoke the start callback, since we're now ready to be used
invoke(start);
// start tracking optional services
startTrackingOptional(state);
// register the service in the framework's service registry
registerService();
// inform the state listeners we've started
stateListenersStarted();
}
Now my question is: what's the difference between init() and start() method? Since they are both invoked before the service being registered on OSGi framework, why they needed to be seperated? Thx for any ideas, and please tell me if any of my understanding is incorrect.
Best Regards,
Jay