You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Erwin Müller <er...@deventm.org> on 2016/04/13 20:49:18 UTC

Command and SCR Reference

Hello,

I setup an OSGi application with Felix SCR and Karaf. But how can I use the 
SCR annotations for Karaf commands? I tried the example command from the Karaf 
manual and it works fine, but if I add the Felix SCR annotations, the command 
is not working anymore in Karaf. 

I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam, Sling), 
I'm only stuck

----------
@Command(scope = "sscontrol", name = "parse", description = "Parses the 
specified resource and checks for eventual errors.")
@Service
@Component
public class ParseCommand implements Action {

    @Argument(name = "resource", description = "The resource URI to be 
parsed.", required = true, multiValued = false)
    private String resource;

    @Reference
    private ParserService parseService;

    @Override
    public Object execute() throws Exception {
        Parser parser = parseService.create();
        SscontrolScript script = parser.parse(toUri(resource));
        return script;
    }

    private URI toUri(String resource) throws URISyntaxException {
        return new URI(resource);
    }
}
--------
-- 
Erwin Müller - erwin.mueller@deventm.org
Software Entwickler - (+49)  01577-9505569
Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
Meine Seite - http://www.mueller-public.de/
ANRI - http://www.anr-institute.com/

Re: Command and SCR Reference

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

We have a complete new dev guide in progress.

Regards
JB

On 04/14/2016 10:14 AM, Erwin Müller wrote:
> Ok, thank you, now it works.
>
> It would be nice to add this to the documentation in
>
> https://karaf.apache.org/manual/latest/developers-guide/extending.html
>
> Maybe under the examples of a simple command.
>
> Something like
>
> -------
> package org.apache.karaf.shell.samples;
>
> import org.apache.karaf.shell.api.action.Action;
> import org.apache.karaf.shell.api.action.Argument;
> import org.apache.karaf.shell.api.action.Command;
> import org.apache.karaf.shell.api.action.Completion;
> import org.apache.karaf.shell.api.action.lifecycle.Reference;
> import org.apache.karaf.shell.api.action.lifecycle.Service;
>
> @Command(scope = "test", name = "hello", description="Says hello")
> @Service
> public class HelloShellCommand implements Action {
>
>      @Argument(index = 0, name = "name", description = "The name that sends the
> greet.", required = true, multiValued = false)
>      @Completion(SimpleNameCompleter.class)
>      String name = null;
>
>      @Reference
>      MyService service;
>
>      @Override
>      public Object execute() throws Exception {
>          System.out.println("Hello " + name);
>          return null;
>      }
> }
> ------
>
> Regards, Erwin.
>
> On Thursday, April 14, 2016 06:36:12 AM Jean-Baptiste Onofré wrote:
>> Hi Erwin,
>>
>> you have the @Reference annotation in command:
>> org.apache.karaf.shell.api.action.lifecycle.Reference
>>
>> Same usage as in SCR.
>>
>> Regards
>> JB
>>
>> On 04/13/2016 10:38 PM, Erwin Müller wrote:
>>> Thank you,
>>>
>>> but how can I inject services like I do in SCR? Or do I need that to do
>>> manually via the BundleContext?
>>>
>>> -------
>>>
>>>         @Reference
>>>         private ParserService parseService;
>>>
>>> -------
>>>
>>> Regards, Erwin.
>>>
>>> On Wednesday, April 13, 2016 08:57:47 PM Jean-Baptiste Onofré wrote:
>>>> Hi,
>>>>
>>>> You don't need SCR if you use the command annotation.
>>>>
>>>> Just use the plugin:
>>>>                <plugin>
>>>>
>>>>                    <groupId>org.apache.karaf.tooling</groupId>
>>>>                    <artifactId>karaf-services-maven-plugin</artifactId>
>>>>
>>>>                </plugin>
>>>>
>>>> And no need to use @Component.
>>>>
>>>> Regards
>>>> JB
>>>>
>>>> On 04/13/2016 08:49 PM, Erwin Müller wrote:
>>>>> Hello,
>>>>>
>>>>> I setup an OSGi application with Felix SCR and Karaf. But how can I use
>>>>> the
>>>>> SCR annotations for Karaf commands? I tried the example command from the
>>>>> Karaf manual and it works fine, but if I add the Felix SCR annotations,
>>>>> the command is not working anymore in Karaf.
>>>>>
>>>>> I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam,
>>>>> Sling), I'm only stuck
>>>>>
>>>>> ----------
>>>>> @Command(scope = "sscontrol", name = "parse", description = "Parses the
>>>>> specified resource and checks for eventual errors.")
>>>>> @Service
>>>>> @Component
>>>>> public class ParseCommand implements Action {
>>>>>
>>>>>        @Argument(name = "resource", description = "The resource URI to be
>>>>>
>>>>> parsed.", required = true, multiValued = false)
>>>>>
>>>>>        private String resource;
>>>>>
>>>>>        @Reference
>>>>>        private ParserService parseService;
>>>>>
>>>>>        @Override
>>>>>        public Object execute() throws Exception {
>>>>>
>>>>>            Parser parser = parseService.create();
>>>>>            SscontrolScript script = parser.parse(toUri(resource));
>>>>>            return script;
>>>>>
>>>>>        }
>>>>>
>>>>>        private URI toUri(String resource) throws URISyntaxException {
>>>>>
>>>>>            return new URI(resource);
>>>>>
>>>>>        }
>>>>>
>>>>> }
>>>>> --------
>

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

Re: Command and SCR Reference

Posted by Erwin Müller <er...@deventm.org>.
Ok, thank you, now it works.

It would be nice to add this to the documentation in

https://karaf.apache.org/manual/latest/developers-guide/extending.html

Maybe under the examples of a simple command.

Something like

-------
package org.apache.karaf.shell.samples;

import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Command(scope = "test", name = "hello", description="Says hello")
@Service
public class HelloShellCommand implements Action {

    @Argument(index = 0, name = "name", description = "The name that sends the 
greet.", required = true, multiValued = false)
    @Completion(SimpleNameCompleter.class)
    String name = null;

    @Reference
    MyService service;

    @Override
    public Object execute() throws Exception {
        System.out.println("Hello " + name);
        return null;
    }
}
------

Regards, Erwin.

On Thursday, April 14, 2016 06:36:12 AM Jean-Baptiste Onofré wrote:
> Hi Erwin,
> 
> you have the @Reference annotation in command:
> org.apache.karaf.shell.api.action.lifecycle.Reference
> 
> Same usage as in SCR.
> 
> Regards
> JB
> 
> On 04/13/2016 10:38 PM, Erwin Müller wrote:
> > Thank you,
> > 
> > but how can I inject services like I do in SCR? Or do I need that to do
> > manually via the BundleContext?
> > 
> > -------
> > 
> >        @Reference
> >        private ParserService parseService;
> > 
> > -------
> > 
> > Regards, Erwin.
> > 
> > On Wednesday, April 13, 2016 08:57:47 PM Jean-Baptiste Onofré wrote:
> >> Hi,
> >> 
> >> You don't need SCR if you use the command annotation.
> >> 
> >> Just use the plugin:
> >>               <plugin>
> >>               
> >>                   <groupId>org.apache.karaf.tooling</groupId>
> >>                   <artifactId>karaf-services-maven-plugin</artifactId>
> >>               
> >>               </plugin>
> >> 
> >> And no need to use @Component.
> >> 
> >> Regards
> >> JB
> >> 
> >> On 04/13/2016 08:49 PM, Erwin Müller wrote:
> >>> Hello,
> >>> 
> >>> I setup an OSGi application with Felix SCR and Karaf. But how can I use
> >>> the
> >>> SCR annotations for Karaf commands? I tried the example command from the
> >>> Karaf manual and it works fine, but if I add the Felix SCR annotations,
> >>> the command is not working anymore in Karaf.
> >>> 
> >>> I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam,
> >>> Sling), I'm only stuck
> >>> 
> >>> ----------
> >>> @Command(scope = "sscontrol", name = "parse", description = "Parses the
> >>> specified resource and checks for eventual errors.")
> >>> @Service
> >>> @Component
> >>> public class ParseCommand implements Action {
> >>> 
> >>>       @Argument(name = "resource", description = "The resource URI to be
> >>> 
> >>> parsed.", required = true, multiValued = false)
> >>> 
> >>>       private String resource;
> >>>       
> >>>       @Reference
> >>>       private ParserService parseService;
> >>>       
> >>>       @Override
> >>>       public Object execute() throws Exception {
> >>>       
> >>>           Parser parser = parseService.create();
> >>>           SscontrolScript script = parser.parse(toUri(resource));
> >>>           return script;
> >>>       
> >>>       }
> >>>       
> >>>       private URI toUri(String resource) throws URISyntaxException {
> >>>       
> >>>           return new URI(resource);
> >>>       
> >>>       }
> >>> 
> >>> }
> >>> --------

-- 
Erwin Müller - erwin.mueller@deventm.org
Software Entwickler - (+49)  01577-9505569
Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
Meine Seite - http://www.mueller-public.de/
ANRI - http://www.anr-institute.com/

Re: Command and SCR Reference

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

you have the @Reference annotation in command: 
org.apache.karaf.shell.api.action.lifecycle.Reference

Same usage as in SCR.

Regards
JB

On 04/13/2016 10:38 PM, Erwin Müller wrote:
> Thank you,
>
> but how can I inject services like I do in SCR? Or do I need that to do
> manually via the BundleContext?
>
> -------
>        @Reference
>        private ParserService parseService;
> -------
>
> Regards, Erwin.
>
>
> On Wednesday, April 13, 2016 08:57:47 PM Jean-Baptiste Onofré wrote:
>> Hi,
>>
>> You don't need SCR if you use the command annotation.
>>
>> Just use the plugin:
>>
>>               <plugin>
>>                   <groupId>org.apache.karaf.tooling</groupId>
>>                   <artifactId>karaf-services-maven-plugin</artifactId>
>>               </plugin>
>>
>>
>> And no need to use @Component.
>>
>> Regards
>> JB
>>
>> On 04/13/2016 08:49 PM, Erwin Müller wrote:
>>> Hello,
>>>
>>> I setup an OSGi application with Felix SCR and Karaf. But how can I use
>>> the
>>> SCR annotations for Karaf commands? I tried the example command from the
>>> Karaf manual and it works fine, but if I add the Felix SCR annotations,
>>> the command is not working anymore in Karaf.
>>>
>>> I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam,
>>> Sling), I'm only stuck
>>>
>>> ----------
>>> @Command(scope = "sscontrol", name = "parse", description = "Parses the
>>> specified resource and checks for eventual errors.")
>>> @Service
>>> @Component
>>> public class ParseCommand implements Action {
>>>
>>>       @Argument(name = "resource", description = "The resource URI to be
>>>
>>> parsed.", required = true, multiValued = false)
>>>
>>>       private String resource;
>>>
>>>       @Reference
>>>       private ParserService parseService;
>>>
>>>       @Override
>>>       public Object execute() throws Exception {
>>>
>>>           Parser parser = parseService.create();
>>>           SscontrolScript script = parser.parse(toUri(resource));
>>>           return script;
>>>
>>>       }
>>>
>>>       private URI toUri(String resource) throws URISyntaxException {
>>>
>>>           return new URI(resource);
>>>
>>>       }
>>>
>>> }
>>> --------
>

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

Re: Command and SCR Reference

Posted by Erwin Müller <er...@deventm.org>.
Thank you, 

but how can I inject services like I do in SCR? Or do I need that to do 
manually via the BundleContext?

-------
      @Reference
      private ParserService parseService;
-------

Regards, Erwin.


On Wednesday, April 13, 2016 08:57:47 PM Jean-Baptiste Onofré wrote:
> Hi,
> 
> You don't need SCR if you use the command annotation.
> 
> Just use the plugin:
> 
>              <plugin>
>                  <groupId>org.apache.karaf.tooling</groupId>
>                  <artifactId>karaf-services-maven-plugin</artifactId>
>              </plugin>
> 
> 
> And no need to use @Component.
> 
> Regards
> JB
> 
> On 04/13/2016 08:49 PM, Erwin Müller wrote:
> > Hello,
> > 
> > I setup an OSGi application with Felix SCR and Karaf. But how can I use
> > the
> > SCR annotations for Karaf commands? I tried the example command from the
> > Karaf manual and it works fine, but if I add the Felix SCR annotations,
> > the command is not working anymore in Karaf.
> > 
> > I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam,
> > Sling), I'm only stuck
> > 
> > ----------
> > @Command(scope = "sscontrol", name = "parse", description = "Parses the
> > specified resource and checks for eventual errors.")
> > @Service
> > @Component
> > public class ParseCommand implements Action {
> > 
> >      @Argument(name = "resource", description = "The resource URI to be
> > 
> > parsed.", required = true, multiValued = false)
> > 
> >      private String resource;
> >      
> >      @Reference
> >      private ParserService parseService;
> >      
> >      @Override
> >      public Object execute() throws Exception {
> >      
> >          Parser parser = parseService.create();
> >          SscontrolScript script = parser.parse(toUri(resource));
> >          return script;
> >      
> >      }
> >      
> >      private URI toUri(String resource) throws URISyntaxException {
> >      
> >          return new URI(resource);
> >      
> >      }
> > 
> > }
> > --------

-- 
Erwin Müller - erwin.mueller@deventm.org
Software Entwickler - (+49)  01577-9505569
Pgp - https://pgp.mit.edu/pks/lookup?op=get&search=0x02E820911DD910FD
Meine Seite - http://www.mueller-public.de/
ANRI - http://www.anr-institute.com/

Re: Command and SCR Reference

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

You don't need SCR if you use the command annotation.

Just use the plugin:

             <plugin>
                 <groupId>org.apache.karaf.tooling</groupId>
                 <artifactId>karaf-services-maven-plugin</artifactId>
             </plugin>


And no need to use @Component.

Regards
JB

On 04/13/2016 08:49 PM, Erwin Müller wrote:
> Hello,
>
> I setup an OSGi application with Felix SCR and Karaf. But how can I use the
> SCR annotations for Karaf commands? I tried the example command from the Karaf
> manual and it works fine, but if I add the Felix SCR annotations, the command
> is not working anymore in Karaf.
>
> I'm pretty new to OSGi but I got everything working (Karaf, Pax Exam, Sling),
> I'm only stuck
>
> ----------
> @Command(scope = "sscontrol", name = "parse", description = "Parses the
> specified resource and checks for eventual errors.")
> @Service
> @Component
> public class ParseCommand implements Action {
>
>      @Argument(name = "resource", description = "The resource URI to be
> parsed.", required = true, multiValued = false)
>      private String resource;
>
>      @Reference
>      private ParserService parseService;
>
>      @Override
>      public Object execute() throws Exception {
>          Parser parser = parseService.create();
>          SscontrolScript script = parser.parse(toUri(resource));
>          return script;
>      }
>
>      private URI toUri(String resource) throws URISyntaxException {
>          return new URI(resource);
>      }
> }
> --------
>

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