@Override
  public synchronized void register(IndexProvider provider) throws RepositoryException {
    if (providers.containsKey(provider.getName())) {
      throw new IndexProviderExistsException(
          JcrI18n.indexProviderAlreadyExists.text(provider.getName(), repository.name()));
    }

    // Set the repository name field ...
    Reflection.setValue(provider, "repositoryName", repository.name());

    // Set the logger instance
    Reflection.setValue(provider, "logger", ExtensionLogger.getLogger(provider.getClass()));

    if (initialized.get()) {
      // This manager is already initialized, so we have to initialize the new provider ...
      doInitialize(provider);
    }

    // Do this last so that it doesn't show up in the list of providers before it's properly
    // initialized ...
    IndexProvider existing = providers.putIfAbsent(provider.getName(), provider);
    if (existing != null) {
      throw new IndexProviderExistsException(
          JcrI18n.indexProviderAlreadyExists.text(provider.getName(), repository.name()));
    }

    // Re-read the index definitions in case there were disabled index definitions that used the
    // now-available provider ...
    readIndexDefinitions();

    // Refresh the index writer ...
    refreshIndexWriter();
  }
 /**
  * Get the query index writer that will delegate to only those registered providers with the given
  * names.
  *
  * @param providerNames the names of the providers that require indexing
  * @return a query index writer instance; never null
  */
 IndexWriter getIndexWriterForProviders(Set<String> providerNames) {
   List<IndexProvider> reindexProviders = new LinkedList<>();
   for (IndexProvider provider : providers.values()) {
     if (providerNames.contains(provider.getName())) {
       reindexProviders.add(provider);
     }
   }
   return CompositeIndexWriter.create(reindexProviders);
 }
 void shutdown() {
   for (IndexProvider provider : providers.values()) {
     try {
       provider.shutdown();
     } catch (RepositoryException e) {
       logger.error(
           e,
           JcrI18n.errorShuttingDownIndexProvider,
           repository.name(),
           provider.getName(),
           e.getMessage());
     }
   }
 }
  /**
   * Initialize the supplied provider.
   *
   * @param provider the provider; may not be null
   * @throws RepositoryException if there is a problem initializing the provider
   */
  protected void doInitialize(IndexProvider provider) throws RepositoryException {

    // Set the execution context instance ...
    Reflection.setValue(provider, "context", repository.context());

    // Set the environment
    Reflection.setValue(provider, "environment", repository.environment());

    provider.initialize();

    // If successful, call the 'postInitialize' method reflectively (due to inability to call
    // directly) ...
    Method postInitialize = Reflection.findMethod(IndexProvider.class, "postInitialize");
    Reflection.invokeAccessibly(provider, postInitialize, new Object[] {});

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Successfully initialized index provider '{0}' in repository '{1}'",
          provider.getName(), repository.name());
    }
  }
  /**
   * Initialize this manager by calling {@link IndexProvider#initialize()} on each of the
   * currently-registered providers.
   *
   * @return the information about the portions of the repository that need to be scanned to
   *     (re)build indexes; null if no scanning is required
   */
  protected synchronized ScanningTasks initialize() {
    if (initialized.get()) {
      // nothing to do ...
      return null;
    }

    // Initialize each of the providers, removing any that are not properly initialized ...
    for (Iterator<Map.Entry<String, IndexProvider>> providerIter = providers.entrySet().iterator();
        providerIter.hasNext(); ) {
      IndexProvider provider = providerIter.next().getValue();
      try {
        doInitialize(provider);
      } catch (Throwable t) {
        if (t.getCause() != null) {
          t = t.getCause();
        }
        repository.error(
            t,
            JcrI18n.unableToInitializeIndexProvider,
            provider.getName(),
            repository.name(),
            t.getMessage());
        providerIter.remove();
      }
    }
    // Re-read the index definitions in case there were disabled index definitions that used the
    // now-available provider ...
    RepositoryIndexes indexes = readIndexDefinitions();

    // Notify the providers of all the index definitions (which we'll treat as "new" since we're
    // just starting up) ...
    ScanningTasks feedback = new ScanningTasks();
    for (Iterator<Map.Entry<String, IndexProvider>> providerIter = providers.entrySet().iterator();
        providerIter.hasNext(); ) {
      IndexProvider provider = providerIter.next().getValue();
      if (provider == null) continue;
      final String providerName = provider.getName();

      IndexChanges changes = new IndexChanges();
      for (IndexDefinition indexDefn : indexes.getIndexDefinitions().values()) {
        if (!providerName.equals(indexDefn.getProviderName())) continue;
        changes.change(indexDefn);
      }
      // Even if there are no definitions, we still want to notify each of the providers ...
      try {
        provider.notify(
            changes,
            repository.changeBus(),
            repository.nodeTypeManager(),
            repository.repositoryCache().getWorkspaceNames(),
            feedback.forProvider(providerName));
      } catch (RuntimeException e) {
        logger.error(
            e,
            JcrI18n.errorNotifyingProviderOfIndexChanges,
            providerName,
            repository.name(),
            e.getMessage());
      }
    }

    // Refresh the index writer ...
    refreshIndexWriter();
    initialized.set(true);
    return feedback;
  }