OSGi 系列(十二)之 Http Service

时间:2022-05-22 17:24:47

OSGi 系列(十二)之 Http Service

1. 原始的 HttpService

(1) 新建 web-osgi 工程,目录结构如下:

OSGi 系列(十二)之 Http Service

(2) HomeServlet

package com.github.binarylei.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class HomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("<h1>osgi http service</h1>");
}
}

(3) Activator

package com.github.binarylei.servlet;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService; import javax.servlet.Servlet;
import java.util.Dictionary;
import java.util.Hashtable; public class Activator implements BundleActivator { private HttpService http = null;
private ServiceRegistration<Servlet> serviceRegistration; public void start(BundleContext context) throws Exception { ServiceReference<HttpService> ref = context.getServiceReference(HttpService.class);
if(ref == null) {
System.out.println("http service is null");
} else {
http = context.getService(ref); HttpContext httpContext = http.createDefaultHttpContext(); //注册servlet
http.registerServlet("/home", new HomeServlet(), null, httpContext); //注册静态资源
http.registerResources("/static", "web", httpContext);
} //通过发布服务的方式,注册servlet。不能注册静态资源
Dictionary<String, String> properties = new Hashtable<>();
properties.put("alias", "/home2");
serviceRegistration = context.registerService(Servlet.class, new HomeServlet(), properties);
} public void stop(BundleContext context) throws Exception {
if(http != null) {
http.unregister("/home");
http.unregister("/static");
}
serviceRegistration.unregister();
}
}

(4) karaf 测试:

先测试 http.registerServlet() 注册方式

feature:install http

注意: 要先启动 http 服务,再启动 web-osgi

OSGi 系列(十二)之 Http Service

再测试 context.registerService(Servlet.class, new HomeServlet(), properties) 注册方式

feature:install http http-whiteboard

OSGi 系列(十二)之 Http Service

2. OSGi 中运行 war 包

(1) 新建 webapp-osgi 工程,目录结构如下:

OSGi 系列(十二)之 Http Service

(2) LoginServlet

@WebServlet("/web/app")
public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("this is osgi web app");
}
}

(3) web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="false"> </web-app>

(4) 配制 war 打包方式

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<!-- 表示 MANIFEST.MF 的内容由 maven-bundle-plugin 插件生成 -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Web-ContextPath>/osgiweb</Web-ContextPath>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
</instructions>
</configuration>
</plugin>

(5) 测试:

karaf 默认是不支持 war 包安装方式

feature:install war

OSGi 系列(十二)之 Http Service