0

我正在尝试在本地 Glassfish 服务器上使用声明式服务来做一个简单的 OSGi 服务。提供的插件始终处于活动状态。

我在注入使用我的服务的 servlet 时遇到了麻烦,调用 servlet 时引用为空,因为它与注入服务引用的对象不同。

我通过在我的引用设置器中放置一个断点来测试它,我看到我的服务被注入了,但是当我单击将我的 servlet 调用到我的应用程序中的按钮时,服务引用为空,因为它不是同一个对象(即获取注入 servlet_Instance #1 但调用 servlet_Instance #2 上的方法。我一定遗漏了一点细节,因为我可以在做的时候找到并使用我的服务

final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
     loggingTestServiceInterface = (LoggingTestServiceInterface) bundleContext.getService(bundleContext
     .getServiceReference(LoggingTestServiceInterface.class.getName()));

用于生成我的 XMLs 文件的插件:maven-scr-plugin

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.14.0</version>
<executions>
    <execution>
        <id>generate-scr-scrdescriptor</id>
        <goals>
            <goal>scr</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <supportedProjectTypes>
        <supportedProjectType>war</supportedProjectType>
        <supportedProjectType>jar</supportedProjectType>
        <supportedProjectType>bundle</supportedProjectType>
    </supportedProjectTypes>
</configuration>
</plugin>

这是我的服务班

@Component(immediate = false, name = "Shikashi", service = {LoggingTestServiceInterface.class}, enabled = true)
public class LoggingTestService implements LoggingTestServiceInterface
{
private final LoggerUtils loggerUtils = new LoggerUtils();

public LoggingTestService()
{
}

@Activate
public void start(final BundleContext bundleContext)
{
    System.out.println("StartTest Service Fune");
}

@Deactivate
public void stop()
{
    System.out.println("Stop Test Service Jitensha");
}

@Modified
public void modify()
{
    System.out.println("Stop Test Service onnanogo");
}

private Logger createLogger(final Class<?> clazz)
{
    return Logger.getLogger(clazz);
}

@Override
public void logDebug(final Class<?> clazz, final String message)
{
    logDebug(clazz, message, null);
}
    @Override
public void logDebug(final Class<?> clazz, final String message, final Throwable throwable)
{
    final Logger logger = createLogger(clazz);
    logger.debug(message, throwable);
}

}

生成的 XML 是

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component enabled="true" immediate="false" name="Shikashi" activate="start" deactivate="stop" modified="modify">
    <implementation class="com.sti.logging.service.LoggingTestService"/>
    <service servicefactory="false">
        <provide interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface"/>
    </service>
</scr:component>

我的小服务程序是

@WebServlet(name = "Wakarimashita", urlPatterns = { "/Wakarimashita"})
@Component
public class Wakarimashita extends HttpServlet
{
private LoggingTestServiceInterface loggingTestServiceInterface;

@Override
protected void doGet(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException
{
    // Method just to setup the Servlet to understand how it works

    final String language = "language";
    final String path = "/sigbud/language/";

    if (httpServletRequest.getParameter(language) != null)
    {
        if (httpServletRequest.getParameter(language).equalsIgnoreCase("Nihongo"))
        {
            httpServletResponse.sendRedirect(path + "nihongo.jsp");
        }
        else if (httpServletRequest.getParameter(language).equalsIgnoreCase("Eigo"))
        {
            httpServletResponse.sendRedirect(path + "eigo.jsp");
        }
        else if (httpServletRequest.getParameter(language).equalsIgnoreCase("Funansugo"))
        {
            httpServletResponse.sendRedirect(path + "funansugo.jsp");
        }
        else
        {
            httpServletResponse.sendRedirect(path + "unknown.jsp");
        }
    }
    else
    {
        super.doGet(httpServletRequest, httpServletResponse);
    }

    loggingTestServiceInterface.logError(getClass(), "Wakarimasen");
}
    @Reference(service = LoggingTestServiceInterface.class, cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC)
public void bindLoggingTestServiceInterface(final LoggingTestServiceInterface loggingTestServiceInterface)
{
    this.loggingTestServiceInterface = loggingTestServiceInterface;
}

public void unbindLoggingTestServiceInterface(final LoggingTestServiceInterface loggingTestServiceInterface)
{
    if (this.loggingTestServiceInterface.equals(loggingTestServiceInterface))
    {
        this.loggingTestServiceInterface = null;
    }
}
@Activate
public void start(final BundleContext bundleContext)
{
    System.out.println("StartTest Service Taisho");
}

@Deactivate
public void stop()
{
    System.out.println("Stop Test Service Fukutaisho");
}

@Modified
public void modify()
{
    System.out.println("Stop Test Service san jyû kyû");
}
}

生成的 XML

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component name="com.sti.sigbud.servlet.Wakarimashita" activate="start" deactivate="stop" modified="modify">
    <implementation class="com.sti.sigbud.servlet.Wakarimashita"/>
    <reference name="LoggingTestServiceInterface" interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface" cardinality="1..1" policy="dynamic" bind="bindLoggingTestServiceInterface" unbind="unbindLoggingTestServiceInterface"/>
</scr:component>

我也尝试过,但没有运气,因为似乎没有找到我的 servlet(错误 404 - 请求的资源()不可用。),正如 Peter Kriens 在那里写的那样:如何使用 OSGi HTTP 服务中的 OSGi 服务

所以我像这样修改了我的servlet:

@Component(service = Servlet.class, property = {"alias=/Wakarimashita"})
public class Wakarimashita extends HttpServlet

生成的 XML 是

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component name="com.sti.sigbud.servlet.Wakarimashita" activate="start" deactivate="stop" modified="modify">
    <implementation class="com.sti.sigbud.servlet.Wakarimashita"/>
    <service servicefactory="false">
        <provide interface="javax.servlet.Servlet"/>
    </service>
    <property name="alias" value="/Wakarimashita"/>
    <reference name="LoggingTestServiceInterface" interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface" cardinality="1..1" policy="dynamic" bind="bindLoggingTestServiceInterface" unbind="unbindLoggingTestServiceInterface"/>
</scr:component>

我从我的 JSP 访问 servlet

<form action="Wakarimashita" method="GET">
        <input type="text" name="language" size="50"/>
    <input type="submit" value="Submit" />
</form>

为了测试上述内容,我在我部署的包 org.apache.felix.http.api-2.2.1、org.apache.felix.http.whiteboard-2.2.1 中进行了测试,就像在帖子中一样。没查到有没有开关。

我还检查了 org.apache.felix.webconsole-4.2.0-所有捆绑包,服务已经启动并运行,它说我的消费者捆绑包正在使用它。

4

1 回答 1

1

您有两方创建 servlet 的实例。一个是 DS,另一个是 Web 容器。你不能有 2 个主人。Web 容器基本上必须负责,因为它只会向它创建的 servlet 实例发送请求。

如果有一个同时支持 Web 容器和 DS 的实现,那么您将被设置。但我从来没有听说过这样的事情。

我不知道 Glassfish 是否支持 OSGi Web 应用程序规范(第 128 章)。如果是这样,那么您可以与 OSGi 服务层进行交互,如 128.6 中所述。

于 2013-10-02T20:14:52.557 回答