Hi Alexander,
I don't know how it worked with previous gogo version, but when using a
command processor, if I'm correct then I think you should test if the
execute command has returned a result object and display it.
I adapted your example which seems to work for me:
*import* java.io.FileDescriptor;
*import* java.io.FileInputStream;
*import* java.io.FileOutputStream;
*import* org.apache.felix.dm.annotation.api.Component;
*import* org.apache.felix.dm.annotation.api.Property;
*import* org.apache.felix.dm.annotation.api.ServiceDependency;
*import* org.apache.felix.service.command.CommandProcessor;
*import* org.apache.felix.service.command.CommandSession;
*import* org.apache.felix.service.command.Converter;
*import* org.apache.felix.service.command.Descriptor;
@Component(provides = Object.*class*)
@Property(name = CommandProcessor.*COMMAND_SCOPE*, value = "test")
@Property(name = CommandProcessor.*COMMAND_FUNCTION*, value = { "execute" })
*public* *class* Example {
@ServiceDependency
CommandProcessor _cmdProc;
*public* *void* execute(String cmd) {
CommandSession session = _cmdProc.createSession(*new*
FileInputStream(FileDescriptor.*in*),
*new* FileOutputStream(FileDescriptor.*out*), *new*
FileOutputStream(FileDescriptor.*err*));
*try* {
Object result = session.execute(cmd);
*if* (result != *null*) {
System.*out*.println("displaying result returned by command:");
System.*out*.println(session.format(result, Converter.*INSPECT*));
}
} *catch* (Exception e) {
e.printStackTrace(System.*out*);
}
}
}
so, if you type under gogo shell "test:exec lb", then the session.execute()
method will return an object; in this case, you can (optionally) use the
session.format method in order to convert it and then display the result.
If now you type for example "test:exec dm", then since the dm gogo command
does not return a result (and display the result internally, using
System.out), then the session.execute method will return null, then in this
case nothing special to do ...
hope this helps
regards
/Pierre
On Tue, Oct 19, 2021 at 10:44 AM Alexander Broekhuis <[email protected]>
wrote:
> Hi all,
>
> I am trying to use the GoGo shell's CommandProcessor to execute commands
> via a rest-like interface. Using older versions of the shell, this used to
> work. But after updating to the latest version (runtime 1.1.4) this doesn't
> work anymore.
>
> Strangely enough, the DependencyManager commands do work, but the GoGo
> Commands don't. Looking in the source, the difference I can see is that the
> DM commands use System.out.println, while the GoGo Commands return the
> output in each command method.
>
> Is there something that I am missing?
>
> Creating a trivial example already shows this:
>
> ------------
> @Component(provides = TestShell.class)
> @Property(name = CommandProcessor.COMMAND_SCOPE, value = "test")
> @Property(name = CommandProcessor.COMMAND_FUNCTION, value = {"execute"})
> public class TestShell {
>
> @ServiceDependency
> private volatile CommandProcessor commandProcessor;
>
> public void execute(String command) throws Exception {
> CommandSession session = commandProcessor.createSession(new
> FileInputStream(FileDescriptor.in), new
> FileOutputStream(FileDescriptor.out), new
> FileOutputStream(FileDescriptor.err));
> session.execute(command);
> session.close();
> }
> }
> ------------
>
> Thanks in advance!
>
> --
> Met vriendelijke groet,
>
> Alexander Broekhuis
>