1 前言
1.1 本指南基于 OSGi.基于Spring,Hibernate的Web應(yīng)用快速開發(fā)指南,所以開發(fā)中的某些具體步驟將省略,只具體到文件。
1.2 本指南將開發(fā)三個(gè)Bundle
com.dw.calc.service Bundle,發(fā)布接口ICalcService,接口功能為返回兩個(gè)整數(shù)的和
com.dw.calc.service.impl Bundle,實(shí)現(xiàn)接口ICalcService接口
com.dw.calc.client Bundle,實(shí)現(xiàn)OSGi遠(yuǎn)程服務(wù)調(diào)用ICalcService接口

2 下載ECF SDK
http://ftp.cs.pu.edu.tw/pub/eclipse/rt/ecf/3.5.6/org.eclipse.ecf.sdk_3.5.6.v20120610-1946.zip

3 安裝 ECF Bundle 依賴到 virgo-tomcat 服務(wù)器
解壓ECF SDK,復(fù)制org.eclipse.ecf.sdk\plugins目錄下的所有.jar結(jié)尾的Bundles,拷貝到virgo-tomcat-server\repository\usr目錄下
本指南中依賴的的bundle包基本都是從http://ebr.springsource.com/repository/app/下載
org.eclipse.ecf.springframework這個(gè)Bundle從git://git.eclipse.org/gitroot/ecf/org.eclipse.ecf.git獲取,git的使用我就省略了,請(qǐng)直接下載,下載回來(lái)后改后綴名為.jar

4 開發(fā) Bundle com.dw.calc.service
4.1 src/META-INF/MANIFEST.MF
Manifest-Version: 1.0
Bundle
-Version: 1.0.0
Bundle
-Name: Service
Bundle
-ManifestVersion: 2
Bundle
-SymbolicName: com.dw.calc.service
Export
-Package: com.dw.calc.service

4.2 src/com.dw.calc.service.ICalcService.ICalcService.java
package com.dw.calc.service;

public interface ICalcService {
    
public Integer plus(Integer i1, Integer i2);
}

5開發(fā) Bundle com.dw.calc.service.impl

5.1 src/com.dw.calc.service.ICalcService.CalcServiceImpl.java
package com.dw.clac.service.impl;

import com.dw.calc.service.ICalcService;

public class CalcServiceImpl implements ICalcService{

@Override
public Integer plus(Integer i1, Integer i2) {
return i1 + i2;
}


}

5.2 src/META-INF/MANIFEST.MF
Manifest-Version: 1.0
Export
-Package: com.dw.clac.service.impl;uses:="com.dw.calc.service"
Bundle
-Version: 1.0.0
Tool: Bundlor
1.1.0.RELEASE
Bundle
-Name: Impl
Bundle
-ManifestVersion: 2
Import
-Package: com.dw.calc.service,
org.eclipse.ecf.core;version
="[3.0.0,3.0.0]",
org.eclipse.ecf.springframework,
org.eclipse.ecf.springframework.identity,
org.eclipse.equinox.app;version
="[1.1.0,1.1.0]"
Bundle
-SymbolicName: com.dw.calc.service.impl
Import
-Bundle: org.eclipse.ecf.provider;version="[4.2.100.v20120610-1946,4.2.100.v20120610-1946]"

5.3 src/META-INF/spring/osgiContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<osgi:reference id="containerFactory" interface="org.eclipse.ecf.core.IContainerFactory" timeout="1000" cardinality="1..1" />

<bean class="org.eclipse.ecf.springframework.HostContainerFactoryBean">
<property name="containerType" value="ecf.generic.server" />
<property name="containerFactory" ref="containerFactory" />
</bean>

<osgi:service interface="com.dw.calc.service.ICalcService">
<osgi:service-properties>
<entry key="service.exported.interfaces" value="*" />
<entry key="service.exported.configs" value="ecf.generic.server" />
<entry key="org.eclipse.ecf.containerFactoryArgs" value="ecftcp://127.0.0.1:3787/server" />
</osgi:service-properties>
<bean class="com.dw.clac.service.impl.CalcServiceImpl" />
</osgi:service>

</beans>


6 開發(fā)Bundle com.dw.calc.client
6.1 src/com.dw.calc.client.CalcServiceConsumer.java
package com.dw.calc.client;

import com.dw.calc.service.ICalcService;

public class CalcServiceConsumer {

ICalcService calcService;

public void setCalcService(ICalcService calcService) {
this.calcService = calcService;
}


public void start() {
System.out.println(
"2+3=" + calcService.plus(2, 3));
}


}

6.2 src\META-INF\MANIFEST.MF
Manifest-Version: 1.0
Bundle
-Version: 1.0.0
Bundle
-Name: Client
Bundle
-ManifestVersion: 2
Bundle
-SymbolicName: com.dw.calc.client
Import
-Package: com.dw.calc.service,
org.eclipse.core.runtime;version
="[3.4.0,3.4.0]",
org.eclipse.ecf.core;version
="[3.0.0,3.0.0]",
org.eclipse.ecf.springframework,
org.eclipse.ecf.springframework.identity,
org.eclipse.equinox.app;version
="[1.1.0,1.1.0]"
Import
-Bundle: org.eclipse.ecf.provider;version="[4.2.100.v20120610-1946,4.2.100.v20120610-1946]"

6.3 src/META-INF/spring/appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">

<bean class="com.dw.calc.client.CalcServiceConsumer" init-method="start">
<property name="calcService" ref="calcServiceRef" />
</bean>

</beans>

6.4 src/META-INF/spring/osgiContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<bean class="org.eclipse.ecf.springframework.ConsumerContainerFactoryBean">
<property name="containerType" value="ecf.generic.client" />
</bean>

<osgi:reference id="calcServiceRef" interface="com.dw.calc.service.ICalcService"
timeout
="1000" cardinality=
"0..1" filter="(org.eclipse.ecf.containerFactoryArgs=ecftcp://127.0.0.1:3787/server)" />

</beans>