1、官网下载http://felix.apache.org/downloads.cgi,

当前最新版本6.0.3

运行felix窗口有两种方式

(1) 可以直接下载发布版本

Felix Framework Distribution(org.apache.felix.main.distribution-6.0.3.zip)

解压后目录

进入项目目录,命令行下执行

java -jar ./bin/felix.jar

felix.jar 默认会在当前运行目录的 bundle 文件夹内加载启动所需的插件jar,config 目录下的 config.propeties 和 system.properties 里面加载环境变量,如果将其他目录作为启动根目录,该目录下不存在 felix 启动所需信息,启动就会有问题。项目第一次启动的时候会在启动目录下创建一个felix-cache的目录,用于存放框架运行过程产生的数据,当然该目录可以在启动的时候配置,使用 java -jar ./bin/felix.jar 即可。

解压org.apache.felix.main-6.0.3-project.zip,进入目录,命令行下

运行mvn eclipse:eclipse工程文件,

再输入mvn package,打包编译

File->Import导入到eclipse

在eclipse的org.apache.felix.main.Main类右键运行Run As Java Application

常用命令

lb:查看已装插件包

start:运行插件包

stop:停止运行插件包

install:安装插件包

uninstall:删除插件包

其它命令可以通过help查看

先定义一个服务类:

package test.penngo.service;

/**

* 简单的定义了一个字典服务接口,该接口的功能只是简单的验证一个词是否正确。

**/

public interface DictionaryService

{

/**

* 检查 单词是否存在

**/

public boolean checkWord(String word);

}

英文词典插件包开发

package tutorial.example2;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import test.penngo.service.DictionaryService;

/**

* 英文字典服务插件

**/

public class Activator implements BundleActivator {

/**

* 服务启动

*

* @param context the framework context for the bundle.

**/

public void start(BundleContext context) {

Hashtable props = new Hashtable();

props.put("Language", "English");

context.registerService(DictionaryService.class.getName(), new DictionaryImpl(), props);

}

/**

* 服务停止

*

* @param context the framework context for the bundle.

**/

public void stop(BundleContext context) {

// NOTE: The service is automatically unregistered.

}

/**

* 英文词典服务实现

**/

private static class DictionaryImpl implements DictionaryService {

String[] m_dictionary = { "welcome", "to", "the", "osgi", "tutorial" };

/**

* 英文单词检测

**/

public boolean checkWord(String word) {

word = word.toLowerCase();

for (int i = 0; i < m_dictionary.length; i++) {

if (m_dictionary[i].equals(word)) {

return true;

}

}

return false;

}

}

}

META-INF.MF

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-SymbolicName: English

Bundle-Name: English dictionary

Bundle-Description: A bundle that registers an English dictionary service

Bundle-Vendor: penngo

Bundle-Version: 1.0.0

Bundle-Activator: tutorial.example2.Activator

Import-Package: org.osgi.framework, test.penngo.service

英文词典插件打包导出,eclipse自带的export打包会有问题,本文使用fatjar工具打包,

中文词典插件包开发

package tutorial.example2b;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import test.penngo.service.DictionaryService;

/**

* 中文词典包

**/

public class Activator implements BundleActivator

{

public void start(BundleContext context)

{

Hashtable props = new Hashtable();

props.put("Language", "Chinese");

context.registerService(

DictionaryService.class.getName(), new DictionaryImpl(), props);

}

public void stop(BundleContext context)

{

// NOTE: The service is automatically unregistered.

}

/**

* 中文词典服务实现

**/

private static class DictionaryImpl implements DictionaryService

{

// The set of words contained in the dictionary.

String[] m_dictionary =

{ "欢迎", "你好", "教程", "模块" };

public boolean checkWord(String word)

{

word = word.toLowerCase();

for (int i = 0; i < m_dictionary.length; i++)

{

if (m_dictionary[i].equals(word))

{

return true;

}

}

return false;

}

}

}

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-SymbolicName: Chinese

Bundle-Name: Chinese dictionary

Bundle-Description: A bundle that registers a Chinese dictionary service

Bundle-Vendor: penngo

Bundle-Version: 1.1.0

Bundle-Activator: tutorial.example2b.Activator

Import-Package: org.osgi.framework, test.penngo.service

插件打包

客户端调用词典开发,也是作为一个felix插件

package tutorial.example3;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceReference;

import test.penngo.service.DictionaryService;

/**

* 此客户端在启动时,接收控制台输入,检查输入的单词是否正确,并且打印初相应的检测结果,

* 注意:当我们没有输入任何信息的时候,单词检测便会结束,如果需要重新进入单词检测程序,我们需要重新启动插件。

**/

public class Activator implements BundleActivator

{

public void start(BundleContext context) throws Exception

{

// 获取所有词典插件

ServiceReference[] refs = context.getServiceReferences(

DictionaryService.class.getName(), "(Language=*)");

if (refs != null)

{

System.out.println("=======当前可用词典插件:" + refs.length);

try

{

System.out.println("Enter a blank line to exit.");

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String word = "";

while (true)

{

System.out.print("Enter word: ");

word = in.readLine();

if (word.length() == 0)

{

break;

}

boolean isCorrect = false;

for(ServiceReference service:refs) {

DictionaryService dictionary =

(DictionaryService) context.getService(service);

if (dictionary.checkWord(word))

{

isCorrect = true;

System.out.println("Correct.");

}

}

if(isCorrect == false)

{

System.out.println("Incorrect.");

}

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

else

{

System.out.println("Couldn't find any dictionary service...");

}

}

/**

* Implements BundleActivator.stop(). Does nothing since

* the framework will automatically unget any used services.

* @param context the framework context for the bundle.

**/

public void stop(BundleContext context)

{

// NOTE: The service is automatically released.

}

}

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-SymbolicName: client

Bundle-Name: Dictionary client

Bundle-Description: A bundle that uses the dictionary service if it finds it at startup

Bundle-Vendor: penngo

Bundle-Version: 1.3.0

Bundle-Activator: tutorial.example3.Activator

Export-Package: test.penngo.service

Import-Package: org.osgi.framework

#//注意:test.penngo.service这个包与tutorial.example3打在同一个jar中,如果example3.jar更新,其它依赖test.penngo.service的也需要更新或重启felix.

安装插件

把上边生成的插件包example2.jar,example2b.jar,example3.jar入到org.apache.felix.main-6.0.3项目的plugins文件夹中

安装和运行效果:

通过插件方式开发,后期继续扩展增加其它语言词典(日语,俄语,韩语),不需要重启服务,也不影响已存在的语言词典功能(英文、中文)。

3、开发http服务插件

package test.penngo.http.example;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

resp.getWriter().write("hello penngo");

}

}

package test.penngo.http.example;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceReference;

import org.osgi.service.http.HttpService;

import org.osgi.util.tracker.ServiceTracker;

public class Activator implements BundleActivator {

private ServiceTracker httpTracker;

public void start(BundleContext context) throws Exception {

httpTracker = new ServiceTracker(context, HttpService.class.getName(), null) {

public void removedService(ServiceReference reference, Object service) {

// HTTP service is no longer available, unregister our servlet...

try {

((HttpService) service).unregister("/hello");

} catch (IllegalArgumentException exception) {

// Ignore; servlet registration probably failed earlier on...

}

}

public Object addingService(ServiceReference reference) {

// HTTP service is available, register our servlet...

HttpService httpService = (HttpService) this.context.getService(reference);

try {

httpService.registerServlet("/hello", new TestServlet(), null, null);

} catch (Exception exception) {

exception.printStackTrace();

}

return httpService;

}

};

// start tracking all HTTP services...

httpTracker.open();

}

public void stop(BundleContext context) throws Exception {

// stop tracking all HTTP services...

httpTracker.close();

}

}

#MANIFEST.MF

Manifest-Version: 1.0

Bundle-ManifestVersion: 2

Bundle-SymbolicName: hello

Bundle-Name: hello penngo

Bundle-Description: Http Hello World

Bundle-Vendor: penngo

Bundle-Version: 1.0.0

Bundle-Activator: test.penngo.http.example.Activator

Import-Package: org.osgi.framework,

org.osgi.service.http,

org.osgi.util.tracker,

javax.servlet.http,

javax.servlet

注意htpp插件包依赖:

org.apache.felix.http.base-4.0.6.jar,

org.apache.felix.http.bridge-4.0.8.jar,

org.apache.felix.http.jetty-4.0.10.jar,

org.apache.felix.http.servlet-api-1.1.2.jar,

org.apache.felix.http.whiteboard-4.0.0.jar

需要先安装上边的插件包,才能正常在felix中运行http功能。

安装运行效果

浏览器访问:

附件源码:

java 调用felix_使用eclipse开发felix的OSGI插件相关推荐

  1. ictclas包 java_ICTCLAS分词系统Java调用接口在Eclipse中的安装

    ICTCLAS分词系统Java调用接口在Eclipse中的安装 实验环境:JDK1.5.Eclipse3.1.XP操作系统 分词系统Java接口解压包:d:\fenci(http://www.nlp. ...

  2. Java编程讲义之Eclipse开发工具

    本章学习目标 熟练掌握Eclipse的安装和配置 熟练掌握Eclipse开发.运行.调试程序 掌握Eclipse中jar包的导入和导出 Java的学习和开发过程中,离不开一款功能强大.使用简单.能够辅 ...

  3. eclipse选择java版本_使用 Eclipse 开发版本选择和下载说明

    现在越来越多的人开发使用 IDEA,使用 Eclipse 开发的已经渐渐变少了,这篇就简单介绍下 Eclipse 的版本选择和下载,供目前还在坚守使用 Eclipse 开发的同胞们~ Eclipse ...

  4. java调用rapidminer_基于RapidMiner开发问题和解决

    RapidMiner(前身是YALE)是一个十分流行的开源数据挖掘软件,它不仅提供了一个GUI的数据处理和分析环境,还提供了Java API以便将它的能力嵌入其他应用程序.本文记录了基于RapidMi ...

  5. java调用corba_用JACORB开发corba应用

    JACORB开发以及corba的请求方式: JacORB的应用开发一般分为以下五步: 1.写IDL接口定义 2.编译IDL接口定义生成Java类 3.实现步骤2中生成的接口 4.写服务器启动类,并注册 ...

  6. eclipse jsp 调用java_使用Eclipse开发JSP

    Eclipse JSP/Servlet 环境搭建 本文假定你已安装了 JDK 环境,如未安装,可参阅 Java 开发环境配置 . 我们可以使用 Eclipse 来搭建 JSP 开发环境,首先我们分别下 ...

  7. eclipse开发java项目_用eclipse 开发java 项目

    一般分为如下4个步骤: 一.创建Java项目(创建项目之前要选择工作空间) 二.创建程序包 三.编写Java源程序 四.运行Java程序 详细说明 1.创建Java 项目 1.1 打开Eclipse, ...

  8. java调用高德地图API开发,高德在线地图开发——未完待续

    这是目录 一.引入高德地图API 二.高德地图开发 1.定义一个div来存放地图 2.生成地图 3.添加一个跳跃的点 4.添加控件 5.有其他需要的请留言 一.引入高德地图API 高德地图官方示例:h ...

  9. java调用银联支付接口开发,银联在线Java接口开发

    netpayclinet.jar 根据项目工程的需要放置对应路径下 用于提供数字签名的方法调用 MerPrk.key 可以放置到任意路径下,但是需要调用方法指定文件位置和名称 商户签名私钥 PgPub ...

  10. eclipse开发jsp环境的插件

    Ecl ipse作为一个 java应用的IDE,使用非常方便,但是对于 jsp的开发支持还显得不够,在这里向大家推荐一个eclipse的plugins来协助JSP开发.这个名称叫lomboz,不但支持 ...

最新文章

  1. java i线程安全吗_Java中 i++ 是线程安全的么?为什么?
  2. 如何使用Docker轻松集成OnlyOffice和NextCloud--快速搭建私有云办公系统/私有云盘/私有OfficeOnline
  3. ANDROID窗体跳转
  4. 7,复习,多对多表的三种创建,form组件,cookie,session
  5. jtree和mysql_Jtable和JTree的写法示例代码
  6. 主要省份城市的DNS服务器地址
  7. TableView Within Alert
  8. 计算机网络基础高职pdf,高职《计算机网络基础》课程教学改革的思考.pdf
  9. java 文件工具类_java文件工具类,文件的一些基本操作
  10. windows便签快捷键_Windows10便签快捷键在哪里设置?
  11. Python 汇率换算
  12. R语言计算dataframe中指定数据列的值为缺失值的样本个数(行的个数)
  13. [HNOI2015]亚瑟王 题解
  14. 阿里云体验--搭建超级小班课网课系统
  15. Oracle:错误码ORA-28040 的坑
  16. 电商项目day11(商品搜索功能实现排序结果分页)
  17. 读书/纪录片笔记:《手术两百年》
  18. 备战软考(6) 2014年度下半年软考备战分析报告
  19. python爬虫-豆瓣爬取数据保存为html文件
  20. Android基础控件——ProgressBar自定义的介绍、动画效果实现、附加三个漂亮的进度条

热门文章

  1. i春秋web-Backdoor(.git泄露、vim备份泄露、代码审计)
  2. The Innovation | 用系统生物学的观点鸟瞰肿瘤易感基因
  3. 广度优先搜索(BFS)与深度优先搜索(DFS)的对比及优缺点
  4. 解决:adb devices error protocol falut(no status)
  5. 十年之后再看,腾讯位置服务的发展与腾讯地图的融合
  6. 用Markdown画流程图
  7. Glide4.0 Transformation大全,罗列搜集所有Transformation,实现图片的变换
  8. IntelliJ IDEA常用设置及快捷键以及自定义
  9. MATLAB遇到问题:错误使用mex的解决办法
  10. 原神紫晶块采集点位置在哪 紫晶块采集点路线图详情