Search in sources :

Example 1 with ConfigParser

use of org.jvnet.hk2.config.ConfigParser 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 2 with ConfigParser

use of org.jvnet.hk2.config.ConfigParser 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)

Example 3 with ConfigParser

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

the class ConfigurationPopulator method run.

public void run(ConfigParser parser) {
    try {
        InputStream is = new ByteArrayInputStream(xmlContent.getBytes());
        XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is, "utf-8");
        parser.parse(reader, doc, Dom.unwrap((ConfigBeanProxy) parent));
    } catch (XMLStreamException e) {
        LOG.log(Level.SEVERE, ConfigApiLoggerInfo.DEFAULT_CFG_READ_FAILED, e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 4 with ConfigParser

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

the class DomainXml method parseDomainXml.

/**
 * Parses <tt>domain.xml</tt>
 * @param parser
 * @param domainXml
 * @param serverName
 */
protected void parseDomainXml(ConfigParser parser, final URL domainXml, final String serverName) {
    long startNano = System.nanoTime();
    try {
        ServerReaderFilter xsr = null;
        // Set the resolver so that any external entity references, such
        // as a reference to a DTD, return an empty file.  The domain.xml
        // file doesn't support entity references.
        xif.setXMLResolver(new XMLResolver() {

            @Override
            public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
                return new ByteArrayInputStream(new byte[0]);
            }
        });
        if (null == env.getRuntimeType()) {
            throw new RuntimeException("Internal Error: Unknown server type: " + env.getRuntimeType());
        } else {
            switch(env.getRuntimeType()) {
                case DAS:
                case EMBEDDED:
                case MICRO:
                    xsr = new DasReaderFilter(domainXml, xif);
                    break;
                case INSTANCE:
                    xsr = new InstanceReaderFilter(env.getInstanceName(), domainXml, xif);
                    break;
                default:
                    throw new RuntimeException("Internal Error: Unknown server type: " + env.getRuntimeType());
            }
        }
        Lock lock = null;
        try {
            // lock the domain.xml for reading if not embedded
            try {
                lock = configAccess.accessRead();
            } catch (Exception e) {
            // ignore
            }
            parser.parse(xsr, getDomDocument());
            xsr.close();
        } finally {
            if (lock != null) {
                lock.unlock();
            }
        }
        String errorMessage = xsr.configWasFound();
        if (errorMessage != null) {
            LogRecord lr = new LogRecord(Level.WARNING, errorMessage);
            lr.setLoggerName(getClass().getName());
            EarlyLogHandler.earlyMessages.add(lr);
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException("Fatal Error.  Unable to parse " + domainXml, e);
        }
    }
    Long l = System.nanoTime() - startNano;
    LogRecord lr = new LogRecord(Level.FINE, totalTimeToParseDomain + l.toString());
    lr.setLoggerName(getClass().getName());
    EarlyLogHandler.earlyMessages.add(lr);
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ConfigPopulatorException(org.jvnet.hk2.config.ConfigPopulatorException) Lock(java.util.concurrent.locks.Lock) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) LogRecord(java.util.logging.LogRecord) XMLResolver(javax.xml.stream.XMLResolver)

Example 5 with ConfigParser

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

the class ConfigTest method parseDomainXml.

// @Test
public void parseDomainXml() {
    ConfigParser parser = new ConfigParser(habitat);
    URL url = this.getClass().getResource("/domain.xml");
    System.out.println("URL : " + url);
    try {
        DomDocument doc = parser.parse(url);
        System.out.println("[parseDomainXml] ==> Successfully parsed");
        assert (doc != null);
    } catch (Exception ex) {
        ex.printStackTrace();
        assert (false);
    }
}
Also used : ConfigParser(org.jvnet.hk2.config.ConfigParser) URL(java.net.URL) PropertyVetoException(java.beans.PropertyVetoException) DomDocument(org.jvnet.hk2.config.DomDocument)

Aggregations

ConfigParser (org.jvnet.hk2.config.ConfigParser)12 DomDocument (org.jvnet.hk2.config.DomDocument)12 URL (java.net.URL)8 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)6 PropertyVetoException (java.beans.PropertyVetoException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 LogRecord (java.util.logging.LogRecord)2 GlassFishConfigBean (org.glassfish.config.support.GlassFishConfigBean)2 MultiMap (org.jvnet.hk2.component.MultiMap)2 ConfigModel (org.jvnet.hk2.config.ConfigModel)2 ConfigPopulatorException (org.jvnet.hk2.config.ConfigPopulatorException)2