基于OSGi的搜索程序实现

最近很多人问我在Ericsson搞点什么,说实话,什么都没有搞就是在看书。实习2周就见了BOSS三面,除了第一天的介绍,然后一次是告诉我们换桌子,最后一次是来看看我们坐在哪里。。。我们就这样从Ericsso白拿了400+。。

不过BOSS还是吩咐了点学的东西,那就是OSGi,OSGi到底是什么大家可以Google一下。简单的来说就是一个FRAMEWORK,其最大的特点就是提供了软件模块的动态加载,各模块通过接口向外提供服务,所以说OSGi也是一个面向服务的程序构架。

可以说OSGi是很优秀的,但是国内基本没有什么有用的教程(OSGi已经诞生10年了,可见国内的技术落后别人多少)。



下面就写一个简单的OSGi的例子

OSGi 中模块单元叫Bundle,Bundle可以使用某个服务,也可以提供某个服务。在OSGi 的R4 版本中提供了Declarative Services,在Declarative Services 中 Bundle又由Component组成,每个Component同Bundle一样也既可以提供服务也可以使用服务



————————————————华丽的分割线———————————————————



例子是由两个Bundle组成OSGi_Test和OSGi_TestService,其中OSGi_TestService有两个Component,分别是initialize和search,initialize是用于初始化查找数据的。search使用了initialize向外提高的服务,得到一个数组,并对其进行搜索操作。而OSGi_Test则使用了OSGi_TestService中search提供的搜索服务。



————————————————华丽的分割线———————————————————



————————————————OSGi_TestService————————————————

下面的是第一个Component,用于初始话数据

----InitializeData.java-----



package org.xudong.testservice.component.initialize;

import java.util.List;

public interface InitializeData {
public List<String> initializeData();

}



-----InitializeDataImpl------

package org.xudong.testservice.component.initialize;

import java.util.LinkedList;
import java.util.List;

import org.osgi.service.component.ComponentContext;


public class InitializeDataImpl implements InitializeData{
public List<String> initializeData(){
String[] name = {"jame","dfdf","allen","adfdefa","adfadf"};
List<String> namelist = new LinkedList<String>();
for(int i = 0; i<name.length; i++){
namelist.add(name[i]);
}
return namelist;
}
protected void activate(ComponentContext context) {
System.out.println("InitializeData Component Active,within the bundle lifecircle.");
}
public void deactivate(ComponentContext context) throws Exception {
System.out.println("InitializeData Component Deactive,within the bundle lifecircle.");
}

}



这个是第二个Component,其使用了前一个Component的服务,得到了初始化后的数组

-----Search-----

package org.xudong.testservice.component.search;

public interface Search {
public void searchByKeyword(String keyword);
}

-------SearchImpl---------



package org.xudong.testservice.component.search;

import java.util.LinkedList;
import java.util.List;

import org.osgi.service.component.ComponentContext;
import org.xudong.testservice.component.initialize.InitializeData;

public class SearchImpl implements Search{
List<String> namelist = new LinkedList<String>();
protected void activate(ComponentContext context) {
System.out.println("Search Component Active,within the bundle lifecircle.");
InitializeData initializedata = (InitializeData) context.locateService("InitializeData");
namelist.addAll(initializedata.initializeData());
}
public void deactivate(ComponentContext context) throws Exception {
System.out.println("Search Component Deactive,within the bundle lifecircle.");
}

public void searchByKeyword(String keyword){
for (int i = 0 ; i<namelist.size();i++){
if (namelist.get(i).equals(keyword)){
System.out.println("The keyword"+keyword+"has found.");
}else{
System.out.println("The keyword"+keyword+"has not found.");
}
}
}

}



-----------------------------------OSGi_Test-------------------------------------

其使用了名叫Search的Component的服务

package org.xudong.test;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.xudong.testservice.component.search.Search;

public class Activator implements BundleActivator {

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/

public static BundleContext context = null;

public void start(BundleContext context) throws Exception {
String keyword = "jame";
Activator.context = context;
ServiceReference reference = context.getServiceReference(Search.class.getName());
if (reference != null){
Search service = (Search)context.getService(reference);
service.searchByKeyword(keyword);
context.ungetService(reference);
}else{
System.out.println("No Service available!");
}
}
public void stop(BundleContext bc) throws Exception {

Activator.context = null;
}

/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值