On Sep 19, 2008, at 17:12 , Arjun Panday wrote:
Simply to make sure there's no confusion..
I agree that the possibility to override the default implementation
is a plus and I'm glad to see you're actively working on it.
Yet my original concern was the ability to give a (non-interface)
class (typically a superclass of the service to bind) as argument to
the setInterface() method.
If I understand you correctly, this is what you want to do:
public class Activator extends DependencyActivatorBase {
public void init(BundleContext context, DependencyManager
manager) throws Exception {
manager.add(createService()
.setInterface(A.class.getName(), null)
.setImplementation(new B())
);
}
public void destroy(BundleContext context, DependencyManager
manager) throws Exception {
}
}
abstract class A {
}
class B extends A {
public void start() {
System.out.println("B started");
}
}
You can argue that setInterface() might not be the best name for the
method in this case. :)
In my example I was trying to express dependency on a ClassLoader,
and expected to be injected with an object implementing a subclass
of ClassLoader. My point was not to provide a default implementation
for this dependency.
Okay. That would make the example something like this (all rolled into
one bundle to keep the example short, different options explained in
comments, use only one at the same time):
public class Activator extends DependencyActivatorBase {
public void init(BundleContext context, DependencyManager
manager) throws Exception {
manager.add(createService()
.setInterface(A.class.getName(), null)
.setImplementation(new B())
);
manager.add(createService()
.setImplementation(new C())
.add(createServiceDependency()
.setService(A.class)
// option 1) the dependency is required, and we want
it injected
.setRequired(true)
// option 2) the dependency is optional, and we want
it injected
.setRequired(false)
.setDefaultImplementation(new A() { /* my default impl
*/ })
// option 3) the dependency is optional, and we use
callbacks
.setRequired(false)
.setAutoConfig(false)
.setCallbacks("add", "remove")
)
);
}
public void destroy(BundleContext context, DependencyManager
manager) throws Exception {
}
}
abstract class A {
}
class B extends A {
public void start() {
System.out.println("B started");
}
}
class C {
volatile A member;
public void start() {
System.out.println("C started");
}
// callbacks for option 3)
public void add(A object) {
System.out.println("adding A");
}
public void remove(A object) {
}
}
Hope this helps.
Greetings, Marcel