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; }
AbstractActiveDescriptor<? extends Supplier<?>> supplierBuilder = BuilderHelper.createConstantDescriptor(supplier); binding.getContracts().forEach(contract -> { supplierBuilder.addContractType(new ParameterizedTypeImpl(Supplier.class, contract)); }; ServiceLocatorUtilities.bind(locator, createBinder(bindConsumer));
if (locator.getBestDescriptor(BuilderHelper.createContractFilter(PROPERTY_GENERATED_INJECTOR_CLASS)) != null) return; descriptionList.add(LEAF); DescriptorImpl injectorDescriptor = BuilderHelper.link(PROPERTY_GENERATED_INJECTOR_CLASS). to(CONFIG_INJECTOR_CLASS). in(Singleton.class.getName()). injectorDescriptor.removeQualifier(Named.class.getName()); injectorDescriptor.setLoader(loader); ServiceLocatorUtilities.addOneDescriptor(locator, injectorDescriptor);
private void addDescriptorFileFinder( ServiceLocator serviceLocator, DescriptorFileFinder descriptorFileFinder) { if (descriptorFileFinder != null) { DynamicConfigurationService dcs = serviceLocator .getService(DynamicConfigurationService.class); DynamicConfiguration config = dcs.createDynamicConfiguration(); config.addActiveDescriptor(BuilderHelper .createConstantDescriptor(descriptorFileFinder)); config.commit(); } }
Context<PerThread> perThreadContext = locator.getService((new TypeLiteral<Context<PerThread>>() {}).getType()); if (perThreadContext != null) return; DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration config = dcs.createDynamicConfiguration(); final DescriptorImpl descriptor = BuilderHelper.link(PerThreadContext.class). to(Context.class). in(Singleton.class.getName()). descriptor.setLoader(new HK2Loader() { @Override public Class<?> loadClass(String className) throws MultiException { config.bind(descriptor, false); config.commit();
if (locator == null) throw new IllegalArgumentException(); DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration cd = dcs.createDynamicConfiguration(); FactoryDescriptors fds = cd.addActiveFactoryDescriptor((Class<Factory<Object>>) factoryConstant.getClass()); intermediateState.add(fds); cd = dcs.createDynamicConfiguration(); final Factory<?> constant = constants[lcv++]; Descriptor constantDescriptor = BuilderHelper.createConstantDescriptor(constant); Descriptor addProvideMethod = new DescriptorImpl(provideMethod); retVal.add(cd.bind(fdi)); cd.commit();
private static void atomicReplace(ServiceLocator locator, ProgramOptions options) { DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration config = dcs.createDynamicConfiguration(); config.addUnbindFilter(BuilderHelper.createContractFilter(ProgramOptions.class.getName())); ActiveDescriptor<ProgramOptions> desc = BuilderHelper.createConstantDescriptor( options, null, ProgramOptions.class); config.addActiveDescriptor(desc); config.commit(); }
@Override public void initializeSpringBridge(ServiceLocator locator) throws MultiException { boolean addService = true; if (locator.getBestDescriptor(BuilderHelper.createContractFilter(SpringIntoHK2Bridge.class.getName())) != null) { addService = false; } boolean addContext = true; if (locator.getBestDescriptor(BuilderHelper.createContractFilter(SpringScopeContext.class.getName())) != null) { addContext = false; } if (addService == false && addContext == false) return; DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); if (dcs == null) return; DynamicConfiguration config = dcs.createDynamicConfiguration(); if (addService) { config.addActiveDescriptor(SpringIntoHK2BridgeImpl.class); } if (addContext) { config.addActiveDescriptor(SpringScopeContext.class); } config.commit(); }
@Override public Object create(Context ic) throws NamingException { ActiveDescriptor<?> descriptor = habitat.getBestDescriptor( BuilderHelper.createContractFilter("javax.transaction.UserTransaction")); if (descriptor == null) return null; return habitat.getServiceHandle(descriptor).getService(); } }, false);
@Override public void stop() throws IOException { ServiceLocator locator = grizzlyService.getHabitat(); IndexedFilter removeFilter = BuilderHelper.createNameAndContractFilter(Mapper.class.getName(), (address.toString() + port)); DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration config = dcs.createDynamicConfiguration(); config.addUnbindFilter(removeFilter); config.commit(); unregisterMonitoringStatsProviders(); super.stop(); }
SimpleConnector sc = habitat.getService(SimpleConnector.class); assertEquals("Extensions", 1, sc.getExtensions().size()); assertEquals("Nested children", 2, sc.getExtensions().get(0).getExtensions().size()); habitat.getDescriptors(BuilderHelper.createContractFilter(GenericContainer.class.getName())), habitat.getService(GenericContainer.class)); assertNull("GenericConfig descriptor test still has " + habitat.getDescriptors(BuilderHelper.createContractFilter(GenericConfig.class.getName())), habitat.getService(GenericConfig.class, "test")); assertNull("GenericConfig descriptor still has " + habitat.getDescriptors(BuilderHelper.createContractFilter(GenericConfig.class.getName())), habitat.getService(GenericConfig.class));
@Override public Injectee apply(CacheKey cacheKey) { Injectee injectee = cacheKey.getInjectee(); if (injectee.getParent() != null) { if (Field.class.isAssignableFrom(injectee.getParent().getClass())) { Field f = (Field) injectee.getParent(); if (foreignRequestScopedComponents.get().contains(f.getDeclaringClass())) { final Class<?> clazz = f.getType(); if (serviceLocator.getServiceHandle(clazz).getActiveDescriptor().getScopeAnnotation() == RequestScoped.class) { final AbstractActiveDescriptor<Object> descriptor = BuilderHelper.activeLink(clazz) .to(clazz) .in(RequestScoped.class) .build(); return new DescriptorOverridingInjectee(injectee, descriptor); } } } } return injectee; } });
public Class getClassForFullName(String configBeanClassName) { ActiveDescriptor<?> descriptor = serviceLocator.getBestDescriptor(BuilderHelper.createContractFilter(configBeanClassName)); if (descriptor != null) { if (!descriptor.isReified()) { descriptor = serviceLocator.reifyDescriptor(descriptor); } return getClassFromDescriptor(descriptor); } else { descriptor = serviceLocator.getBestDescriptor(BuilderHelper.createContractFilter(configBeanClassName + "Injector")); if (!descriptor.isReified()) { descriptor = serviceLocator.reifyDescriptor(descriptor); } ConfigInjector injector = (ConfigInjector) serviceLocator.getServiceHandle(descriptor).getService(); return getClassFromInjector(injector); } }
private void dumpInhabitantsImplementingContractPattern(String pattern, StringBuilder sb) { sb.append("\n*********** List of all services for contract named like " + contract + " **************\n"); List<ActiveDescriptor<?>> allDescriptors = serviceLocator.getDescriptors(BuilderHelper.allFilter()); HashSet<String> allContracts = new HashSet<String>(); for (ActiveDescriptor<?> aDescriptor : allDescriptors) { allContracts.addAll(aDescriptor.getAdvertisedContracts()); } Iterator<String> it = allContracts.iterator(); while (it.hasNext()) { String cn = it.next(); if (cn.toLowerCase(Locale.ENGLISH).indexOf(pattern.toLowerCase(Locale.ENGLISH)) < 0) continue; sb.append("\n-----------------------------\n"); for ( ActiveDescriptor<?> descriptor : serviceLocator.getDescriptors(BuilderHelper.createContractFilter(cn))) { sb.append("Inhabitant-Metadata: " + descriptor.getMetadata()); sb.append("\n"); boolean isStarted = Boolean.parseBoolean(started); if (isStarted) { ServiceHandle<?> handle = serviceLocator.getServiceHandle(descriptor); sb.append((handle.isActive() ? " started" : " not started")); } } } }
dci.bind(BuilderHelper.link(DynamicConfigurationServiceImpl.class, false). to(DynamicConfigurationService.class). in(Singleton.class.getName()). build()); dci.bind(BuilderHelper.createConstantDescriptor( new DefaultClassAnalyzer(sli))); dci.bind(BuilderHelper.createDescriptorFromClass(ServiceLocatorRuntimeImpl.class)); dci.bind(BuilderHelper.createConstantDescriptor( new InstantiationServiceImpl()));
/** * This method adds one existing object to the given service locator. The caller * of this will not get a chance to customize the descriptor that goes into the * locator, and hence must rely completely on the analysis of the system to determine * the set of contracts and metadata associated with the descriptor. The same algorithm * is used in this method as in the {@link BuilderHelper#createConstantDescriptor(Object)} * method. * * @param locator The non-null locator to add this descriptor to * @param constant The non-null constant to add to the service locator * @return The descriptor that was added to the service locator */ public static <T> ActiveDescriptor<T> addOneConstant(ServiceLocator locator, Object constant) { if (locator == null || constant == null) throw new IllegalArgumentException(); return addOneDescriptor(locator, BuilderHelper.createConstantDescriptor(constant), false); }
@SuppressWarnings("unused") @PostConstruct private void postConstruct() { fighterFishHandles = locator.getAllServiceHandles( BuilderHelper.createContractFilter(FIGHTERFISH_START_SERVICE)); for (ServiceHandle<?> fighterFishHandle : fighterFishHandles) { fighterFishHandle.getService(); } }
/** * Given a command name, return the name of the class that implements * that command in the server. */ private static String getCommandClass(String cmdName) { ServiceLocator h = getManHabitat(); String cname = "org.glassfish.api.admin.AdminCommand"; for (ActiveDescriptor<?> ad : h.getDescriptors(BuilderHelper.createContractFilter(cname))) { if (ad.getName() != null && ad.getName().equals(cmdName)) { return ad.getImplementation(); } } return null; }
configurator.bind(BuilderHelper.link(ConfigSupport.class) .in(Singleton.class.getName()) .build()); configurator.bind(BuilderHelper.link(Transactions.class) .in(Singleton.class.getName()) .build()); configurator.bind(BuilderHelper.link(SimpleConfigBeanDomDecorator.class) .to(DomDecorator.class).in(Singleton.class.getName()) .build()); configurator.bind(BuilderHelper.link(ConfigurationPopulator.class) .in(Singleton.class.getName()) .build()); configurator.bind(BuilderHelper.link(DummyPopulator.class) .to(Populator.class).in(Singleton.class.getName()) .build());
void removeInstances(ServiceLocator serviceLocator, ConcurrentLinkedDeque<Instance<?>> instances) { instances.forEach(instance -> { String name = instance.getName(); Class contract = instance.getContract(); Class instanceType = instance.getClass(); IndexedFilter filter = BuilderHelper.createContractFilter(instanceType .getTypeName()); removeFilter(serviceLocator, filter, true); if (name != null && contract != null) { removeFilter(serviceLocator, createNameAndContractFilter(contract.getName(), name), true); removeFilter(serviceLocator, createNameFilter(name), true); removeFilter(serviceLocator, createContractFilter(contract.getName()), true); } else if (name != null) { removeFilter(serviceLocator, createNameFilter(name), true); } else if (contract != null) { removeFilter(serviceLocator, createContractFilter(contract.getName()), true); } }); }