Search in sources :

Example 31 with TransactionFailure

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

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

the class JavaMailResourceManager method create.

public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target) throws Exception {
    setAttributes(attributes, target);
    ResourceStatus validationStatus = isValid(resources, true, target);
    if (validationStatus.getStatus() == ResourceStatus.FAILURE) {
        return validationStatus;
    }
    // ensure we don't already have one of this name
    if (ResourceUtil.getBindableResourceByName(resources, jndiName) != null) {
        String msg = localStrings.getLocalString("create.mail.resource.duplicate.1", "A Mail Resource named {0} already exists.", jndiName);
        return new ResourceStatus(ResourceStatus.FAILURE, msg, true);
    }
    try {
        ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                MailResource newResource = createConfigBean(param, properties);
                param.getResources().add(newResource);
                return newResource;
            }
        }, resources);
        resourceUtil.createResourceRef(jndiName, enabledValueForTarget, target);
        String msg = localStrings.getLocalString("create.mail.resource.success", "Mail Resource {0} created.", jndiName);
        return new ResourceStatus(ResourceStatus.SUCCESS, msg, true);
    } catch (TransactionFailure tfe) {
        String msg = localStrings.getLocalString("" + "create.mail.resource.fail", "Unable to create Mail Resource {0}.", jndiName) + " " + tfe.getLocalizedMessage();
        return new ResourceStatus(ResourceStatus.FAILURE, msg, true);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) Resources(com.sun.enterprise.config.serverbeans.Resources) MailResource(org.glassfish.resources.javamail.config.MailResource)

Example 33 with TransactionFailure

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

the class DeleteJavaMailResourceTest method tearDown.

@After
public void tearDown() throws TransactionFailure {
    ConfigSupport.apply(new SingleConfigCode<Resources>() {

        public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
            Resource target = null;
            for (Resource resource : param.getResources()) {
                if (resource instanceof MailResource) {
                    MailResource r = (MailResource) resource;
                    if (r.getJndiName().equals("mail/MyMailSession")) {
                        target = resource;
                        break;
                    }
                }
            }
            if (target != null) {
                param.getResources().remove(target);
            }
            return null;
        }
    }, resources);
    parameters = new ParameterMap();
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) TransactionFailure(org.jvnet.hk2.config.TransactionFailure) MailResource(org.glassfish.resources.javamail.config.MailResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ParameterMap(org.glassfish.api.admin.ParameterMap) Resources(com.sun.enterprise.config.serverbeans.Resources) MailResource(org.glassfish.resources.javamail.config.MailResource) After(org.junit.After)

Example 34 with TransactionFailure

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

the class JndiResourceManager method createConfigBean.

private ExternalJndiResource createConfigBean(Resources param, Properties properties) throws PropertyVetoException, TransactionFailure {
    ExternalJndiResource newResource = param.createChild(ExternalJndiResource.class);
    newResource.setJndiName(jndiName);
    newResource.setFactoryClass(factoryClass);
    newResource.setJndiLookupName(jndiLookupName);
    newResource.setResType(resType);
    newResource.setEnabled(enabled);
    if (description != null) {
        newResource.setDescription(description);
    }
    if (properties != null) {
        for (Map.Entry e : properties.entrySet()) {
            Property prop = newResource.createChild(Property.class);
            prop.setName((String) e.getKey());
            prop.setValue((String) e.getValue());
            newResource.getProperty().add(prop);
        }
    }
    return newResource;
}
Also used : ExternalJndiResource(org.glassfish.resources.config.ExternalJndiResource) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.jvnet.hk2.config.types.Property)

Example 35 with TransactionFailure

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

the class CustomResourceManager method createConfigBean.

private CustomResource createConfigBean(Resources param, Properties properties) throws PropertyVetoException, TransactionFailure {
    CustomResource newResource = param.createChild(CustomResource.class);
    newResource.setJndiName(jndiName);
    newResource.setFactoryClass(factoryClass);
    newResource.setResType(resType);
    newResource.setEnabled(enabled);
    if (description != null) {
        newResource.setDescription(description);
    }
    if (properties != null) {
        for (Map.Entry e : properties.entrySet()) {
            Property prop = newResource.createChild(Property.class);
            prop.setName((String) e.getKey());
            prop.setValue((String) e.getValue());
            newResource.getProperty().add(prop);
        }
    }
    return newResource;
}
Also used : CustomResource(org.glassfish.resources.config.CustomResource) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.jvnet.hk2.config.types.Property)

Aggregations

TransactionFailure (org.jvnet.hk2.config.TransactionFailure)191 PropertyVetoException (java.beans.PropertyVetoException)132 ActionReport (org.glassfish.api.ActionReport)86 Property (org.jvnet.hk2.config.types.Property)61 Config (com.sun.enterprise.config.serverbeans.Config)52 HashMap (java.util.HashMap)30 Test (org.junit.Test)27 Resources (com.sun.enterprise.config.serverbeans.Resources)25 Map (java.util.Map)25 List (java.util.List)21 CommandTarget (org.glassfish.config.support.CommandTarget)21 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)21 Target (org.glassfish.internal.api.Target)21 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)20 Protocol (org.glassfish.grizzly.config.dom.Protocol)20 ConfigBean (org.jvnet.hk2.config.ConfigBean)20 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)18 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)17 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)16 ParameterMap (org.glassfish.api.admin.ParameterMap)14