Search in sources :

Example 11 with ConfigBeanProxy

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

the class ConfigModularityUtils method getOwningObject.

public ConfigBeanProxy getOwningObject(String location) {
    if (!location.startsWith("domain/configs")) {
        if (!location.startsWith("domain")) {
            // Sorry only know domain and below :D
            return null;
        }
        StringTokenizer tokenizer = new StringTokenizer(location, "/", false);
        // something directly inside the domain itself as we know one token is domain for sure
        if (tokenizer.countTokens() == 1) {
            return serviceLocator.getService(Domain.class);
        }
        location = location.substring(location.indexOf("/", "domain".length()) + 1);
        tokenizer = new StringTokenizer(location, "/", false);
        ConfigBeanProxy parent = serviceLocator.getService(Domain.class);
        // skipping the domain itself as a token, we know it and took it away.
        String parentElement = "domain";
        String childElement = null;
        while (tokenizer.hasMoreTokens()) {
            try {
                childElement = tokenizer.nextToken();
                parent = getOwner(parent, parentElement, childElement);
                parentElement = childElement;
            } catch (Exception e) {
                LogHelper.log(LOG, Level.INFO, cannotGetParentConfigBean, e, childElement);
            }
        }
        return parent;
    } else {
        Class typeToFindGetter = getOwningClassForLocation(location);
        if (typeToFindGetter == null) {
            return null;
        }
        // Check if config object is where the location or it goes deeper in the config layers.
        StringTokenizer tokenizer = new StringTokenizer(location, "/", false);
        // something directly inside the config itself
        if (tokenizer.countTokens() == 3) {
            String expression = location.substring(location.lastIndexOf("[") + 1, location.length() - 1);
            String configName = resolveExpression(expression);
            return serviceLocator.<Domain>getService(Domain.class).getConfigNamed(configName);
        }
        location = location.substring(location.indexOf("/", "domain/configs".length()) + 1);
        tokenizer = new StringTokenizer(location, "/", false);
        String curLevel = tokenizer.nextToken();
        String expression;
        if (curLevel.contains("[")) {
            expression = curLevel.substring(curLevel.lastIndexOf("[") + 1, curLevel.length() - 1);
        } else {
            expression = curLevel;
        }
        String configName = resolveExpression(expression);
        ConfigBeanProxy parent = serviceLocator.<Domain>getService(Domain.class).getConfigNamed(configName);
        String childElement;
        String parentElement = "Config";
        while (tokenizer.hasMoreTokens()) {
            try {
                childElement = tokenizer.nextToken();
                parent = getOwner(parent, parentElement, childElement);
                parentElement = childElement;
            } catch (Exception e) {
                LogHelper.log(LOG, Level.INFO, cannotGetParentConfigBean, e, configName);
            }
        }
        return parent;
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Domain(com.sun.enterprise.config.serverbeans.Domain) PropertyVetoException(java.beans.PropertyVetoException) XMLStreamException(javax.xml.stream.XMLStreamException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 12 with ConfigBeanProxy

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

the class ConfigModularityUtils method setConfigBean.

public <T extends ConfigBeanProxy> T setConfigBean(T finalConfigBean, ConfigBeanDefaultValue configBeanDefaultValue, ConfigBeanProxy parent) {
    Class owningClassForLocation = getOwningClassForLocation(configBeanDefaultValue.getLocation());
    Class configBeanClass = getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
    try {
        ConfigBeanProxy configBeanInstance = null;
        if (getNameForConfigBean(finalConfigBean, configBeanClass) == null) {
            List<ConfigBeanProxy> extensions = getExtensions(parent);
            for (ConfigBeanProxy extension : extensions) {
                try {
                    configBeanInstance = (ConfigBeanProxy) configBeanClass.cast(extension);
                    break;
                } catch (Exception e) {
                // ignore, not the right type.
                }
            }
            if (!configBeanDefaultValue.replaceCurrentIfExists() || !stackPositionHigher(finalConfigBean, configBeanInstance)) {
                if (configBeanInstance != null)
                    return (T) configBeanInstance;
            }
            if (configBeanInstance != null) {
                extensions.remove(configBeanInstance);
            }
        }
    } catch (InvocationTargetException e) {
        LOG.log(Level.INFO, cannotSetConfigBean, e);
    } catch (IllegalAccessException e) {
        LOG.log(Level.INFO, cannotSetConfigBean, e);
    }
    Method m = getMatchingSetterMethod(owningClassForLocation, configBeanClass);
    if (m != null) {
        try {
            if (configBeanClass.getAnnotation(HasCustomizationTokens.class) != null) {
                applyCustomTokens(configBeanDefaultValue, finalConfigBean, parent);
            }
            m.invoke(parent, finalConfigBean);
        } catch (Exception e) {
            LogHelper.log(LOG, Level.INFO, cannotSetConfigBeanFor, e, finalConfigBean.getClass().getName());
        }
        return finalConfigBean;
    }
    m = findSuitableCollectionGetter(owningClassForLocation, configBeanClass);
    if (m != null) {
        try {
            Collection col = (Collection) m.invoke(parent);
            String name = getNameForConfigBean(finalConfigBean, configBeanClass);
            ConfigBeanProxy itemToRemove = getNamedConfigBeanFromCollection(col, name, configBeanClass);
            if (configBeanDefaultValue.replaceCurrentIfExists()) {
                try {
                    if (itemToRemove != null) {
                        if (stackPositionHigher(finalConfigBean, itemToRemove)) {
                            col.remove(itemToRemove);
                        }
                    }
                } catch (Exception ex) {
                    LogHelper.log(LOG, Level.INFO, cannotRemoveConfigBean, ex, finalConfigBean.getClass().getName());
                }
            }
            if (configBeanClass.getAnnotation(HasCustomizationTokens.class) != null) {
                applyCustomTokens(configBeanDefaultValue, finalConfigBean, parent);
            }
            if (itemToRemove != null && !configBeanDefaultValue.replaceCurrentIfExists()) {
                // Check for duplication here.
                if (((ConfigView) Proxy.getInvocationHandler(itemToRemove)).getProxyType().isAssignableFrom(configBeanClass)) {
                    return finalConfigBean;
                }
            }
            col.add(finalConfigBean);
            return finalConfigBean;
        } catch (Exception e) {
            LogHelper.log(LOG, Level.INFO, cannotSetConfigBeanFor, e, finalConfigBean.getClass().getName());
        }
    }
    return null;
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Collection(java.util.Collection) Method(java.lang.reflect.Method) HasCustomizationTokens(com.sun.enterprise.config.modularity.annotation.HasCustomizationTokens) PropertyVetoException(java.beans.PropertyVetoException) XMLStreamException(javax.xml.stream.XMLStreamException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with ConfigBeanProxy

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

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

the class ConfigModularityUtils method getOwner.

public ConfigBeanProxy getOwner(ConfigBeanProxy parent, String parentElement, String childElement) throws InvocationTargetException, IllegalAccessException {
    if (childElement.contains("CURRENT_INSTANCE_CONFIG_NAME")) {
        return serviceLocator.<Config>getService(Config.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
    }
    if (childElement.contains("CURRENT_INSTANCE_SERVER_NAME")) {
        return serviceLocator.<Server>getService(Server.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
    }
    if (childElement.endsWith("]")) {
        String componentName;
        String elementName;
        elementName = childElement.substring(childElement.lastIndexOf("/") + 1, childElement.indexOf("["));
        componentName = childElement.substring(childElement.lastIndexOf("[") + 1, childElement.indexOf("]"));
        Class childClass = getClassFor(elementName);
        Class parentClass = getClassFor(parentElement);
        Method m = findSuitableCollectionGetter(parentClass, childClass);
        if (m != null) {
            try {
                Collection col = (Collection) m.invoke(parent);
                componentName = resolveExpression(componentName);
                return getNamedConfigBeanFromCollection(col, componentName, childClass);
            } catch (Exception e) {
                LogHelper.log(LOG, Level.INFO, invalidPath, e, childElement, componentName);
            }
        }
        return null;
    } else {
        Class clz = getClassFor(childElement);
        if (parent == null)
            return null;
        Method m = getMatchingGetterMethod(parent.getClass(), clz);
        if (m != null) {
            return (ConfigBeanProxy) m.invoke(parent);
        } else {
            try {
                m = parent.getClass().getMethod("getExtensionByType", java.lang.Class.class);
            } catch (NoSuchMethodException e) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.log(Level.FINE, "Cannot find getExtensionByType", e);
                }
            }
            if (m != null) {
                return (ConfigBeanProxy) m.invoke(parent, clz);
            }
            return null;
        }
    }
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) Collection(java.util.Collection) Method(java.lang.reflect.Method) PropertyVetoException(java.beans.PropertyVetoException) XMLStreamException(javax.xml.stream.XMLStreamException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 15 with ConfigBeanProxy

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

Aggregations

ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)41 PropertyVetoException (java.beans.PropertyVetoException)21 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)14 Method (java.lang.reflect.Method)11 ArrayList (java.util.ArrayList)11 ConfigCode (org.jvnet.hk2.config.ConfigCode)10 Config (com.sun.enterprise.config.serverbeans.Config)8 Server (com.sun.enterprise.config.serverbeans.Server)6 IOException (java.io.IOException)6 List (java.util.List)6 ActionReport (org.glassfish.api.ActionReport)6 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)6 Protocol (org.glassfish.grizzly.config.dom.Protocol)6 MultiException (org.glassfish.hk2.api.MultiException)6 ConfigModel (org.jvnet.hk2.config.ConfigModel)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)5 ConfigBean (org.jvnet.hk2.config.ConfigBean)5 Property (org.jvnet.hk2.config.types.Property)5