OSGI中Command -控制台命令
来源:互联网 发布:天龙八部九阴神爪 知 编辑:程序博客网 时间:2024/06/07 00:58
在OSGI的中开发bundle,在Karaf容器中加载bundle后,往往需要获取bundle处理的中间信息,用于调试、故障定位等。而org.apache.karaf.shell.console提供一种可以以控制台命令的方式介入bundle。提供一种在运行时,以命令触发原代码中的逻辑功能。
具体开发,与开发bundle过程一样,有几点需要注意:
1、@Command @Option @Argument 的使用,父类OsgiCommandSupport和重载方法doExecute
2、pom中的build的plugin需要增加maven-scr-plugin
3、pom的build的maven-bundle-plugin显示Import org.apache.karaf.shell.*,
示例:
package com.zte.sdn.oscp.yang.adapter;import org.apache.karaf.shell.commands.Argument;import org.apache.karaf.shell.commands.Command;import org.apache.karaf.shell.commands.Option;import org.apache.karaf.shell.console.OsgiCommandSupport;/** * Created by sunquan on 2017/10/20. */@Command(scope = "livio", name = "example", description = "livio exmaple command")public class ExampleCommand extends OsgiCommandSupport { @Option(name = "-n", aliases = {"--Name"}, description = "Show the information of specific shard module", required = false, multiValued = false) private String shardModuleName = ""; @Argument(index = 0, name = "command", description = "[help]", required = true, multiValued = false) private String command = "help"; @Override protected Object doExecute() throws Exception { if (!shardModuleName.isEmpty()) { System.out.println("Name:" + shardModuleName); } switch (command) { case "help": session.getConsole().println("help command output"); break; case "print": session.getConsole().println("print command output"); break; default: break; } return null; }}
其中multiValued为false,command字段则为字符串help或print,即输入命令中字面量
但如果为true,表示该参数可以配置多个值command字段,会自动加上[],并以逗号分隔。如livio:example print help 则command字段为[print,help]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zte.sunquan.demo</groupId> <artifactId>yang-adapter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>bundle</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--<dependency>--> <!--<groupId>com.zte.sunquan.demo</groupId>--> <!--<artifactId>yang-middle</artifactId>--> <!--<version>1.0-SNAPSHOT</version>--> <!--</dependency>--> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr</artifactId> <version>1.8.2</version> </dependency> <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.9.12</version> </dependency> <dependency> <groupId>org.apache.karaf.shell</groupId> <artifactId>org.apache.karaf.shell.console</artifactId> <version>3.0.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.zte.sunquan.demo</groupId> <artifactId>yang-admodel</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>3.0.1</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.description}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> <Bundle-Version>${project.version}</Bundle-Version> <Bundle-Vendor>${project.groupId}</Bundle-Vendor> <Import-Package> !com.zte.sdn.oscp.commons.serialize.binary.protostuff*, org.apache.karaf.shell.*, com.zte.sdn.oscp.yang.gen.v1.ip.device.rev20170324.*, com.zte.sdn.oscp.yang.gen.v1.ip.device.rev20170324.NetconfState.*, *</Import-Package> <!--<Private-Package></Private-Package>--> <!--<Embed-Dependecy></Embed-Dependecy>--> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.21.0</version> <executions> <execution> <id>generate-scr-scrdescriptor</id> <goals> <goal>scr</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> <command> <action class="com.zte.sdn.oscp.yang.adapter.ExampleCommand"/> </command> </command-bundle></blueprint>
阅读全文
0 0
- OSGI中Command -控制台命令
- osgi控制台命令:getprop
- OSGi 控制台命令大全
- OSGI框架常用控制台命令
- OSGI中自定义command(2)
- osgi 学习系列(三)自定义osgi控制台命令
- Equinox OSGi 主要的控制台命令表
- OSGi 控制台
- # OSGI中使用自定义的command
- OSGI启动控制台报错,解决中
- osgi 学习系列--自定义osgi控制台命令say,iamcommand两个自定义命令
- OSGi命令(不断添加中)
- Mac的控制台命令无法使用command not found
- U-boot中控制台命令
- command控制台设置classpath
- virsh命令中使用qemu-monitor-command
- MySQL command Line 中命令无效解决
- osgi 命令演示
- 编码与Python逻辑运算
- Lucene.NET使用入门(二)【简单的搜索网站示例】
- vue中的所有axios请求都会发送2次,但是第一次不返回数据的原因
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
- 学习心得
- OSGI中Command -控制台命令
- 文件操作
- 前端工程化之路:初探 Sass 技术
- linux下搭建搭建svn服务器
- 399. Evaluate Division
- 搜索练习1:派对灯
- 生成树和生成森林
- LWF——世界上第一个去中心化物流平台
- 怎样给域用户赋予本机管理员权限