Search in sources :

Example 1 with ServiceReference

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);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) Qualifier(org.qi4j.api.service.qualifier.Qualifier) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) Annotation(java.lang.annotation.Annotation) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 2 with ServiceReference

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);
    }
}
Also used : ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Activation(org.qi4j.api.activation.Activation) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 3 with ServiceReference

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());
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) PassivationException(org.qi4j.api.activation.PassivationException) ArrayList(java.util.ArrayList) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Example 4 with ServiceReference

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();
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Example 5 with ServiceReference

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);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceReference (org.qi4j.api.service.ServiceReference)15 Test (org.junit.Test)7 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)4 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)4 PassivationException (org.qi4j.api.activation.PassivationException)3 Module (org.qi4j.api.structure.Module)3 ArrayList (java.util.ArrayList)2 JSONObject (org.json.JSONObject)2 ActivationEvent (org.qi4j.api.activation.ActivationEvent)2 ActivationException (org.qi4j.api.activation.ActivationException)2 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 LinkedHashSet (java.util.LinkedHashSet)1 ServletException (javax.servlet.ServletException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Source (javax.xml.transform.Source)1