Thanks JB,

Initially, the .cfg would be populated almost entirely with the defaults  the 
service receives using a configuration type so the idea is that the service 
creation needs to come first so that the service can be used to get the values 
to write the .cfg.

Is there a way to get those value without actually having a service instance or 
hard-coding them? 

Scott

-----Original Message-----
From: Jean-Baptiste Onofré [mailto:j...@nanthrax.net] 
Sent: Thursday, February 22, 2018 9:38 PM
To: user@karaf.apache.org
Subject: Re: On another topic: Creating services programatically again

Hi Scott,

why don't use a managed service factory ?

It would automatically create a service based on a cfg. So for your user, he 
creates the cfg file, and then, automatically, the corresponding service is 
created.

Thoughts ?

Regards
JB

On 02/22/2018 09:18 PM, Leschke, Scott wrote:
> 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;
> 
>        }
> 
> }
> 
>  
> 

--
Jean-Baptiste Onofré
jbono...@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Reply via email to