Search in sources :

Example 46 with SingleConfigCode

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

the class DeleteJavaMailResource 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();
    // ensure we already have this resource
    if (!isResourceExists(domain.getResources(), jndiName)) {
        report.setMessage(localStrings.getLocalString("delete.mail.resource.notfound", "A Mail resource named {0} does not exist.", jndiName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (environment.isDas()) {
        if ("domain".equals(target)) {
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 0) {
                report.setMessage(localStrings.getLocalString("delete.mail.resource.resource-ref.exist", "mail-resource [ {0} ] is referenced in an" + "instance/cluster target, Use delete-resource-ref on appropriate target", jndiName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        } else {
            if (!resourceUtil.isResourceRefInTarget(jndiName, target)) {
                report.setMessage(localStrings.getLocalString("delete.mail.resource.no.resource-ref", "mail-resource [ {0} ] is not referenced in target [ {1} ]", jndiName, target));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 1) {
                report.setMessage(localStrings.getLocalString("delete.mail.resource.multiple.resource-refs", "mail-resource [ {0} ] is referenced in multiple " + "instance/cluster targets, Use delete-resource-ref on appropriate target", jndiName));
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        }
    }
    try {
        // delete resource-ref
        resourceUtil.deleteResourceRef(jndiName, target);
        // delete java-mail-resource
        ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                MailResource resource = (MailResource) ResourceUtil.getBindableResourceByName(domain.getResources(), jndiName);
                return param.getResources().remove(resource);
            }
        }, domain.getResources());
        report.setMessage(localStrings.getLocalString("delete.mail.resource.success", "Mail resource {0} deleted", jndiName));
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (TransactionFailure tfe) {
        report.setMessage(localStrings.getLocalString("delete.mail.resource.failed", "Unable to delete mail resource {0}", jndiName) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Resources(com.sun.enterprise.config.serverbeans.Resources) ActionReport(org.glassfish.api.ActionReport) MailResource(org.glassfish.resources.javamail.config.MailResource)

Example 47 with SingleConfigCode

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

the class ConfigTest method testDomTx.

// @Test
public void testDomTx() {
    SimpleConnector sc = habitat.getService(SimpleConnector.class);
    EjbContainerAvailability ejb = sc.getEjbContainerAvailability();
    Dom ejbDom = Dom.unwrap(ejb);
    assert (ejbDom.getHabitat() != null);
    String avEnabled = ejb.getAvailabilityEnabled();
    try {
        ConfigSupport.apply(new SingleConfigCode<EjbContainerAvailability>() {

            @Override
            public Object run(EjbContainerAvailability param) throws PropertyVetoException, TransactionFailure {
                param.setSfsbHaPersistenceType("coherence");
                param.setSfsbCheckpointEnabled("**MUST BE**");
                return null;
            }
        }, ejb);
        // printEjb("AFTER CHANGES", ejb);
        assert (ejb.getSfsbHaPersistenceType().equals("coherence") && ejb.getSfsbCheckpointEnabled().equals("**MUST BE**") && ejb.getAvailabilityEnabled().equals(avEnabled));
    } catch (Exception e) {
        e.printStackTrace();
        assert (false);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Dom(org.jvnet.hk2.config.Dom) PropertyVetoException(java.beans.PropertyVetoException)

Example 48 with SingleConfigCode

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

the class SetConfigCacheDuration method execute.

@Override
public void execute(AdminCommandContext context) {
    Config configVal = targetUtil.getConfig(target);
    MicroprofileConfigConfiguration serviceConfig = configVal.getExtensionByType(MicroprofileConfigConfiguration.class);
    if (serviceConfig != null) {
        try {
            ConfigSupport.apply(new SingleConfigCode<MicroprofileConfigConfiguration>() {

                @Override
                public Object run(MicroprofileConfigConfiguration config) {
                    config.setCacheDurationSeconds(Integer.toString(durationInSeconds));
                    return null;
                }
            }, serviceConfig);
        } catch (TransactionFailure ex) {
            // set failure
            context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "Failed to update message", ex);
        }
    } else {
        context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "No configuration with name " + target);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) MicroprofileConfigConfiguration(fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration) Config(com.sun.enterprise.config.serverbeans.Config)

Example 49 with SingleConfigCode

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

the class SetConfigOrdinal method execute.

@Override
public void execute(AdminCommandContext context) {
    Config configVal = targetUtil.getConfig(target);
    MicroprofileConfigConfiguration serviceConfig = configVal.getExtensionByType(MicroprofileConfigConfiguration.class);
    if (serviceConfig != null) {
        try {
            // to perform a transaction on the domain.xml you need to use this construct
            // see https://github.com/hk2-project/hk2/blob/master/hk2-configuration/persistence/hk2-xml-dom/hk2-config/src/main/java/org/jvnet/hk2/config/ConfigSupport.java
            ConfigSupport.apply(new SingleConfigCode<MicroprofileConfigConfiguration>() {

                @Override
                public Object run(MicroprofileConfigConfiguration config) {
                    switch(source) {
                        case DOMAIN:
                            {
                                config.setDomainOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case CONFIG:
                            {
                                config.setConfigOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case SERVER:
                            {
                                config.setServerOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case APPLICATION:
                            {
                                config.setApplicationOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case MODULE:
                            {
                                config.setModuleOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case CLUSTER:
                            {
                                config.setClusterOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case JNDI:
                            {
                                config.setJndiOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case SECRETS:
                            {
                                config.setSecretDirOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case PASSWORD:
                            {
                                config.setPasswordOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case JDBC:
                            {
                                config.setJdbcOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case CLOUD:
                            {
                                config.setCloudOrdinality(Integer.toString(ordinal));
                                break;
                            }
                        case LDAP:
                            {
                                config.setLdapOrdinality(Integer.toString(ordinal));
                                break;
                            }
                    }
                    return null;
                }
            }, serviceConfig);
        } catch (TransactionFailure ex) {
            // set failure
            context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "Failed to update message", ex);
        }
    } else {
        context.getActionReport().failure(Logger.getLogger(SetConfigOrdinal.class.getName()), "No configuration with name " + target);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) MicroprofileConfigConfiguration(fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration) Config(com.sun.enterprise.config.serverbeans.Config)

Example 50 with SingleConfigCode

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

the class ConfigConfigSource method setValue.

public boolean setValue(final String propertyName, final String propertyValue) throws TransactionFailure {
    boolean success = false;
    Config config = domainConfiguration.getConfigs().getConfigByName(configurationName);
    if (config != null) {
        Property p = config.getProperty(PROPERTY_PREFIX + propertyName);
        if (p == null) {
            ConfigSupport.apply(new SingleConfigCode<Config>() {

                @Override
                public Object run(Config config) throws TransactionFailure, PropertyVetoException {
                    Property prop = config.createChild(Property.class);
                    prop.setName(PROPERTY_PREFIX + propertyName);
                    prop.setValue(propertyValue);
                    config.getProperty().add(prop);
                    return null;
                }
            }, config);
        } else {
            ConfigSupport.apply(new SingleConfigCode<Property>() {

                @Override
                public Object run(Property config) throws TransactionFailure, PropertyVetoException {
                    config.setValue(propertyValue);
                    return null;
                }
            }, p);
        }
        success = true;
    }
    return success;
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) PropertyVetoException(java.beans.PropertyVetoException) Config(com.sun.enterprise.config.serverbeans.Config) Property(org.jvnet.hk2.config.types.Property)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)153 PropertyVetoException (java.beans.PropertyVetoException)130 ActionReport (org.glassfish.api.ActionReport)76 Config (com.sun.enterprise.config.serverbeans.Config)47 Property (org.jvnet.hk2.config.types.Property)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 List (java.util.List)23 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)19 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 CommandTarget (org.glassfish.config.support.CommandTarget)15 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)15 Protocol (org.glassfish.grizzly.config.dom.Protocol)15 Target (org.glassfish.internal.api.Target)15 Test (org.junit.Test)15 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)13 ArrayList (java.util.ArrayList)11 Protocols (org.glassfish.grizzly.config.dom.Protocols)11 Resource (com.sun.enterprise.config.serverbeans.Resource)9 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)9 Properties (java.util.Properties)9