Search in sources :

Example 1 with BlueExtensionClassContainer

use of io.jenkins.blueocean.rest.model.BlueExtensionClassContainer in project blueocean-plugin by jenkinsci.

the class JenkinsJSExtensions method refreshCache.

private static synchronized void refreshCache(List<PluginWrapper> latestPlugins) {
    if (!latestPlugins.equals(pluginCache)) {
        pluginCache.clear();
        pluginCache.addAll(latestPlugins);
        refreshCache(pluginCache);
    }
    for (PluginWrapper pluginWrapper : pluginCache) {
        //skip if not active
        if (!pluginWrapper.isActive()) {
            continue;
        }
        //skip probing plugin if already read
        if (jsExtensionCache.get(pluginWrapper.getLongName()) != null) {
            continue;
        }
        try {
            Enumeration<URL> dataResources = pluginWrapper.classLoader.getResources("jenkins-js-extension.json");
            boolean hasDefinedExtensions = false;
            while (dataResources.hasMoreElements()) {
                URL dataRes = dataResources.nextElement();
                LOGGER.debug("Reading 'jenkins-js-extension.json' from '{}'.", dataRes);
                try (InputStream dataResStream = dataRes.openStream()) {
                    Map<String, Object> extensionData = mapper.readValue(dataResStream, Map.class);
                    String pluginId = getGav(extensionData);
                    if (pluginId != null) {
                        // future/past iteration of this loop.
                        if (!pluginId.equals(pluginWrapper.getShortName())) {
                            continue;
                        }
                    } else {
                        LOGGER.error(String.format("Plugin %s JS extension has missing hpiPluginId", pluginWrapper.getLongName()));
                        continue;
                    }
                    List<Map> extensions = (List<Map>) extensionData.get(PLUGIN_EXT);
                    if (extensions != null) {
                        for (Map extension : extensions) {
                            try {
                                String type = (String) extension.get("type");
                                if (type != null) {
                                    BlueExtensionClassContainer extensionClassContainer = Jenkins.getInstance().getExtensionList(BlueExtensionClassContainer.class).get(0);
                                    Map classInfo = (Map) mergeObjects(extensionClassContainer.get(type));
                                    List classInfoClasses = (List) classInfo.get("_classes");
                                    classInfoClasses.add(0, type);
                                    extension.put("_class", type);
                                    extension.put("_classes", classInfoClasses);
                                }
                            } catch (Exception e) {
                                LOGGER.error("An error occurred when attempting to read type information from jenkins-js-extension.json from: " + dataRes, e);
                            }
                        }
                    }
                    extensionData.put(PLUGIN_VER, pluginWrapper.getVersion());
                    jsExtensionCache.put(pluginId, mergeObjects(extensionData));
                    hasDefinedExtensions = true;
                }
            }
            if (!hasDefinedExtensions) {
                // Manufacture an entry for all plugins that do not have any defined
                // extensions. This adds some info about the plugin that the UI might
                // need access to e.g. the plugin version.
                Map<String, Object> extensionData = new LinkedHashMap<>();
                extensionData.put(PLUGIN_ID, pluginWrapper.getShortName());
                extensionData.put(PLUGIN_VER, pluginWrapper.getVersion());
                extensionData.put(PLUGIN_EXT, Collections.emptyList());
                jsExtensionCache.put(pluginWrapper.getShortName(), mergeObjects(extensionData));
            }
        } catch (IOException e) {
            LOGGER.error(String.format("Error locating jenkins-js-extension.json for plugin %s", pluginWrapper.getLongName()));
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) BlueExtensionClassContainer(io.jenkins.blueocean.rest.model.BlueExtensionClassContainer) PluginWrapper(hudson.PluginWrapper) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

PluginWrapper (hudson.PluginWrapper)1 BlueExtensionClassContainer (io.jenkins.blueocean.rest.model.BlueExtensionClassContainer)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1