Search in sources :

Example 56 with Attribute

use of org.dom4j.Attribute in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getIdClass.

private IdClass getIdClass(Element tree, XMLContext.Default defaults) {
    Element element = tree == null ? null : tree.element("id-class");
    if (element != null) {
        Attribute attr = element.attribute("class");
        if (attr != null) {
            AnnotationDescriptor ad = new AnnotationDescriptor(IdClass.class);
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(attr.getValue(), defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find id-class: " + attr.getValue(), e);
            }
            ad.setValue("value", clazz);
            return AnnotationFactory.create(ad);
        } else {
            throw new AnnotationException("id-class without class. " + SCHEMA_VALIDATION);
        }
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(IdClass.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) IdClass(javax.persistence.IdClass) Attribute(org.dom4j.Attribute) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 57 with Attribute

use of org.dom4j.Attribute in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getInheritance.

private Inheritance getInheritance(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("inheritance") : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Inheritance.class);
        Attribute attr = element.attribute("strategy");
        InheritanceType strategy = InheritanceType.SINGLE_TABLE;
        if (attr != null) {
            String value = attr.getValue();
            if ("SINGLE_TABLE".equals(value)) {
                strategy = InheritanceType.SINGLE_TABLE;
            } else if ("JOINED".equals(value)) {
                strategy = InheritanceType.JOINED;
            } else if ("TABLE_PER_CLASS".equals(value)) {
                strategy = InheritanceType.TABLE_PER_CLASS;
            } else {
                throw new AnnotationException("Unknown InheritanceType in XML: " + value + " (" + SCHEMA_VALIDATION + ")");
            }
        }
        ad.setValue("strategy", strategy);
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(Inheritance.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Attribute(org.dom4j.Attribute) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) Inheritance(javax.persistence.Inheritance) AnnotationException(org.hibernate.AnnotationException) InheritanceType(javax.persistence.InheritanceType)

Example 58 with Attribute

use of org.dom4j.Attribute in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method applyXmlDefinedConverts.

private void applyXmlDefinedConverts(Element containingElement, XMLContext.Default defaults, String attributeNamePrefix, Map<String, Convert> convertAnnotationsMap) {
    final List<Element> convertElements = containingElement.elements("convert");
    for (Element convertElement : convertElements) {
        final AnnotationDescriptor convertAnnotationDescriptor = new AnnotationDescriptor(Convert.class);
        copyStringAttribute(convertAnnotationDescriptor, convertElement, "attribute-name", false);
        copyBooleanAttribute(convertAnnotationDescriptor, convertElement, "disable-conversion");
        final Attribute converterClassAttr = convertElement.attribute("converter");
        if (converterClassAttr != null) {
            final String converterClassName = XMLContext.buildSafeClassName(converterClassAttr.getValue(), defaults);
            try {
                final Class converterClass = classLoaderAccess.classForName(converterClassName);
                convertAnnotationDescriptor.setValue("converter", converterClass);
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find specified converter class id-class: " + converterClassName, e);
            }
        }
        final Convert convertAnnotation = AnnotationFactory.create(convertAnnotationDescriptor);
        final String qualifiedAttributeName = qualifyConverterAttributeName(attributeNamePrefix, convertAnnotation.attributeName());
        convertAnnotationsMap.put(qualifiedAttributeName, convertAnnotation);
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Convert(javax.persistence.Convert) Attribute(org.dom4j.Attribute) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 59 with Attribute

use of org.dom4j.Attribute in project Openfire by igniterealtime.

the class ConfigManager method saveOptionSetting.

/**
     * Helper function designed to handle saving option type.
     *
     * @param node Node describing the configuration item.
     * @param options Options passed from form.
     */
private void saveOptionSetting(Element node, HashMap<String, String> options) {
    Attribute type = node.attribute("type");
    if (type.getText().equals("text")) {
        // Required fields
        Attribute desckey = node.attribute("desckey");
        Attribute var = node.attribute("var");
        Attribute sysprop = node.attribute("sysprop");
        if (desckey == null || var == null || sysprop == null) {
            Log.error("Missing variable from options config.");
            return;
        }
        // Process any variables that we are setting.
        JiveGlobals.setProperty(sysprop.getText(), options.get(var.getText()));
    } else if (type.getText().equals("toggle")) {
        // Required fields
        Attribute desckey = node.attribute("desckey");
        Attribute var = node.attribute("var");
        Attribute sysprop = node.attribute("sysprop");
        if (desckey == null || var == null || sysprop == null) {
            Log.error("Missing variable from options config.");
            return;
        }
        // Process any variables that we are setting.
        JiveGlobals.setProperty(sysprop.getText(), options.get(var.getText()));
        // Process any sub-fields.
        for (Object itemObj : node.elements("item")) {
            Element item = (Element) itemObj;
            Log.debug("Found web item " + item);
            saveOptionSetting(item, options);
        }
    }
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element)

Example 60 with Attribute

use of org.dom4j.Attribute in project Openfire by igniterealtime.

the class PluginManager method loadPlugin.

/**
     * Loads a plugin.
     *
     * @param pluginDir the plugin directory.
     */
boolean loadPlugin(Path pluginDir) {
    // Only load the admin plugin during setup mode.
    final String pluginName = pluginDir.getFileName().toString();
    if (XMPPServer.getInstance().isSetupMode() && !(pluginName.equals("admin"))) {
        return false;
    }
    if (failureToLoadCount.containsKey(pluginName) && failureToLoadCount.get(pluginName) > JiveGlobals.getIntProperty("plugins.loading.retries", 5)) {
        Log.debug("The unloaded file for plugin '{}' is silently ignored, as it has failed to load repeatedly.", pluginName);
        return false;
    }
    Log.debug("Loading plugin '{}'...", pluginName);
    try {
        final Path pluginConfig = pluginDir.resolve("plugin.xml");
        if (!Files.exists(pluginConfig)) {
            Log.warn("Plugin '{}' could not be loaded: no plugin.xml file found.", pluginName);
            // Don't retry - this cannot be recovered from.
            failureToLoadCount.put(pluginName, Integer.MAX_VALUE);
            return false;
        }
        final SAXReader saxReader = new SAXReader();
        saxReader.setEncoding("UTF-8");
        final Document pluginXML = saxReader.read(pluginConfig.toFile());
        // See if the plugin specifies a version of Openfire required to run.
        final Element minServerVersion = (Element) pluginXML.selectSingleNode("/plugin/minServerVersion");
        if (minServerVersion != null) {
            final Version requiredVersion = new Version(minServerVersion.getTextTrim());
            final Version currentVersion = XMPPServer.getInstance().getServerInfo().getVersion();
            if (requiredVersion.isNewerThan(currentVersion)) {
                Log.warn("Ignoring plugin '{}': requires server version {}. Current server version is {}.", pluginName, requiredVersion, currentVersion);
                // Don't retry - this cannot be recovered from.
                failureToLoadCount.put(pluginName, Integer.MAX_VALUE);
                return false;
            }
        }
        // Properties to be used to load external resources. When set, plugin is considered to run in DEV mode.
        final String devModeClassesDir = System.getProperty(pluginName + ".classes");
        final String devModewebRoot = System.getProperty(pluginName + ".webRoot");
        final boolean devMode = devModewebRoot != null || devModeClassesDir != null;
        final PluginDevEnvironment dev = (devMode ? configurePluginDevEnvironment(pluginDir, devModeClassesDir, devModewebRoot) : null);
        // Initialize the plugin class loader, which is either a new instance, or a the loader from a parent plugin.
        final PluginClassLoader pluginLoader;
        // Check to see if this is a child plugin of another plugin. If it is, we re-use the parent plugin's class
        // loader so that the plugins can interact.
        String parentPluginName = null;
        Plugin parentPlugin = null;
        final Element parentPluginNode = (Element) pluginXML.selectSingleNode("/plugin/parentPlugin");
        if (parentPluginNode != null) {
            // The name of the parent plugin as specified in plugin.xml might have incorrect casing. Lookup the correct name.
            for (final Map.Entry<String, Plugin> entry : plugins.entrySet()) {
                if (entry.getKey().equalsIgnoreCase(parentPluginNode.getTextTrim())) {
                    parentPluginName = entry.getKey();
                    parentPlugin = entry.getValue();
                    break;
                }
            }
            // See if the parent is loaded.
            if (parentPlugin == null) {
                Log.info("Unable to load plugin '{}': parent plugin '{}' has not been loaded.", pluginName, parentPluginNode.getTextTrim());
                Integer count = failureToLoadCount.get(pluginName);
                if (count == null) {
                    count = 0;
                }
                failureToLoadCount.put(pluginName, ++count);
                return false;
            }
            pluginLoader = classloaders.get(parentPlugin);
        } else {
            // This is not a child plugin, so create a new class loader.
            pluginLoader = new PluginClassLoader();
        }
        // Add the plugin sources to the classloaded.
        pluginLoader.addDirectory(pluginDir.toFile(), devMode);
        // When running in DEV mode, add optional other sources too.
        if (dev != null && dev.getClassesDir() != null) {
            pluginLoader.addURLFile(dev.getClassesDir().toURI().toURL());
        }
        // Instantiate the plugin!
        final String className = pluginXML.selectSingleNode("/plugin/class").getText().trim();
        final Plugin plugin = (Plugin) pluginLoader.loadClass(className).newInstance();
        // Bookkeeping!
        classloaders.put(plugin, pluginLoader);
        plugins.put(pluginName, plugin);
        pluginDirs.put(plugin, pluginDir);
        if (dev != null) {
            pluginDevelopment.put(plugin, dev);
        }
        // If this is a child plugin, register it as such.
        if (parentPlugin != null) {
            List<String> childrenPlugins = parentPluginMap.get(parentPlugin);
            if (childrenPlugins == null) {
                childrenPlugins = new ArrayList<>();
                parentPluginMap.put(parentPlugin, childrenPlugins);
            }
            childrenPlugins.add(pluginName);
            // Also register child to parent relationship.
            childPluginMap.put(plugin, parentPluginName);
        }
        // Check the plugin's database schema (if it requires one).
        if (!DbConnectionManager.getSchemaManager().checkPluginSchema(plugin)) {
            // The schema was not there and auto-upgrade failed.
            Log.error("Error while loading plugin '{}': {}", pluginName, LocaleUtils.getLocalizedString("upgrade.database.failure"));
        }
        // Load any JSP's defined by the plugin.
        final Path webXML = pluginDir.resolve("web").resolve("WEB-INF").resolve("web.xml");
        if (Files.exists(webXML)) {
            PluginServlet.registerServlets(this, plugin, webXML.toFile());
        }
        // Load any custom-defined servlets.
        final Path customWebXML = pluginDir.resolve("web").resolve("WEB-INF").resolve("web-custom.xml");
        if (Files.exists(customWebXML)) {
            PluginServlet.registerServlets(this, plugin, customWebXML.toFile());
        }
        // Configure caches of the plugin
        configureCaches(pluginDir, pluginName);
        // Initialze the plugin.
        final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(pluginLoader);
        plugin.initializePlugin(this, pluginDir.toFile());
        Log.debug("Initialized plugin '{}'.", pluginName);
        Thread.currentThread().setContextClassLoader(oldLoader);
        // If there a <adminconsole> section defined, register it.
        final Element adminElement = (Element) pluginXML.selectSingleNode("/plugin/adminconsole");
        if (adminElement != null) {
            final Element appName = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/appname");
            if (appName != null) {
                // Set the plugin name so that the proper i18n String can be loaded.
                appName.addAttribute("plugin", pluginName);
            }
            // If global images are specified, override their URL.
            Element imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/logo-image");
            if (imageEl != null) {
                imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText());
                // Set the plugin name so that the proper i18n String can be loaded.
                imageEl.addAttribute("plugin", pluginName);
            }
            imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/login-image");
            if (imageEl != null) {
                imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText());
                // Set the plugin name so that the proper i18n String can be loaded.
                imageEl.addAttribute("plugin", pluginName);
            }
            // Modify all the URL's in the XML so that they are passed through the plugin servlet correctly.
            final List urls = adminElement.selectNodes("//@url");
            for (final Object url : urls) {
                final Attribute attr = (Attribute) url;
                attr.setValue("plugins/" + pluginName + "/" + attr.getValue());
            }
            // In order to internationalize the names and descriptions in the model, we add a "plugin" attribute to
            // each tab, sidebar, and item so that the the renderer knows where to load the i18n Strings from.
            final String[] elementNames = new String[] { "tab", "sidebar", "item" };
            for (final String elementName : elementNames) {
                final List values = adminElement.selectNodes("//" + elementName);
                for (final Object value : values) {
                    final Element element = (Element) value;
                    // Make sure there's a name or description. Otherwise, no need to i18n settings.
                    if (element.attribute("name") != null || element.attribute("value") != null) {
                        element.addAttribute("plugin", pluginName);
                    }
                }
            }
            AdminConsole.addModel(pluginName, adminElement);
        }
        firePluginCreatedEvent(pluginName, plugin);
        Log.info("Successfully loaded plugin '{}'.", pluginName);
        return true;
    } catch (Throwable e) {
        Log.error("An exception occurred while loading plugin '{}':", pluginName, e);
        Integer count = failureToLoadCount.get(pluginName);
        if (count == null) {
            count = 0;
        }
        failureToLoadCount.put(pluginName, ++count);
        return false;
    }
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document) Version(org.jivesoftware.util.Version)

Aggregations

Attribute (org.dom4j.Attribute)114 Element (org.dom4j.Element)88 Document (org.dom4j.Document)30 ArrayList (java.util.ArrayList)28 List (java.util.List)26 Iterator (java.util.Iterator)23 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 File (java.io.File)11 SAXReader (org.dom4j.io.SAXReader)11 IOException (java.io.IOException)10 Map (java.util.Map)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 QTIObject (org.olat.ims.qti.editor.beecom.objects.QTIObject)6 Namespace (org.dom4j.Namespace)5 QName (org.dom4j.QName)5 PropertyString (org.pentaho.commons.util.repository.type.PropertyString)5 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 OutcomesProcessing (org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3