예제 #1
0
  /**
   * Handles injections of APSLogger, BundleContext, or other class types with default constructor.
   *
   * @param field The field to inject into.
   * @param managedClass Used to lookup or create an instance of this class to inject into.
   * @param context The bundle context.
   */
  protected void handleInstanceInjections(Field field, Class managedClass, BundleContext context) {
    Managed managed = field.getAnnotation(Managed.class);
    if (managed != null) {
      String namedInstanceKey = managed.name() + field.getType().getName();
      Object namedInstance = this.namedInstances.get(namedInstanceKey);
      if (namedInstance == null) {
        if (field.getType().equals(APSLogger.class)) {
          namedInstance = new APSLogger(System.out);
          if (managed.loggingFor().length() > 0) {
            ((APSLogger) namedInstance).setLoggingFor(managed.loggingFor());
          }
          ((APSLogger) namedInstance).start(context);
        } else if (field.getType().equals(BundleContext.class)) {
          namedInstance = context;
        } else {
          namedInstance = getManagedInstance(field.getType());
        }
        this.namedInstances.put(namedInstanceKey, namedInstance);
      } else {
        this.activatorLogger.info(
            "Got named instance for key '"
                + namedInstanceKey
                + "': "
                + namedInstance.getClass().getName());
      }

      List<Object> managedInstances = getManagedInstances(managedClass);
      for (Object managedInstance : managedInstances) {
        injectObject(managedInstance, namedInstance, field);
      }

      this.activatorLogger.info(
          "Injected '"
              + namedInstance.getClass().getName()
              + "' instance for name '"
              + managed.name()
              + "' "
              + "into '"
              + managedClass.getName()
              + "."
              + field.getName()
              + "' for bundle: "
              + context.getBundle().getSymbolicName()
              + "!");
    }
  }
예제 #2
0
  /**
   * Called when this bundle is stopped so the Framework can perform the bundle-specific activities
   * necessary to stop the bundle. In general, this method should undo the work that the
   * `BundleActivator.start()` method started. There should be no active threads that were started
   * by this bundle when this bundle returns. A stopped bundle must not call any Framework objects.
   *
   * <p>This method must complete and return to its caller in a timely manner.
   *
   * @param context The execution context of the bundle being stopped.
   * @throws Exception If this method throws an exception, the bundle is still marked as stopped,
   *     and the Framework will remove the bundle's listeners, unregister all services registered by
   *     the bundle, and release all services used by the bundle.
   */
  @Override
  public void stop(BundleContext context) throws Exception {
    if (this.services == null) {
      System.err.println("ERROR: Stopping non started instance of APSActivator!");
      return; // Not started!
    }

    Exception failure = null;

    this.activatorLogger.info(
        "Stopping APSActivator for bundle '"
            + context.getBundle().getSymbolicName()
            + "' with activatorMode: "
            + this.activatorMode);

    for (ListenerWrapper listenerWrapper : this.listeners) {
      listenerWrapper.stop(context);
    }
    this.listeners = null;

    for (Tuple2<Method, Object> shutdownMethod : this.shutdownMethods) {
      try {
        shutdownMethod.t1.invoke(shutdownMethod.t2, null);

        this.activatorLogger.info(
            "Called bundle shutdown method '"
                + shutdownMethod.t2.getClass()
                + "."
                + shutdownMethod.t1.getName()
                + "() for bundle: "
                + context.getBundle().getSymbolicName()
                + "!");
      } catch (Exception e) {
        this.activatorLogger.error("Bundle stop problem!", e);
        failure = e;
      }
    }
    this.shutdownMethods = null;

    for (ServiceRegistration serviceRegistration : this.services) {
      try {
        serviceRegistration.unregister();
      } catch (Exception e) {
        this.activatorLogger.error("Bundle stop problem!", e);
        failure = e;
      }
    }
    this.services = null;

    for (String trackerKey : this.trackers.keySet()) {
      APSServiceTracker tracker = this.trackers.get(trackerKey);
      try {
        tracker.stop(context);
      } catch (Exception e) {
        this.activatorLogger.error("Bundle stop problem!", e);
        failure = e;
      }
    }
    this.trackers = null;

    for (String namedInstanceKey : this.namedInstances.keySet()) {
      Object namedInstance = this.namedInstances.get(namedInstanceKey);
      if (namedInstance instanceof APSLogger) {
        try {
          ((APSLogger) namedInstance).stop(context);
        } catch (Exception e) {
          this.activatorLogger.error("Bundle stop problem!", e);
          failure = e;
        }
      }
    }
    this.namedInstances = null;

    this.activatorLogger.stop(context);
    this.activatorLogger = null;

    if (failure != null) {
      throw new APSActivatorException("Bundle stop not entirely successful!", failure);
    }
  }