Search in sources :

Example 11 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration 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 12 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project Payara by payara.

the class HK2DomConfigUtilities method addIfNotThere.

private static boolean addIfNotThere(ServiceLocator locator, DynamicConfiguration config, DescriptorImpl desc, HK2Loader loader) {
    IndexedFilter filter = BuilderHelper.createContractFilter(desc.getImplementation());
    if (locator.getBestDescriptor(filter) != null)
        return false;
    if (loader != null) {
        desc.setLoader(loader);
    }
    config.bind(desc);
    return true;
}
Also used : IndexedFilter(org.glassfish.hk2.api.IndexedFilter)

Example 13 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project Payara by payara.

the class ACCModulesManager method initialize.

public static synchronized void initialize(final ClassLoader loader) throws URISyntaxException {
    /*
         * The habitat might have been initialized earlier.  Currently
         * we use a single habitat for the JVM.  
         */
    if (habitat == null) {
        habitat = prepareHabitat(loader);
        /*
             * Set up the default habitat in Globals as soon as we know
             * which habitat we'll use.
             */
        Globals.setDefaultHabitat(habitat);
        ServiceLocator locator = habitat;
        DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
        DynamicConfiguration config = dcs.createDynamicConfiguration();
        /*
             * Remove any already-loaded startup context so we can replace it
             * with the ACC one.
             */
        config.addUnbindFilter(BuilderHelper.createContractFilter(StartupContext.class.getName()));
        /*
             * Following the example from AppServerStartup, remove any
             * pre-loaded lazy inhabitant for ProcessEnvironment that exists
             * from HK2's scan for services.  Then add in
             * an ACC ProcessEnvironment.
             */
        config.addUnbindFilter(BuilderHelper.createContractFilter(ProcessEnvironment.class.getName()));
        config.commit();
        config = dcs.createDynamicConfiguration();
        StartupContext startupContext = new ACCStartupContext();
        AbstractActiveDescriptor<?> startupContextDescriptor = BuilderHelper.createConstantDescriptor(startupContext);
        startupContextDescriptor.addContractType(StartupContext.class);
        config.addActiveDescriptor(startupContextDescriptor);
        ModulesRegistry modulesRegistry = new StaticModulesRegistry(ACCModulesManager.class.getClassLoader());
        config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(modulesRegistry));
        config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(new ProcessEnvironment(ProcessEnvironment.ProcessType.ACC)));
        /*
             * Create the ClientNamingConfigurator used by naming.
             */
        ClientNamingConfigurator cnc = new ClientNamingConfiguratorImpl();
        config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(cnc));
        Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
        AbstractActiveDescriptor<Logger> di = BuilderHelper.createConstantDescriptor(logger);
        di.addContractType(Logger.class);
        config.addActiveDescriptor(di);
        config.commit();
    }
}
Also used : DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) StartupContext(com.sun.enterprise.module.bootstrap.StartupContext) StaticModulesRegistry(com.sun.enterprise.module.single.StaticModulesRegistry) Logger(java.util.logging.Logger) ProcessEnvironment(org.glassfish.api.admin.ProcessEnvironment) ClientNamingConfigurator(org.glassfish.api.naming.ClientNamingConfigurator) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ClientNamingConfiguratorImpl(com.sun.enterprise.naming.impl.ClientNamingConfiguratorImpl) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ModulesRegistry(com.sun.enterprise.module.ModulesRegistry) StaticModulesRegistry(com.sun.enterprise.module.single.StaticModulesRegistry)

Example 14 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project Payara by payara.

the class MicroGlassFishRuntime method newGlassFish.

@Override
public GlassFish newGlassFish(GlassFishProperties glassfishProperties) throws GlassFishException {
    System.setProperty("com.sun.aas.installRoot", System.getProperty("com.sun.aas.instanceRoot"));
    System.setProperty("com.sun.aas.installRootURI", System.getProperty("com.sun.aas.instanceRootURI"));
    glassfishProperties.setProperty("com.sun.aas.installRoot", System.getProperty("com.sun.aas.instanceRoot"));
    glassfishProperties.setProperty("com.sun.aas.installRootURI", System.getProperty("com.sun.aas.instanceRootURI"));
    StartupContext context = new StartupContext(glassfishProperties.getProperties());
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    if (tccl instanceof OpenURLClassLoader) {
        // Only our runtime classloaders list individual runtime jars.
        SingleHK2Factory.initialize(tccl);
    } else {
        // Otherwise we assume we're in rootdir classloader and runtime jars are listed in boot jar's Class-Path
        // manifest attribute
        initializeWithJarManifest(tccl);
    }
    ModulesRegistry registry = AbstractFactory.getInstance().createModulesRegistry();
    ServiceLocator habitat = registry.newServiceLocator();
    DynamicConfigurationService dcs = habitat.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    config.addActiveDescriptor(BuilderHelper.createConstantDescriptor(context));
    config.commit();
    registry.populateServiceLocator("default", habitat, Arrays.asList(new PayaraMicroInhabitantsParser(), new EmbeddedInhabitantsParser(), new DuplicatePostProcessor()));
    registry.populateConfig(habitat);
    ModuleStartup kernel = habitat.getService(ModuleStartup.class);
    gf = new MicroGlassFish(kernel, habitat, glassfishProperties.getProperties());
    return gf;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) StartupContext(com.sun.enterprise.module.bootstrap.StartupContext) DynamicConfigurationService(org.glassfish.hk2.api.DynamicConfigurationService) OpenURLClassLoader(fish.payara.micro.boot.loader.OpenURLClassLoader) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) ModulesRegistry(com.sun.enterprise.module.ModulesRegistry) DuplicatePostProcessor(org.glassfish.hk2.utilities.DuplicatePostProcessor) OpenURLClassLoader(fish.payara.micro.boot.loader.OpenURLClassLoader) EmbeddedInhabitantsParser(com.sun.enterprise.glassfish.bootstrap.EmbeddedInhabitantsParser) ModuleStartup(com.sun.enterprise.module.bootstrap.ModuleStartup)

Example 15 with DynamicConfiguration

use of org.glassfish.hk2.api.DynamicConfiguration in project sandbox by backpaper0.

the class GuiceComponentProvider method bind.

@Override
public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {
    if (component.getPackage().getName().startsWith("echo") == false) {
        return false;
    }
    if (logger.isLoggable(Level.CONFIG)) {
        logger.log(Level.CONFIG, "Bound: {0}", new Object[] { component });
    }
    DynamicConfiguration configuration = ServiceLocatorUtilities.createDynamicConfiguration(locator);
    ServiceBindingBuilder<Object> builder = BindingBuilderFactory.newFactoryBinder(new GuiceFactory(component));
    builder.to(component);
    for (Class<?> providerContract : providerContracts) {
        builder.to(providerContract);
    }
    BindingBuilderFactory.addBinding(builder, configuration);
    configuration.commit();
    return true;
}
Also used : DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration)

Aggregations

DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)140 DynamicConfigurationService (org.glassfish.hk2.api.DynamicConfigurationService)91 Test (org.junit.Test)72 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)55 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)29 MultiException (org.glassfish.hk2.api.MultiException)18 Singleton (jakarta.inject.Singleton)14 List (java.util.List)12 Descriptor (org.glassfish.hk2.api.Descriptor)9 Filter (org.glassfish.hk2.api.Filter)9 DescriptorImpl (org.glassfish.hk2.utilities.DescriptorImpl)9 Type (java.lang.reflect.Type)8 LinkedList (java.util.LinkedList)8 Factory (org.glassfish.hk2.api.Factory)8 ServiceLocatorFactory (org.glassfish.hk2.api.ServiceLocatorFactory)8 WriteableBeanDatabase (org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase)8 AliasDescriptor (org.glassfish.hk2.utilities.AliasDescriptor)7 ArrayList (java.util.ArrayList)6 FactoryDescriptors (org.glassfish.hk2.api.FactoryDescriptors)6 BaseHK2JAXBBean (org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean)5