private boolean specIsIn(ServiceSpecification spec, Collection<Service> servicePool) { for (Service s : servicePool) { if (s.getServiceSpecification().matches(spec)) { return true; } } return false; }
/** * activate all collected services * * @param services the services to activate * @param serviceContext the context to activate them with. */ protected void activate(Collection<Service> allServices, final ServiceContext serviceContext) { List<Service> services = new LinkedList<Service>(allServices); // add the services already provided by the bootstrap with null // activator. ServiceActivator doNothingActivator = new DoNothingServiceActivator(); services.add( ServiceBuilder.forContract(BootstrapService.class) .activatedBy(doNothingActivator) .newInstance()); services.add( ServiceBuilder.forContract(LoggingService.class) .activatedBy(doNothingActivator) .newInstance()); services.add( ServiceBuilder.forContract(FileContextService.class) .activatedBy(doNothingActivator) .newInstance()); services.add( ServiceBuilder.forContract(LocalizationService.class) .activatedBy(doNothingActivator) .newInstance()); services.add( ServiceBuilder.forContract(WiringService.class) .activatedBy(doNothingActivator) .newInstance()); Map<ServiceSpecification, Service> servicesMap = new HashMap<ServiceSpecification, Service>(); Map<Service, DependencyGraph> serviceGraphMapping = new HashMap<Service, DependencyGraph>(); // add all services specifications targets to map and inicialize the seed graphs for (Service s : services) { servicesMap.put(s.getServiceSpecification(), s); if (s.getDependencies().isEmpty()) { // is seed serviceGraphMapping.put(s, new DependencyGraph()); } } // treat possible all optional not present // // for (Service s : services) { // // final Collection<ServiceSpecification> optionalDependencies = s.getOptionalDependencies(); // if (optionalDependencies.size() == s.getDependencies().size()){ // serviceGraphMapping.put(s, new DependencyGraph()); // } // } // DependencyInicilizer ini = new ServiceDependencyInicilizer(serviceContext); // Create DependencyGraph for (Service s : services) { for (ServiceSpecification dependencySpecification : s.getDependencies()) { Service dependencyService = servicesMap.get(dependencySpecification); DependencyGraph targetGraph = serviceGraphMapping.get(dependencyService); DependencyGraph serviceGraph = serviceGraphMapping.get(s); final DependencyGraphNode dependency = new DependencyGraphNode(dependencyService, ini); if (targetGraph == null) { for (DependencyGraph g : serviceGraphMapping.values()) { for (Graph.Vertex<DependencyGraphNode, DependencyGraphEdge> vertex : g.getVertices()) { if (vertex.getObject().equals(dependency)) { // this is the graph if (serviceGraph != null && !serviceGraph.equals(g)) { // need merge merge( serviceGraphMapping, ini, s, dependencySpecification, dependencyService, targetGraph, serviceGraph, dependency); } else { g.addDependency( dependency, new DependencyGraphNode(s, ini), !dependencySpecification.isOptional()); serviceGraphMapping.put(s, g); } } } } } else { if (serviceGraph != null && !serviceGraph.equals(targetGraph)) { // need merge merge( serviceGraphMapping, ini, s, dependencySpecification, dependencyService, targetGraph, serviceGraph, dependency); } else { targetGraph.addDependency( dependency, new DependencyGraphNode(s, ini), !dependencySpecification.isOptional()); serviceGraphMapping.put(s, targetGraph); } } } } for (Map.Entry<Service, DependencyGraph> entry : serviceGraphMapping.entrySet()) { DependencyGraph graph = entry.getValue(); if (graph.isEmpty()) { // there is no vertice in the graph so initilize the mapped service directly ini.inicialize(new DependencyGraphNode(entry.getKey(), ini)); } else { graph.resolve(); } } for (Iterator<Service> it = services.iterator(); it.hasNext(); ) { final Service service = it.next(); if (!service.isActivated()) { throw new IllegalStateException("Service " + service.getName() + " was not initialized."); } } }