1. 创建工程
mvn archetype:create -DgroupId=com.manning.sdmia -DartifactId=SpringDM-Web-Sample
文件目录结构:
SpringDM-Web-Sample\
src/
main/
java/
webapp/
WEB-INF/
web.xml
index.html
index.jsp
test/
pom.xml:
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Include-Resource>
src/main/webapp
</Include-Resource>
<Web-ContextPath>
SpringDM-Web-Sample
</Web-ContextPath>
</instructions>
</configuration>
</plugin>
...
2. 工程打包
The bundle will be deployed in two steps: it will be deployed as a bundle in the OSGi container,
then the Spring DM web extender will react to its registration with OSGi by deploying it to the
web container. To be deployed by the web extender, a bundle needs to meet one of two
conditions:
■ Its filename ends with .war.
■ It contains a WEB-INF directory at its root.
3. 部署测试
install file:///D:/sts_workspace/SpringDM-Web-Sample/target/SpringDM-Web-Sample-1.0.0.jar
访问地址路径:
■ http://localhost:8080/SpringDM-Web-Sample/
■ http://localhost:8080/SpringDM-Web-Sample/index.jsp
测试代码:
public class SpringDmWebSampleTest extends AbstractConfigurableBundleCreatorTests {
public void testIntegration() throws Exception{
boolean bundleIsHereAndStarted = false;
for(Bundle bundle : bundleContext.getBundles()){
if("com.manning.sdmia.SpringDM-Web-Sample".equals(bundle.getSymbolicName())
&&bundle.getState() == Bundle.ACTIVE){
bundleIsHereAndStarted = true;
break;
}
}
assertEquals("SpringDM-Web-Bundle is not installed or activated!", bundleIsHereAndStarted);
Thread.sleep(10*1000);
testConnection("http://localhost:8080/SpringDM-Web-Sample/index.html");
testConnection("http://localhost:8080/SpringDM-Web-Sample/index.jsp");
}
private void testConnection(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
try {
con.connect();
assertEquals(HttpURLConnection.HTTP_OK, con.getResponseCode());
} finally {
con.disconnect();
}
}
@Override
protected String[] getTestBundlesNames() {
return new String[] {
"org.springframework.osgi, spring-osgi-web,"
+ getSpringDMVersion(),
"org.springframework.osgi, spring-osgi-web-extender,"
+ getSpringDMVersion(),
"javax.servlet, com.springsource.javax.servlet, 2.4.0",
"org.springframework.osgi, catalina.osgi, 5.5.23-SNAPSHOT",
"org.springframework.osgi, catalina.start.osgi, 1.0.0",
"org.springframework.osgi, jsp-api.osgi, 2.0-SNAPSHOT",
"org.springframework.osgi, jasper.osgi, 5.5.23-SNAPSHOT",
"org.springframework.osgi, commons-el.osgi, 1.0-SNAPSHOT",
"org.springframework.osgi, jstl.osgi, 1.1.2-SNAPSHOT",
"com.manning.sdmia, SpringDM-Web-Sample, 1.0.0" };
}
@Override
protected Resource getTestingFrameworkBundlesConfiguration() {
return new InputStreamResource(SpringDmWebSampleTest.class
.getResourceAsStream("boot-bundles.properties"));
}
}
其中文件boot-bundles.properties:
4.启动容器
java -jar org.eclipse.osgi-3.5.1.R35x_v20090827.jar -console
5.案例代码
将附件SpringDM-Web-Sample.rar