javaosgiblueprint-osgiaries

Aries NamespaceHandler to get Reference of Bean


After successfully tried to implement my own blueprint interceptor to track method entry/exits using Namespace Handler, I'm now looking for enhancing it where the interceptor itself is defined as another bean in same blueprint xml and my namespace element use ref="interceptorBeanName". i.e., I'm trying to register an Interceptor that actually delegates to another Instance of Interceptor defined in the same blueprint.

But, how to get the Reference of Interceptor bean defined in the blueprint ?

I searched in Google for many hours. But, didn't any documentation of how to do that in Blueprint.

Any help on how to approach this will be highly appreciated.

Edit:

@Override
public ComponentMetadata decorate(Node node, ComponentMetadata compMeta,
        ParserContext cxt) {
    cxt.getComponentDefinitionRegistry().registerInterceptorWithComponent(compMeta, new Interceptor() {

        @Override
        public Object preCall(ComponentMetadata arg0, Method arg1, Object... arg2)
                throws Throwable {
            info_logger.info(arg1.getName() + " before call");
            return null;
        }

        @Override
        public void postCallWithReturn(ComponentMetadata arg0, Method arg1,
                Object arg2, Object arg3) throws Throwable {
            info_logger.info(arg1.getName() + " after call");
        }

        @Override
        public void postCallWithException(ComponentMetadata arg0, Method arg1,
                Throwable arg2, Object arg3) throws Throwable {
            info_logger.info(arg1.getName() + " after exception");
        }

        @Override
        public int getRank() {
            return 0;
        }
    });
    return compMeta;
}

Solution

  • I see two possibilities, use a BeanProcessor, or a ComponentDefinitionRegistryProcessor

    The BeanProcessor is a classic bean, which can be injected like any other bean, but they can preprocess/postprocess other beans definition, and add an interceptor on this bean definition. this incerceptor can have reference on other beans

    see for example the tx annotation parser

    a ComponentDefinitionRegistryProcessor is invoked after the definition of the beans, but before the context is started. it can use the BlueprintContainer, which references all the beans of the context, and can add interceptor too. with the help of the BlueprintContainer, the interceptor can references other beans of the context.

    see for example the jpa processor

    Old Answer :

    If you want, in an aries-blueprint NamespaceHandler, create a reference to another bean, you can use the RefMetaData, and associate this ref like a normal bean.

    private RefMetadata createRef(ParserContext context, String value) {
       MutableRefMetadata m = context.createMetadata(MutableRefMetadata.class);
       m.setComponentId(value);
       return m;
    }
    

    and something like :

    MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
    metadata.addRuntimeClass(DelegateInterceptor.class);
    ...
    metadata.addProperty("delegate", createRef(pc, element.getAttribute("ref")));