/**
  * Schedules the given <code>task</code> for asynchrounous execution or synchronously runs the
  * task if the thread is not running. If this instance is {@link #isActive() not active}, the task
  * is not executed.
  *
  * @param task The component task to execute
  */
 public void schedule(Runnable task) {
   if (isActive()) {
     ComponentActorThread cat = m_componentActor;
     if (cat != null) {
       cat.schedule(task);
     } else {
       log(
           LogService.LOG_INFO,
           "Component Actor Thread not running, calling synchronously",
           null,
           null);
       try {
         synchronized (this) {
           task.run();
         }
       } catch (Throwable t) {
         log(LogService.LOG_INFO, "Unexpected problem executing task", null, t);
       }
     }
   } else {
     log(
         LogService.LOG_INFO,
         "BundleComponentActivator is not active; not scheduling {0}",
         new Object[] {task},
         null,
         null);
   }
 }
  /** Dispose of this component activator instance and all the component managers. */
  void dispose(int reason) {
    if (m_context == null) {
      return;
    }

    // mark instance inactive (no more component activations)
    m_active = false;

    log(
        LogService.LOG_DEBUG,
        "BundleComponentActivator : Bundle [{0}] will destroy {1} instances",
        new Object[] {
          new Long(m_context.getBundle().getBundleId()), new Integer(m_managers.size())
        },
        null,
        null);

    while (m_managers.size() != 0) {
      ComponentHolder holder = (ComponentHolder) m_managers.get(0);
      try {
        m_managers.remove(holder);
        holder.disposeComponents(reason);
      } catch (Exception e) {
        log(
            LogService.LOG_ERROR,
            "BundleComponentActivator : Exception invalidating",
            holder.getComponentMetadata(),
            e);
      } finally {
        m_componentRegistry.unregisterComponentHolder(holder.getComponentMetadata().getName());
      }
    }

    log(
        LogService.LOG_DEBUG,
        "BundleComponentActivator : Bundle [{0}] STOPPED",
        new Object[] {new Long(m_context.getBundle().getBundleId())},
        null,
        null);

    if (m_logService != null) {
      m_logService.close();
      m_logService = null;
    }

    m_componentActor = null;
    m_componentRegistry = null;
    m_context = null;
  }
 /**
  * Method to actually emit the log message. If the LogService is available, the message will be
  * logged through the LogService. Otherwise the message is logged to stdout (or stderr in case of
  * LOG_ERROR level messages),
  *
  * @param level The log level to log the message at
  * @param pattern The <code>java.text.MessageFormat</code> message format string for preparing the
  *     message
  * @param arguments The format arguments for the <code>pattern</code> string.
  * @param ex An optional <code>Throwable</code> whose stack trace is written, or <code>null</code>
  *     to not log a stack trace.
  */
 public void log(
     int level, String pattern, Object[] arguments, ComponentMetadata metadata, Throwable ex) {
   if (isLogEnabled(level)) {
     final String message = MessageFormat.format(pattern, arguments);
     log(level, message, metadata, ex);
   }
 }
  /**
   * Gets the MetaData location, parses the meta data and requests the processing of binder
   * instances
   *
   * @param descriptorLocations A comma separated list of locations of component descriptors. This
   *     must not be <code>null</code>.
   * @throws IllegalStateException If the bundle has already been uninstalled.
   */
  private void initialize(String descriptorLocations) {

    // 112.4.1: The value of the the header is a comma separated list of XML entries within the
    // Bundle
    StringTokenizer st = new StringTokenizer(descriptorLocations, ", ");

    while (st.hasMoreTokens()) {
      String descriptorLocation = st.nextToken();

      URL[] descriptorURLs = findDescriptors(m_context.getBundle(), descriptorLocation);
      if (descriptorURLs.length == 0) {
        // 112.4.1 If an XML document specified by the header cannot be located in the bundle and
        // its attached
        // fragments, SCR must log an error message with the Log Service, if present, and continue.
        log(
            LogService.LOG_ERROR,
            "Component descriptor entry ''{0}'' not found",
            new Object[] {descriptorLocation},
            null,
            null);
        continue;
      }

      // load from the descriptors
      for (int i = 0; i < descriptorURLs.length; i++) {
        loadDescriptor(descriptorURLs[i]);
      }
    }
  }
  private void loadDescriptor(final URL descriptorURL) {
    // simple path for log messages
    final String descriptorLocation = descriptorURL.getPath();

    InputStream stream = null;
    try {
      stream = descriptorURL.openStream();

      BufferedReader in = new BufferedReader(new InputStreamReader(stream));
      XmlHandler handler = new XmlHandler(m_context.getBundle(), this);
      KXml2SAXParser parser;

      parser = new KXml2SAXParser(in);

      parser.parseXML(handler);

      // 112.4.2 Component descriptors may contain a single, root component element
      // or one or more component elements embedded in a larger document
      Iterator i = handler.getComponentMetadataList().iterator();
      while (i.hasNext()) {
        ComponentMetadata metadata = (ComponentMetadata) i.next();
        try {
          // check and reserve the component name
          m_componentRegistry.checkComponentName(metadata.getName());

          // validate the component metadata
          metadata.validate(this);

          // Request creation of the component manager
          ComponentHolder holder = m_componentRegistry.createComponentHolder(this, metadata);

          // register the component after validation
          m_componentRegistry.registerComponentHolder(metadata.getName(), holder);
          m_managers.add(holder);

          // enable the component
          if (metadata.isEnabled()) {
            holder.enableComponents();
          }
        } catch (Throwable t) {
          // There is a problem with this particular component, we'll log the error
          // and proceed to the next one
          log(LogService.LOG_ERROR, "Cannot register Component", metadata, t);

          // make sure the name is not reserved any more
          m_componentRegistry.unregisterComponentHolder(metadata.getName());
        }
      }
    } catch (IOException ex) {
      // 112.4.1 If an XML document specified by the header cannot be located in the bundle and its
      // attached
      // fragments, SCR must log an error message with the Log Service, if present, and continue.

      log(
          LogService.LOG_ERROR,
          "Problem reading descriptor entry ''{0}''",
          new Object[] {descriptorLocation},
          null,
          ex);
    } catch (Exception ex) {
      log(
          LogService.LOG_ERROR,
          "General problem with descriptor entry ''{0}''",
          new Object[] {descriptorLocation},
          null,
          ex);
    } finally {
      if (stream != null) {
        try {
          stream.close();
        } catch (IOException ignore) {
        }
      }
    }
  }
 public void log(int level, String message, Object[] arguments, Throwable ex) {
   BundleComponentActivator activator = getActivator();
   if (activator != null) {
     activator.log(level, message, arguments, getComponentMetadata(), m_componentId, ex);
   }
 }