Search in sources :

Example 1 with JAR_FILE_EXTENSION

use of com.aws.greengrass.dependency.EZPlugins.JAR_FILE_EXTENSION in project aws-greengrass-nucleus by aws-greengrass.

the class Kernel method locateExternalPlugin.

@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.CloseResource" })
private Class<?> locateExternalPlugin(String name, Topics serviceRootTopics) throws ServiceLoadException {
    ComponentIdentifier componentId = ComponentIdentifier.fromServiceTopics(serviceRootTopics);
    Path pluginJar;
    try {
        pluginJar = nucleusPaths.artifactPath(componentId).resolve(componentId.getName() + JAR_FILE_EXTENSION);
    } catch (IOException e) {
        throw new ServiceLoadException(e);
    }
    if (!pluginJar.toFile().exists() || !pluginJar.toFile().isFile()) {
        throw new ServiceLoadException(String.format("Unable to find %s because %s does not exist", name, pluginJar));
    }
    Topic storedDigest = config.find(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME, GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC, SERVICE_DIGEST_TOPIC_KEY, componentId.toString());
    if (storedDigest == null || storedDigest.getOnce() == null) {
        logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local external plugin is not supported by this greengrass version");
        throw new ServiceLoadException("Custom plugins is not supported by this greengrass version");
    }
    ComponentStore componentStore = context.get(ComponentStore.class);
    if (!componentStore.validateComponentRecipeDigest(componentId, Coerce.toString(storedDigest))) {
        logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local plugin does not match the version in cloud!!");
        throw new ServiceLoadException("Plugin has been modified after it was downloaded");
    }
    Class<?> clazz;
    try {
        AtomicReference<Class<?>> classReference = new AtomicReference<>();
        EZPlugins ezPlugins = context.get(EZPlugins.class);
        ezPlugins.loadPlugin(pluginJar, (sc) -> sc.matchClassesWithAnnotation(ImplementsService.class, (c) -> {
            // Only use the class whose name matches what we want
            ImplementsService serviceImplementation = c.getAnnotation(ImplementsService.class);
            if (serviceImplementation.name().equals(name)) {
                if (classReference.get() != null) {
                    logger.atWarn().log("Multiple classes implementing service found in {} " + "for component {}. Using the first one found: {}", pluginJar, name, classReference.get());
                    return;
                }
                classReference.set(c);
            }
        }));
        clazz = classReference.get();
    } catch (Throwable e) {
        throw new ServiceLoadException(String.format("Unable to load %s as a plugin", name), e);
    }
    if (clazz == null) {
        throw new ServiceLoadException(String.format("Unable to find %s. Could not find any ImplementsService annotation with the same name.", name));
    }
    return clazz;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) Deployment(com.aws.greengrass.deployment.model.Deployment) SERVICE_LIFECYCLE_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC) BootstrapManager(com.aws.greengrass.deployment.bootstrap.BootstrapManager) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) DeploymentCapability(com.amazon.aws.iot.greengrass.configuration.common.DeploymentCapability) Path(java.nio.file.Path) Configuration(com.aws.greengrass.config.Configuration) Node(com.aws.greengrass.config.Node) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Pair(com.aws.greengrass.util.Pair) Executors(java.util.concurrent.Executors) SERVICES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC) Topics(com.aws.greengrass.config.Topics) CrashableFunction(com.aws.greengrass.util.CrashableFunction) NucleusPaths(com.aws.greengrass.util.NucleusPaths) List(java.util.List) Writer(java.io.Writer) ImplementsService(com.aws.greengrass.dependency.ImplementsService) CommitableWriter(com.aws.greengrass.util.CommitableWriter) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) ProxyUtils(com.aws.greengrass.util.ProxyUtils) DependencyOrder(com.aws.greengrass.util.DependencyOrder) Setter(lombok.Setter) DeploymentDirectoryManager(com.aws.greengrass.deployment.DeploymentDirectoryManager) VERSION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY) Getter(lombok.Getter) InputValidationException(com.aws.greengrass.lifecyclemanager.exceptions.InputValidationException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) EZPlugins(com.aws.greengrass.dependency.EZPlugins) DeploymentQueue(com.aws.greengrass.deployment.DeploymentQueue) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Coerce(com.aws.greengrass.util.Coerce) Constructor(java.lang.reflect.Constructor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Platform(com.aws.greengrass.util.platforms.Platform) HashSet(java.util.HashSet) MAIN_SERVICE_NAME(com.aws.greengrass.lifecyclemanager.KernelCommandLine.MAIN_SERVICE_NAME) AccessLevel(lombok.AccessLevel) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) JAR_FILE_EXTENSION(com.aws.greengrass.dependency.EZPlugins.JAR_FILE_EXTENSION) Context(com.aws.greengrass.dependency.Context) REQUEST_REBOOT(com.aws.greengrass.deployment.bootstrap.BootstrapSuccessCode.REQUEST_REBOOT) REQUEST_RESTART(com.aws.greengrass.deployment.bootstrap.BootstrapSuccessCode.REQUEST_RESTART) SERVICE_DEPENDENCIES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_DEPENDENCIES_NAMESPACE_TOPIC) LinkedHashSet(java.util.LinkedHashSet) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) DeploymentStage(com.aws.greengrass.deployment.model.Deployment.DeploymentStage) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) Files(java.nio.file.Files) Executor(java.util.concurrent.Executor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConfigurationWriter(com.aws.greengrass.config.ConfigurationWriter) IOException(java.io.IOException) DependencyType(com.amazon.aws.iot.greengrass.component.common.DependencyType) Topic(com.aws.greengrass.config.Topic) Utils(com.aws.greengrass.util.Utils) DeploymentActivatorFactory(com.aws.greengrass.deployment.activator.DeploymentActivatorFactory) ServiceUpdateException(com.aws.greengrass.deployment.exceptions.ServiceUpdateException) DeviceConfigurationException(com.aws.greengrass.deployment.exceptions.DeviceConfigurationException) Clock(java.time.Clock) Collections(java.util.Collections) Logger(com.aws.greengrass.logging.api.Logger) DEFAULT_VALUE_TIMESTAMP(com.aws.greengrass.config.Topic.DEFAULT_VALUE_TIMESTAMP) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) EZPlugins(com.aws.greengrass.dependency.EZPlugins) ImplementsService(com.aws.greengrass.dependency.ImplementsService) Topic(com.aws.greengrass.config.Topic) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException)

Aggregations

DependencyType (com.amazon.aws.iot.greengrass.component.common.DependencyType)1 DeploymentCapability (com.amazon.aws.iot.greengrass.configuration.common.DeploymentCapability)1 ComponentStore (com.aws.greengrass.componentmanager.ComponentStore)1 VERSION_CONFIG_KEY (com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY)1 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)1 Configuration (com.aws.greengrass.config.Configuration)1 ConfigurationWriter (com.aws.greengrass.config.ConfigurationWriter)1 Node (com.aws.greengrass.config.Node)1 Topic (com.aws.greengrass.config.Topic)1 DEFAULT_VALUE_TIMESTAMP (com.aws.greengrass.config.Topic.DEFAULT_VALUE_TIMESTAMP)1 Topics (com.aws.greengrass.config.Topics)1 Context (com.aws.greengrass.dependency.Context)1 EZPlugins (com.aws.greengrass.dependency.EZPlugins)1 JAR_FILE_EXTENSION (com.aws.greengrass.dependency.EZPlugins.JAR_FILE_EXTENSION)1 ImplementsService (com.aws.greengrass.dependency.ImplementsService)1 DeploymentDirectoryManager (com.aws.greengrass.deployment.DeploymentDirectoryManager)1 DeploymentQueue (com.aws.greengrass.deployment.DeploymentQueue)1 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)1 DeploymentActivatorFactory (com.aws.greengrass.deployment.activator.DeploymentActivatorFactory)1 BootstrapManager (com.aws.greengrass.deployment.bootstrap.BootstrapManager)1