我有3个组件(A,B,C)提供服务(As,As,Cs)(A和B实现相同的(As)服务)。和1个组件(E)需要服务。
E的组件类,我声明:
@Component
@.....
class E {
@requires
As dep1,
}
我有一个控制器来设置" dep1"的过滤器。它控制" dep1"选择组件A(B)的(As)服务。
所有组件都运行良好。我可以控制A和B组件之间的服务。
现在,我修改了以下类E:
@Component
@.....
class E {
@requires
As dep1;
@requires
Cs dep2;
}
在这种情况下,我也可以从控制器控制过滤器,但是:
" DEP1"即使是" dep1"的过滤器,也总是使用组件" A"的服务。设置为" B"
请问如何解决此案? 提前谢谢。
答案 0 :(得分:0)
如果我不在控制器组件中使用resetLocalCache,我不知道为什么它不能正常工作。这是我的程序
@Component(name =“callHello”,immediate = true)
@Instantiate
public class CallHello {
@Requires(filter="(name.instance=A)")
DelayService delayService;
@Requires
DirectService directService;
public CallHello() {
...
Label lb = new Label();
Button bt = new Button("Printer 1");
pn.add(bt);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (directService != null)
lb.setText(delayService.informDelay(tf.getText()));
}
});
...
}
}
控制器组件
@Component(name =“Controller”,immediate = true)
@Instantiate
public class ReconfigurationController {
...
public ReconfigurationController() {
fr = new JFrame("Controller");
fr.setSize(350,150);
...
final Button bt2 = new Button("To 2");
bt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (chk2) {
if (changeConnector(ctx, "hello.call.CallHello", "hello", "delayService", "B")) {
bt2.setLabel("To 1");
chk2 = false;
}
} else {
if (changeConnector(ctx, "hello.call.CallHello", "hello", "delayService", "A")) {
bt2.setLabel("To 2");
chk2 = true;
}
}
}});
...
public boolean changeConnector(BundleContext ctx, String componentName, String srcInstanceName, String dependencyName, String dstInstanceName) {
boolean result = false;
ServiceReference[] references;
try {
references = ctx.getServiceReferences(Factory.class.getName()," (factory.name="+componentName+")");
Factory fact = (Factory)ctx.getService(references[0]);
InstanceManager im = (InstanceManager) fact.getInstances().get(0);
for(ComponentInstance ci: fact.getInstances()) {
//System.out.println(ci.getInstanceName());
if (ci.getInstanceName().equals(srcInstanceName) ) {
im = (InstanceManager)ci;
}
}
DependencyHandler handler = (DependencyHandler) im.getHandler("org.apache.felix.ipojo:requires");
Dependency[] deps = handler.getDependencies();
for (Dependency d : deps ) {
// Lookup your dependency from deps (by id, by specification…)
if (d.getField().equals(dependencyName)) {
String srcFilter = d.getFilter();
Filter filter = FrameworkUtil.createFilter("(instance.name="+dstInstanceName+")");
d.setFilter(filter);
if (d.getState() == 1) {
result = true;
d.resetLocalCache();
} else {
System.out.println("rollback");
filter = FrameworkUtil.createFilter(srcFilter);
//d.setFilter(filter);
result = false;
}
}
}
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
我有2个组件实例(A和B)提供服务“informDelay”