karaf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jean-Baptiste Onofré ...@nanthrax.net>
Subject Re: Null service in Karaf command
Date Wed, 06 Jan 2016 06:46:04 GMT
You should not mix DS and Karaf shell API in the same bundle (it's one 
or the other).

So, you should have:

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

@Command(scope = "myscope", name = "myCmd", description = "desc")
@Service
public class MyCmd implements Action {

    @Reference
    private Model model;

       @Override
       public Object execute() throws Exception {
	...
       }

}

And you just use the Karaf command plugin:

                 <plugin>
                     <groupId>org.apache.karaf.tooling</groupId>
                     <artifactId>karaf-services-maven-plugin</artifactId>
                     <version>4.0.3</version>
                     <executions>
                         <execution>
                             <id>service-metadata-generate</id>
                             <phase>process-classes</phase>
                             <goals>
                                 <goal>service-metadata-generate</goal>
                             </goals>
                         </execution>
                     </executions>
                 </plugin>


No need to use DS in the command.

Regards
JB

On 01/05/2016 09:30 PM, ben@petinou.fr wrote:
> Hi everyone,
>
> I'm trying to create a Karaf Command that uses one of my services (via
> Declarative Services configured with maven-bundle-plugin).
>
> Here is my Karaf command:
>
> @Command(scope = "myscope", name = "myCmd", description = "desc")
> @Service
> @Component(immediate = true)
> public class MyCmd implements Action {
>      /** Reference to the model. */
>      private Model model;
>
>      @Override
>      public Object execute() throws Exception {
>           System.out.println("Model: " + model);
>           return null;
>      }
>
>      @Reference
>      public void setModel(final Model modelToSet) {
>          System.out.println("MyCmd.setModel: " + modelToSet);
>          model = modelToSet;
>      }
>
>      public void unsetModel(final Model modelToSet) {
>          System.out.println("MyCmd.unsetModel");
>          model = null;
>      }
> }
>
> When I start Karaf, I have the following output in the console:
> MyCmd.setModel: ModelImpl@23623746
>
> But when I call the command, the model is null...
> karaf@root()> myscope:myCmd
> Model: null
>
> I guess there is something wrong with how Karaf creates the commands (I
> guess the commands are instantiated each time they are called from the
> console).
>
> I just declared the ModelImpl class with @Component (and it implements
> my Model interface).
>
> How could I make it work?
>
> Kind regards,
>
> Ben

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

Mime
View raw message