Search in sources :

Example 66 with Property

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

the class ConfigConfigSource method deleteValue.

public boolean deleteValue(String propertyName) throws TransactionFailure {
    boolean result = false;
    Config config = domainConfiguration.getConfigs().getConfigByName(configurationName);
    if (config != null) {
        for (Property object : config.getProperty()) {
            if ((PROPERTY_PREFIX + propertyName).equals(object.getName())) {
                ConfigSupport.deleteChild((ConfigBean) ConfigBean.unwrap(config), (ConfigBean) ConfigBean.unwrap(object));
                result = true;
            }
        }
    }
    return result;
}
Also used : Config(com.sun.enterprise.config.serverbeans.Config) Property(org.jvnet.hk2.config.types.Property)

Example 67 with Property

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

the class GlassfishNetworkListener method configureHttpProtocol.

@Override
protected void configureHttpProtocol(final ServiceLocator habitat, final NetworkListener networkListener, final Http http, final FilterChainBuilder filterChainBuilder, boolean securityEnabled) {
    if (httpAdapter == null) {
        registerMonitoringStatsProviders();
        final V3Mapper mapper = new V3Mapper(logger);
        mapper.setPort(port);
        mapper.setId(name);
        final ContainerMapper containerMapper = new ContainerMapper(grizzlyService, this);
        containerMapper.setMapper(mapper);
        containerMapper.setDefaultHost(http.getDefaultVirtualServer());
        containerMapper.setRequestURIEncoding(http.getUriEncoding());
        containerMapper.configureMapper();
        VirtualServer vs = null;
        String webAppRootPath = null;
        final Collection<VirtualServer> list = grizzlyService.getHabitat().getAllServices(VirtualServer.class);
        final String vsName = http.getDefaultVirtualServer();
        for (final VirtualServer virtualServer : list) {
            if (virtualServer.getId().equals(vsName)) {
                vs = virtualServer;
                webAppRootPath = vs.getDocroot();
                if (!grizzlyService.hasMapperUpdateListener() && vs.getProperty() != null && !vs.getProperty().isEmpty()) {
                    for (final Property p : vs.getProperty()) {
                        final String propertyName = p.getName();
                        if (propertyName.startsWith("alternatedocroot")) {
                            String value = p.getValue();
                            String[] mapping = value.split(" ");
                            if (mapping.length != 2) {
                                logger.log(Level.WARNING, "Invalid alternate_docroot {0}", value);
                                continue;
                            }
                            String docBase = mapping[1].substring("dir=".length());
                            String urlPattern = mapping[0].substring("from=".length());
                            containerMapper.addAlternateDocBase(urlPattern, docBase);
                        }
                    }
                }
                break;
            }
        }
        httpAdapter = new HttpAdapterImpl(vs, containerMapper, webAppRootPath);
        containerMapper.addDocRoot(webAppRootPath);
        AbstractActiveDescriptor<V3Mapper> aad = BuilderHelper.createConstantDescriptor(mapper);
        aad.addContractType(Mapper.class);
        aad.setName(address.toString() + port);
        ServiceLocatorUtilities.addOneDescriptor(grizzlyService.getHabitat(), aad);
        super.configureHttpProtocol(habitat, networkListener, http, filterChainBuilder, securityEnabled);
        final Protocol protocol = http.getParent();
        for (NetworkListener listener : protocol.findNetworkListeners()) {
            grizzlyService.notifyMapperUpdateListeners(listener, mapper);
        }
    } else {
        super.configureHttpProtocol(habitat, networkListener, http, filterChainBuilder, securityEnabled);
    }
}
Also used : Protocol(org.glassfish.grizzly.config.dom.Protocol) V3Mapper(org.glassfish.internal.grizzly.V3Mapper) Property(org.jvnet.hk2.config.types.Property) VirtualServer(com.sun.enterprise.config.serverbeans.VirtualServer) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 68 with Property

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

the class CreateSystemProperties method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the paramter names and the values the parameter values
 *
 * @param context information
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    String sysPropName = "";
    try {
        for (final Object key : properties.keySet()) {
            final String propName = (String) key;
            sysPropName = propName;
            // value of an existing property
            try {
                TranslatedConfigView.doSubstitution.set(Boolean.FALSE);
                if (spb.containsProperty(sysPropName) && spb.getSystemProperty(sysPropName).getValue().equals(properties.getProperty(propName))) {
                    continue;
                }
            } finally {
                TranslatedConfigView.doSubstitution.set(Boolean.TRUE);
            }
            ConfigSupport.apply(new SingleConfigCode<SystemPropertyBag>() {

                @Override
                public Object run(SystemPropertyBag param) throws PropertyVetoException, TransactionFailure {
                    // update existing system property
                    for (SystemProperty sysProperty : param.getSystemProperty()) {
                        if (sysProperty.getName().equals(propName)) {
                            Transaction t = Transaction.getTransaction(param);
                            sysProperty = t.enroll(sysProperty);
                            sysProperty.setValue(properties.getProperty(propName));
                            return sysProperty;
                        }
                    }
                    // create system-property
                    SystemProperty newSysProp = param.createChild(SystemProperty.class);
                    newSysProp.setName(propName);
                    newSysProp.setValue(properties.getProperty(propName));
                    param.getSystemProperty().add(newSysProp);
                    return newSysProp;
                }
            }, spb);
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        }
    } catch (TransactionFailure tfe) {
        report.setMessage(localStrings.getLocalString("create.system.properties.failed", "System property {0} creation failed", sysPropName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    } catch (Exception e) {
        report.setMessage(localStrings.getLocalString("create.system.properties.failed", "System property {0} creation failed", sysPropName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Transaction(org.jvnet.hk2.config.Transaction) SystemPropertyBag(com.sun.enterprise.config.serverbeans.SystemPropertyBag) ActionReport(org.glassfish.api.ActionReport) SystemProperty(com.sun.enterprise.config.serverbeans.SystemProperty) PropertyVetoException(java.beans.PropertyVetoException)

Example 69 with Property

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

the class DeleteSystemProperty method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the paramter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    Property domainProp = domain.getProperty("administrative.domain.name");
    String domainName = domainProp.getValue();
    if (!spb.containsProperty(propName)) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        String msg = localStrings.getLocalString("no.such.property", "System Property named {0} does not exist at the given target {1}", propName, target);
        report.setMessage(msg);
        return;
    }
    if (definitions(propName) == 1) {
        // implying user is deleting the "last" definition of this property
        List<String> refs = new ArrayList<String>();
        List<Dom> doms = new ArrayList<Dom>();
        if ("domain".equals(target) || target.equals(domainName)) {
            for (Server s : domain.getServers().getServer()) {
                Config config = s.getConfig();
                Cluster cluster = s.getCluster();
                if (!s.containsProperty(propName) && !config.containsProperty(propName)) {
                    if (cluster != null) {
                        if (!cluster.containsProperty(propName)) {
                            doms.add(Dom.unwrap(s));
                        }
                    } else {
                        doms.add(Dom.unwrap(s));
                    }
                }
            }
        } else {
            Config config = domain.getConfigNamed(target);
            if (config != null) {
                doms.add(Dom.unwrap(config));
                String configName = config.getName();
                for (Server s : domain.getServers().getServer()) {
                    String configRef = s.getConfigRef();
                    if (configRef.equals(configName)) {
                        if (!s.containsProperty(propName)) {
                            doms.add(Dom.unwrap(s));
                        }
                    }
                }
                for (Cluster c : domain.getClusters().getCluster()) {
                    String configRef = c.getConfigRef();
                    if (configRef.equals(configName)) {
                        if (!c.containsProperty(propName)) {
                            doms.add(Dom.unwrap(c));
                        }
                    }
                }
            } else {
                Cluster cluster = domain.getClusterNamed(target);
                if (cluster != null) {
                    doms.add(Dom.unwrap(cluster));
                    Config clusterConfig = domain.getConfigNamed(cluster.getConfigRef());
                    doms.add(Dom.unwrap(clusterConfig));
                    for (Server s : cluster.getInstances()) {
                        if (!s.containsProperty(propName)) {
                            doms.add(Dom.unwrap(s));
                        }
                    }
                } else {
                    Server server = domain.getServerNamed(target);
                    doms.add(Dom.unwrap(server));
                    doms.add(Dom.unwrap(domain.getConfigNamed(server.getConfigRef())));
                }
            }
        }
        String sysPropName = SystemPropertyConstants.getPropertyAsValue(propName);
        for (Dom d : doms) {
            listRefs(d, sysPropName, refs);
        }
        if (!refs.isEmpty()) {
            // there are some references
            String msg = localStrings.getLocalString("cant.delete.referenced.property", "System Property {0} is referenced by {1} in the configuration. Please remove the references first.", propName, Arrays.toString(refs.toArray()));
            report.setMessage(msg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    // now we are sure that the target exits in the config, just remove the given property
    try {
        ConfigSupport.apply(new SingleConfigCode<SystemPropertyBag>() {

            public Object run(SystemPropertyBag param) throws PropertyVetoException, TransactionFailure {
                param.getSystemProperty().remove(param.getSystemProperty(propName));
                return param;
            }
        }, spb);
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        String msg = localStrings.getLocalString("delete.sysprops.ok", "System Property named {0} deleted from given target {1}. Make sure you check its references.", propName, target);
        report.setMessage(msg);
    } catch (TransactionFailure tf) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tf);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Dom(org.jvnet.hk2.config.Dom) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) PropertyVetoException(java.beans.PropertyVetoException) Property(org.jvnet.hk2.config.types.Property)

Example 70 with Property

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

the class SetCommand method set.

private boolean set(AdminCommandContext context, SetOperation op) {
    String pattern = op.pattern;
    String value = op.value;
    String target = op.target;
    String attrName = op.attrName;
    boolean isProperty = op.isProperty;
    // now
    // first let's get the parent for this pattern.
    TreeNode[] parentNodes = getAliasedParent(domain, pattern);
    // reset the pattern.
    String prefix;
    boolean lookAtSubNodes = true;
    if (parentNodes[0].relativeName.length() == 0 || parentNodes[0].relativeName.equals("domain")) {
        // handle the case where the pattern references an attribute of the top-level node
        prefix = "";
        // pattern is already set properly
        lookAtSubNodes = false;
    } else if (!pattern.startsWith(parentNodes[0].relativeName)) {
        prefix = pattern.substring(0, pattern.indexOf(parentNodes[0].relativeName));
        pattern = parentNodes[0].relativeName;
    } else {
        prefix = "";
        pattern = parentNodes[0].relativeName;
    }
    String targetName = prefix + pattern;
    if (modularityHelper != null) {
        synchronized (utils) {
            boolean oldv = utils.isCommandInvocation();
            utils.setCommandInvocation(true);
            modularityHelper.getLocationForDottedName(targetName);
            utils.setCommandInvocation(oldv);
        }
    }
    Map<Dom, String> matchingNodes;
    boolean applyOverrideRules = false;
    Map<Dom, String> dottedNames = new HashMap<Dom, String>();
    if (lookAtSubNodes) {
        for (TreeNode parentNode : parentNodes) {
            dottedNames.putAll(getAllDottedNodes(parentNode.node));
        }
        matchingNodes = getMatchingNodes(dottedNames, pattern);
        applyOverrideRules = true;
    } else {
        matchingNodes = new HashMap<Dom, String>();
        for (TreeNode parentNode : parentNodes) {
            matchingNodes.put(parentNode.node, pattern);
        }
    }
    if (matchingNodes.isEmpty()) {
        // it's possible they are trying to create a property object.. lets check this.
        // strip out the property name
        pattern = target.substring(0, trueLastIndexOf(target, '.'));
        if (pattern.endsWith("property")) {
            // need to find the right parent.
            Dom parentNode = null;
            if ("property".equals(pattern)) {
                // create and set the property
                try {
                    final String fname = attrName;
                    final String fvalue = value;
                    ConfigSupport.apply(new SingleConfigCode<Domain>() {

                        @Override
                        public Object run(Domain domain) throws PropertyVetoException, TransactionFailure {
                            Property p = domain.createChild(Property.class);
                            p.setName(fname);
                            p.setValue(fvalue);
                            domain.getProperty().add(p);
                            return p;
                        }
                    }, domain);
                    success(context, targetName, value);
                    runLegacyChecks(context);
                    if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
                        return false;
                    }
                    return true;
                } catch (TransactionFailure transactionFailure) {
                    // fail(context, "Could not change the attributes: " +
                    // transactionFailure.getMessage(), transactionFailure);
                    fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
                    return false;
                }
            } else {
                pattern = pattern.substring(0, trueLastIndexOf(pattern, '.'));
                parentNodes = getAliasedParent(domain, pattern);
            }
            pattern = parentNodes[0].relativeName;
            matchingNodes = getMatchingNodes(dottedNames, pattern);
            if (matchingNodes.isEmpty()) {
                // fail(context, "No configuration found for " + targetName);
                fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
                return false;
            }
            for (Map.Entry<Dom, String> node : matchingNodes.entrySet()) {
                if (node.getValue().equals(pattern)) {
                    parentNode = node.getKey();
                }
            }
            if (parentNode == null) {
                // fail(context, "No configuration found for " + targetName);
                fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
                return false;
            }
            if (value == null || value.length() == 0) {
                // setting to the empty string means to remove the property, so don't create it
                success(context, targetName, value);
                return true;
            }
            // create and set the property
            Map<String, String> attributes = new HashMap<String, String>();
            attributes.put("value", value);
            attributes.put("name", attrName);
            try {
                if (!(parentNode instanceof ConfigBean)) {
                    final ClassCastException cce = new ClassCastException(parentNode.getClass().getName());
                    fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", cce.getMessage(), cce));
                    return false;
                }
                ConfigSupport.createAndSet((ConfigBean) parentNode, Property.class, attributes);
                success(context, targetName, value);
                runLegacyChecks(context);
                if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
                    return false;
                }
                return true;
            } catch (TransactionFailure transactionFailure) {
                // fail(context, "Could not change the attributes: " +
                // transactionFailure.getMessage(), transactionFailure);
                fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
                return false;
            }
        }
    }
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    boolean setElementSuccess = false;
    boolean delPropertySuccess = false;
    boolean delProperty = false;
    Map<String, String> attrChanges = new HashMap<String, String>();
    if (isProperty) {
        attrName = "value";
        if ((value == null) || (value.length() == 0)) {
            delProperty = true;
        }
        attrChanges.put(attrName, value);
    }
    List<Map.Entry> mNodes = new ArrayList(matchingNodes.entrySet());
    if (applyOverrideRules) {
        mNodes = applyOverrideRules(mNodes);
    }
    for (Map.Entry<Dom, String> node : mNodes) {
        final Dom targetNode = node.getKey();
        for (String name : targetNode.model.getAttributeNames()) {
            String finalDottedName = node.getValue() + "." + name;
            if (matches(finalDottedName, pattern)) {
                if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
                    if (isDeprecatedAttr(targetNode, name)) {
                        warning(context, localStrings.getLocalString("admin.set.deprecated", "Warning: The attribute {0} is deprecated.", finalDottedName));
                    }
                    if (!isProperty) {
                        targetName = prefix + finalDottedName;
                        if (value != null && value.length() > 0) {
                            attrChanges.put(name, value);
                        } else {
                            attrChanges.put(name, null);
                        }
                    } else {
                        targetName = prefix + node.getValue();
                    }
                    if (delProperty) {
                        // delete property element
                        String str = node.getValue();
                        if (trueLastIndexOf(str, '.') != -1) {
                            str = str.substring(trueLastIndexOf(str, '.') + 1);
                        }
                        try {
                            if (str != null) {
                                ConfigSupport.deleteChild((ConfigBean) targetNode.parent(), (ConfigBean) targetNode);
                                delPropertySuccess = true;
                            }
                        } catch (IllegalArgumentException ie) {
                            fail(context, localStrings.getLocalString("admin.set.delete.property.failure", "Could not delete the property: {0}", ie.getMessage()), ie);
                            return false;
                        } catch (TransactionFailure transactionFailure) {
                            fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
                            return false;
                        }
                    } else {
                        changes.put((ConfigBean) node.getKey(), attrChanges);
                    }
                }
            }
        }
        for (String name : targetNode.model.getLeafElementNames()) {
            String finalDottedName = node.getValue() + "." + name;
            if (matches(finalDottedName, pattern)) {
                if (attrName.equals(name) || attrName.replace('_', '-').equals(name.replace('_', '-'))) {
                    if (isDeprecatedAttr(targetNode, name)) {
                        warning(context, localStrings.getLocalString("admin.set.elementdeprecated", "Warning: The element {0} is deprecated.", finalDottedName));
                    }
                    try {
                        setLeafElement((ConfigBean) targetNode, name, value);
                    } catch (TransactionFailure ex) {
                        fail(context, localStrings.getLocalString("admin.set.badelement", "Cannot change the element: {0}", ex.getMessage()), ex);
                        return false;
                    }
                    setElementSuccess = true;
                    break;
                }
            }
        }
    }
    if (!changes.isEmpty()) {
        try {
            config.apply(changes);
            success(context, targetName, value);
            runLegacyChecks(context);
        } catch (TransactionFailure transactionFailure) {
            // fail(context, "Could not change the attributes: " +
            // transactionFailure.getMessage(), transactionFailure);
            fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
            return false;
        }
    } else if (delPropertySuccess || setElementSuccess) {
        success(context, targetName, value);
    } else {
        fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
        return false;
    }
    if (targetService.isThisDAS() && !replicateSetCommand(context, target, value)) {
        return false;
    }
    return true;
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Property(org.jvnet.hk2.config.types.Property) Dom(org.jvnet.hk2.config.Dom) ConfigBean(org.jvnet.hk2.config.ConfigBean) PropertyVetoException(java.beans.PropertyVetoException) Domain(com.sun.enterprise.config.serverbeans.Domain) HashMap(java.util.HashMap) Map(java.util.Map) ParameterMap(org.glassfish.api.admin.ParameterMap)

Aggregations

Property (org.jvnet.hk2.config.types.Property)149 PropertyVetoException (java.beans.PropertyVetoException)30 HashMap (java.util.HashMap)27 Properties (java.util.Properties)22 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)22 ArrayList (java.util.ArrayList)18 ActionReport (org.glassfish.api.ActionReport)17 Map (java.util.Map)15 File (java.io.File)13 ConnectorConfigProperty (com.sun.enterprise.deployment.ConnectorConfigProperty)12 Config (com.sun.enterprise.config.serverbeans.Config)11 List (java.util.List)11 AuthRealm (com.sun.enterprise.config.serverbeans.AuthRealm)10 HttpService (com.sun.enterprise.config.serverbeans.HttpService)9 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)9 Server (com.sun.enterprise.config.serverbeans.Server)8 SystemProperty (com.sun.enterprise.config.serverbeans.SystemProperty)8 VirtualServer (com.sun.enterprise.config.serverbeans.VirtualServer)8 Application (com.sun.enterprise.config.serverbeans.Application)7 EnvironmentProperty (com.sun.enterprise.deployment.EnvironmentProperty)7