/**
   * Removes the specified account from the list of accounts that this provider factory is handling.
   * If the specified accountID is unknown to the ProtocolProviderFactory, the call has no effect
   * and false is returned. This method is persistent in nature and once called the account
   * corresponding to the specified ID will not be loaded during future runs of the project.
   *
   * @param accountID the ID of the account to remove.
   * @return true if an account with the specified ID existed and was removed and false otherwise.
   */
  public boolean uninstallAccount(AccountID accountID) {
    // unregister the protocol provider
    ServiceReference serRef = getProviderForAccount(accountID);

    if (serRef == null) return false;

    ProtocolProviderService protocolProvider =
        (ProtocolProviderService) SipActivator.getBundleContext().getService(serRef);

    try {
      protocolProvider.unregister();
    } catch (OperationFailedException e) {
      logger.error(
          "Failed to unregister protocol provider for account : "
              + accountID
              + " caused by : "
              + e);
    }

    ServiceRegistration registration = (ServiceRegistration) registeredAccounts.remove(accountID);

    if (registration == null) return false;

    // kill the service
    registration.unregister();

    return removeStoredAccount(SipActivator.getBundleContext(), accountID);
  }
  /**
   * Returns the ServiceReference for the protocol provider corresponding to the specified accountID
   * or null if the accountID is unknown.
   *
   * @param accountID the accountID of the protocol provider we'd like to get
   * @return a ServiceReference object to the protocol provider with the specified account id and
   *     null if the account id is unknown to the provider factory.
   */
  public ServiceReference getProviderForAccount(AccountID accountID) {
    ServiceRegistration registration;

    synchronized (registeredAccounts) {
      registration = registeredAccounts.get(accountID);
    }
    return (registration == null) ? null : registration.getReference();
  }
 @After
 public void tearDown() {
   for (ServiceRegistration<?> serviceRegistration : serviceRegistrations) {
     try {
       serviceRegistration.unregister();
     } catch (IllegalStateException e) {
       // Service was already unregistered.
     }
   }
 }
  /** Prepares the factory for bundle shutdown. */
  public void stop() {
    if (logger.isTraceEnabled()) logger.trace("Preparing to stop all protocol providers of" + this);

    synchronized (registeredAccounts) {
      for (Enumeration<ServiceRegistration> registrations = registeredAccounts.elements();
          registrations.hasMoreElements(); ) {
        ServiceRegistration reg = registrations.nextElement();

        stop(reg);

        reg.unregister();
      }

      registeredAccounts.clear();
    }
  }
  /** Prepares the factory for bundle shutdown. */
  public void stop() {
    logger.trace("Preparing to stop all SIP protocol providers.");
    Enumeration registrations = this.registeredAccounts.elements();

    while (registrations.hasMoreElements()) {
      ServiceRegistration reg = ((ServiceRegistration) registrations.nextElement());

      ProtocolProviderServiceSipImpl provider =
          (ProtocolProviderServiceSipImpl)
              SipActivator.getBundleContext().getService(reg.getReference());

      // do an attempt to kill the provider
      provider.shutdown();

      reg.unregister();
    }

    registeredAccounts.clear();
  }
  /**
   * Removes the specified account from the list of accounts that this provider factory is handling.
   * If the specified accountID is unknown to the ProtocolProviderFactory, the call has no effect
   * and false is returned. This method is persistent in nature and once called the account
   * corresponding to the specified ID will not be loaded during future runs of the project.
   *
   * @param accountID the ID of the account to remove.
   * @return true if an account with the specified ID existed and was removed and false otherwise.
   */
  public boolean uninstallAccount(AccountID accountID) {
    // Unregister the protocol provider.
    ServiceReference serRef = getProviderForAccount(accountID);

    boolean wasAccountExisting = false;

    // If the protocol provider service is registered, first unregister the
    // service.
    if (serRef != null) {
      BundleContext bundleContext = getBundleContext();
      ProtocolProviderService protocolProvider =
          (ProtocolProviderService) bundleContext.getService(serRef);

      try {
        protocolProvider.unregister();
      } catch (OperationFailedException ex) {
        logger.error(
            "Failed to unregister protocol provider for account: "
                + accountID
                + " caused by: "
                + ex);
      }
    }

    ServiceRegistration registration;

    synchronized (registeredAccounts) {
      registration = registeredAccounts.remove(accountID);
    }

    // first remove the stored account so when PP is unregistered we can
    // distinguish between deleted or just disabled account
    wasAccountExisting = removeStoredAccount(accountID);

    if (registration != null) {
      // Kill the service.
      registration.unregister();
    }

    return wasAccountExisting;
  }
Beispiel #7
0
  /**
   * This gets called for required services when the tracker have timed out waiting for a service to
   * become available and is about to throw an APSNoServiceAvailableException. This will unpublish
   * all published services that have a requirement on the timed out service. The service will be
   * republished later when it becomes available again by onServiceAvailable() above.
   *
   * @throws RuntimeException
   */
  @Override
  public void onTimeout() throws RuntimeException {
    this.activatorLogger.warn("A required service have gone away!");
    List<Class> uniqueClasses = new LinkedList<>();
    for (Tuple4<APSServiceTracker, Class, Boolean, List<ServiceRegistration>> requiredService :
        this.requiredServices) {
      if (!uniqueClasses.contains(requiredService.t2)) {
        uniqueClasses.add(requiredService.t2);
      }
    }

    for (Class managedClass : uniqueClasses) {
      boolean allRequiredAvailable = true;
      for (Tuple4<APSServiceTracker, Class, Boolean, List<ServiceRegistration>> requiredService :
          this.requiredServices) {
        if (requiredService.t2.equals(managedClass) && !requiredService.t1.hasTrackedService()) {
          allRequiredAvailable = false;
          break;
        }
      }

      if (!allRequiredAvailable) {
        for (Tuple4<APSServiceTracker, Class, Boolean, List<ServiceRegistration>> requiredService :
            this.requiredServices) {
          if (requiredService.t2.equals(managedClass)) {
            for (ServiceRegistration serviceRegistration : requiredService.t4) {
              try {
                serviceRegistration.unregister();
                requiredService.t3 = false;
                this.services.remove(serviceRegistration);
                this.activatorLogger.warn(
                    "Removed registration for: " + serviceRegistration.getReference());
              } catch (Exception e) {
                this.activatorLogger.error("Bundle stop problem!", e);
              }
            }
          }
        }
      }
    }
  }
  /**
   * Unloads the account corresponding to the given <tt>accountID</tt>. Unregisters the
   * corresponding protocol provider, but keeps the account in contrast to the uninstallAccount
   * method.
   *
   * @param accountID the account identifier
   * @return true if an account with the specified ID existed and was unloaded and false otherwise.
   */
  public boolean unloadAccount(AccountID accountID) {
    // Unregister the protocol provider.
    ServiceReference serRef = getProviderForAccount(accountID);

    if (serRef == null) {
      return false;
    }

    BundleContext bundleContext = getBundleContext();
    ProtocolProviderService protocolProvider =
        (ProtocolProviderService) bundleContext.getService(serRef);

    try {
      protocolProvider.unregister();
    } catch (OperationFailedException ex) {
      logger.error(
          "Failed to unregister protocol provider for account : "
              + accountID
              + " caused by: "
              + ex);
    }

    ServiceRegistration registration;

    synchronized (registeredAccounts) {
      registration = registeredAccounts.remove(accountID);
    }
    if (registration == null) {
      return false;
    }

    // Kill the service.
    registration.unregister();

    return true;
  }
  public void stop() {
    if (reaper != null) {
      bReap = false;
      try {
        reaper.wait(1000);
      } catch (Exception ignored) {
      }
      reaper = null;
    }
    if (reg != null) {
      reg.unregister();
      reg = null;

      slTracker.close();
    }
  }
  /**
   * Shuts down the <code>ProtocolProviderService</code> representing an account registered with
   * this factory.
   *
   * @param registeredAccount the <code>ServiceRegistration</code> of the <code>
   *     ProtocolProviderService</code> representing an account registered with this factory
   */
  protected void stop(ServiceRegistration registeredAccount) {
    ProtocolProviderService protocolProviderService =
        (ProtocolProviderService) getBundleContext().getService(registeredAccount.getReference());

    protocolProviderService.shutdown();
  }
	public void testSingletons() throws BundleException {
		Region region1 = digraph.createRegion(getName() + "_1");
		Region region2 = digraph.createRegion(getName() + "_2");

		// first install into the same region; higher version 2 should resolve
		Bundle singleton1 = bundleInstaller.installBundle(SINGLETON1, region1);
		Bundle singleton2 = bundleInstaller.installBundle(SINGLETON2, region1);
		assertFalse(bundleInstaller.resolveBundles(new Bundle[] {singleton1, singleton2}));
		assertEquals("singleton1", Bundle.INSTALLED, singleton1.getState());
		assertEquals("singleton2", Bundle.RESOLVED, singleton2.getState());

		// now install into different regions; both 1 and 2 should resolve
		singleton2.uninstall();
		singleton2 = bundleInstaller.installBundle(SINGLETON2, region2);
		assertTrue(bundleInstaller.resolveBundles(new Bundle[] {singleton1, singleton2}));
		assertEquals("singleton1", Bundle.RESOLVED, singleton1.getState());
		assertEquals("singleton2", Bundle.RESOLVED, singleton2.getState());

		ServiceRegistration<ResolverHookFactory> disableHook = disableAllResolves();
		try {
			// now refresh to get us to an unresolved state again
			bundleInstaller.refreshPackages(new Bundle[] {singleton1, singleton2});
			// connect region2 -> region1
			region2.connectRegion(region1, digraph.createRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).build());
			// enable resolving again
			disableHook.unregister();
			disableHook = null;

			assertFalse(bundleInstaller.resolveBundles(new Bundle[] {singleton1, singleton2}));
			assertTrue("One and only singleton bundle should be resolved", (singleton1.getState() == Bundle.RESOLVED) ^ (singleton2.getState() == Bundle.RESOLVED));

			singleton2.uninstall();
			disableHook = disableAllResolves();
			// now refresh to get us to an unresolved state again
			bundleInstaller.refreshPackages(new Bundle[] {singleton1, singleton2});
			// enable resolving again
			disableHook.unregister();
			disableHook = null;

			// make sure singleton1 is resolved first
			assertTrue(bundleInstaller.resolveBundles(new Bundle[] {singleton1}));
			assertEquals("singleton1", Bundle.RESOLVED, singleton1.getState());
			singleton2 = bundleInstaller.installBundle(SINGLETON2, region2);
			assertFalse(bundleInstaller.resolveBundles(new Bundle[] {singleton2}));
			assertEquals("singleton2", Bundle.INSTALLED, singleton2.getState());

			singleton1.uninstall();
			disableHook = disableAllResolves();
			// now refresh to get us to an unresolved state again
			bundleInstaller.refreshPackages(new Bundle[] {singleton1, singleton2});
			// enable resolving again
			disableHook.unregister();
			disableHook = null;

			// make sure singleton2 is resolved first
			assertTrue(bundleInstaller.resolveBundles(new Bundle[] {singleton2}));
			assertEquals("singleton2", Bundle.RESOLVED, singleton2.getState());
			singleton1 = bundleInstaller.installBundle(SINGLETON1, region1);
			assertFalse(bundleInstaller.resolveBundles(new Bundle[] {singleton1}));
			assertEquals("singleton1", Bundle.INSTALLED, singleton1.getState());
		} finally {
			if (disableHook != null)
				disableHook.unregister();
		}
	}
  /**
   * Returns the ServiceReference for the protocol provider corresponding to the specified accountID
   * or null if the accountID is unknown.
   *
   * @param accountID the accountID of the protocol provider we'd like to get
   * @return a ServiceReference object to the protocol provider with the specified account id and
   *     null if the account id is unknwon to the provider factory.
   */
  public ServiceReference getProviderForAccount(AccountID accountID) {
    ServiceRegistration registration = (ServiceRegistration) registeredAccounts.get(accountID);

    return (registration == null) ? null : registration.getReference();
  }
Beispiel #13
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);
    }
  }
 /**
  * Unregisters the Metacafe replacement service.
  *
  * @param context BundleContext
  * @throws Exception if anything goes wrong
  */
 public void stop(BundleContext context) throws Exception {
   metacafeServReg.unregister();
   logger.info("Metacafe source implementation [STOPPED].");
 }
 protected void unregisterService(Object service) {
   for (ServiceRegistration<?> serviceRegistration : serviceRegistrations)
     if (getSystemBundleContext().getService(serviceRegistration.getReference()).equals(service))
       serviceRegistration.unregister();
 }