Search in sources :

Example 6 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class ServiceLocator method locateService.

@SuppressWarnings("unchecked")
ServiceReference locateService(Application anApplication) {
    if (layerName != null) {
        Module module = anApplication.findModule(layerName, moduleName);
        Iterable<ServiceReference<Object>> serviceRefs = module.findServices(serviceType);
        for (ServiceReference<Object> serviceRef : serviceRefs) {
            if (serviceId.equals(serviceRef.identity())) {
                return serviceRef;
            }
        }
    }
    return null;
}
Also used : Module(org.qi4j.api.structure.Module) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 7 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class QuikitServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    try {
        mountPoints = new TreeMap<String, Page>();
        documentFactory = DocumentBuilderFactory.newInstance();
        documentFactory.setNamespaceAware(true);
        ClassLoader cl = getClass().getClassLoader();
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Source[] schemaSources = new Source[2];
        schemaSources[0] = new StreamSource(cl.getResourceAsStream("xhtml1-strict.xsd"));
        schemaSources[1] = new StreamSource(cl.getResourceAsStream("xml.xsd"));
        Schema schema = schemaFactory.newSchema(schemaSources);
        documentFactory.setSchema(schema);
        ApplicationAssembler assembler = createApplicationAssembler(config);
        Energy4Java qi4j = new Energy4Java();
        application = qi4j.newApplication(assembler);
        application.activate();
        Module module = application.findModule("WebLayer", "PagesModule");
        finder = module;
        if (application.mode() == Application.Mode.development) {
            DataInitializer initializer = module.newTransient(DataInitializer.class);
            initializer.initialize();
        }
        Iterable<ServiceReference<Page>> iterable = finder.findServices(Page.class);
        for (ServiceReference<Page> page : iterable) {
            PageMetaInfo pageMetaInfo = page.metaInfo(PageMetaInfo.class);
            String mountPoint = pageMetaInfo.mountPoint();
            mountPoints.put(mountPoint, page.get());
        }
    } catch (Exception e) {
        throw new ServletException("Can not initialize Qi4j.", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ServiceReference(org.qi4j.api.service.ServiceReference) ServletException(javax.servlet.ServletException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Module(org.qi4j.api.structure.Module)

Example 8 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class ServiceSelectorImporter method importService.

@Override
@SuppressWarnings({ "raw", "unchecked" })
public T importService(ImportedServiceDescriptor serviceDescriptor) throws ServiceImporterException {
    Specification<ServiceReference<?>> selector = serviceDescriptor.metaInfo(Specification.class);
    Class serviceType = Iterables.first(serviceDescriptor.types());
    Iterable<ServiceReference<T>> services = locator.findServices(serviceType);
    List<ServiceReference<T>> filteredServices = new ArrayList<>();
    for (ServiceReference<T> service : services) {
        Specification selector1 = service.metaInfo(Specification.class);
        if (selector1 != null && selector1 == selector) {
            continue;
        }
        filteredServices.add(service);
    }
    T service = ServiceQualifier.firstService(selector, filteredServices);
    if (service == null) {
        throw new ServiceImporterException("Could not find any service to import that matches the given specification for " + serviceDescriptor.identity());
    }
    return service;
}
Also used : ArrayList(java.util.ArrayList) ServiceImporterException(org.qi4j.api.service.ServiceImporterException) Specification(org.qi4j.functional.Specification) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 9 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class ActivationDelegate method passivate.

@SuppressWarnings("unchecked")
public void passivate(Runnable callback) throws PassivationException {
    Set<Exception> exceptions = new LinkedHashSet<>();
    // Before Passivation Events
    if (fireEvents) {
        ActivationEvent event = new ActivationEvent(target, PASSIVATING);
        for (ActivationEventListener listener : listeners) {
            try {
                listener.onEvent(event);
            } catch (Exception ex) {
                if (ex instanceof PassivationException) {
                    exceptions.addAll(((PassivationException) ex).causes());
                } else {
                    exceptions.add(ex);
                }
            }
        }
    }
    // Before Passivation for Activators
    if (targetActivators != null) {
        try {
            targetActivators.beforePassivation(target);
        } catch (PassivationException ex) {
            exceptions.addAll(ex.causes());
        } catch (Exception ex) {
            exceptions.add(ex);
        }
    }
    // Passivation
    while (!activeChildren.isEmpty()) {
        passivateOneChild(exceptions);
    }
    // Internal Passivation Callback
    if (callback != null) {
        try {
            callback.run();
        } catch (Exception ex) {
            if (ex instanceof PassivationException) {
                exceptions.addAll(((PassivationException) ex).causes());
            } else {
                exceptions.add(ex);
            }
        }
    }
    // After Passivation for Activators
    if (targetActivators != null) {
        try {
            targetActivators.afterPassivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
        } catch (PassivationException ex) {
            exceptions.addAll(ex.causes());
        } catch (Exception ex) {
            exceptions.add(ex);
        }
    }
    targetActivators = null;
    // After Passivation Events
    if (fireEvents) {
        ActivationEvent event = new ActivationEvent(target, PASSIVATED);
        for (ActivationEventListener listener : listeners) {
            try {
                listener.onEvent(event);
            } catch (Exception ex) {
                if (ex instanceof PassivationException) {
                    exceptions.addAll(((PassivationException) ex).causes());
                } else {
                    exceptions.add(ex);
                }
            }
        }
    }
    // Error handling
    if (exceptions.isEmpty()) {
        return;
    }
    throw new PassivationException(exceptions);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ActivationEvent(org.qi4j.api.activation.ActivationEvent) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 10 with ServiceReference

use of org.qi4j.api.service.ServiceReference in project qi4j-sdk by Qi4j.

the class ImportedServicesInstance method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("Services{");
    String sep = " ";
    for (ServiceReference serviceReference : serviceReferences) {
        sb.append(sep).append(serviceReference.identity()).append("(active=").append(serviceReference.isActive()).append(")");
        sep = ", ";
    }
    return sb.append(" }").toString();
}
Also used : ServiceReference(org.qi4j.api.service.ServiceReference)

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