【问题标题】:Extending org.eclipse.equinox.http.registry.servlets does not work扩展 org.eclipse.equinox.http.registry.servlets 不起作用
【发布时间】:2014-02-20 03:59:30
【问题描述】:

我正在尝试对 org.eclipse.equinox.http.registry.servlets 扩展点进行 servlet 扩展,但无法使其正常工作。生病尝试写下我的过程,所以也许有人可以告诉我我做错了什么或我没有做什么:)

所以,我首先创建了一个带有默认 servlet 的码头服务器。

这是我最初的 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

现在,当我输入 url http://localhost:8080/test 时,一切正常,我的 hello 文本出现了。

现在我尝试使用 org.eclipse.equinox.http.registry.servlets 的扩展来做同样的事情。

这是我的工作:

1) 在pom.xml下的Require-Bundle中添加org.eclipse.equinox.http.registry

2) 打开清单,选择扩展选项卡,单击添加,选择 org.eclipse.equinox.http.registry.servlets 扩展点,添加,在扩展详细信息下将类 com.example.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,我会收到 not found 错误。

也许我留下了一些未完成的事情? 我希望我没有详细说明,但我真的希望有人可以帮助我解决这个问题。谢谢! :)

【问题讨论】:

  • 在您的第一个示例中,您先执行 server.start(),然后执行 server.join()。我相信 join() 会阻塞调用线程,直到服务器关闭。这在 BundleActivator 启动方法中确实是一件非常糟糕的事情……您窃取了启动器的线程,实际上会阻止 OSGi 框架正常启动。
  • 感谢您的回复,我删除了加入,但问题仍然存在。你能看出我的代码或工作流程还有什么问题吗?
  • 不是真的,但我不熟悉这个扩展点。但是我想知道你为什么要尝试使用扩展点来做到这一点?这似乎比仅仅将您的 Servlet 发布为服务并使用 Felix HTTP Whiteboard 包要多得多
  • 好的,我开始工作了。似乎我在做一些非常非常错误的事情(而且很愚蠢:))。我根本不需要自己启动码头服务器。这是帮助我的教程:bryanhunt.wordpress.com/2010/05/14/…。只是旁注一下,最后提到的 org.mortbay.jetty 包已更改为 org.eclipse.jetty。无论如何,据我了解,其中一个包含的捆绑包以某种方式为我启动了一个码头服务器,我所要做的就是编写一个扩展并指向我的 servlet。码头服务器究竟是如何/在哪里启动的,我不知道,但我会试着找出答案。
  • 尝试:localhost:8080/test2 而不是 localhost:8080/test1。我不知道这是否只是一个错字,所以我仅将其作为评论发布。

标签: java eclipse maven servlets osgi


【解决方案1】:

你放错地方了,因为描述了 alias="/test2",并且 试试http://localhost:8080/test1

你应该使用http://localhost:8080/test2

【讨论】:

    猜你喜欢
    • 2014-11-30
    • 2019-03-21
    • 2011-08-18
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多