各位编码员:
我目前正在尝试找到一种简单而简洁的方法来获取使用给定接口的服务/组件的列表。我正在使用一个正在运行的Liferay 7.1.x服务器的gogo-shell,似乎找不到一种简单而直接的方法来实现这一点。
我们希望覆盖对使用的服务via OSGI-configuration的引用,但首先需要找到使用它的所有组件。由于存在对服务组件的静态不情愿引用,简单地提供一个具有更高排名的替代方案不是一个可行的解决方案。
下面是我正在使用的gogo相关包:
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)列出接口的所有提供者
{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
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。
无论如何,出于调试目的,这种查找将非常有用。因此,如果有人知道如何检索接口的所有使用者列表,请发布您的解决方案!
发布于 2020-02-13 21:28:25
标准解决方案是使用inspect命令。它有一个特殊的服务命名空间。由于服务注册是一种功能,因此您可以使用inspect capability service
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比人们所知道的要强大得多。首先,您可以在捆绑包上下文中使用所有方法。
g! servicereferences org.osgi.service.startlevel.StartLevel null
000003 0 StartLevel如果您想查看每个服务的属性:
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]您可以将其添加到内置函数中:
g! srv = { servicereferences $1 null }
servicereferences $1 null
g! srv org.osgi.service.startlevel.StartLevel
000003 0 StartLevel 不幸的是,OSGi在getServiceReferences()的捆绑包上下文中添加了一个重载方法,该方法在使用null调用时会抛出NPE。Gogo在重载方法上很糟糕:-(
但是,使用声明性服务组件添加您自己命令是微不足道的。您可以使用以下内容:
@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:
g! srv Help Basic
000004 1 Basic
000005 1 Inspect 更新如果您想要找出哪些包正在使用特定的服务,您可以使用:
g! each (srv X) { $it usingbundles }确保您的类路径具有以下依赖项:
-buildpath: \
org.osgi.service.component.annotations,\
org.apache.felix.gogo.runtime, \
org.osgi.frameworkhttps://stackoverflow.com/questions/60207709
复制相似问题