private void merge(
      Map<Service, DependencyGraph> serviceGraphMapping,
      DependencyInicilizer inicializer,
      Service service,
      ServiceSpecification dependencySpecification,
      Service dependencyService,
      DependencyGraph targetGraph,
      DependencyGraph serviceGraph,
      final DependencyGraphNode dependency) {
    if (targetGraph.isEmpty()) {
      serviceGraph.addDependency(
          dependency,
          new DependencyGraphNode(service, inicializer),
          !dependencySpecification.isOptional());
      serviceGraphMapping.put(dependencyService, serviceGraph);

    } else {
      DependencyGraph mergedGraph = serviceGraph.merge(targetGraph);

      List<Service> keyServices = new LinkedList<Service>();

      for (Map.Entry<Service, DependencyGraph> entry : serviceGraphMapping.entrySet()) {
        if (entry.getValue().equals(targetGraph) || entry.getValue().equals(serviceGraph)) {
          keyServices.add(entry.getKey());
        }
      }

      serviceGraphMapping.values().remove(serviceGraph);
      serviceGraphMapping.values().remove(targetGraph);

      for (Service sk : keyServices) {
        serviceGraphMapping.put(sk, mergedGraph);
      }
    }
  }
    private void internalRequireService(
        ServiceSpecification spec, Collection<Service> servicePool, ServiceProvider provider) {

      if (specIsIn(spec, servicePool)) {
        return;
      }

      // check for native provider
      Service service = null;
      try {
        service = bootstrap.getContainer().provideService(spec);

      } catch (ServiceNotAvailableException e) {
        // log ?
      }

      if (service == null && provider != null) {
        service = provider.provide(spec);
      }

      if (service == null) {
        throw new ServiceNotAvailableException(spec.getServiceContractType().getName());
      }

      servicePool.add(service);
    }
Exemple #3
0
 /** {@inheritDoc} */
 @Override
 public void collectPublishedServicesSpecifications(Collection<ServiceSpecification> specs) {
   specs.add(ServiceSpecification.forService(DataBaseService.class));
 }
Exemple #4
0
 /** {@inheritDoc} */
 @Override
 public void collectRequiredServicesSpecifications(Collection<ServiceSpecification> specs) {
   specs.add(ServiceSpecification.forService(DataSourceService.class));
   specs.add(ServiceSpecification.forService(DataService.class));
 }
  /**
   * 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.");
      }
    }
  }