Refine search
@Override public ForeignDescriptor createForeignDescriptor(Binding binding) { ForeignDescriptor foreignDescriptor = createAndTranslateForeignDescriptor(binding); ActiveDescriptor<Object> activeDescriptor = ServiceLocatorUtilities .addOneDescriptor(getServiceLocator(), (Descriptor) foreignDescriptor.get(), false); return ForeignDescriptor.wrap(activeDescriptor, activeDescriptor::dispose); }
public ActorBaseTest() { ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance(); serviceLocator = factory.create(UUID.randomUUID().toString()); ServiceLocatorUtilities.addOneConstant(serviceLocator, fakeSync); }
/** * Private constructor. * * @param parent parent of type {@link org.glassfish.jersey.internal.inject.InjectionManager} or {@link ServiceLocator}. */ AbstractHk2InjectionManager(Object parent) { ServiceLocator parentLocator = resolveServiceLocatorParent(parent); this.locator = createLocator(parentLocator); // Register all components needed for proper HK2 locator bootstrap ServiceLocatorUtilities.bind(locator, new Hk2BootstrapBinder(locator)); this.locator.setDefaultClassAnalyzerName(JerseyClassAnalyzer.NAME); // clear HK2 caches ServiceLocatorRuntimeBean serviceLocatorRuntimeBean = locator.getService(ServiceLocatorRuntimeBean.class); if (serviceLocatorRuntimeBean != null) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine(LocalizationMessages.HK_2_CLEARING_CACHE(serviceLocatorRuntimeBean.getServiceCacheSize(), serviceLocatorRuntimeBean.getReflectionCacheSize())); } serviceLocatorRuntimeBean.clearReflectionCache(); serviceLocatorRuntimeBean.clearServiceCache(); } }
private ActiveDescriptor<Dom> addWithAlias(ServiceLocator locator, AbstractActiveDescriptor<?> descriptor, Class<?> contract, String name) { ActiveDescriptor<Dom> added = ServiceLocatorUtilities.findOneDescriptor(locator, descriptor); if (added == null) { if (ConfigBean.class.isAssignableFrom(this.getClass())) { if (!descriptor.getAdvertisedContracts().contains(ConfigBean.class.getName())) { descriptor.addContractType(ConfigBean.class); } } added = ServiceLocatorUtilities.addOneDescriptor(locator, descriptor); } AliasDescriptor<Dom> alias = new AliasDescriptor<Dom>(locator, added, contract.getName(), name); ServiceLocatorUtilities.addOneDescriptor(locator, alias); return added; }
/** * This method will add the Hub service to the given service locator. * If there is already a Hub service available this method will do * nothing * * @param locator The non-null ServiceLocator to add the configuration * Hub service to */ public static void enableConfigurationHub(ServiceLocator locator) { if (locator.getService(Hub.class) != null) { return; } ServiceLocatorUtilities.addClasses(locator, true, HubImpl.class); }
private void prepareHabitat() { ModulesRegistry registry = new StaticModulesRegistry(getClass().getClassLoader()); ServiceLocator serviceLocator = registry.createServiceLocator("default"); habitat = serviceLocator.getService(ServiceLocator.class); StartupContext startupContext = new StartupContext(); ServiceLocatorUtilities.addOneConstant(habitat, startupContext); ServiceLocatorUtilities.addOneConstant(habitat, new ProcessEnvironment(ProcessEnvironment.ProcessType.Other)); }
public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.runOnContext(aVoid -> { // Set up the jersey configuration // The minimum config required is a package to inspect for JAX-RS endpoints vertx.getOrCreateContext().config() .put("jersey", new JsonObject() .put("port", 8080) .put("packages", new JsonArray() .add(HelloWorldEndpoint.class.getPackage().getName()))); // Use a service locator (HK2 or Guice are supported by default) to create the jersey server ServiceLocator locator = ServiceLocatorUtilities.bind(new HK2JerseyBinder(), new HK2VertxBinder(vertx)); JerseyServer server = locator.getService(JerseyServer.class); // Start the server which simply returns "Hello World!" to each GET request. server.start(); }); }
/** * This method will create or find a ServiceLocator with the given name and * bind all of the binders given together in a single config transaction. * * @param name The non-null name of the locator to use for the configuration action * @param binders The non-null list of binders to be added to the locator * @return The service locator that was either found or created * @throws MultiException if any error was encountered while binding services */ public static ServiceLocator bind(String name, Binder... binders) { ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance(); ServiceLocator locator = factory.create(name); bind(locator, binders); return locator; }
/** * This method will add the ability to use the {@link Immediate} scope to * the given locator. If the locator already has a {@link Context} implementation * that handles the {@link Immediate} scope this method does nothing. The Immediate * scope will start in the suspended state, allowing the caller to customize the * Immediate scope using the {@link ImmediateController} * <p> * * @param locator The non-null locator to enable the Immediate scope on * @return The ImmediateController that can be used to further configure * the Immediate scope * @throws MultiException if there were errors when committing the service */ public static ImmediateController enableImmediateScopeSuspended(ServiceLocator locator) { List<ServiceHandle<?>> immediateContexts = locator.getAllServiceHandles((new TypeLiteral<Context<Immediate>>() {}).getType()); for (ServiceHandle<?> immediateContext : immediateContexts) { ActiveDescriptor<?> contextDescriptor = immediateContext.getActiveDescriptor(); if (contextDescriptor.getLocatorId() == locator.getLocatorId()) { return locator.getService(ImmediateController.class); } } addClasses(locator, ImmediateContext.class, ImmediateHelper.class); return locator.getService(ImmediateController.class); }
/** * This method will add the ability to use the {@link Immediate} scope to * the given locator. If the locator already has a {@link Context} implementation * that handles the {@link Immediate} scope this method does nothing. The Immediate * scope will start in the suspended state, allowing the caller to customize the * Immediate scope using the {@link ImmediateController} * <p> * * @param locator The non-null locator to enable the Immediate scope on * @return The ImmediateController that can be used to further configure * the Immediate scope * @throws MultiException if there were errors when committing the service */ public static ImmediateController enableImmediateScopeSuspended(ServiceLocator locator) { try { addClasses(locator, true, ImmediateContext.class, ImmediateHelper.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } return locator.getService(ImmediateController.class); }
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()). ServiceLocatorUtilities.addOneDescriptor(locator, injectorDescriptor);
/** * 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); }
/** * Ensures that even the non-standard format of metadata from the hk2-config subsystem can * be read from the service in addClasses. addClasses will now read both forms, if the * documented form fails, it'll try the hk2-config form */ @Test public void testAddClassOfInjector() { ServiceLocator locator = ServiceLocatorFactory.getInstance().create(null); List<ActiveDescriptor<?>> added = ServiceLocatorUtilities.addClasses(locator, EjbContainerAvailabilityInjector.class); ActiveDescriptor<?> descriptor = added.get(0); Assert.assertEquals("org.jvnet.hk2.config.test.EjbContainerAvailability", ServiceLocatorUtilities.getOneMetadataField(descriptor, "target")); }
locator = ServiceLocatorFactory.getInstance().create(null, parent); ServiceLocatorUtilities.bind(locator, bootstraps.toArray(new Binder[bootstraps.size()])); return (Verticle) locator.createAndInitialize(clazz);
/** * Returns the service in this service locator given the current descriptor. * * @param locator The non-null locator in which to get the service associated with * this descriptor * @param descriptor The non-null descriptor to find the corresponding service for * @return The service object */ @SuppressWarnings("unchecked") public static <T> T getService(ServiceLocator locator, Descriptor descriptor) { if (locator == null || descriptor == null) throw new IllegalArgumentException(); Long locatorId = descriptor.getLocatorId(); if (locatorId != null && (locatorId.longValue() == locator.getLocatorId()) && (descriptor instanceof ActiveDescriptor)) { return locator.getServiceHandle((ActiveDescriptor<T>) descriptor).getService(); } ActiveDescriptor<T> found = findOneDescriptor(locator, descriptor); if (found == null) return null; return locator.getServiceHandle(found).getService(); }
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); } }); }
protected void injectMultiBindings(ServiceLocator locator, Injector injector, Key<? extends Set<?>> key, Type type) { Set<?> set = injector.getInstance(key); if (set != null && !set.isEmpty()) { for (Object obj : set) { ServiceLocatorUtilities.addOneConstant(locator, obj, null, type); } } }
@Test public void shouldGetAllServicesWithoutAnyExceptions() { ServiceLocator serviceLocator = ServiceLocatorUtilities.bind("serviceLocatorTestName1", new CommonBinder(), new ConsumersBinder(), new TestBinder(configFactory), new TrackersBinder() ); serviceLocator.getAllServices(d -> true); }
@Override public void completeRegistration() throws IllegalStateException { Hk2Helper.bind(this, bindings); ServiceLocatorUtilities.bind(getServiceLocator(), providers.toArray(new org.glassfish.hk2.utilities.Binder[]{})); completed = true; } }
AbstractActiveDescriptor<? extends Supplier<?>> supplierBuilder = BuilderHelper.createConstantDescriptor(supplier); binding.getContracts().forEach(contract -> { supplierBuilder.addContractType(new ParameterizedTypeImpl(Supplier.class, contract)); }; ServiceLocatorUtilities.bind(locator, createBinder(bindConsumer));