private void initDependencyManagers() {
    if (m_dependencyManagersInitialized) {
      return;
    }
    final Bundle bundle = getBundle();
    if (bundle == null) {
      log(
          LogService.LOG_ERROR,
          "bundle shut down while trying to load implementation object class",
          null);
      throw new IllegalStateException(
          "bundle shut down while trying to load implementation object class");
    }
    Class<?> implementationObjectClass;
    try {
      implementationObjectClass =
          bundle.loadClass(getComponentMetadata().getImplementationClassName());
    } catch (ClassNotFoundException e) {
      log(LogService.LOG_ERROR, "Could not load implementation object class", e);
      throw new IllegalStateException("Could not load implementation object class");
    }
    m_componentMethods.initComponentMethods(m_componentMetadata, implementationObjectClass);

    for (DependencyManager dependencyManager : m_dependencyManagers) {
      dependencyManager.initBindingMethods(
          m_componentMethods.getBindMethods(dependencyManager.getName()));
    }
    m_dependencyManagersInitialized = true;
  }
 /**
  * Collect and store in m_dependencies_map all the services for dependencies, outside of any
  * locks. Throwing IllegalStateException on failure to collect all the dependencies is needed so
  * getService can know to return null.
  *
  * @return true if this thread collected the dependencies; false if some other thread successfully
  *     collected the dependencies;
  * @throws IllegalStateException if some dependency is no longer available.
  */
 protected boolean collectDependencies() throws IllegalStateException {
   if (m_dependenciesCollected) {
     log(
         LogService.LOG_DEBUG,
         "dependencies already collected, do not collect dependencies",
         null);
     return false;
   }
   initDependencyManagers();
   for (DependencyManager<S, ?> dependencyManager : m_dependencyManagers) {
     if (!dependencyManager.prebind()) {
       // not actually satisfied any longer
       deactivateDependencyManagers();
       log(
           LogService.LOG_DEBUG,
           "Could not get required dependency for dependency manager: {0}",
           new Object[] {dependencyManager.getName()},
           null);
       throw new IllegalStateException("Missing dependencies, not satisfied");
     }
   }
   m_dependenciesCollected = true;
   log(LogService.LOG_DEBUG, "This thread collected dependencies", null);
   return true;
 }
 private void disableDependencyManagers() {
   log(LogService.LOG_DEBUG, "Disabling dependency managers", null);
   AtomicInteger trackingCount = new AtomicInteger();
   for (DependencyManager<S, ?> dm : getDependencyManagers()) {
     dm.unregisterServiceListener(trackingCount);
   }
 }
  DependencyManager<S, ?> getDependencyManager(String name) {
    for (DependencyManager<S, ?> dm : getDependencyManagers()) {
      if (name.equals(dm.getName())) {
        return dm;
      }
    }

    // not found
    return null;
  }
  protected boolean verifyDependencyManagers() {
    // indicates whether all dependencies are satisfied
    boolean satisfied = true;

    for (DependencyManager<S, ?> dm : getDependencyManagers()) {

      if (!dm.hasGetPermission()) {
        // bundle has no service get permission
        if (dm.isOptional()) {
          log(
              LogService.LOG_DEBUG,
              "No permission to get optional dependency: {0}; assuming satisfied",
              new Object[] {dm.getName()},
              null);
        } else {
          log(
              LogService.LOG_DEBUG,
              "No permission to get mandatory dependency: {0}; assuming unsatisfied",
              new Object[] {dm.getName()},
              null);
          satisfied = false;
        }
      } else if (!dm.isSatisfied()) {
        // bundle would have permission but there are not enough services
        log(
            LogService.LOG_DEBUG,
            "Dependency not satisfied: {0}",
            new Object[] {dm.getName()},
            null);
        satisfied = false;
      }
    }

    return satisfied;
  }
Esempio n. 6
0
  /** {@inheritDoc} */
  @Override
  public void init(final BundleContext aContext, final DependencyManager aManager)
      throws Exception {
    // Do not start if we're running headless...
    if (GraphicsEnvironment.isHeadless()) {
      throw new RuntimeException("Cannot start client: running headless.");
    }

    final ClientController clientController = new ClientController(aContext);

    aManager.add(
        createBundleAdapterService(Bundle.ACTIVE, CP_BUNDLE_FILTER, true /* propagate */) //
            .setImplementation(ComponentProviderBundleAdapter.class));

    aManager.add(
        createBundleAdapterService(Bundle.ACTIVE, TOOL_BUNDLE_FILTER, true /* propagate */) //
            .setImplementation(ToolBundleAdapter.class));

    aManager.add(
        createBundleAdapterService(Bundle.ACTIVE, DEVICE_BUNDLE_FILTER, true /* propagate */) //
            .setImplementation(DeviceBundleAdapter.class));

    aManager.add(
        createBundleAdapterService(Bundle.ACTIVE, EXPORTER_BUNDLE_FILTER, true /* propagate */) //
            .setImplementation(ExporterBundleAdapter.class));

    Properties props = new Properties();
    props.put(Constants.SERVICE_PID, UIManagerConfigurator.PID);

    String[] serviceNames =
        new String[] {UIManagerConfigurator.class.getName(), ManagedService.class.getName()};

    // UI Manager Configuration...
    aManager.add(
        createComponent() //
            .setInterface(serviceNames, props) //
            .setImplementation(UIManagerConfigurator.class) //
        );

    props.put(Constants.SERVICE_PID, UIColorSchemeManager.PID);

    serviceNames =
        new String[] {UIColorSchemeManager.class.getName(), ManagedServiceFactory.class.getName()};

    // UI Manager Configuration...
    aManager.add(
        createComponent() //
            .setInterface(serviceNames, props) //
            .setImplementation(UIColorSchemeManager.class));

    // User session manager...
    aManager.add(
        createComponent() //
            .setImplementation(new UserSessionManager()) //
            .add(
                createServiceDependency() //
                    .setService(ProjectManager.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(UserSettingsManager.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(PreferencesService.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(LogService.class) //
                    .setRequired(false) //
                ));

    // All the interfaces we're registering the client controller under...
    serviceNames =
        new String[] {
          AcquisitionDataListener.class.getName(),
          AcquisitionProgressListener.class.getName(),
          AcquisitionStatusListener.class.getName(),
          AnnotationListener.class.getName(),
          PlatformCallback.class.getName()
        };

    // Client controller...
    aManager.add(
        createComponent() //
            .setInterface(serviceNames, null) //
            .setImplementation(clientController) //
            .add(
                createServiceDependency() //
                    .setService(HostProperties.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(ProjectManager.class) //
                    .setRequired(true) //
                    .setCallbacks("setProjectManager", "removeProjectManager")) //
            .add(
                createServiceDependency() //
                    .setService(DataAcquisitionService.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(UIColorSchemeManager.class) //
                    .setRequired(true)) //
            .add(
                createServiceDependency() //
                    .setService(ComponentProvider.class, "(OLS-ComponentProvider=Menu)") //
                    .setCallbacks("addMenu", "removeMenu") //
                    .setRequired(false)) //
            .add(
                createServiceDependency() //
                    .setService(Device.class) //
                    .setCallbacks("addDevice", "removeDevice") //
                    .setRequired(false)) //
            .add(
                createServiceDependency() //
                    .setService(Tool.class) //
                    .setCallbacks("addTool", "removeTool") //
                    .setRequired(false)) //
            .add(
                createServiceDependency() //
                    .setService(Exporter.class) //
                    .setCallbacks("addExporter", "removeExporter") //
                    .setRequired(false)) //
            .add(
                createConfigurationDependency() //
                    .setPid(UIManagerConfigurator.PID)) //
        );
  }
 private void deactivateDependencyManagers() {
   log(LogService.LOG_DEBUG, "Deactivating dependency managers", null);
   for (DependencyManager<S, ?> dm : getDependencyManagers()) {
     dm.deactivate();
   }
 }
 final void updateTargets(Dictionary<String, Object> properties) {
   for (DependencyManager<S, ?> dm : getDependencyManagers()) {
     dm.setTargetFilter(properties);
   }
 }