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;
}
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);
}
}
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;
}
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);
}
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();
}
Aggregations