As I mentioned a few weeks ago, I'm trying to create a service instance
programmatically and write the associated .cfg file for it. Based on feedback
I got, I create the following method.
The goal is to create the service instance and then get its configuration which
at this point will mostly be the defaults that it receives in its configuration
type (hence my previous post). It appears to work, except what is really
happening is that I get two services. One is created by
createFactoryConfguration call and the other by Felix FileInstall after the
.cfg file is written.
Actually I'm a bit surprised that placeholder .cfg file isn't created if
property felix.fileinstall.filename is defined, as I think that would solve
this problem, but perhaps there's another way?
Thoughts?
private void createConfig(String cpid, String dir, String name,
Map<String,Object> props)
throws Exception
{
Configuration conf = parent.cfgAdmin.createFactoryConfiguration(cpid,
"?");
dir = (dir.endsWith("/") ? dir : dir + '/');
name = name.replaceAll(" ", "_").toLowerCase();
File cfg = new File(dir + cpid + '-' + name + ".cfg");
Dictionary<String,Object> confProps = toDictionary(props);
confProps.put("felix.fileinstall.filename", cfg.toURI().toString());
try
{
conf.update(confProps);
// Get the properties for the new service object
props = getProps(conf.getPid());
// Create .cfg file with initial property set
ITemplate tmp = parent.templateMgr.getTemplate(
configTemplates.get(cpid) );
tmp.bind("cfg", props);
try (FileLineWriter w = new FileLineWriter(cfg))
{
w.putLine(tmp.render());
}
}
catch (Exception e)
{
cfg.delete();
conf.delete();
throw e;
}
}