Osgi注解处理

huangapple go评论149阅读模式
英文:

Osgi annotation processing

问题

我有一个使用 @Component 注解声明的组件,在其中有一组方法用于与另一个 API 进行通信,在我的产品中,某些操作对于使用匿名 ID 的用户是被禁止的。我想要创建一个注解,例如 @ProhibitedForAnonym,每次调用方法时,它将会检查匿名客户的 ID(通过方法参数传递),如果 ID 匹配,则抛出一个错误。但我不太清楚如何在 OSGI 中进行注解处理,也许可以使用某种拦截器方式来实现?

英文:

I have a component declared using the @Component annotation, in which there is a set of methods that implement communication with another api, in my product there are operations that are prohibited for a user with an anonymous id. I want to create an annotation, for example @ProhibitedForAnonym, which, every time the method is called, will check the ID of the anonymous customer, with the ID in the method parameter and throw an error if the IDs match. But I don't understand how to do annotation processing in OSGI, maybe some kind of interceptor?

答案1

得分: 1

在OSGi中没有通用的拦截框架。但是,您可以通过以下方式进行拦截:

  1. 不进行拦截。就个人而言,我认为自从引入了Lambda表达式以来,基于代码的解决方案已经在“魔法”注解检查上取得了胜利。虽然字符数大致相同,但基于Lambda的调用允许我逐步执行,为安全检查提供上下文,不会遇到“THIS”问题,可进行测试,并且不需要具有许多错误机会的复杂框架。

  2. 使用OSGi中的字节码编织支持。您需要早早地注册一个编织器,然后编织具有这些注解的任何类。您可以查看https://github.com/aQute-os/biz.aQute.osgi.util/tree/master/biz.aQute.trace,了解如何使用字节码编织器的示例。确保您的编织器首先加载。如果您使用bndtools,您可以将其添加到-runpath以在其他任何人之前运行。或者使用启动级别。

  3. 使用代理。您可以通过服务钩子“隐藏”原始服务,然后注册一个代理。在代理中,您可以进行注解检查。这还要求此代码首先运行且无法更新。我认为规范中有一个示例。

您可能希望阅读:https://www.aqute.biz/appnotes/interceptors.html

英文:

There is no general interception framework in OSGi. However, you could do interception in the following ways:

  1. Don't. Personally, I feel that since we've lambdas a code-based solution has won hands on over a 'magic' annotation check. It is about the same number of characters but a lambda based call allows me to single step, provide context to the security check, does not suffer from the THIS problem, is testable, and requires no complex framework with lots of bug opportunities.
  2. Use the byte code weaving support in OSGi. You need to register a weaver early and then weave any class that has these annotations. You can take a look at https://github.com/aQute-os/biz.aQute.osgi.util/tree/master/biz.aQute.trace for an example of how to use the byte code weaver. Make sure your weaver is there first. If you use bndtools you can add it to the -runpath to run before anybody else. Or use start levels.
  3. Use proxying. You can 'hide' and original service with the Service Hooks and then register a proxy. In the proxy you can then do the annotation check. This also requires that this code runs first and cannot be updated. I think the spec has an example of this

You might want to read: https://www.aqute.biz/appnotes/interceptors.html

huangapple
  • 本文由 发表于 2020年9月29日 00:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64106327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定