/*
   * (non-Javadoc)
   * @see com.socialize.SocializeService#destroy(boolean)
   */
  @Override
  public synchronized void destroy(boolean force) {
    if (force) {

      if (AsyncTaskManager.isManaged()) {
        AsyncTaskManager.terminateAll(10, TimeUnit.SECONDS);
      }

      if (container != null) {
        if (logger != null && logger.isDebugEnabled()) {
          logger.debug("Destroying IOC container");
        }
        container.destroy();
      }

      config.destroy();
      system.destroy();
      initCount = 0;
      initPaths = null;
      entityLoader = null;
      session = null;
    } else {
      destroy();
    }
  }
  public synchronized IOCContainer initWithContainer(
      Context context, SocializeInitListener listener, String... paths) throws Exception {
    boolean init = false;

    // Check socialize is supported on this device.
    isSocializeSupported(context);

    String[] localPaths = getInitPaths();

    if (paths != null) {

      if (isInitialized()) {

        if (localPaths != null) {
          for (String path : paths) {
            if (binarySearch(localPaths, path) < 0) {

              if (logger != null && logger.isInfoEnabled()) {
                logger.info("New path found for beans [" + path + "].  Re-initializing Socialize");
              }

              this.initCount = 0;

              // Destroy the container so we recreate bean references
              if (container != null) {
                if (logger != null && logger.isDebugEnabled()) {
                  logger.debug("Destroying IOC container");
                }
                container.destroy();
              }

              init = true;

              break;
            }
          }
        } else {
          String msg =
              "Socialize reported as initialize, but no initPaths were found.  This should not happen!";
          if (logger != null) {
            logger.error(msg);
          } else {
            System.err.println(msg);
          }

          destroy();
          init = true;
        }
      } else {
        init = true;
      }

      if (init) {
        try {
          Logger.LOG_KEY = Socialize.LOG_KEY;
          Logger.logLevel = Log.WARN;

          this.initPaths = paths;

          sort(this.initPaths);

          if (container == null) {
            container = newSocializeIOC();
          }

          ResourceLocator locator = newResourceLocator();

          locator.setLogger(newLogger());

          ClassLoaderProvider provider = newClassLoaderProvider();

          locator.setClassLoaderProvider(provider);

          if (logger != null) {

            if (logger.isDebugEnabled()) {
              for (String path : paths) {
                logger.debug("Initializing Socialize with path [" + path + "]");
              }

              Logger.logLevel = Log.DEBUG;
            } else if (logger.isInfoEnabled()) {
              Logger.logLevel = Log.INFO;
            }
          }

          ((SocializeIOC) container).init(context, locator, paths);

          init(context, container, listener); // initCount incremented here
        } catch (Exception e) {
          throw e;
        }
      } else {
        this.initCount++;
      }

      // Always set the context on the container
      setContext(context);
    } else {
      String msg = "Attempt to initialize Socialize with null bean config paths";
      if (logger != null) {
        logger.error(msg);
      } else {
        System.err.println(msg);
      }
    }

    return container;
  }