/** * Tests that a service that fails in the constructor has the error passed to the error service */ @Test public void testServiceFailsInConstructor() { ServiceLocator locator = LocatorHelper.getServiceLocator( RecordingErrorService.class, ServiceFailsInConstructor.class); ActiveDescriptor<?> serviceDescriptor = locator.getBestDescriptor( BuilderHelper.createContractFilter(ServiceFailsInConstructor.class.getName())); Assert.assertNotNull(serviceDescriptor); try { locator.getService(ServiceFailsInConstructor.class); Assert.fail("Should have failed"); } catch (MultiException me) { Assert.assertTrue(me.getMessage().contains(ERROR_STRING)); } List<ErrorInformation> errors = locator.getService(RecordingErrorService.class).getAndClearErrors(); Assert.assertEquals(1, errors.size()); ErrorInformation ei = errors.get(0); Assert.assertEquals(ErrorType.SERVICE_CREATION_FAILURE, ei.getErrorType()); Assert.assertEquals(serviceDescriptor, ei.getDescriptor()); Assert.assertNull(ei.getInjectee()); Throwable associatedException = ei.getAssociatedException(); Assert.assertTrue(associatedException.getMessage().contains(ERROR_STRING)); }
@Override void complete(DynamicConfiguration configuration, HK2Loader defaultLoader) { if (this.loader == null) { this.loader = defaultLoader; } AbstractActiveDescriptor descriptor = BuilderHelper.createConstantDescriptor(service); descriptor.setName(name); descriptor.setLoader(this.loader); if (scope != null) { descriptor.setScope(scope.getName()); } if (ranked != null) { descriptor.setRanking(ranked); } for (String key : metadata.keySet()) { for (String value : metadata.get(key)) { descriptor.addMetadata(key, value); } } for (Annotation annotation : qualifiers) { descriptor.addQualifierAnnotation(annotation); } for (Type contract : contracts) { descriptor.addContractType(contract); } configuration.bind(descriptor); }
/** Tests that a third-party service that fails in dispose */ @Test public void testFailingInDisposeThirdPartyService() { ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class); AlwaysFailInDisposeActiveDescriptor thirdPartyDescriptor = new AlwaysFailInDisposeActiveDescriptor(); ServiceLocatorUtilities.addOneDescriptor(locator, thirdPartyDescriptor); ActiveDescriptor<?> serviceDescriptor = locator.getBestDescriptor( BuilderHelper.createContractFilter(SimpleService.class.getName())); Assert.assertNotNull(serviceDescriptor); ServiceHandle<SimpleService> handle = locator.getServiceHandle(SimpleService.class); Assert.assertNotNull(handle); Assert.assertNotNull(handle.getService()); handle.destroy(); List<ErrorInformation> errors = locator.getService(RecordingErrorService.class).getAndClearErrors(); Assert.assertEquals(1, errors.size()); ErrorInformation ei = errors.get(0); Assert.assertEquals(ErrorType.SERVICE_DESTRUCTION_FAILURE, ei.getErrorType()); Assert.assertEquals(serviceDescriptor, ei.getDescriptor()); Assert.assertNull(ei.getInjectee()); Throwable associatedException = ei.getAssociatedException(); Assert.assertTrue(associatedException.getMessage().contains(ERROR_STRING)); }
@Override void complete(final DynamicConfiguration configuration, final HK2Loader defaultLoader) { if (this.loader == null) { this.loader = defaultLoader; } ActiveDescriptorBuilder builder = BuilderHelper.activeLink(service).named(name).andLoadWith(this.loader); if (scope != null) { builder.in(scope); } if (ranked != null) { builder.ofRank(ranked); } for (String key : metadata.keySet()) { for (String value : metadata.get(key)) { builder.has(key, value); } } for (Annotation annotation : qualifiers) { builder.qualifiedBy(annotation); } for (Type contract : contracts) { builder.to(contract); } configuration.bind(builder.build()); }
/** Tests that adding the whole annotation adds the class and string */ @Test public void testDescWithAnnotationGivenDirectly() { AbstractActiveDescriptor<?> desc = BuilderHelper.activeLink(ServiceA.class) .in(ServiceLocatorUtilities.getSingletonAnnotation()) .build(); Assert.assertEquals( desc.getScopeAsAnnotation(), ServiceLocatorUtilities.getSingletonAnnotation()); Assert.assertEquals(desc.getScopeAnnotation(), Singleton.class); Assert.assertEquals(desc.getScope(), Singleton.class.getName()); }
@Override void complete(DynamicConfiguration configuration, HK2Loader defaultLoader) { if (this.loader == null) { this.loader = defaultLoader; } ActiveDescriptorBuilder factoryDescriptorBuilder = BuilderHelper.activeLink(factoryClass).named(name).andLoadWith(this.loader); if (factoryScope != null) { factoryDescriptorBuilder.in(factoryScope); } ActiveDescriptorBuilder descriptorBuilder = BuilderHelper.activeLink(factoryClass).named(name).andLoadWith(this.loader); if (scope != null) { descriptorBuilder.in(scope); } if (ranked != null) { // factoryContractDescriptor.ofRank(factoryRank); descriptorBuilder.ofRank(ranked); } for (Annotation qualifier : qualifiers) { factoryDescriptorBuilder.qualifiedBy(qualifier); descriptorBuilder.qualifiedBy(qualifier); } for (Type contract : contracts) { factoryDescriptorBuilder.to(new ParameterizedTypeImpl(Factory.class, contract)); descriptorBuilder.to(contract); } configuration.bind( new FactoryDescriptorsImpl( factoryDescriptorBuilder.build(), descriptorBuilder.buildFactory())); }
@Override void complete(DynamicConfiguration configuration, HK2Loader defaultLoader) { if (this.loader == null) { this.loader = defaultLoader; } AbstractActiveDescriptor factoryContractDescriptor = BuilderHelper.createConstantDescriptor(factory); factoryContractDescriptor.addContractType(factory.getClass()); factoryContractDescriptor.setName(name); factoryContractDescriptor.setLoader(this.loader); ActiveDescriptorBuilder descriptorBuilder = BuilderHelper.activeLink(factory.getClass()).named(name).andLoadWith(this.loader); if (scope != null) { descriptorBuilder.in(scope); } if (ranked != null) { descriptorBuilder.ofRank(ranked); } for (Annotation qualifier : qualifiers) { factoryContractDescriptor.addQualifierAnnotation(qualifier); descriptorBuilder.qualifiedBy(qualifier); } for (Type contract : contracts) { factoryContractDescriptor.addContractType( new ParameterizedTypeImpl(Factory.class, contract)); descriptorBuilder.to(contract); } configuration.bind( new FactoryDescriptorsImpl(factoryContractDescriptor, descriptorBuilder.buildFactory())); }
public static void initRecovery(boolean force) { if (_logger.isLoggable(Level.FINE)) { _logger.log(Level.FINE, "initRecovery:recoveryInitialized: " + recoveryInitialized); } if (recoveryInitialized) { // Only start initial recovery if it wasn't started before return; } if (_logger.isLoggable(Level.FINE)) { _logger.log(Level.FINE, "initRecovery:properties: " + properties); } if (properties == null) { if (force) { _logger.log(Level.WARNING, "", new IllegalStateException()); } return; } // Start if force is true or automatic-recovery is set String value = properties.getProperty(Configuration.MANUAL_RECOVERY); if (_logger.isLoggable(Level.FINE)) { _logger.log(Level.FINE, "initRecovery:Configuration.MANUAL_RECOVERY: " + value); } if (force || (isValueSet(value) && "true".equals(value))) { recoveryInitialized = true; ServiceLocator serviceLocator = (ServiceLocator) properties.get(HABITAT); if (serviceLocator != null) { ProcessEnvironment processEnv = serviceLocator.getService(ProcessEnvironment.class); if (processEnv.getProcessType().isServer()) { // Start ResourceManager if it hadn't started yet serviceLocator.getAllServices(BuilderHelper.createNameFilter("ResourceManager")); value = properties.getProperty("pending-txn-cleanup-interval"); int interval = -1; if (isValueSet(value)) { interval = Integer.parseInt(value); } new RecoveryHelperThread(serviceLocator, interval).start(); } // Release all locks RecoveryManager.startResyncThread(); if (_logger.isLoggable(Level.FINE)) _logger.log(Level.FINE, "[JTS] Started ResyncThread"); } } }
/** Ensures we can add a scope to a service with no scope at all */ @Test public void testGiveClassWithNoScopeAScope() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.enableLookupExceptions(locator); Descriptor desc = BuilderHelper.activeLink(NoScopeService.class) .to(NoScopeService.class) .in(ServiceLocatorUtilities.getSingletonAnnotation()) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); NoScopeService one = locator.getService(NoScopeService.class); NoScopeService two = locator.getService(NoScopeService.class); Assert.assertNotNull(one); Assert.assertEquals(one, two); }
/** * Ensures that you can change from one hard-coded scope on the descriptor to a different scope */ @Test public void testSwitchFromExplicitScopeToGhostedScope() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.enableLookupExceptions(locator); ServiceLocatorUtilities.addClasses(locator, GhostedContext.class); Descriptor desc = BuilderHelper.activeLink(SingletonScopedService.class) .to(SingletonScopedService.class) .in(new GhostedScopeImpl(0)) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); SingletonScopedService one = locator.getService(SingletonScopedService.class); SingletonScopedService two = locator.getService(SingletonScopedService.class); Assert.assertNotNull(one); Assert.assertNotSame(one, two); }
/** * Tests that a service that fails but tells HK2 to NOT report the failure to the error handler * service */ @Test public void testSilentFailureInPostConstruct() { ServiceLocator locator = LocatorHelper.getServiceLocator( RecordingErrorService.class, ServiceDirectsNoErrorService.class); ActiveDescriptor<?> serviceDescriptor = locator.getBestDescriptor( BuilderHelper.createContractFilter(ServiceDirectsNoErrorService.class.getName())); Assert.assertNotNull(serviceDescriptor); try { locator.getService(ServiceDirectsNoErrorService.class); Assert.fail("Should have failed"); } catch (MultiException me) { Assert.assertTrue(me.getMessage().contains(ERROR_STRING)); } List<ErrorInformation> errors = locator.getService(RecordingErrorService.class).getAndClearErrors(); Assert.assertEquals(0, errors.size()); }
/** Tests a simple and mostly empty descriptor */ @Test public void testOnlyImpl() { AbstractActiveDescriptor<?> desc = BuilderHelper.activeLink(ServiceA.class).build(); Assert.assertSame(ServiceA.class, desc.getImplementationClass()); Assert.assertSame(ServiceA.class.getName(), desc.getImplementation()); Assert.assertNull(desc.getName()); Assert.assertEquals(PerLookup.class, desc.getScopeAnnotation()); Assert.assertEquals(PerLookup.class.getName(), desc.getScope()); Assert.assertTrue(desc.getAdvertisedContracts().isEmpty()); Assert.assertTrue(desc.getContractTypes().isEmpty()); Assert.assertTrue(desc.getQualifiers().isEmpty()); Assert.assertTrue(desc.getQualifierAnnotations().isEmpty()); Assert.assertNull(desc.getLoader()); Assert.assertSame(DescriptorType.CLASS, desc.getDescriptorType()); Assert.assertTrue(desc.getInjectees().isEmpty()); Assert.assertFalse(desc.isReified()); }
/** Tests that we can change the value of a field in a scope with a ghost added annotation */ @Test public void testModifyExistingScopeWithDifferentValue() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.addClasses(locator, GhostedContext.class); Descriptor desc = BuilderHelper.activeLink(GhostedServiceWithValue.class) .to(GhostedServiceWithValue.class) .in(new GhostedScopeImpl(0)) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); GhostedServiceWithValue ghosted = locator.getService(GhostedServiceWithValue.class); Assert.assertNotNull(ghosted); ActiveDescriptor<?> gDesck = ghosted.getDescriptor(); Annotation anno = gDesck.getScopeAsAnnotation(); Assert.assertNotNull(anno); GhostedScope gs = (GhostedScope) anno; Assert.assertEquals(0, gs.value()); }
@Test public void testDescWithFields() { SimpleQualifier1 sq1 = new SimpleQualifier1Impl(); AbstractActiveDescriptor<?> desc = BuilderHelper.activeLink(ServiceA.class) .to(SimpleInterface1.class) .in(Singleton.class) .qualifiedBy(sq1) .named(NAME) .has(NAME, NAME) .ofRank(1) .proxy() .proxyForSameScope(false) .localOnly() .andLoadWith( new HK2Loader() { @Override public Class<?> loadClass(String className) throws MultiException { throw new AssertionError("not called"); } }) .build(); Assert.assertSame(ServiceA.class, desc.getImplementationClass()); Assert.assertSame(ServiceA.class.getName(), desc.getImplementation()); Assert.assertSame(NAME, desc.getName()); Assert.assertEquals(Singleton.class, desc.getScopeAnnotation()); Assert.assertEquals(Singleton.class.getName(), desc.getScope()); Assert.assertEquals(Boolean.TRUE, desc.isProxiable()); Assert.assertEquals(Boolean.FALSE, desc.isProxyForSameScope()); Assert.assertEquals(DescriptorVisibility.LOCAL, desc.getDescriptorVisibility()); testSetOfOne(desc.getAdvertisedContracts(), SimpleInterface1.class.getName()); testSetOfOne(desc.getContractTypes(), SimpleInterface1.class); boolean foundSQ1 = false; boolean foundName = false; for (Annotation anno : desc.getQualifierAnnotations()) { if (anno.annotationType().equals(SimpleQualifier1.class)) { foundSQ1 = true; } else if (anno.annotationType().equals(Named.class)) { String annoName = ((Named) anno).value(); Assert.assertSame(annoName, NAME); foundName = true; } else { Assert.fail("Unknown annotation found " + anno); } } Assert.assertTrue(foundName); Assert.assertTrue(foundSQ1); foundSQ1 = false; foundName = false; for (String anno : desc.getQualifiers()) { if (anno.equals(SimpleQualifier1.class.getName())) { foundSQ1 = true; } else if (anno.equals(Named.class.getName())) { foundName = true; } else { Assert.fail("Unknown annotation found " + anno); } } Assert.assertTrue(foundName); Assert.assertTrue(foundSQ1); Assert.assertNotNull(desc.getLoader()); Assert.assertSame(DescriptorType.CLASS, desc.getDescriptorType()); Assert.assertTrue(desc.getInjectees().isEmpty()); Assert.assertFalse(desc.isReified()); }