本文介绍了未解决的要求:osgi.component的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Karaf上开发OSGi WAB,其中包含REST API和来自定制服务的调用.但是,出于某些奇怪的原因,OSGi框架抱怨其功能osgi.component不令人满意.

I'm trying to develop onto Karaf an OSGi WAB containing a REST API and a call from a custom-made service. However, for some weird reason, the OSGi framework complains about an unsatisfied capability, osgi.component.

我想知道:

  1. 我该如何解决这个问题?
  2. 什么是osgi.component捆绑包?为什么需要?
  3. 为什么maven-bundle-plugin(因此也绑定)在条目"Require-Capability"中声明它?
  4. 如果我需要在OSGi框架上安装它,在哪里可以找到它?
  1. How can I solve this issue?
  2. What is osgi.component bundle? Why is needed?
  3. Why maven-bundle-plugin (thereby also bnd), declares it inside the entry "Require-Capability"?
  4. If I need to install it on the OSGi framework, where may I find it?

一些其他信息:

  • karaf版本:4.0.7;
  • maven包插件:3.2.0;
  • 操作系统:Windows 10 64bit;
  • IDE:Eclipse Neon;

一些提供其他信息的代码:

Some code to provide additional info:

整个错误:

清单文件:

Manifest-Version: 1.0
Bundle-SymbolicName: com.massimobono.karaf.examples.user-fully-rest
Archiver-Version: Plexus Archiver
Built-By: massi
Bnd-LastModified: 1479908575162
Bundle-ActivationPolicy: lazy
Bundle-ManifestVersion: 2
Import-Package: com.massimobono.karaf.examples.user;version="[0.0,1)",
 com.massimobono.karaf.examples.user.service;version="[0.0,1)",javax.w
 s.rs;version="[2.0,3)",javax.ws.rs.core;version="[2.0,3)"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=osgi.compo
 nent)(version>=1.3.0)(!(version>=2.0.0)))",osgi.service;filter:="(obj
 ectClass=com.massimobono.karaf.examples.user.service.UserService)";ef
 fective:=active,osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/com.massimobono.karaf.examples.user.ui.ful
 lyrest.UserRest.xml
Tool: Bnd-3.2.0.201605172007
Originally-Created-By: Maven Integration for Eclipse
Export-Package: com.massimobono.karaf.examples.user.ui.fullyrest;uses:
 ="javax.ws.rs,javax.ws.rs.core";version="0.0.1"
Bundle-Name: user-fully-rest Maven Webapp
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Build-Jdk: 1.8.0_91

其他基本类别:

package com.massimobono.karaf.examples.user.ui.fullyrest;

import java.time.LocalDateTime;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.massimobono.karaf.examples.user.User;
import com.massimobono.karaf.examples.user.service.UserService;
import com.massimobono.karaf.examples.user.service.UserServiceException;

@Path("user")
@Component(immediate=true)
public class UserRest {

    @Reference
    private volatile UserService userService;

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getUserNumber() {
        try {
            return String.format("<h1>Total users: %d</h1>", this.userService.size());
        } catch (UserServiceException e) {
            return String.format("Couldn't fetch total users because %s", e.getMessage());
        }
    }

    @PUT
    @Path("add/{name}/{surname}")
    @Produces(MediaType.TEXT_HTML)
    public String add(@PathParam("name") String name, @PathParam("surname") String surname) {
        try {
            User u = new User(name, surname, LocalDateTime.now());
            this.userService.addUser(u);
            return String.format("<h1>New user with id %d</h1>", u.getId());
        } catch (UserServiceException e) {
            return String.format("<h1>Couldn't fethc total users because %s</h1>", e.getMessage());
        }
    }

    @DELETE
    @Path("remove/{id}")
    @Produces(MediaType.TEXT_HTML)
    public String remove(@PathParam("id") int id) {
        User u;
        try {
            u = this.userService.getUser(id);
            this.userService.removeUser(u);
            return String.format("<h1>User name=%s surname=%s removed correctly</h1>", u.getName(), u.getSurname());
        } catch (UserServiceException e) {
            return String.format("<h1>Couldn't remove user because %s</h1>", e.getMessage());
        }

    }

}

感谢您的任何回复

推荐答案

很可能您在Karaf运行时中缺少SCR.您可以使用feature:install scr

Most likely you are missing SCR in your Karaf runtime. You can install it with feature:install scr

这不是捆绑包,而是必要条件.基本上,它说您的捆绑软件需要SCR(或其他东西),该SCR知道如何处理和注册通过声明式服务定义在其中的组件.

It's not a bundle but a requirement. Basically it says your bundle needs SCR (or something) that knows how to process and register the components defined in it via Declarative Services.

因为它看到您正在使用声明式服务,并且知道它们将无法工作,除非您在运行时拥有一些了解声明的方式并知道如何管理其生命周期的内容.如果没有要求(我相信bnd的早期版本就是这种情况),那么您的捆绑包将开始运行而不会出现问题,但服务仍不会被注册/激活.

Because it sees that you are using Declarative Services and knows they will not work unless you have something at runtime that understands how they are declared and knows how to manage their lifecycle. If the requirement was not there (which I believe was the case with earlier versions of bnd) then your bundle would start without issues but services would still be not registered / activated.

在Karaf中,它可以作为功能部件使用(请参阅第一个问题的答案).在普通的OSGi运行时(Felix,Equinox等)中,您需要手动安装它. 它在Maven Central中可用.

In Karaf it's available as feature (see the answer to your first question). In plain OSGi runtime (Felix, Equinox, ...) you need to install it manually. It's available in Maven central.

这篇关于未解决的要求:osgi.component的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 01:49