Search in sources :

Example 1 with DescriptorBuilder

use of org.glassfish.hk2.utilities.DescriptorBuilder 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)

Example 2 with DescriptorBuilder

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

the class AppServerStartupTest method bindService.

private void bindService(DynamicConfiguration configurator, Class<?> service) {
    final DescriptorBuilder descriptorBuilder = BuilderHelper.link(service);
    final RunLevel rla = service.getAnnotation(RunLevel.class);
    if (rla != null) {
        descriptorBuilder.to(RunLevel.class).has(RunLevel.RUNLEVEL_VAL_META_TAG, Collections.singletonList(((Integer) rla.value()).toString())).has(RunLevel.RUNLEVEL_MODE_META_TAG, Collections.singletonList(((Integer) rla.mode()).toString()));
        descriptorBuilder.in(RunLevel.class);
    }
    Class clazz = service;
    while (clazz != null) {
        Class<?>[] interfaces = clazz.getInterfaces();
        for (int j = 0; j < interfaces.length; j++) {
            descriptorBuilder.to(interfaces[j]);
        }
        clazz = clazz.getSuperclass();
    }
    final Named named = service.getAnnotation(Named.class);
    if (named != null) {
        descriptorBuilder.named(named.value());
    }
    configurator.bind(descriptorBuilder.build());
}
Also used : Named(javax.inject.Named) PostStartupRunLevel(org.glassfish.internal.api.PostStartupRunLevel) RunLevel(org.glassfish.hk2.runlevel.RunLevel) InitRunLevel(org.glassfish.internal.api.InitRunLevel) StartupRunLevel(org.glassfish.api.StartupRunLevel) DescriptorBuilder(org.glassfish.hk2.utilities.DescriptorBuilder)

Aggregations

DescriptorBuilder (org.glassfish.hk2.utilities.DescriptorBuilder)2 Named (javax.inject.Named)1 StartupRunLevel (org.glassfish.api.StartupRunLevel)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1 RunLevel (org.glassfish.hk2.runlevel.RunLevel)1 AliasDescriptor (org.glassfish.hk2.utilities.AliasDescriptor)1 InitRunLevel (org.glassfish.internal.api.InitRunLevel)1 PostStartupRunLevel (org.glassfish.internal.api.PostStartupRunLevel)1 Service (org.jvnet.hk2.annotations.Service)1 InjectionTarget (org.jvnet.hk2.config.InjectionTarget)1