karaf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@petinou.fr
Subject Null service in Karaf command
Date Tue, 05 Jan 2016 20:30:25 GMT
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

Mime
View raw message