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

ro.fortsoft.pf4j.LegacyExtensionFinder Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
/*
 * Copyright 2013 Decebal Suiu
 *
 * 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 ro.fortsoft.pf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.processor.LegacyExtensionStorage;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * All extensions declared in a plugin are indexed in a file "META-INF/extensions.idx".
 * This class lookup extensions in all extensions index files "META-INF/extensions.idx".
 *
 * @author Decebal Suiu
 */
public class LegacyExtensionFinder extends AbstractExtensionFinder {

	private static final Logger log = LoggerFactory.getLogger(LegacyExtensionFinder.class);

	public LegacyExtensionFinder(PluginManager pluginManager) {
        super(pluginManager);
	}

    @Override
    public Map> readClasspathStorages() {
        log.debug("Reading extensions storages from classpath");
        Map> result = new LinkedHashMap<>();

        Set bucket = new HashSet<>();
        try {
            Enumeration urls = getClass().getClassLoader().getResources(getExtensionsResource());
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                log.debug("Read '{}'", url.getFile());
                Reader reader = new InputStreamReader(url.openStream(), "UTF-8");
                LegacyExtensionStorage.read(reader, bucket);
            }

            if (bucket.isEmpty()) {
                log.debug("No extensions found");
            } else {
                log.debug("Found possible {} extensions:", bucket.size());
                for (String entry : bucket) {
                    log.debug("   " + entry);
                }
            }

            result.put(null, bucket);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return result;
    }

    @Override
    public Map> readPluginsStorages() {
        log.debug("Reading extensions storages from plugins");
        Map> result = new LinkedHashMap<>();

        List plugins = pluginManager.getPlugins();
        for (PluginWrapper plugin : plugins) {
            String pluginId = plugin.getDescriptor().getPluginId();
            log.debug("Reading extensions storage for plugin '{}'", pluginId);
            Set bucket = new HashSet<>();

            try {
                URL url = ((PluginClassLoader) plugin.getPluginClassLoader()).findResource(getExtensionsResource());
                if (url != null) {
                    log.debug("Read '{}'", url.getFile());
                    Reader reader = new InputStreamReader(url.openStream(), "UTF-8");
                    LegacyExtensionStorage.read(reader, bucket);
                } else {
                    log.debug("Cannot find '{}'", getExtensionsResource());
                }

                if (bucket.isEmpty()) {
                    log.debug("No extensions found");
                } else {
                    log.debug("Found possible {} extensions:", bucket.size());
                    for (String entry : bucket) {
                        log.debug("   " + entry);
                    }
                }

                result.put(pluginId, bucket);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }

        return result;
    }

    private static String getExtensionsResource() {
        return LegacyExtensionStorage.EXTENSIONS_RESOURCE;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy