Search code examples
javaosgi-bundle

OSGI bundle osgi.command.function run more than one function


I have an OSGI bundle, in my bundle I need to call more than one method from the command line, but I haven't find a way to do it.

Is it possible something like this?

osgi.command.function = myMethod1

osgi.command.function = myMethod2

and then call from the command line one of this method?

My workaround since now is to have one method with parameters and one of this parameters decide which method to call. Something like this:

   myDefaultMethod(String foo, String bar, String test){
    switch (foo) {
            case "load":
                    myMethod1(bar)
                    break
            case:"export":
                    myMethod2(bar, test)
                    break
    }

I don't like this solution because also to call the myMethod1 from the command line I must pass 3 paramenters(foo, bar and test) also if I need only 2

There is a better way to do that?


Solution

  • Yes you can absolutely, even have many overloaded version of the same method

    As here:

    @Component(
        service = TenantFactory.class,
        property = {
            "osgi.command.scope=myscope",
            "osgi.command.function=listTenants",
            "osgi.command.function=listTenantUsers"
        }
    )
    public final class TenantFactory {
        @Descriptor("List all tenants")
        public void listTenants() {
            ....
        }
    
        @Descriptor("List all users of given tenant")
        public void listTenantUsers(@Descriptor(TENANT_DESCR) String tenantName) {
            ....
        }
    }