1 前言
1.1 本指南基于 OSGi.基于Spring,Hibernate的Web应用快速开发指南,所以开发中的某些具体步骤将省略,只具体到文件。
1.2 本指南将开发三个Bundle
com.dw.calc.service Bundle,发布接口ICalcService,接口功能为返回两个整数的和
com.dw.calc.service.impl Bundle,实现接口ICalcService接口
com.dw.calc.client Bundle,实现OSGi远程服务调用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 服务器
解压ECF SDK,复制org.eclipse.ecf.sdk\plugins目录下的所有.jar结尾的Bundles,拷贝到virgo-tomcat-server\repository\usr目录下
本指南中依赖的的bundle包基本都是从http://ebr.springsource.com/repository/app/下载
org.eclipse.ecf.springframework这个Bundle从git://git.eclipse.org/gitroot/ecf/org.eclipse.ecf.git获取,git的使用我就省略了,请直接下载,下载回来后改后缀名为.jar

4 开发 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开发 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 开发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>