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

com.rometools.rome.io.impl.PluginManager Maven / Gradle / Ivy

Go to download

All Roads Lead to ROME. ROME is a set of Atom/RSS Java utilities that make it easy to work in Java with most syndication formats. Today it accepts all flavors of RSS (0.90, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0), Atom 0.3 and Atom 1.0 feeds. Rome includes a set of parsers and generators for the various flavors of feeds, as well as converters to convert from one format to another. The parsers can give you back Java objects that are either specific for the format you want to work with, or a generic normalized SyndFeed object that lets you work on with the data without bothering about the underlying format.

There is a newer version: 2.1.0
Show newest version
/*
 * Copyright 2004 Sun Microsystems, 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 com.rometools.rome.io.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.rometools.rome.feed.impl.ConfigurableClassLoader;
import com.rometools.rome.io.DelegatingModuleGenerator;
import com.rometools.rome.io.DelegatingModuleParser;
import com.rometools.rome.io.WireFeedGenerator;
import com.rometools.rome.io.WireFeedParser;

public abstract class PluginManager {

    private final String[] propertyValues;
    private final List keys;
    private final WireFeedParser parentParser;
    private final WireFeedGenerator parentGenerator;

    private Map pluginsMap;
    private List pluginsList;

    /**
     * Creates a PluginManager
     *
     * @param propertyKey property key defining the plugins classes
     *
     */
    protected PluginManager(final String propertyKey) {
        this(propertyKey, null, null);
    }

    protected PluginManager(final String propertyKey, final WireFeedParser parentParser, final WireFeedGenerator parentGenerator) {
        this.parentParser = parentParser;
        this.parentGenerator = parentGenerator;
        propertyValues = PropertiesLoader.getPropertiesLoader().getTokenizedProperty(propertyKey, ", ");
        loadPlugins();
        pluginsMap = Collections.unmodifiableMap(pluginsMap);
        pluginsList = Collections.unmodifiableList(pluginsList);
        keys = Collections.unmodifiableList(new ArrayList(pluginsMap.keySet()));
    }

    protected abstract String getKey(T obj);

    protected List getKeys() {
        return keys;
    }

    protected List getPlugins() {
        return pluginsList;
    }

    protected Map getPluginMap() {
        return pluginsMap;
    }

    protected T getPlugin(final String key) {
        return pluginsMap.get(key);
    }

    // PRIVATE - LOADER PART

    private void loadPlugins() {

        final List finalPluginsList = new ArrayList();
        pluginsList = new ArrayList();
        pluginsMap = new HashMap();
        String className = null;

        try {
            final Class[] classes = getClasses();
            for (final Class clazz : classes) {

                className = clazz.getName();
                final T plugin = clazz.newInstance();

                if (plugin instanceof DelegatingModuleParser) {
                    ((DelegatingModuleParser) plugin).setFeedParser(parentParser);
                }

                if (plugin instanceof DelegatingModuleGenerator) {
                    ((DelegatingModuleGenerator) plugin).setFeedGenerator(parentGenerator);
                }

                pluginsMap.put(getKey(plugin), plugin);

                // to preserve the order of definition in the rome.properties files
                pluginsList.add(plugin);

            }

            final Collection plugins = pluginsMap.values();
            for (final T plugin : plugins) {
                // to remove overridden plugin impls
                finalPluginsList.add(plugin);
            }

            final Iterator iterator = pluginsList.iterator();
            while (iterator.hasNext()) {
                final T plugin = iterator.next();
                if (!finalPluginsList.contains(plugin)) {
                    iterator.remove();
                }
            }

        } catch (final Exception ex) {
            throw new RuntimeException("could not instantiate plugin " + className, ex);
        } catch (final ExceptionInInitializerError er) {
            throw new RuntimeException("could not instantiate plugin " + className, er);
        }

    }

    /**
     * Loads and returns the classes defined in the properties files. If the system property
     * "rome.pluginmanager.useloadclass" is set to true then classLoader.loadClass will be used to
     * load classes (instead of Class.forName). This is designed to improve OSGi compatibility.
     * Further information can be found in https://rome.dev.java.net/issues/show_bug.cgi?id=118
     * 

* * @return array containing the classes defined in the properties files. * @throws java.lang.ClassNotFoundException thrown if one of the classes defined in the * properties file cannot be loaded and hard failure is ON. * */ @SuppressWarnings("unchecked") private Class[] getClasses() throws ClassNotFoundException { final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader(); final List> classes = new ArrayList>(); final boolean useLoadClass = Boolean.valueOf(System.getProperty("rome.pluginmanager.useloadclass", "false")).booleanValue(); for (final String propertyValue : propertyValues) { final Class mClass; if (useLoadClass) { mClass = (Class) classLoader.loadClass(propertyValue); } else { mClass = (Class) Class.forName(propertyValue, true, classLoader); } classes.add(mClass); } final Class[] array = new Class[classes.size()]; classes.toArray(array); return array; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy