Search in sources :

Example 1 with AliasDescriptor

use of org.glassfish.hk2.utilities.AliasDescriptor in project Payara by payara.

the class ConfigModule method bindInjector.

private void bindInjector(DynamicConfiguration configurator, String elementName, Class contract, final Class clz) {
    DescriptorBuilder db = BuilderHelper.link(clz).to(ConfigInjector.class).to(InjectionTarget.class).to(contract).in(Singleton.class.getName()).qualifiedBy(clz.getAnnotation(InjectionTarget.class)).named(elementName).andLoadWith(new MyHk2Loader(clz.getClassLoader()));
    String metaData = ((Service) clz.getAnnotation(Service.class)).metadata();
    Map<String, List<String>> metaMap = new HashMap<String, List<String>>();
    for (StringTokenizer st = new StringTokenizer(metaData, ","); st.hasMoreTokens(); ) {
        String tok = st.nextToken();
        int index = tok.indexOf('=');
        if (index > 0) {
            String key = tok.substring(0, index);
            String value = tok.substring(index + 1);
            List<String> lst = metaMap.get(key);
            if (lst == null) {
                lst = new LinkedList<String>();
                metaMap.put(key, lst);
            }
            lst.add(value);
        // System.out.println("**     Added Metadata: " + tok.substring(0, index) + "  : " + tok.substring(index+1));
        }
    // db.andLoadWith(new MyHk2Loader(clz.getClassLoader()));
    }
    for (String key : metaMap.keySet()) {
        db.has(key, metaMap.get(key));
    }
    ActiveDescriptor desc = configurator.bind(db.build());
    configurator.bind(new AliasDescriptor(serviceLocator, desc, InjectionTarget.class.getName(), contract.getName()));
    System.out.println("**Successfully bound an alias descriptor for: " + elementName);
}
Also used : ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Service(org.jvnet.hk2.annotations.Service) DescriptorBuilder(org.glassfish.hk2.utilities.DescriptorBuilder) AliasDescriptor(org.glassfish.hk2.utilities.AliasDescriptor) InjectionTarget(org.jvnet.hk2.config.InjectionTarget)

Aggregations

ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 AliasDescriptor (org.glassfish.hk2.utilities.AliasDescriptor)1 DescriptorBuilder (org.glassfish.hk2.utilities.DescriptorBuilder)1 Service (org.jvnet.hk2.annotations.Service)1 InjectionTarget (org.jvnet.hk2.config.InjectionTarget)1