/** * 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")); }
/** * It is very often the case that one wishes to add classes that hk2 * will automatically analyze for contracts and qualifiers to * a service locator. This method adds those classes. * <p> * If the class to add implements {@link Factory} then two descriptors * will be added, one for the {@link Factory} class itself, and one for * the {@link Factory#provide()} method of the factory. In the output * list the descriptor for the {@link Factory} will be added first, followed * by the descriptor for the {@link Factory#provide()} method * * @param locator The non-null locator to add this descriptor to * @param toAdd The classes to add to the locator. If a class in this list implements * {@link Factory} then two descriptors will be added for that class * @return The list of descriptors added to the system. Will not return null but * may return an empty list * @throws MultiException On a commit failure */ public static List<ActiveDescriptor<?>> addClasses(ServiceLocator locator, Class<?>... toAdd) { return addClasses(locator, false, toAdd); }
@Override public boolean configure(FeatureContext context) { for (ClassInfo classInfo : classInfoSet) { ServiceLocatorUtilities.addClasses(locator, classInfo.toClass()); } classInfoSet.clear(); return true; } };
/** * 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); }
/** * This method will add the ability to use the {@link PerThread} scope to * the given locator. If the locator already has a {@link Context} implementation * that handles the {@link PerThread} scope this method does nothing. * * @param locator The non-null locator to enable the PerThread scope on * @throws MultiException if there were errors when committing the service */ public static void enablePerThreadScope(ServiceLocator locator) { try { addClasses(locator, true, PerThreadContext.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } }
/** * This method will cause lookup operations to throw exceptions when * exceptions are encountered in underlying operations such as * classloading. This method is idempotent. This method works * by adding {@link RethrowErrorService} to the service registry * <p> * Do not use this methods in secure applications where callers to lookup * should not be given the information that they do NOT have access * to a service * * @param locator The service locator to enable lookup exceptions on. May not be null */ public static void enableLookupExceptions(ServiceLocator locator) { if (locator == null) throw new IllegalArgumentException(); if (locator.getService(RethrowErrorService.class) != null) return; addClasses(locator, RethrowErrorService.class); }
/** * This method will cause lookup operations to throw exceptions when * exceptions are encountered in underlying operations such as * classloading. This method is idempotent. This method works * by adding {@link RethrowErrorService} to the service registry * <p> * Do not use this methods in secure applications where callers to lookup * should not be given the information that they do NOT have access * to a service * * @param locator The service locator to enable lookup exceptions on. May not be null */ public static void enableLookupExceptions(ServiceLocator locator) { if (locator == null) throw new IllegalArgumentException(); if (locator.getService(RethrowErrorService.class) != null) return; addClasses(locator, RethrowErrorService.class); }
/** * This method will cause lookup operations to throw exceptions when * exceptions are encountered in underlying operations such as * classloading. This method is idempotent. This method works * by adding {@link RethrowErrorService} to the service registry * <p> * Do not use this methods in secure applications where callers to lookup * should not be given the information that they do NOT have access * to a service * * @param locator The service locator to enable lookup exceptions on. May not be null */ public static void enableLookupExceptions(ServiceLocator locator) { if (locator == null) throw new IllegalArgumentException(); if (locator.getService(RethrowErrorService.class) != null) return; addClasses(locator, RethrowErrorService.class); }
/** * This method will add the ability to use the {@link InheritableThread} * scope to the given locator. If the locator already has a {@link Context} * implementation that handles the {@link InheritableThread} scope this * method does nothing. * * @param locator The non-null locator to enable the PerThread scope on * @throws MultiException if there were errors when committing the service */ public static void enableInheritableThreadScope(ServiceLocator locator) { try { addClasses(locator, true, InheritableThreadContext.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } }
/** * Enables greedy service resolution in this service locator by * adding the {@link GredyResolver} into the service locator. This * method is idempotent. * <p> * WARNING: Use of greedy resolution may cause classes that were not * intended to be instantiated by hk2 to be instantiated by hk2. Please * use this with care * * @param locator The locator to enable for greedy resolution */ public static void enableGreedyResolution(ServiceLocator locator) { if (locator == null) throw new IllegalArgumentException(); try { addClasses(locator, true, GreedyResolver.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } }
/** * This method will cause lookup operations to throw exceptions when * exceptions are encountered in underlying operations such as * classloading. This method is idempotent. This method works * by adding {@link RethrowErrorService} to the service registry * <p> * Do not use this methods in secure applications where callers to lookup * should not be given the information that they do NOT have access * to a service * * @param locator The service locator to enable lookup exceptions on. May not be null */ public static void enableLookupExceptions(ServiceLocator locator) { if (locator == null) throw new IllegalArgumentException(); try { addClasses(locator, true, RethrowErrorService.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } }
@Override public boolean justInTimeResolution(Injectee failedInjectionPoint) { Type type = failedInjectionPoint.getRequiredType(); if (type == null) return false; Class<?> clazzToAdd = null; if (type instanceof Class) { clazzToAdd = (Class<?>) type; } else if (type instanceof ParameterizedType) { Type rawType = ((ParameterizedType) type).getRawType(); if (rawType instanceof Class) { clazzToAdd = (Class<?>) rawType; } } if (clazzToAdd == null) return false; if (clazzToAdd.isInterface()) { GreedyDefaultImplementation gdi = clazzToAdd.getAnnotation(GreedyDefaultImplementation.class); if (gdi != null) { clazzToAdd = gdi.value(); } else { return false; } } ServiceLocatorUtilities.addClasses(locator, clazzToAdd); return true; }
/** * Enables the RunLevelService in the given {@link ServiceLocator}. * If the {@link RunLevelContext} is already registered then * this method does nothing. * <p> * All services needed by the * RunLevelService feature are marked with {@link org.jvnet.hk2.annotations.Service} and * hence would be automatically picked up in environments that * use automatic service discovery * * @param locator the non-null service locator to add * the run-level service to */ public static void enableRunLevelService(ServiceLocator locator) { if (locator.getService(RunLevelContext.class) != null) return; try { ServiceLocatorUtilities.addClasses(locator, true, RunLevelContext.class, AsyncRunLevelContext.class, RunLevelControllerImpl.class); } catch (MultiException me) { if (!isDupException(me)) throw me; } }
/** * 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); }
/** * 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")); }
/** * 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) { 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) { 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); }