Search in sources :

Example 1 with Extension

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

the class NucleusContextHelper method createStoreManagerForProperties.

/**
 * Method to create a StoreManager based on the specified properties passed in.
 * @param props The overall persistence properties
 * @param datastoreProps Persistence properties to apply to the datastore
 * @param clr ClassLoader resolver
 * @param nucCtx NucleusContext
 * @return The StoreManager
 * @throws NucleusUserException if impossible to create the StoreManager (not in CLASSPATH?, invalid definition?)
 */
public static StoreManager createStoreManagerForProperties(Map<String, Object> props, Map<String, Object> datastoreProps, ClassLoaderResolver clr, NucleusContext nucCtx) {
    Extension[] exts = nucCtx.getPluginManager().getExtensionPoint("org.datanucleus.store_manager").getExtensions();
    Class[] ctrArgTypes = new Class[] { ClassConstants.CLASS_LOADER_RESOLVER, ClassConstants.PERSISTENCE_NUCLEUS_CONTEXT, Map.class };
    Object[] ctrArgs = new Object[] { clr, nucCtx, datastoreProps };
    StoreManager storeMgr = null;
    // Try using the URL of the data source
    String url = (String) props.get(PropertyNames.PROPERTY_CONNECTION_URL.toLowerCase());
    if (url != null) {
        int idx = url.indexOf(':');
        if (idx > -1) {
            url = url.substring(0, idx);
        }
        for (int e = 0; storeMgr == null && e < exts.length; e++) {
            ConfigurationElement[] confElm = exts[e].getConfigurationElements();
            for (int c = 0; storeMgr == null && c < confElm.length; c++) {
                String urlKey = confElm[c].getAttribute("url-key");
                if (url == null || urlKey.equalsIgnoreCase(url)) {
                    // Either no URL, or url defined so take this StoreManager
                    try {
                        storeMgr = (StoreManager) nucCtx.getPluginManager().createExecutableExtension("org.datanucleus.store_manager", "url-key", url == null ? urlKey : url, "class-name", ctrArgTypes, ctrArgs);
                    } catch (InvocationTargetException ex) {
                        Throwable t = ex.getTargetException();
                        if (t instanceof RuntimeException) {
                            throw (RuntimeException) t;
                        } else if (t instanceof Error) {
                            throw (Error) t;
                        } else {
                            throw new NucleusException(t.getMessage(), t).setFatal();
                        }
                    } catch (Exception ex) {
                        throw new NucleusException(ex.getMessage(), ex).setFatal();
                    }
                }
            }
        }
    } else {
        // Assumed to be using RDBMS since only that allows ConnectionFactory/ConnectionFactoryName TODO If any other stores start supporting ConnectionFactory then update this
        try {
            storeMgr = (StoreManager) nucCtx.getPluginManager().createExecutableExtension("org.datanucleus.store_manager", "key", "rdbms", "class-name", ctrArgTypes, ctrArgs);
        } catch (InvocationTargetException ex) {
            Throwable t = ex.getTargetException();
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else if (t instanceof Error) {
                throw (Error) t;
            } else {
                throw new NucleusException(t.getMessage(), t).setFatal();
            }
        } catch (Exception ex) {
            throw new NucleusException(ex.getMessage(), ex).setFatal();
        }
    }
    if (storeMgr == null) {
        throw new NucleusUserException(Localiser.msg("008004", url)).setFatal();
    }
    return storeMgr;
}
Also used : ConfigurationElement(org.datanucleus.plugin.ConfigurationElement) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransactionIsolationNotSupportedException(org.datanucleus.exceptions.TransactionIsolationNotSupportedException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StoreManager(org.datanucleus.store.StoreManager) Extension(org.datanucleus.plugin.Extension) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 2 with Extension

use of org.datanucleus.plugin.Extension 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 3 with Extension

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

the class OSGiBundleParser method parseExtensions.

/**
 * Method to parse Extensions 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 extensions, if any
 * @throws NucleusException if an error occurs during parsing
 */
private static List<Extension> parseExtensions(Element rootElement, Bundle plugin, org.osgi.framework.Bundle osgiBundle) {
    List<Extension> extensions = new ArrayList<Extension>();
    try {
        NodeList elements = rootElement.getElementsByTagName("extension");
        for (int i = 0; i < elements.getLength(); i++) {
            Element element = (Element) elements.item(i);
            Extension ex = new Extension(element.getAttribute("point"), plugin);
            NodeList elms = element.getChildNodes();
            extensions.add(ex);
            for (int e = 0; e < elms.getLength(); e++) {
                if (elms.item(e) instanceof Element) {
                    ex.addConfigurationElement(PluginParser.parseConfigurationElement(ex, (Element) elms.item(e), null));
                }
            }
        }
    } catch (NucleusException ex) {
        throw ex;
    }
    return extensions;
}
Also used : Extension(org.datanucleus.plugin.Extension) 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 4 with Extension

use of org.datanucleus.plugin.Extension 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 5 with Extension

use of org.datanucleus.plugin.Extension 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)

Aggregations

Extension (org.datanucleus.plugin.Extension)6 ExtensionPoint (org.datanucleus.plugin.ExtensionPoint)4 ArrayList (java.util.ArrayList)3 NucleusException (org.datanucleus.exceptions.NucleusException)3 Bundle (org.datanucleus.plugin.Bundle)3 List (java.util.List)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)2 ConfigurationElement (org.datanucleus.plugin.ConfigurationElement)2 NonManagedPluginRegistry (org.datanucleus.plugin.NonManagedPluginRegistry)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 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 Map (java.util.Map)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1