You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Paul Spencer <pa...@mindspring.com> on 2022/05/12 16:38:56 UTC

How do I load configuration within a shell command?

Karaf 4.3.6
I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.


In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?

Paul Spencer

Re: How do I load configuration within a shell command?

Posted by Paul Spencer <pa...@mindspring.com>.
João,
I would prefer not to add component bundles whose sole purpose is to pass configuration values from pid.cfg to a command via @Reference.

Paul Spencer

> On May 13, 2022, at 9:48 AM, João Assunção <jo...@exploitsys.com> wrote:
> 
> Why not have the components implement the required operations as a service, or expose those values in some service operation, and then make the shell command use that service ?
> 
> João Assunção
> 
> Email: joao.assuncao@exploitsys.com
> Mobile: +351 916968984
> Phone: +351 211933149
> Web: www.exploitsys.com
> 
> 
> 
> 
> On Fri, May 13, 2022 at 2:27 PM Paul Spencer <pa...@mindspring.com> wrote:
> JB,
> The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.
> 
> Paul Spencer
> 
> > On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> > 
> > Hi Paul,
> > 
> > Not sure I understand exactly the request, but you can do:
> > 
> > config:edit pid
> > config:property-list ...
> > config:cancel | config:commit
> > 
> > Regards
> > JB
> > 
> > On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
> >> 
> >> Karaf 4.3.6
> >> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
> >> 
> >> 
> >> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
> >> 
> >> Paul Spencer
> 


Re: How do I load configuration within a shell command?

Posted by João Assunção <jo...@exploitsys.com>.
Why not have the components implement the required operations as a service,
or expose those values in some service operation, and then make the shell
command use that service ?

João Assunção

Email: joao.assuncao@exploitsys.com
Mobile: +351 916968984
Phone: +351 211933149
Web: www.exploitsys.com




On Fri, May 13, 2022 at 2:27 PM Paul Spencer <pa...@mindspring.com>
wrote:

> JB,
> The command needs to use values stored in pid.cfg files, like instance
> specific CustomerName, just like other components.
>
> Paul Spencer
>
> > On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net>
> wrote:
> >
> > Hi Paul,
> >
> > Not sure I understand exactly the request, but you can do:
> >
> > config:edit pid
> > config:property-list ...
> > config:cancel | config:commit
> >
> > Regards
> > JB
> >
> > On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com>
> wrote:
> >>
> >> Karaf 4.3.6
> >> I am looking to load configuration into a shell command, but I do not
> see where to set the configuration PID nor do I see where the configuration
> is passed or injected into the Action class.
> >>
> >>
> >> In a component, the configuration loaded by @Activate is defined by
> @Component and the configurationPid attribute. What are the equivalents for
> shell commands, class that implement
> org.apache.karaf.shell.api.action.Action?
> >>
> >> Paul Spencer
>
>

Re: How do I load configuration within a shell command?

Posted by Paul Spencer <pa...@mindspring.com>.
JB,
Thank you for the answer!

Below is the resulting use case.
***
* Command use case of getting companyName from customerInfo configuration PID 
***

import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

@Service
@Command(scope = "customer", name = "info")
public class InfoCommand implements Action {
    @Reference
    private ConfigurationAdmin configAdmin;

    @Override
    public Object execute() {
        Configuration[] configs = configAdmin.listConfigurations("(service.pid=customerInfo)");
        if (configs != null && configs.length == 1) {
           String companyName = (String) configs[0].getProperties().get("companyName");
           System.out.println("Company name = " + companyName);
        } 
    }
}


Paul Spencer

> On May 13, 2022, at 3:23 PM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> 
> I don't understand your question, sorry ;)
> 
> You want to display companyName ?
> 
> String companyName = null;
> Configuration[] configs =
> configAdmin.listConfiguration("(service.pid=customerInfo)");
> if (configs != null && configs.length == 1) {
>  companyName = configs[0].getProperties().get("companyName");
> }
> 
> Regards
> JB
> 
> On Fri, May 13, 2022 at 8:57 PM Paul Spencer <pa...@mindspring.com> wrote:
>> 
>> JB,
>> The configuration PID files already exist and are used by other components.
>> 
>> Below are two use cases that will print the companyName property from the companyInfo configuration PID.  The component use case  works today.  Please complete the command use case.
>> 
>> ***
>> * /etc/companyInfo.cfg
>> ***
>> companyName = FooBar Inc.
>> 
>> ***
>> * Component use case of getting companyName from customerInfo configuration PID
>> ***
>> @Component configuationPid = {"customerInfo"})
>> public class Foo implements FooService {
>> 
>>  @Activate
>>  protected activate(final Map<String,Object> configProperties) {
>>    String companyName = configProperties.get("companyName").toString();
>>    log.info("Foo Service activated for " companyName);
>>  }
>> }
>> 
>> ***
>> * Command use case if getting companyName from customerInfo configuration PID
>> ***
>> @Service
>> @Command(scope = "customer", name = "info")
>> public class InfoCommand implements Action {
>> 
>>  @Override
>>  public Object execute(){
>>     // How to populate company name for customerInfo configuration PID?
>>     String companyName = ??
>> 
>>     // Print company info
>>     System.out.println("Company name = " + companyName);
>>  }
>> 
>> Paul Spencer
>> 
>> 
>>> On May 13, 2022, at 12:53 PM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>>> 
>>> No, you don't need to have a cfg file.
>>> 
>>> You can create the config like this, if the pid doesn't exist, it will
>>> be created and populated with config:property-set.
>>> 
>>> Regards
>>> JB
>>> 
>>> On Fri, May 13, 2022 at 3:27 PM Paul Spencer <pa...@mindspring.com> wrote:
>>>> 
>>>> JB,
>>>> The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.
>>>> 
>>>> Paul Spencer
>>>> 
>>>>> On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>>>>> 
>>>>> Hi Paul,
>>>>> 
>>>>> Not sure I understand exactly the request, but you can do:
>>>>> 
>>>>> config:edit pid
>>>>> config:property-list ...
>>>>> config:cancel | config:commit
>>>>> 
>>>>> Regards
>>>>> JB
>>>>> 
>>>>> On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
>>>>>> 
>>>>>> Karaf 4.3.6
>>>>>> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
>>>>>> 
>>>>>> 
>>>>>> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
>>>>>> 
>>>>>> Paul Spencer
>>>> 
>> 


Re: How do I load configuration within a shell command?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
I don't understand your question, sorry ;)

You want to display companyName ?

String companyName = null;
Configuration[] configs =
configAdmin.listConfiguration("(service.pid=customerInfo)");
if (configs != null && configs.length == 1) {
  companyName = configs[0].getProperties().get("companyName");
}

Regards
JB

On Fri, May 13, 2022 at 8:57 PM Paul Spencer <pa...@mindspring.com> wrote:
>
> JB,
> The configuration PID files already exist and are used by other components.
>
> Below are two use cases that will print the companyName property from the companyInfo configuration PID.  The component use case  works today.  Please complete the command use case.
>
> ***
> * /etc/companyInfo.cfg
> ***
> companyName = FooBar Inc.
>
> ***
> * Component use case of getting companyName from customerInfo configuration PID
> ***
> @Component configuationPid = {"customerInfo"})
> public class Foo implements FooService {
>
>   @Activate
>   protected activate(final Map<String,Object> configProperties) {
>     String companyName = configProperties.get("companyName").toString();
>     log.info("Foo Service activated for " companyName);
>   }
> }
>
> ***
> * Command use case if getting companyName from customerInfo configuration PID
> ***
> @Service
> @Command(scope = "customer", name = "info")
> public class InfoCommand implements Action {
>
>   @Override
>   public Object execute(){
>      // How to populate company name for customerInfo configuration PID?
>      String companyName = ??
>
>      // Print company info
>      System.out.println("Company name = " + companyName);
>   }
>
> Paul Spencer
>
>
> > On May 13, 2022, at 12:53 PM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> >
> > No, you don't need to have a cfg file.
> >
> > You can create the config like this, if the pid doesn't exist, it will
> > be created and populated with config:property-set.
> >
> > Regards
> > JB
> >
> > On Fri, May 13, 2022 at 3:27 PM Paul Spencer <pa...@mindspring.com> wrote:
> >>
> >> JB,
> >> The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.
> >>
> >> Paul Spencer
> >>
> >>> On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> >>>
> >>> Hi Paul,
> >>>
> >>> Not sure I understand exactly the request, but you can do:
> >>>
> >>> config:edit pid
> >>> config:property-list ...
> >>> config:cancel | config:commit
> >>>
> >>> Regards
> >>> JB
> >>>
> >>> On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
> >>>>
> >>>> Karaf 4.3.6
> >>>> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
> >>>>
> >>>>
> >>>> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
> >>>>
> >>>> Paul Spencer
> >>
>

Re: How do I load configuration within a shell command?

Posted by Paul Spencer <pa...@mindspring.com>.
JB,
The configuration PID files already exist and are used by other components. 

Below are two use cases that will print the companyName property from the companyInfo configuration PID.  The component use case  works today.  Please complete the command use case.

***
* /etc/companyInfo.cfg
***
companyName = FooBar Inc.

***
* Component use case of getting companyName from customerInfo configuration PID 
***
@Component configuationPid = {"customerInfo"})
public class Foo implements FooService {

  @Activate
  protected activate(final Map<String,Object> configProperties) {
    String companyName = configProperties.get("companyName").toString();
    log.info("Foo Service activated for " companyName);
  }
}

***
* Command use case if getting companyName from customerInfo configuration PID 
***
@Service
@Command(scope = "customer", name = "info")
public class InfoCommand implements Action {

  @Override
  public Object execute(){
     // How to populate company name for customerInfo configuration PID? 
     String companyName = ?? 

     // Print company info
     System.out.println("Company name = " + companyName);
  }

Paul Spencer


> On May 13, 2022, at 12:53 PM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> 
> No, you don't need to have a cfg file.
> 
> You can create the config like this, if the pid doesn't exist, it will
> be created and populated with config:property-set.
> 
> Regards
> JB
> 
> On Fri, May 13, 2022 at 3:27 PM Paul Spencer <pa...@mindspring.com> wrote:
>> 
>> JB,
>> The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.
>> 
>> Paul Spencer
>> 
>>> On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>>> 
>>> Hi Paul,
>>> 
>>> Not sure I understand exactly the request, but you can do:
>>> 
>>> config:edit pid
>>> config:property-list ...
>>> config:cancel | config:commit
>>> 
>>> Regards
>>> JB
>>> 
>>> On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
>>>> 
>>>> Karaf 4.3.6
>>>> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
>>>> 
>>>> 
>>>> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
>>>> 
>>>> Paul Spencer
>> 


Re: How do I load configuration within a shell command?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
No, you don't need to have a cfg file.

You can create the config like this, if the pid doesn't exist, it will
be created and populated with config:property-set.

Regards
JB

On Fri, May 13, 2022 at 3:27 PM Paul Spencer <pa...@mindspring.com> wrote:
>
> JB,
> The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.
>
> Paul Spencer
>
> > On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> >
> > Hi Paul,
> >
> > Not sure I understand exactly the request, but you can do:
> >
> > config:edit pid
> > config:property-list ...
> > config:cancel | config:commit
> >
> > Regards
> > JB
> >
> > On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
> >>
> >> Karaf 4.3.6
> >> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
> >>
> >>
> >> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
> >>
> >> Paul Spencer
>

Re: How do I load configuration within a shell command?

Posted by Paul Spencer <pa...@mindspring.com>.
JB,
The command needs to use values stored in pid.cfg files, like instance specific CustomerName, just like other components.

Paul Spencer

> On May 13, 2022, at 1:46 AM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> 
> Hi Paul,
> 
> Not sure I understand exactly the request, but you can do:
> 
> config:edit pid
> config:property-list ...
> config:cancel | config:commit
> 
> Regards
> JB
> 
> On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
>> 
>> Karaf 4.3.6
>> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
>> 
>> 
>> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
>> 
>> Paul Spencer


Re: How do I load configuration within a shell command?

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Paul,

Not sure I understand exactly the request, but you can do:

config:edit pid
config:property-list ...
config:cancel | config:commit

Regards
JB

On Thu, May 12, 2022 at 6:38 PM Paul Spencer <pa...@mindspring.com> wrote:
>
> Karaf 4.3.6
> I am looking to load configuration into a shell command, but I do not see where to set the configuration PID nor do I see where the configuration is passed or injected into the Action class.
>
>
> In a component, the configuration loaded by @Activate is defined by @Component and the configurationPid attribute. What are the equivalents for shell commands, class that implement org.apache.karaf.shell.api.action.Action?
>
> Paul Spencer