summaryrefslogtreecommitdiffstats
path: root/mod/designtool/designtool-web/src/main/java/org/apache/nifi/nar/DCAEAutoLoader.java
blob: ec15ba6247804b5ac7b6f6429b6b27d1c80cb4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.apache.nifi.nar;

import org.apache.nifi.bundle.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;

/**
 * Uses the Java executor service scheduler to continuously load new DCAE jars
 */
public class DCAEAutoLoader {

    private static final Logger LOGGER = LoggerFactory.getLogger(DCAEAutoLoader.class);

    private static final long POLL_INTERVAL_MS = 5000;

    /**
     * Runnable task that grabs list of remotely stored jars, identifies ones that haven't
     * been processed, builds Nifi bundles for those unprocessed ones and loads them into
     * the global extension manager.
     */
    private static class LoaderTask implements Runnable {

        private static final Logger LOGGER = LoggerFactory.getLogger(LoaderTask.class);

        private final URI indexJsonDcaeJars;
        private final ExtensionDiscoveringManager extensionManager;
        private final Set<URL> processed = new LinkedHashSet();

        private LoaderTask(URI indexJsonDcaeJars, ExtensionDiscoveringManager extensionManager) {
            this.indexJsonDcaeJars = indexJsonDcaeJars;
            this.extensionManager = extensionManager;
        }

        @Override
        public void run() {
            try {
                List<URL> toProcess = DCAEClassLoaders.getDCAEJarsURLs(this.indexJsonDcaeJars);
                toProcess.removeAll(processed);

                if (!toProcess.isEmpty()) {
                    Set<Bundle> bundles = DCAEClassLoaders.createDCAEBundles(toProcess);
                    this.extensionManager.discoverExtensions(bundles);
                    processed.addAll(toProcess);

                    LOGGER.info(String.format("#Added DCAE bundles: %d, #Total DCAE bundles: %d ",
                        bundles.size(), processed.size()));
                }
            } catch (final Exception e) {
                LOGGER.error("Error loading DCAE jars due to: " + e.getMessage(), e);
            }
        }
    }

    private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    private ScheduledFuture taskFuture;

    public synchronized void start(URI indexJsonDcaeJars, final ExtensionDiscoveringManager extensionManager) {
        // Restricting to a single thread
        if (taskFuture != null && !taskFuture.isCancelled()) {
            return;
        }

        LOGGER.info("Starting DCAE Auto-Loader: {}", new Object[]{indexJsonDcaeJars});

        LoaderTask task = new LoaderTask(indexJsonDcaeJars, extensionManager);
        this.taskFuture = executor.scheduleAtFixedRate(task, 0, POLL_INTERVAL_MS, TimeUnit.MILLISECONDS);
        LOGGER.info("DCAE Auto-Loader started");
    }

    public synchronized void stop() {
        if (this.taskFuture != null) {
            this.taskFuture.cancel(true);
            LOGGER.info("DCAE Auto-Loader stopped");
        }
    }

}