Custom command in OSGi (2)

Source: Internet
Author: User

Custom command in OSGi (2) Previous

In the previous blog post, we talked about what is the command in OSGi, and also wrote a simple command that implements org.apache.felix.gogo.commands.Action this interface and also implements the same functionality as the
org.apache.karaf.shell.console.OsgiCommandSupportThis abstract class, but in this program, these two interfaces or abstract classes are labeled @deprecated, it is not recommended to use, and the current recommended is a KARAF in the interface, for org.apache.karaf.shell.api.action.Action , but this in the use of the process will appear a certain problem, this later, This article is mainly about the use of Karaf provided by the @service, @Command and inheritance or implementation of the relevant interface to write command, Instead of configuring a command in the Blueprint.xml way, this text is not very good description, the following directly with the relevant procedures to explain the written command.

Custom command

First of all, to create a new class, named Samplecommand, which is written in three methods, two non-parametric method, a parameter, for the following:

    publicvoidfirst() {        System.out.println("first");    }    publicvoidsecond() {        System.out.println("second");    }    publicvoidhello(String name) {        System.out.println("hello " + name);    }

Above, and so will again console display, the above two no parameter method, is the ordinary command, and the parameter method is can pass in a parameter, and will be typed in the console, after the completion of the above, but also in the blueprint.xml to configure the relevant commands, Blueprint.xml content as follows:

<Bean class="Cn.com.files.command.SampleCommand" id=" Samplecommand "/>    <service auto-export="all-classes" ref="Samplecommand" >        <service-properties>            <entry key="Osgi.command.scope">                <!--define the prefix for you commands--                <value>Sample</value>            </Entry>            <entry key="Osgi.command.function">                <!--Declare the list of all the methods-want to expose-                <array value-type="java.lang.String">                    <value>First</value>                    <value>Second</value>                    <value>Hello</value>                </array>            </Entry>        </service-properties>    </Service>

We first set up a bean configuration, we will configure the Samplecommand as a bean, and then we configure a service, and the value in Osgi.command.scope is specified as sample, which is the scope we wrote previously, and the value in the array specified in the specified osgi.command.function, which is a few method names written for us, so after the configuration is complete, our Comman D is basically complete, now only need to re-mvn clean install-u-dskiptests in the console, again open the Karaf console, observe the current bundle start state, as well as the command we wrote.

Run

Open the Karaf console and enter the List,bundle running status as follows:

Where the ID is 13, the name of the storage state is active, the bundle starts normally, now try to run the command we wrote, enter sample, this is the scope we defined previously, press the TAB key, the following prompt:

In the prompt, three methods are prompted in turn, we execute the Sample:first,sample:second will be in the console to play the content, as follows:

The above in order to execute the contents of our method, now start our Method 3 is Sample:hello, after entering the above command, press the TAB key, there is no hint at this time, but we need to enter a parameter, this hint in the previous chapter of the command definition method, and specify a parameter , and write the relevant method is indeed a hint after this later, after we entered Sample:hello World, the console typed the content:

As a function, the command with parameters also executes normally.

Other implementations

In fact, in the implementation of the command with parameters, there is another implementation, here you can say, here is another kind of annotation, that is, @argument, add this to our command in the parameters, like the following:

@Argument0"arg""the command argument"falsefalse)String arg;

And then you need a Completer class, which, as a hint to the parameter, I write as follows:

/** * Created by Xiaxuan in 16/7/6. * A simple completer */ Public  class simplecompeter implements completer {     Public int  Complete(String buffer,intCursor, list<string> candidates) {Stringscompleter delegate =NewStringscompleter (); Delegate.getstrings (). Add ("One"); Delegate.getstrings (). Add ("both"); Delegate.getstrings (). Add ("three");returnDelegate.complete (buffer, cursor, candidates); }}

You also need to configure the relevant completer in the commands in Blueprint.xml, as follows:

<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">        <command name="Test/args">            <action class="Cn.com.files.command.ArgsCommand">            </Action>            <completers>                <ref Component-id="Simplecompleter"/>                <null/>            </completers>        </command>    </command-bundle>

This also completes a command with parameters, but the problem is that when I execute this command in the Karaf console, I do have a hint when I press the TAB key, which is the one, the other, the three we wrote in Completer. But enter any of them, output in the console is always null, I look at the Karaf source code, Karaf in any of the modules are basically using a lot of commands, I use the method and their similar, but the output is a certain problem, If the people who have studied hope to discuss this problem with me.

Summarize
    • In fact, this command, in more, is the extension of their own, used to make third-party clients more, in the normal application of the writing, this command is still relatively small, because in the production environment, most of the ssh login limit, So it's almost impossible to telnet to the Karaf console to execute your own commands.

    • In order to use the command, more or use the follow-up of this command is better, this official provides more support, and provides many kinds of annotations can be used, can add @argument, @Option and so on.

    • Commands are more than just an aid to our application, and in a real production environment, it is recommended that you do not use this command in a large number of applications, either using interfaces or interfaces.

Custom command in OSGi (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.