我目前正在尝试设置自己的ManagedServiceFactory实现.这是我正在尝试做的事情:我需要在每个配置基础上提供一些服务的多个实例.使用DS,组件工作得很好,但现在我发现这些服务应该处理自己的生命周期(即服务注册表中的(de)注册),具体取决于某些外部资源的可用性,这对DS来说是不可能的.
因此,我的想法是创建一个ManagedServiceFactory,然后从ConfigurationAdmin接收配置并创建我的类的实例.这些将再次尝试在单独的线程中连接到资源,并在它们准备好运行时将自己注册为服务.
由于我没有运气实现这一点,我试图将所有内容分解为最基本的部分,甚至没有处理动态(de)注册,只是试图让ManagedServiceFacotry工作:
package my.project.factory;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
public class Factory implements BundleActivator, ManagedServiceFactory {
private ServiceRegistration myReg;
private BundleContext ctx;
private Map services;
@Override
public void start(BundleContext context) throws Exception {
System.out.println("starting factory...");
this.ctx = context;
java.util.Dictionary properties = new Hashtable();
properties.put(Constants.SERVICE_PID, "my.project.servicefactory");
myReg = context.registerService(ManagedServiceFactory.class, this,
properties);
System.out.println("registered as ManagedServiceFactory");
services = new HashMap();
}
@Override
public void stop(BundleContext context) throws Exception {
for(ServiceRegistration reg : services.values()) {
System.out.println("deregister " + reg);
reg.unregister();
}
if(myReg != null) {
myReg.unregister();
} else {
System.out.println("my service registration as already null " +
"(although it shouldn't)!");
}
}
@Override
public String getName() {
System.out.println("returning facotry name");
return "ServiceFactory";
}
@Override
public void updated(String pid, Dictionary properties)
throws ConfigurationException {
System.out.println("retrieved update for pid " + pid);
ServiceRegistration reg = services.get(pid);
if (reg == null) {
services.put(pid, ctx.registerService(ServiceInterface.class,
new Service(), properties));
} else {
// i should do some update here
}
}
@Override
public void deleted(String pid) {
ServiceRegistration reg = services.get(pid);
if (reg != null) {
reg.unregister();
}
}
}
现在,它应该从ConfigurationAdmin接收PID my.project.servicefactory的配置,不应该吗?
但它没有从ConfigurationAdmin接收任何配置.捆绑包已启动,服务已注册,在Web控制台中,我可以看到配置管理员拥有对我的ManagedServiceFactory的引用.是否应该设置某个属性? interface specification并不暗示.实际上我的实现或多或少与那里的例子相同.我不知道我在这里做错了什么,对解决方案的任何指示都非常受欢迎.
另外,我原本认为将ManagedServiceFactory本身实现为DS,这也应该是可能的,但我在同一点上失败了:ConfigAdmin没有移交任何配置.
更新
澄清问题:我认为这主要是配置问题.在我看来,我应该能够为工厂指定两个PID,一个用于识别工厂本身的配置(如果有的话),另一个用于通过该工厂生产服务,我认为应该是factory.pid .但是框架常量并没有这样的东西.
更新2
在搜索了一下Felix Fileinstall源代码之后,我发现当文件名中有 – 时,它会以不同的方式处理配置文件.如果配置文件名为my.project.servicefactory.cfg,则无效,但名为my.project.servicefactory-foo.cfg和my.project.servicefactory-bar.cfg的配置已按预期正确移交给我的ManagedServiceFactory,并注册了ServiceInterface的多个服务.欢呼!