use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.
the class ServiceInjectionProviderFactory method newInjectionProvider.
@Override
@SuppressWarnings("unchecked")
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
// TODO This could be changed to allow multiple @Qualifier annotations
Annotation qualifierAnnotation = first(filter(Specifications.translate(Annotations.type(), hasAnnotation(Qualifier.class)), iterable(dependencyModel.annotations())));
Specification<ServiceReference<?>> serviceQualifier = null;
if (qualifierAnnotation != null) {
Qualifier qualifier = qualifierAnnotation.annotationType().getAnnotation(Qualifier.class);
try {
serviceQualifier = qualifier.value().newInstance().qualifier(qualifierAnnotation);
} catch (Exception e) {
throw new InvalidInjectionException("Could not instantiate qualifier serviceQualifier", e);
}
}
if (dependencyModel.rawInjectionType().equals(Iterable.class)) {
Type iterableType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
if (Classes.RAW_CLASS.map(iterableType).equals(ServiceReference.class)) {
// @Service Iterable<ServiceReference<MyService<Foo>> serviceRefs
Type serviceType = ((ParameterizedType) iterableType).getActualTypeArguments()[0];
return new IterableServiceReferenceProvider(serviceType, serviceQualifier);
} else {
// @Service Iterable<MyService<Foo>> services
return new IterableServiceProvider(iterableType, serviceQualifier);
}
} else if (dependencyModel.rawInjectionType().equals(ServiceReference.class)) {
// @Service ServiceReference<MyService<Foo>> serviceRef
Type referencedType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
return new ServiceReferenceProvider(referencedType, serviceQualifier);
} else {
// @Service MyService<Foo> service
return new ServiceProvider(dependencyModel.injectionType(), serviceQualifier);
}
}
use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.
the class ActivationDelegate method activate.
@SuppressWarnings("unchecked")
public void activate(ActivatorsInstance targetActivators, Iterable<? extends Activation> children, Runnable callback) throws ActivationException {
if (this.targetActivators != null) {
throw new IllegalStateException("Activation.activate() called multiple times " + "or without calling passivate() first!");
}
try {
// Before Activation Events
if (fireEvents) {
fireEvent(new ActivationEvent(target, ACTIVATING));
}
// Before Activation for Activators
targetActivators.beforeActivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
// Activation
for (Activation child : children) {
if (!activeChildren.contains(child)) {
child.activate();
}
activeChildren.addFirst(child);
}
// Internal Activation Callback
if (callback != null) {
callback.run();
}
// After Activation
targetActivators.afterActivation(target);
// After Activation Events
if (fireEvents) {
fireEvent(new ActivationEvent(target, ACTIVATED));
}
// Activated
this.targetActivators = targetActivators;
} catch (Exception e) {
// Passivate actives
try {
passivate();
} catch (PassivationException e1) {
ActivationException activationEx = new ActivationException("Unable to Activate application.", e);
activationEx.addSuppressed(e1);
throw activationEx;
}
if (e instanceof ActivationException) {
throw ((ActivationException) e);
}
throw new ActivationException("Unable to Activate application.", e);
}
}
use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.
the class PassivationTest method givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled.
@Test
public void givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled() throws Exception {
SingletonAssembler assembly = new SingletonAssembler() {
public void assemble(ModuleAssembly module) throws AssemblyException {
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
}
};
ArrayList<Data> datas = new ArrayList<Data>();
Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices(DataAccess.class);
for (ServiceReference<DataAccess> service : iterable) {
assertTrue("Service should not be Active before accessed", !service.isActive());
Data data = service.get().data();
if (DataAccessService.class.isInstance(service.get())) {
// Collect the expected successes.
datas.add(data);
}
assertTrue("Data should indicate that the service is activated", data.activated);
assertTrue("Service should be Active after access.", service.isActive());
}
try {
assembly.application().passivate();
fail("PassivationException should have been thrown.");
} catch (PassivationException e) {
// Expected
}
// Still ensure that all services have been shutdown.
for (ServiceReference<DataAccess> service : iterable) {
assertFalse("All services should have been shutdown", service.isActive());
}
}
use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.
the class PassivationTest method givenMultipleFailingPassivationWhenPassivatingExpectPassivationExceptionToBubbleUp.
@Test(expected = PassivationException.class)
public void givenMultipleFailingPassivationWhenPassivatingExpectPassivationExceptionToBubbleUp() throws Exception {
SingletonAssembler assembly = new SingletonAssembler() {
public void assemble(ModuleAssembly module) throws AssemblyException {
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
}
};
Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices(DataAccess.class);
for (ServiceReference<DataAccess> service : iterable) {
assertTrue("Service should not be Active before accessed", !service.isActive());
assertTrue(service.get().data().activated);
assertTrue("Service should be Active after access.", service.isActive());
}
assembly.application().passivate();
}
use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.
the class JettyServiceTest method testInstantiation.
@Test
public final void testInstantiation() throws Throwable {
Iterable<ServiceReference<JettyService>> services = module.findServices(JettyService.class);
assertNotNull(services);
Iterator<ServiceReference<JettyService>> iterator = services.iterator();
assertTrue(iterator.hasNext());
ServiceReference<JettyService> serviceRef = iterator.next();
assertNotNull(serviceRef);
JettyService jettyService = serviceRef.get();
assertNotNull(jettyService);
String output = defaultHttpClient.execute(new HttpGet("http://127.0.0.1:8041/helloWorld"), stringResponseHandler);
assertEquals("Hello World", output);
}
Aggregations