All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.cdap.internal.events.EventWriterExtensionProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2022 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.cdap.internal.events;

import com.google.inject.Inject;
import io.cdap.cdap.common.conf.CConfiguration;
import io.cdap.cdap.common.conf.Constants;
import io.cdap.cdap.common.lang.ClassPathResources;
import io.cdap.cdap.common.lang.FilterClassLoader;
import io.cdap.cdap.extension.AbstractExtensionLoader;
import io.cdap.cdap.spi.events.EventWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of {@link EventWriterProvider} which provides Event writer extension classes
 * extending from {@link AbstractExtensionLoader}
 */
public class EventWriterExtensionProvider extends AbstractExtensionLoader
    implements EventWriterProvider {

  private static final Logger LOG = LoggerFactory.getLogger(EventWriterExtensionProvider.class);
  private static final Set ALLOWED_RESOURCES = createAllowedResources();
  private static final Set ALLOWED_PACKAGES = createPackageSets(ALLOWED_RESOURCES);
  private Collection enabledEventWriters;

  @Inject
  public EventWriterExtensionProvider(CConfiguration cConf) {
    super(cConf.get(Constants.Event.EVENTS_WRITER_EXTENSIONS_DIR) != null
        ? cConf.get(Constants.Event.EVENTS_WRITER_EXTENSIONS_DIR) : "");
    this.enabledEventWriters = cConf.getStringCollection(
        Constants.Event.EVENTS_WRITER_EXTENSIONS_ENABLED_LIST);
    if (this.enabledEventWriters == null || this.enabledEventWriters.isEmpty()) {
      LOG.debug("No event writers enabled.");
      return;
    }
    LOG.debug("Enabled event writers are {} .", enabledEventWriters);
  }

  private static Set createAllowedResources() {
    try {
      return ClassPathResources.getResourcesWithDependencies(EventWriter.class.getClassLoader(),
          EventWriter.class);
    } catch (IOException e) {
      throw new RuntimeException("Failed to trace dependencies for writer extension. "
          + "Usage of events writer might fail.", e);
    }
  }

  @Override
  public Map loadEventWriters() {
    return getAll();
  }

  @Override
  protected Set getSupportedTypesForProvider(EventWriter eventWriter) {
    if (enabledEventWriters == null || !enabledEventWriters.contains(eventWriter.getID())) {
      LOG.debug("{} is not present in the enabled list of event writers.", eventWriter.getID());
      return Collections.emptySet();
    }

    return Collections.singleton(eventWriter.getID());
  }

  @Override
  protected FilterClassLoader.Filter getExtensionParentClassLoaderFilter() {
    // Only allow spi classes.
    return new FilterClassLoader.Filter() {
      @Override
      public boolean acceptResource(String resource) {
        return ALLOWED_RESOURCES.contains(resource);
      }

      @Override
      public boolean acceptPackage(String packageName) {
        return ALLOWED_PACKAGES.contains(packageName);
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy