使用出厂配置将允许您根据不同的配置创建FooImpl的不同实例。
例如,在声明式服务,你可以创建一个像
import org.apache.felix.scr.annotations.*;
import org.apache.sling.commons.osgi.PropertiesUtil;
@Component(metatype = true,
name = FooImpl.SERVICE_PID,
configurationFactory = true,
specVersion = "1.1",
policy = ConfigurationPolicy.REQUIRE)
public class FooImpl implements IFoo
{
//The PID can also be defined in interface
public static final String SERVICE_PID = "com.foo.factory";
private static final String DEFAULT_BAR = "yahoo";
@Property
private static final String PROP_BAR = "bar";
@Property(intValue = 0)
static final String PROP_RANKING = "ranking";
private ServiceRegistration reg;
@Activate
public void activate(BundleContext context, Map<String, ?> conf)
throws InvalidSyntaxException
{
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("type", PropertiesUtil.toString(config.get(PROP_BAR), DEFAULT_BAR));
props.put(Constants.SERVICE_RANKING,
PropertiesUtil.toInteger(config.get(PROP_RANKING), 0));
reg = context.registerService(IFoo.class.getName(), this, props);
}
@Deactivate
private void deactivate()
{
if (reg != null)
{
reg.unregister();
}
}
}
要点组件这里是
- 您使用类型
configurationFactory
- 的一个组成部分。在你读取配置的激活方法然后根据该注册服务
- 停用您明确取消注册服务
- 最终用户将创建名称为
<pid>-<some name>.cfg
的配置文件。然后DS会激活组件。
然后你就可以通过(使用文件安装等)与名<pid>-<some name>.cfg
文件中像com.foo.factory-type1.cfg
参考JdbcLoginModuleFactory及其相关config一个这样的例子创建配置创建多个实例。
如果你想通过普通OSGi实现相同,那么你需要注册一个ManagedServiceFactory。一个这样的例子参考JaasConfigFactory。
要点这里是
- 您进行配置PID注册ManagedServiceFactory实例作为服务属性
- 在ManagedServiceFactory(字符串PID,字典属性)FooImpl的回调登记情况基础上,配置属性
请您提供更多详情。我已经添加了部署注释 - 服务应该如何显示在系统中,特别是我无法理解新的显示方式。cfg文件(由FileInstall管理)将引起出现'FooImpl'的新实例 – Dewfy 2013-04-07 07:47:24