我试图对org.eclipse.equinox.http.registry.servlets扩展点进行servlet扩展,但是无法使其正常工作。我试着记下我的过程,所以也许有人可以告诉我我做错了什么或我做了什么:)
所以,首先我只使用默认的servlet创建了一个jetty服务器。
这是我最初的pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myserver</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bundle.symbolicName>myserver</bundle.symbolicName>
<bundle.namespace>com.example</bundle.namespace>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>META-INF</manifestLocation>
<instructions>
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
<Bundle-Version>${pom.version}</Bundle-Version>
<Bundle-Activator>${bundle.namespace}.myserver.App</Bundle-Activator>
<Import-Package>
org.osgi.framework,
javax.servlet,
javax.servlet.http,
org.eclipse.jetty.server,
org.eclipse.jetty.servlet
</Import-Package>
<Require-Bundle>
</Require-Bundle>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的应用程序,它是一个捆绑激活器:
package com.example.myserver;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class App implements BundleActivator {
public void start(BundleContext context) throws Exception {
Server server = new Server(8080);
ServletContextHandler c = new ServletContextHandler(server, "/");
c.addServlet(new ServletHolder(new HelloWorldServlet()),"/test");
server.start();
server.join();
}
public void stop(BundleContext context) throws Exception {
// TODO Auto-generated method stub
}
}
这是我的servlet:
package com.example.myserver;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("Hello from HelloWorldServlet");
}
}
然后我使用以下捆绑包启动我的配置:
javax.servlet,
org.apache.felix.gogo.command,
org.apache.felix.gogo.runtime,
org.apache.felix.gogo.shell,
org.eclipse.equinox.console,
org.eclipse.jetty.continuation,
org.eclipse.jetty.http,
org.eclipse.jetty.io,
org.eclipse.jetty.security,
org.eclipse.jetty.server,
org.eclipse.jetty.servlet,
org.eclipse.jetty.util,
org.eclipse.osgi
现在当我输入网址http://localhost:8080/test
时,一切正常,我的问候文字出现了。
现在我尝试使用org.eclipse.equinox.http.registry.servlets的扩展来做同样的事情。
这就是我的所作所为:
1)将org.eclipse.equinox.http.registry添加到pom.xml下的Require-Bundle
2)打开清单,选择扩展选项卡,单击添加,选择org.eclipse.equinox.http.registry.servlets扩展点,添加,在扩展名详细信息下,将com.example.myserver.myserver.HelloWorldServlet和/ test2作为别名。生成以下plugin.xml:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/test2"
class="com.example.myserver.HelloWorldServlet">
</servlet>
</extension>
</plugin>
在启动配置下,我需要添加这些包:
org.eclipse.equinox.http.registry
org.eclipse.osgi.service,
org.eclipse.osgi.services,
org.eclipse.equinox.common,
org.eclipse.equinox.registry,
javax.xml
然后,当我运行配置时,我收到警告:!MESSAGE The extensions and extension-points from the bundle "myserver" are ignored. The bundle is not marked as singleton.
...所以在pom.xml中我改为:
<Bundle-SymbolicName>${bundle.symbolicName};singleton:=true</Bundle-SymbolicName>
...再次运行,没有错误,一切似乎都很好。
如果我运行http://localhost:8080/test
一切正常。
如果我运行http://localhost:8080/test1
,我会收到未找到错误。
也许我遗漏了一些东西? 我希望我没有深入细节,但我真的希望有人可以帮助我解决这个问题。谢谢! :)