使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

转载请保留作者信息:

作者:88250

Bloghttp:/blog.****.net/DL88250

MSN & Gmail & QQ[email protected]


摘要

上一次,我们了解了OSGi的背景并使用NetBeans6,基于Knopflerfish(OSGi的一个RI) 完成了第一个OSGi应用——FirstOSGi。这一次,我们将对OSGi进行深入一点学习——SecondOSGi,让我们掌握Bundles之间的调用!

准备

上一次 :-)

开工:

1. 创建工程

打开NetBeans6 IDE,创建两个普通的Java App——SecondOSGi、SecondOSGiClient。把KF下的Sources拷贝到两个工程下(记得加入asm3.0的Jar):

使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

在SecondOSGiClient工程的demo包下的两个Java文件(Demo.java, DemoFactory.java)与SecondOSGi工程demo包下的完全一样,这里是懒得发布编译好的class byte file给Client了,直接给的接口源文件。

2. 编写manifest.mf

把工程View切换到Files,改写manifest.mf如下:

使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

关于manifest.mf里各种properties就不罗嗦了,有很多文档可以参考 :-)

3. 编写Activator

下面逐一给出源文件,一一对应上面工程结构图。

service提供者,即SecondOSGi工程下的:

/*
*@(#)Demo.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.demo;


/**
*Asimpledemoservice.
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicinterfaceDemo{

/**
*Addtwointegersandreturntheresult.
*
@parama
*
@paramb
*
@return
*/
publicintadd(inta,intb);
}

/*
*@(#)DemoFactory.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.demo;

/**
*<b>Another</b>verysimpledemoserviceAPI.
*<p>
*Theintentionsofthisinterfaceclassistoshowthatdifferentbundles
*willgetdifferentinstancesofaservice,bythemeansofa
*ServiceFactory.
*</p>
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicinterfaceDemoFactory{

/**
*Sayhello.
*/
voidhello();
}

/*
*@(#)DemoImpl.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.demo.impl;

importsecondosgi.service.demo.Demo;

/**
*ImplementationoftheDemoservice.
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicclassDemoImplimplementsDemo{

publicintadd(inta,intb){
returna+b;
}
}

/*
*@(#)DemoFactoryImpl.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.demo.impl;

importsecondosgi.service.demo.DemoFactory;
importorg.osgi.framework.Bundle;

/**
*ImplementationoftheDemoFactoryservice.Theintentionsofthis
*classistoshowthatdifferentbundleswillgetdifferentinstances
*ofaservice,bythemeansofaServiceFactory.
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicclassDemoFactoryImplimplementsDemoFactory{

privateBundleb;

/**
*Constructorwithargument.
*
@paramba<code>Bundle</code>
*/
publicDemoFactoryImpl(Bundleb){
this.b=b;
}

publicvoidhello(){
System.out.println(
"Hellobundle#"+b.getBundleId());
}
}

很关键的Activator,实现服务的注册,注销:
/*
*@(#)Activator.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.demo.impl;

importsecondosgi.service.demo.Demo;
importsecondosgi.service.demo.DemoFactory;
importjava.util.Hashtable;
importorg.osgi.framework.Bundle;
importorg.osgi.framework.BundleActivator;
importorg.osgi.framework.BundleContext;
importorg.osgi.framework.ServiceFactory;
importorg.osgi.framework.ServiceRegistration;

/**
*ActivatorwhichcreatesandregistersaDemo1service.
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicclassActivatorimplementsBundleActivator{

privateDemodemo;

publicvoidstart(BundleContextbc){
System.out.println(
"start"+getClass().getName());
demo
=newDemoImpl();
bc.registerService(Demo.
class.getName(),
demo,
newHashtable());

//CreateaservicefactoryforDemoFactoryimplementations
ServiceFactoryfactory=newServiceFactory(){

Hashtableservices
=newHashtable();
//Willgetcalledwhenabundlerequestaservice
@SuppressWarnings("unchecked")
publicObjectgetService(Bundleb,
ServiceRegistrationreg){
System.out.println(
"getfrom"+b.getBundleId());

//Createwhennecessary
DemoFactoryimpl=(DemoFactory)services.get(b);
if(impl==null){
impl
=newDemoFactoryImpl(b);
services.put(b,impl);
}
returnimpl;
}

//willgetcalledwhenabundleungetsaserviceorstops
publicvoidungetService(Bundleb,
ServiceRegistrationreg,
Objectservice){
System.out.println(
"ungetfrom"+b.getBundleId());
services.remove(b);
}
};

//NotehowfactoryonlyimplementsServiceFactory,
//butwestillregisterasDemoFactory1service
bc.registerService(DemoFactory.class.getName(),
factory,
newHashtable());
}

publicvoidstop(BundleContextbc){
System.out.println(
"stop"+getClass().getName());

demo
=null;
}
}

client角色,服务使用者,即SecondOSGiClient工程下的(Demo,DemoFactory两个接口同服务提供的)。
在这个Activator里,我们实现了服务的查找、调用、服务提供者状态的监听:
/*
*@(#)Activator.java
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
packagesecondosgi.service.client.impl;

importsecondosgi.service.demo.Demo;
importsecondosgi.service.demo.DemoFactory;
importorg.osgi.framework.BundleActivator;
importorg.osgi.framework.BundleContext;
importorg.osgi.framework.ServiceEvent;
importorg.osgi.framework.ServiceListener;
importorg.osgi.framework.ServiceReference;

/**
*Activatorwhichregistersalistenerfor,andtest,anyDemoservice.
*
@author88250
*
@version1.0.0.0,Feb14,2008
*/
publicclassActivatorimplementsBundleActivator,ServiceListener{

privatestaticBundleContextbc;

privateServiceListenerlistener;

publicvoidstart(BundleContextbc){
System.out.println(
"start"+getClass().getName());
Activator.bc
=bc;

try{
//FilterthatmatchesallDemoservices
Stringfilter="(objectclass="+Demo.class.getName()+")";

//FetchallregisteredDemoservices
//andtestthemmanually
ServiceReference[]srl=
bc.getServiceReferences(Demo.
class.getName(),filter);
for(inti=0;srl!=null&&i<srl.length;i++){
testService(srl[i]);
}

//...andcatchallnewlyregistedonestoo.
bc.addServiceListener(listener,filter);
}
catch(Exceptione){
//soundsunlikely,butfiltersyntaxerrorsareeasytowrite.
e.printStackTrace();
}

testServiceFactory();
}

voidtestServiceFactory(){
//Trytogetareferencetotheserviceproducedbyafactory
ServiceReferencefactorySR=bc.getServiceReference(DemoFactory.class.getName());
if(factorySR!=null){
DemoFactorydf
=(DemoFactory)bc.getService(factorySR);
if(df!=null){
//Differentbundleswillgetdifferentprintouts
df.hello();
}
}
}

voidtestService(ServiceReferencesr){
Demodemo
=(Demo)bc.getService(sr);
intr=demo.add(140,1);

System.out.println(
"Testing"+demo+",result="+r);

//..returntheobjecttobenice
bc.ungetService(sr);
}

publicvoidstop(BundleContextbc){
System.out.println(
"stop"+getClass().getName());
Activator.bc
=null;
}

publicvoidserviceChanged(ServiceEventevent){
ServiceReferencesr
=event.getServiceReference();

//justprintsomeinfoandcalltestService()on
//allregisteredDemoservices
switch(event.getType()){
caseServiceEvent.REGISTERED:
System.out.println(
"GotDemoservice");
testService(sr);
break;
caseServiceEvent.UNREGISTERING:
System.out.println(
"LostDemoservice");
break;
caseServiceEvent.MODIFIED:
System.out.println(
"ModifiedDemoservice");
break;
default:
break;
}
}
}

4. 测试

打开KF控制中心:

使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创][email protected]:~/Work/knopflerfish_osgi_2.0.4/knopflerfish.org/osgi$java-jarframework.jar


打开构建好的SecondOSGi.jar以及SecondOSGiClient.jar,运行!

使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

总结

这一次,我们对OSGi的了解更深了一步。

从设计的角度:一个可扩展的Service-Oriented组件服务模型

从开发的角度:我们发布接口给客户,实现了“针对接口编程”的OO核心实践

不足之处

我们一直都是在KF的控制台下启动的应用,如何做成独立的(standalone)可运行的Jar发布呢?

1. 在命令行下启动OSGi框架

首先,编写一个启动参数文件:secondosgi.xargs
-launch
-istart/home/daniel/Work/Sources/Java/SecondOSGi/dist/SecondOSGi.jar
-istart/home/daniel/Work/Sources/Java/SecondOSGiClient/dist/SecondOSGiClient.jar

然后,进入KF的安装目录,启动我们的应用:
使用NetBeans6开发OSGi应用(2)——SecondOSGi[88250原创]

现在,我们只是脱离了KF的图形界面控制中心,在命令行下面启动的KF框架,并把SecondOSGi与SeondOSGiClient安装运行在KF框架里。一切的主动权还是在KF手里。
结合以前JavaEE的实践,JSP/Servlets,EJBs不都是被控制在容器(container)里的吗?
不过,话又说回来了,我希望自己的框架应用构建于OSGi之上,而不是之内。
之内的部分应该是可扩展的Plug-ins部分,让OSGi作为底层框架,为我们提供稳定的插件机制。
之外的部分应该是我们应用的框架,构建在OSGi之上。对OSGi做一个封装,就KF而言,就是封装它的命令接口,让我们的框架可以对插件随时安装、卸载、运行、停止、更新。。。。
当然,以上是个设想,学习OSGi第二天的设想。。。。
总之不足的地方很多,要把这个示例慢慢演化成正真具有价值的应用可能还需要一些时间。