Search in sources :

Example 1 with ExtensionPoint

use of org.datanucleus.plugin.ExtensionPoint in project datanucleus-core by datanucleus.

the class OSGiBundleParser method parsePluginElements.

/**
 * Method to parse Extensions in plug-in file.
 * @param db DocumentBuilder to use for parsing
 * @param mgr the PluginManager
 * @param fileUrl URL of the plugin.xml file
 * @param plugin The Bundle
 * @param osgiBundle The OSGi Bundle
 * @return array of 2 elements. first element is a List of extensionPoints, and 2nd element is a List of
 * Extension
 * @throws NucleusException if an error occurs during parsing
 */
public static List[] parsePluginElements(DocumentBuilder db, PluginRegistry mgr, URL fileUrl, Bundle plugin, org.osgi.framework.Bundle osgiBundle) {
    List<ExtensionPoint> extensionPoints = Collections.emptyList();
    List<Extension> extensions = Collections.emptyList();
    InputStream is = null;
    InputStreamReader isr = null;
    try {
        is = fileUrl.openStream();
        isr = new InputStreamReader(is);
        Element rootElement = db.parse(new InputSource(isr)).getDocumentElement();
        if (NucleusLogger.GENERAL.isDebugEnabled()) {
            NucleusLogger.GENERAL.debug(Localiser.msg("024003", fileUrl.toString()));
        }
        extensionPoints = parseExtensionPoints(rootElement, plugin, osgiBundle);
        if (NucleusLogger.GENERAL.isDebugEnabled()) {
            NucleusLogger.GENERAL.debug(Localiser.msg("024004", fileUrl.toString()));
        }
        extensions = parseExtensions(rootElement, plugin, osgiBundle);
    } catch (NucleusException ex) {
        throw ex;
    } catch (Exception e) {
        NucleusLogger.GENERAL.error(Localiser.msg("024000", fileUrl.getFile()));
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return new List[] { extensionPoints, extensions };
}
Also used : Extension(org.datanucleus.plugin.Extension) InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusException(org.datanucleus.exceptions.NucleusException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with ExtensionPoint

use of org.datanucleus.plugin.ExtensionPoint in project datanucleus-core by datanucleus.

the class OSGiPluginRegistry method registerExtensions.

public void registerExtensions() {
    if (extensionPoints.length > 0) {
        return;
    }
    List registeringExtensions = new ArrayList();
    org.osgi.framework.Bundle bdl = FrameworkUtil.getBundle(this.getClass());
    BundleContext ctx = bdl.getBundleContext();
    if (ctx == null) {
        // TODO Any way we can handle this better? e.g force OSGi to start it?
        NucleusLogger.GENERAL.error("Bundle " + bdl.getSymbolicName() + " is in state " + bdl.getState() + " and has NULL context, so cannot register it properly!");
    } else {
        // parse the plugin files
        DocumentBuilder docBuilder = OSGiBundleParser.getDocumentBuilder();
        org.osgi.framework.Bundle[] osgiBundles = ctx.getBundles();
        for (org.osgi.framework.Bundle osgiBundle : osgiBundles) {
            URL pluginURL = osgiBundle.getEntry("plugin.xml");
            if (pluginURL == null) {
                continue;
            }
            Bundle bundle = registerBundle(osgiBundle);
            if (bundle == null) {
                // No MANIFEST.MF for this plugin.xml so ignore it
                continue;
            }
            List[] elements = OSGiBundleParser.parsePluginElements(docBuilder, this, pluginURL, bundle, osgiBundle);
            registerExtensionPointsForPluginInternal(elements[0], false);
            registeringExtensions.addAll(elements[1]);
        }
    }
    extensionPoints = extensionPointsByUniqueId.values().toArray(new ExtensionPoint[extensionPointsByUniqueId.values().size()]);
    // Register the extensions now that we have the extension-points all loaded
    for (int i = 0; i < registeringExtensions.size(); i++) {
        Extension extension = (Extension) registeringExtensions.get(i);
        ExtensionPoint exPoint = getExtensionPoint(extension.getExtensionPointId());
        if (exPoint == null) {
            if (extension.getPlugin() != null && extension.getPlugin().getSymbolicName() != null && extension.getPlugin().getSymbolicName().startsWith(DATANUCLEUS_PKG)) {
                NucleusLogger.GENERAL.warn(Localiser.msg("024002", extension.getExtensionPointId(), extension.getPlugin().getSymbolicName(), extension.getPlugin().getManifestLocation()));
            }
        } else {
            extension.setExtensionPoint(exPoint);
            exPoint.addExtension(extension);
        }
    }
}
Also used : Bundle(org.datanucleus.plugin.Bundle) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) ArrayList(java.util.ArrayList) URL(java.net.URL) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) Extension(org.datanucleus.plugin.Extension) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) List(java.util.List) BundleContext(org.osgi.framework.BundleContext)

Example 3 with ExtensionPoint

use of org.datanucleus.plugin.ExtensionPoint in project datanucleus-core by datanucleus.

the class PluginParserTest method testParseExtensionPoint.

public void testParseExtensionPoint() {
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    NonManagedPluginRegistry mgr = new NonManagedPluginRegistry(clr, "EXCEPTION", true);
    assertEquals(0, mgr.getExtensionPoints().length);
    Bundle bundle0 = mgr.registerBundle(clr.getResource("/org/datanucleus/samples/plugin/MANIFEST0.MF", null));
    mgr.registerExtensionsForPlugin(clr.getResource("/org/datanucleus/samples/plugin/plugin1expoint.xml", null), bundle0);
    assertEquals(2, mgr.getExtensionPoints().length);
    assertNull(mgr.getExtensionPoint("testID"));
    assertNull(mgr.getExtensionPoint("testID2"));
    assertNotNull(mgr.getExtensionPoint("org.datanucleus.testID"));
    assertNotNull(mgr.getExtensionPoint("org.datanucleus.testID2"));
    ExtensionPoint point = mgr.getExtensionPoint("org.datanucleus.testID");
    assertEquals("testID", point.getId());
    assertEquals("org.datanucleus.testID", point.getUniqueId());
    assertEquals("testName", point.getName());
    assertNotNull(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xsd", null));
    assertEquals(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xsd", null), point.getSchema());
    assertEquals(0, point.getExtensions().length);
    mgr.registerExtensionsForPlugin(clr.getResource("/org/datanucleus/samples/plugin/plugin1.xml", null), bundle0);
    assertEquals(2, point.getExtensions().length);
    Extension[] exts = point.getExtensions();
    assertEquals(exts[0].getPlugin(), exts[1].getPlugin());
    assertEquals(2, exts[0].getConfigurationElements().length);
    ConfigurationElement[] level1 = exts[0].getConfigurationElements();
    assertEquals(2, level1[0].getChildren().length);
    assertEquals("level1", level1[0].getName());
    assertEquals(1, level1[0].getAttributeNames().length);
    assertEquals("1", level1[0].getAttribute("attr11"));
    assertNull(level1[0].getAttribute("attr11XXX"));
    ConfigurationElement[] level2 = level1[0].getChildren();
    assertEquals(1, level2[0].getChildren().length);
    assertEquals("level2", level2[0].getName());
    assertEquals(2, level2[0].getAttributeNames().length);
    assertEquals("attr21", level2[0].getAttributeNames()[0]);
    assertEquals("attr22", level2[0].getAttributeNames()[1]);
    assertEquals("2211", level2[0].getAttribute("attr21"));
    assertEquals("2221", level2[0].getAttribute("attr22"));
    assertNull(level2[0].getAttribute("attr11XXX"));
    assertEquals(0, level1[1].getChildren().length);
    assertEquals("2", level1[1].getAttribute("attr11"));
    assertEquals(1, exts[1].getConfigurationElements().length);
    level1 = exts[1].getConfigurationElements();
    assertEquals("A", level1[0].getAttribute("attr11"));
    assertEquals(0, level1[0].getChildren().length);
}
Also used : Extension(org.datanucleus.plugin.Extension) ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) NonManagedPluginRegistry(org.datanucleus.plugin.NonManagedPluginRegistry) Bundle(org.datanucleus.plugin.Bundle) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 4 with ExtensionPoint

use of org.datanucleus.plugin.ExtensionPoint in project datanucleus-core by datanucleus.

the class OSGiBundleParser method parseExtensionPoints.

/**
 * Method to parse ExtensionPoints from plug-in file
 * @param rootElement the root element of the plugin xml
 * @param plugin the plugin bundle
 * @param clr the ClassLoaderResolver
 * @return a List of extensionPoints, if any
 * @throws NucleusException if an error occurs during parsing
 */
private static List<ExtensionPoint> parseExtensionPoints(Element rootElement, Bundle plugin, org.osgi.framework.Bundle osgiBundle) {
    List<ExtensionPoint> extensionPoints = new ArrayList<ExtensionPoint>();
    try {
        NodeList elements = rootElement.getElementsByTagName("extension-point");
        for (int i = 0; i < elements.getLength(); i++) {
            Element element = (Element) elements.item(i);
            String id = element.getAttribute("id").trim();
            String name = element.getAttribute("name");
            String schema = element.getAttribute("schema");
            extensionPoints.add(new ExtensionPoint(id, name, osgiBundle.getEntry(schema), plugin));
        }
    } catch (NucleusException ex) {
        throw ex;
    }
    return extensionPoints;
}
Also used : ExtensionPoint(org.datanucleus.plugin.ExtensionPoint) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NucleusException(org.datanucleus.exceptions.NucleusException) ExtensionPoint(org.datanucleus.plugin.ExtensionPoint)

Example 5 with ExtensionPoint

use of org.datanucleus.plugin.ExtensionPoint in project datanucleus-core by datanucleus.

the class OSGiPluginRegistry method registerExtensionPointsForPluginInternal.

/**
 * Register extension-points for the specified plugin.
 * @param extPoints ExtensionPoints for this plugin
 * @param updateExtensionPointsArray Whether to update "extensionPoints" array
 */
protected void registerExtensionPointsForPluginInternal(List extPoints, boolean updateExtensionPointsArray) {
    // Register extension-points
    Iterator<ExtensionPoint> pluginExtPointIter = extPoints.iterator();
    while (pluginExtPointIter.hasNext()) {
        ExtensionPoint exPoint = pluginExtPointIter.next();
        extensionPointsByUniqueId.put(exPoint.getUniqueId(), exPoint);
    }
    if (updateExtensionPointsArray) {
        extensionPoints = extensionPointsByUniqueId.values().toArray(new ExtensionPoint[extensionPointsByUniqueId.values().size()]);
    }
}
Also used : ExtensionPoint(org.datanucleus.plugin.ExtensionPoint)

Aggregations

ExtensionPoint (org.datanucleus.plugin.ExtensionPoint)5 ArrayList (java.util.ArrayList)3 Extension (org.datanucleus.plugin.Extension)3 List (java.util.List)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 Bundle (org.datanucleus.plugin.Bundle)2 Element (org.w3c.dom.Element)2 NodeList (org.w3c.dom.NodeList)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)1 ConfigurationElement (org.datanucleus.plugin.ConfigurationElement)1 NonManagedPluginRegistry (org.datanucleus.plugin.NonManagedPluginRegistry)1 BundleContext (org.osgi.framework.BundleContext)1 InputSource (org.xml.sax.InputSource)1