Search in sources :

Example 6 with Dom

use of org.jvnet.hk2.config.Dom in project Payara by payara.

the class TargetBasedResolver method getTarget.

private <T extends ConfigBeanProxy> T getTarget(Class<? extends ConfigBeanProxy> targetType, Class<T> type) throws ClassNotFoundException {
    // when using the target based parameter, we look first for a configuration of that name,
    // then we look for a cluster of that name and finally we look for a subelement of the right type
    final String name = getName();
    ConfigBeanProxy config = habitat.getService(targetType, target);
    if (config != null) {
        try {
            return type.cast(config);
        } catch (ClassCastException e) {
        // ok we need to do more work to find which object is really requested.
        }
        Dom parentDom = Dom.unwrap(config);
        String elementName = GenericCrudCommand.elementName(parentDom.document, targetType, type);
        if (elementName == null) {
            return null;
        }
        ConfigModel.Property property = parentDom.model.getElement(elementName);
        if (property.isCollection()) {
            Collection<Dom> collection;
            synchronized (parentDom) {
                collection = parentDom.nodeElements(elementName);
            }
            if (collection == null) {
                return null;
            }
            for (Dom child : collection) {
                if (name.equals(child.attribute("ref"))) {
                    return type.cast(child.<ConfigBeanProxy>createProxy());
                }
            }
        }
    }
    return null;
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Dom(org.jvnet.hk2.config.Dom) ConfigModel(org.jvnet.hk2.config.ConfigModel)

Example 7 with Dom

use of org.jvnet.hk2.config.Dom in project Payara by payara.

the class DefaultValueTest method rawAttributeTest.

@Test
public void rawAttributeTest() throws NoSuchMethodException {
    String address = listener.getAddress();
    Dom raw = Dom.unwrap(listener);
    Attribute attr = raw.getProxyType().getMethod("getAddress").getAnnotation(Attribute.class);
    assertEquals(attr.defaultValue(), address);
    assertEquals(raw.attribute("address"), address);
    assertEquals(raw.rawAttribute("address"), address);
}
Also used : Dom(org.jvnet.hk2.config.Dom) Attribute(org.jvnet.hk2.config.Attribute) Test(org.junit.Test)

Example 8 with Dom

use of org.jvnet.hk2.config.Dom in project Payara by payara.

the class ConfigModularityUtils method getCurrentConfigBeanForDefaultValue.

public <T extends ConfigBeanProxy> T getCurrentConfigBeanForDefaultValue(ConfigBeanDefaultValue defaultValue) throws InvocationTargetException, IllegalAccessException {
    // TODO make this method target aware!
    Class parentClass = getOwningClassForLocation(defaultValue.getLocation());
    Class configBeanClass = getClassForFullName(defaultValue.getConfigBeanClassName());
    Method m = findSuitableCollectionGetter(parentClass, configBeanClass);
    if (m != null) {
        ConfigParser configParser = new ConfigParser(serviceLocator);
        // I don't use the GlassFish document here as I don't need persistence
        final DomDocument doc = new DomDocument<GlassFishConfigBean>(serviceLocator) {

            @Override
            public Dom make(final ServiceLocator serviceLocator, XMLStreamReader xmlStreamReader, GlassFishConfigBean dom, ConfigModel configModel) {
                // by default, people get the translated view.
                return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
            }
        };
        ConfigBeanProxy parent = getOwningObject(defaultValue.getLocation());
        ConfigurationPopulator populator = new ConfigurationPopulator(defaultValue.getXmlConfiguration(), doc, parent);
        populator.run(configParser);
        ConfigBeanProxy configBean = doc.getRoot().createProxy(configBeanClass);
        Collection col = (Collection) m.invoke(parent);
        return (T) getConfigBeanFromCollection(col, configBean, configBeanClass);
    }
    return null;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) XMLStreamReader(javax.xml.stream.XMLStreamReader) ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) GlassFishConfigBean(org.glassfish.config.support.GlassFishConfigBean) ConfigurationPopulator(com.sun.enterprise.config.modularity.parser.ConfigurationPopulator) ConfigParser(org.jvnet.hk2.config.ConfigParser) Collection(java.util.Collection) Method(java.lang.reflect.Method) DomDocument(org.jvnet.hk2.config.DomDocument)

Example 9 with Dom

use of org.jvnet.hk2.config.Dom in project Payara by payara.

the class ConfigModularityUtils method serializeConfigBean.

public String serializeConfigBean(ConfigBeanProxy configBean) {
    if (configBean == null)
        return null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;
    IndentingXMLStreamWriter indentingXMLStreamWriter = null;
    String s = null;
    try {
        writer = xmlFactory.createXMLStreamWriter(new BufferedOutputStream(bos));
        indentingXMLStreamWriter = new IndentingXMLStreamWriter(writer);
        Dom configBeanDom = Dom.unwrap(configBean);
        configBeanDom.writeTo(configBeanDom.model.getTagName(), indentingXMLStreamWriter);
        indentingXMLStreamWriter.flush();
        s = bos.toString();
    } catch (XMLStreamException e) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
        }
        return null;
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (writer != null)
                writer.close();
            if (indentingXMLStreamWriter != null)
                indentingXMLStreamWriter.close();
        } catch (IOException e) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
            }
        } catch (XMLStreamException e) {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "Cannot serialize the configbean: " + configBean.toString(), e);
            }
        }
    }
    return s;
}
Also used : IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Dom(org.jvnet.hk2.config.Dom) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) IndentingXMLStreamWriter(org.jvnet.hk2.config.IndentingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 10 with Dom

use of org.jvnet.hk2.config.Dom in project Payara by payara.

the class ConfigurationParser method parseAndSetConfigBean.

/**
 * @param <T> the ConfigBeanProxy type we are looking for
 */
public <T extends ConfigBeanProxy> void parseAndSetConfigBean(List<ConfigBeanDefaultValue> values) {
    ConfigParser configParser = new ConfigParser(serviceLocator);
    // I don't use the GlassFish document here as I don't need persistence
    final DomDocument doc = new DomDocument<GlassFishConfigBean>(serviceLocator) {

        @Override
        public Dom make(final ServiceLocator serviceLocator, XMLStreamReader xmlStreamReader, GlassFishConfigBean dom, ConfigModel configModel) {
            return new GlassFishConfigBean(serviceLocator, this, dom, configModel, xmlStreamReader);
        }
    };
    // the solution is to put the loop into the apply method...  But it would be some fine amount of work
    for (final ConfigBeanDefaultValue configBeanDefaultValue : values) {
        final ConfigBeanProxy parent = configModularityUtils.getOwningObject(configBeanDefaultValue.getLocation());
        if (parent == null)
            continue;
        ConfigurationPopulator populator = null;
        if (replaceSystemProperties)
            try {
                populator = new ConfigurationPopulator(configModularityUtils.replacePropertiesWithCurrentValue(configBeanDefaultValue.getXmlConfiguration(), configBeanDefaultValue), doc, parent);
            } catch (Exception e) {
                LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
            }
        else {
            // Check that parent is not null!
            populator = new ConfigurationPopulator(configBeanDefaultValue.getXmlConfiguration(), doc, parent);
        }
        populator.run(configParser);
        synchronized (configModularityUtils) {
            boolean oldValue = configModularityUtils.isIgnorePersisting();
            try {
                Class configBeanClass = configModularityUtils.getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
                final ConfigBeanProxy pr = doc.getRoot().createProxy(configBeanClass);
                configModularityUtils.setIgnorePersisting(true);
                ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {

                    public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
                        configModularityUtils.setConfigBean(pr, configBeanDefaultValue, param);
                        return param;
                    }
                }, parent);
            } catch (TransactionFailure e) {
                LOG.log(Level.SEVERE, ConfigApiLoggerInfo.CFG_EXT_ADD_FAILED, e);
            } finally {
                configModularityUtils.setIgnorePersisting(oldValue);
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) XMLStreamReader(javax.xml.stream.XMLStreamReader) ConfigParser(org.jvnet.hk2.config.ConfigParser) PropertyVetoException(java.beans.PropertyVetoException) DomDocument(org.jvnet.hk2.config.DomDocument) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) PropertyVetoException(java.beans.PropertyVetoException) ConfigModel(org.jvnet.hk2.config.ConfigModel) ConfigBeanDefaultValue(com.sun.enterprise.config.modularity.customization.ConfigBeanDefaultValue) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) GlassFishConfigBean(org.glassfish.config.support.GlassFishConfigBean)

Aggregations

Dom (org.jvnet.hk2.config.Dom)37 ConfigModel (org.jvnet.hk2.config.ConfigModel)14 Domain (com.sun.enterprise.config.serverbeans.Domain)12 DomDocument (org.jvnet.hk2.config.DomDocument)9 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)9 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)8 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)8 PropertyVetoException (java.beans.PropertyVetoException)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 GET (javax.ws.rs.GET)5 Produces (javax.ws.rs.Produces)5 MultiException (org.glassfish.hk2.api.MultiException)5 Config (com.sun.enterprise.config.serverbeans.Config)4 TreeMap (java.util.TreeMap)4 ConfigParser (org.jvnet.hk2.config.ConfigParser)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ResourcesGenerator (org.glassfish.admin.rest.generator.ResourcesGenerator)3 ExampleServiceConfiguration (fish.payara.service.example.config.ExampleServiceConfiguration)2