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

org.datanucleus.plugin.PluginManager Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.9
Show newest version
/**********************************************************************
Copyright (c) 2006 Erik Bengtson and others. 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.
 

Contributors:
2006 Thomas Marti - Added support for configurable plugin file names
    ...
**********************************************************************/
package org.datanucleus.plugin;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ClassLoaderResolverImpl;
import org.datanucleus.PropertyNames;
import org.datanucleus.exceptions.ClassNotResolvedException;

/**
 * Manages the registry of Extensions and Extension Points for the plugin mechanism.
 */
public class PluginManager
{
    private PluginRegistry registry;

    /**
     * Constructor.
     * @param registryClassName Name of the registry
     * @param clr the ClassLoaderResolver
     * @param props Configuration properties for the plugin system
     */
    public PluginManager(String registryClassName, ClassLoaderResolver clr, Properties props)
    {
        String bundleCheckAction = "EXCEPTION";
        if (props.containsKey("bundle-check-action"))
        {
            bundleCheckAction = props.getProperty("bundle-check-action");
        }
        String allowUserBundles = props.getProperty("allow-user-bundles");
        boolean userBundles = allowUserBundles != null ? Boolean.valueOf(allowUserBundles) : true;

        registry = PluginRegistryFactory.newPluginRegistry(registryClassName, bundleCheckAction, userBundles, clr);

        // Register extension points declared in "/plugin.xml", and then register the extensions of these
        registry.registerExtensionPoints();
        registry.registerExtensions();

        String validateStr = props.containsKey("validate-plugins") ? props.getProperty("validate-plugins") : "false";
        if (validateStr.equalsIgnoreCase("true"))
        {
            registry.resolveConstraints();
        }
    }

    /**
     * Accessor for the PluginRegistry class name.
     * @return Name of the plugin registry
     */
    public String getRegistryClassName()
    {
        return registry.getClass().getName();
    }

    /**
     * Acessor for the ExtensionPoint
     * @param id the unique id of the extension point
     * @return null if the ExtensionPoint is not registered
     */
    public ExtensionPoint getExtensionPoint(String id)
    {
        return registry.getExtensionPoint(id);
    }

    /**
     * Convenience accessor for getting the (first) ConfigurationElement for an extension (of an extension point).
     * @param extensionPointName The extension point
     * @param discrimAttrName Attribute on the extension to use as discriminator
     * @param discrimAttrValue Value for discriminator attribute
     * @return The configuration element
     */
    public ConfigurationElement getConfigurationElementForExtension(String extensionPointName, String discrimAttrName, String discrimAttrValue)
    {
      return getConfigurationElementForExtension(extensionPointName,
          discrimAttrName != null ? new String[] {discrimAttrName} : new String[0],
          discrimAttrValue != null ? new String[] {discrimAttrValue} : new String[0]);
    }

    /**
     * Convenience accessor for getting the ConfigurationElement(s) for an extension (of an extension point).
     * @param extensionPointName The extension point
     * @param discrimAttrName Attribute on the extension to use as discriminator
     * @param discrimAttrValue Value for discriminator attribute
     * @return Configuration elements
     */
    public ConfigurationElement[] getConfigurationElementsForExtension(String extensionPointName, String discrimAttrName, String discrimAttrValue)
    {
        List elems = getConfigurationElementsForExtension(extensionPointName,
            discrimAttrName != null ? new String[] {discrimAttrName} : new String[0],
            discrimAttrValue != null ? new String[] {discrimAttrValue} : new String[0]);
        if (!elems.isEmpty())
        {
            return elems.toArray(new ConfigurationElement[elems.size()]);
        }
        return null;
    }

    /**
     * Convenience accessor for getting the ConfigurationElement for an extension (of an extension point).
     * @param extensionPointName The extension point
     * @param discrimAttrName Attribute on the extension to use as discriminator1
     * @param discrimAttrValue Value for discriminator1 attribute
     * @return Configuration Element
     */
    public ConfigurationElement getConfigurationElementForExtension(String extensionPointName, String[] discrimAttrName, String[] discrimAttrValue)
    {
        List matchingConfigElements = getConfigurationElementsForExtension(extensionPointName, discrimAttrName, discrimAttrValue);
        if (!matchingConfigElements.isEmpty())
        {
            return matchingConfigElements.get(0);
        }
        return null;
    }

    /**
     * Internal accessor for getting getting the ConfigurationElement(s) for an extension (of an extension
     * point), sorted by their priority attribute (if defined).
     * @param extensionPointName The extension point
     * @param discrimAttrName Attributes on the exension to use as discriminator
     * @param discrimAttrValue Values for discriminator attributes
     * @return Configuration elements
     */
    private List getConfigurationElementsForExtension(String extensionPointName, String[] discrimAttrName, String[] discrimAttrValue)
    {
        List matchingConfigElements = new LinkedList<>();

        ExtensionPoint extensionPoint = getExtensionPoint(extensionPointName);
        if (extensionPoint!=null)
        {
            Extension[] ex = extensionPoint.getExtensions();
            for (int i=0; i
    {
        public int compare(ConfigurationElement elm1, ConfigurationElement elm2)
        {
            String pri1 = elm1.getAttribute("priority");
            String pri2 = elm2.getAttribute("priority");
            return (pri2 == null ? 0 : Integer.parseInt(pri2)) - (pri1 == null ? 0 : Integer.parseInt(pri1));
        }
    }

    /**
     * Convenience accessor for getting the value of an attribute for an extension (of an extension point).
     * @param extensionPoint The extension point
     * @param discrimAttrName Attribute on the extension to use as discriminator
     * @param discrimAttrValue Value for discriminator attribute
     * @param attributeName Name of the attribute whose value we want
     * @return The value of the attribute
     */
    public String getAttributeValueForExtension(String extensionPoint, String discrimAttrName, String discrimAttrValue, String attributeName)
    {
        ConfigurationElement elem = getConfigurationElementForExtension(extensionPoint, discrimAttrName, discrimAttrValue);
        if (elem != null)
        {
            return elem.getAttribute(attributeName);
        }
        return null;
    }

    /**
     * Convenience accessor for getting the value of an attribute for an extension (of an extension point).
     * @param extensionPoint The extension point
     * @param discrimAttrName Attribute on the extension to use as discriminator
     * @param discrimAttrValue Value for discriminator attribute
     * @param attributeName Name of the attribute whose value we want
     * @return The value(s) of the attribute
     */
    public String[] getAttributeValuesForExtension(String extensionPoint, String discrimAttrName, String discrimAttrValue, String attributeName)
    {
        ConfigurationElement[] elems = getConfigurationElementsForExtension(extensionPoint, discrimAttrName, discrimAttrValue);
        if (elems != null)
        {
            String[] attrValues = new String[elems.length];
            for (int i=0;i
     * 
  • datanucleus.primaryClassLoader
  • *
  • datanucleus.plugin.pluginRegistryClassName
  • *
  • datanucleus.plugin.pluginRegistryBundleCheck
  • *
  • datanucleus.plugin.allowUserBundles
  • *
  • datanucleus.plugin.validatePlugins
  • * * @param props Any properties defining the plugin manager capabilities * @param loader Any class loader to make use of when loading * @return The PluginManager */ public static PluginManager createPluginManager(Map props, ClassLoader loader) { ClassLoaderResolver clr = loader != null ? new ClassLoaderResolverImpl(loader) : new ClassLoaderResolverImpl(); if (props != null) { clr.registerUserClassLoader((ClassLoader)props.get(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY)); } Properties pluginProps = new Properties(); String registryClassName = null; if (props != null) { registryClassName = (String)props.get(PropertyNames.PROPERTY_PLUGIN_REGISTRY_CLASSNAME); if (props.containsKey(PropertyNames.PROPERTY_PLUGIN_REGISTRYBUNDLECHECK)) { pluginProps.setProperty("bundle-check-action", (String)props.get(PropertyNames.PROPERTY_PLUGIN_REGISTRYBUNDLECHECK)); } if (props.containsKey(PropertyNames.PROPERTY_PLUGIN_ALLOW_USER_BUNDLES)) { pluginProps.setProperty("allow-user-bundles", (String)props.get(PropertyNames.PROPERTY_PLUGIN_ALLOW_USER_BUNDLES)); } if (props.containsKey(PropertyNames.PROPERTY_PLUGIN_VALIDATEPLUGINS)) { pluginProps.setProperty("validate-plugins", (String)props.get(PropertyNames.PROPERTY_PLUGIN_VALIDATEPLUGINS)); } } return new PluginManager(registryClassName, clr, pluginProps); } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy