首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有一种简单的方法来列出接口的消费者组件?

有没有一种简单的方法来列出接口的消费者组件?
EN

Stack Overflow用户
提问于 2020-02-13 20:19:38
回答 1查看 261关注 0票数 1

各位编码员:

我目前正在尝试找到一种简单而简洁的方法来获取使用给定接口的服务/组件的列表。我正在使用一个正在运行的Liferay 7.1.x服务器的gogo-shell,似乎找不到一种简单而直接的方法来实现这一点。

我们希望覆盖对使用的服务via OSGI-configuration的引用,但首先需要找到使用它的所有组件。由于存在对服务组件的静态不情愿引用,简单地提供一个具有更高排名的替代方案不是一个可行的解决方案。

下面是我正在使用的gogo相关包:

代码语言:javascript
复制
   35|Active     |    6|Apache Felix Gogo Command (1.0.2)|1.0.2
   36|Active     |    6|Apache Felix Gogo Runtime (1.1.0.LIFERAY-PATCHED-2)|1.1.0.LIFERAY-PATCHED-2
   72|Active     |    6|Apache Felix Gogo Shell (1.1.0)|1.1.0
  542|Active     |   10|Liferay Foundation - Liferay Gogo Shell - Impl (1.0.13)|1.0.13
  543|Active     |   10|Liferay Gogo Shell Web (2.0.25)|2.0.25

到目前为止,我已经能够通过se (interface=com.liferay.saml.runtime.servlet.profile.WebSsoProfile)列出接口的所有提供者

代码语言:javascript
复制
{com.liferay.saml.runtime.profile.WebSsoProfile, com.liferay.saml.runtime.servlet.profile.WebSsoProfile}={service.id=6293, service.bundleid=79, service.scope=bundle, component.name=com.liferay.saml.opensaml.integration.internal.servlet.profile.WebSsoProfileImpl, component.id=3963}
  "Registered by bundle:" de.haufe.leong.com.liferay.saml.opensaml.integration [79]
  "Bundles using service"
    com.liferay.saml.web_2.0.11 [82]
    com.liferay.saml.impl_2.0.12 [78]

通过以下网址查看所有捆绑包需求:inspect cap service

代码语言:javascript
复制
com.liferay.saml.impl_2.0.12 [78] requires:
...
service; com.liferay.saml.runtime.profile.WebSsoProfile, com.liferay.saml.runtime.servlet.profile.WebSsoProfile provided by:
   de.haufe.leong.com.liferay.saml.opensaml.integration [79]
...

但是到目前为止,我还没能列出这些包中使用给定接口(或服务组件)的实际服务。

到目前为止,我看到的唯一解决方案是通过scr:list bid列出这些捆绑包提供的所有服务,然后使用scr:info componentId检查每个服务,看看它是否使用WebSsoProfile服务。

你们知道使用WebSsoProfile-service更快地找到服务的方法吗?

编辑:我们不必为WebSsoProfile服务的所有使用者提供配置覆盖就解决了这个问题,而是通过在服务器启动时停用默认服务来确保我们的实现被使用。你可以使用see the approach described here

无论如何,出于调试目的,这种查找将非常有用。因此,如果有人知道如何检索接口的所有使用者列表,请发布您的解决方案!

EN

回答 1

Stack Overflow用户

发布于 2020-02-13 21:28:25

标准解决方案是使用inspect命令。它有一个特殊的服务命名空间。由于服务注册是一种功能,因此您可以使用inspect capability service

代码语言:javascript
复制
g! inspect c service
org.apache.felix.framework [0] provides:
----------------------------------------
service; org.osgi.service.resolver.Resolver with properties:
   service.bundleid = 0
   service.id = 1
   service.scope = singleton
service; org.osgi.service.packageadmin.PackageAdmin with properties:
   service.bundleid = 0
   service.id = 2
   service.scope = singleton
service; org.osgi.service.startlevel.StartLevel with properties:
   service.bundleid = 0
   service.id = 3
   service.scope = singleton

....

然而,我发现这个命令非常无用。这个命令不灵活,输出也很糟糕。

然而,Gogo比人们所知道的要强大得多。首先,您可以在捆绑包上下文中使用所有方法。

代码语言:javascript
复制
g! servicereferences org.osgi.service.startlevel.StartLevel null
000003   0 StartLevel

如果您想查看每个服务的属性:

代码语言:javascript
复制
g! each (servicereferences org.osgi.service.startlevel.StartLevel null) { $it properties }
[service.id=3, objectClass=[Ljava.lang.String;@4acd14d7, service.scope=singleton, service.bundleid=0]

您可以将其添加到内置函数中:

代码语言:javascript
复制
g! srv = { servicereferences $1 null }
servicereferences $1 null
g! srv org.osgi.service.startlevel.StartLevel
000003   0 StartLevel                               

不幸的是,OSGi在getServiceReferences()的捆绑包上下文中添加了一个重载方法,该方法在使用null调用时会抛出NPE。Gogo在重载方法上很糟糕:-(

但是,使用声明性服务组件添加您自己命令是微不足道的。您可以使用以下内容:

代码语言:javascript
复制
@GogoCommand(scope="service", function="srv")
@Component(service=ServiceCommand.class)
public class ServiceCommand {

    @Activate
    BundleContext context;        

    @Descriptor("List all services")
    public ServiceReference<?>[] srv() throws InvalidSyntaxException {
        return context.getAllServiceReferences(null, null);
    }

    @Descriptor("List all services that match the name")
    public ServiceReference<?>[] srv(
            String... names)
            throws InvalidSyntaxException {
        ServiceReference<?>[] allServiceReferences = 
            context.getAllServiceReferences(null,null);
        if ( allServiceReferences==null)
            return new ServiceReference[0];
        return Stream.of(allServiceReferences)
                .filter(r -> {
                    String[] objectClass = (String[]) r.getProperty(Constants.OBJECTCLASS);
                    for (String oc : objectClass) {
                        for (String name : names)
                            if (oc.contains(name))
                                return true;
                    }
                    return names.length == 0;
                }).sorted().toArray(ServiceReference[]::new);
    }
}

这会将srv命令添加到Gogo:

代码语言:javascript
复制
g! srv Help Basic
000004   1 Basic                                    
000005   1 Inspect   

更新如果您想要找出哪些包正在使用特定的服务,您可以使用:

代码语言:javascript
复制
g! each (srv X) { $it usingbundles }

确保您的类路径具有以下依赖项:

代码语言:javascript
复制
-buildpath: \
    org.osgi.service.component.annotations,\
    org.apache.felix.gogo.runtime, \
    org.osgi.framework
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60207709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档