Search in sources :

Example 1 with MailResource

use of org.glassfish.resources.javamail.config.MailResource 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 2 with MailResource

use of org.glassfish.resources.javamail.config.MailResource in project Payara by payara.

the class ListJavaMailResources 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();
    try {
        List<String> list = new ArrayList<String>();
        Collection<MailResource> mailResources = domain.getResources().getResources(MailResource.class);
        for (MailResource mailResource : mailResources) {
            if (bindableResourcesHelper.resourceExists(mailResource.getJndiName(), targetOperand)) {
                list.add(mailResource.getJndiName());
            }
        }
        for (String jndiName : list) {
            final ActionReport.MessagePart part = report.getTopMessagePart().addChild();
            part.setMessage(jndiName);
        }
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    } catch (Exception e) {
        report.setMessage(localStrings.getLocalString("list.javamail.resources.failed", "Unable to list mail resources") + " " + e.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
    }
}
Also used : ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) MailResource(org.glassfish.resources.javamail.config.MailResource)

Example 3 with MailResource

use of org.glassfish.resources.javamail.config.MailResource in project Payara by payara.

the class CreateJavaMailResourceTest method testExecuteSuccess.

/**
 * Test of execute method, of class CreateJavaMailResource.
 * asadmin create-javamail-resource --mailuser=test --mailhost=localhost
 * --fromaddress=test@sun.com mail/MyMailSession
 */
@Test
public void testExecuteSuccess() {
    parameters.set("mailhost", "localhost");
    parameters.set("mailuser", "test");
    parameters.set("fromaddress", "test@sun.com");
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource command = habitat.getService(org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource.class);
    assertTrue(command != null);
    cr.getCommandInvocation("create-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof MailResource) {
            MailResource r = (MailResource) resource;
            if (r.getJndiName().equals("mail/MyMailSession")) {
                assertEquals("localhost", r.getHost());
                assertEquals("test", r.getUser());
                assertEquals("test@sun.com", r.getFrom());
                assertEquals("true", r.getEnabled());
                assertEquals("false", r.getDebug());
                assertEquals("imap", r.getStoreProtocol());
                assertEquals("com.sun.mail.imap.IMAPStore", r.getStoreProtocolClass());
                assertEquals("smtp", r.getTransportProtocol());
                assertEquals("com.sun.mail.smtp.SMTPTransport", r.getTransportProtocolClass());
                isCreated = true;
                logger.fine("MailResource config bean mail/MyMailSession is created.");
                break;
            }
        }
    }
    assertTrue(isCreated);
    logger.fine("msg: " + context.getActionReport().getMessage());
    Servers servers = habitat.getService(Servers.class);
    boolean isRefCreated = false;
    for (Server server : servers.getServer()) {
        if (server.getName().equals(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)) {
            for (ResourceRef ref : server.getResourceRef()) {
                if (ref.getRef().equals("mail/MyMailSession")) {
                    assertEquals("true", ref.getEnabled());
                    isRefCreated = true;
                    break;
                }
            }
        }
    }
    assertTrue(isRefCreated);
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) MailResource(org.glassfish.resources.javamail.config.MailResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) Servers(com.sun.enterprise.config.serverbeans.Servers) MailResource(org.glassfish.resources.javamail.config.MailResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 4 with MailResource

use of org.glassfish.resources.javamail.config.MailResource 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 5 with MailResource

use of org.glassfish.resources.javamail.config.MailResource in project Payara by payara.

the class DeleteJavaMailResourceTest method testExecuteSuccessDefaultTarget.

/**
 * Test of execute method, of class DeleteJavaMailResource.
 * asadmin create-javamail-resource --mailuser=test --mailhost=localhost
 * --fromaddress=test@sun.com mail/MyMailSession
 * delete-javamail-resource mail/MyMailSession
 */
@Test
public void testExecuteSuccessDefaultTarget() {
    parameters.set("mailhost", "localhost");
    parameters.set("mailuser", "test");
    parameters.set("fromaddress", "test@sun.com");
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource createCommand = habitat.getService(org.glassfish.resources.javamail.admin.cli.CreateJavaMailResource.class);
    assertTrue(createCommand != null);
    cr.getCommandInvocation("create-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(createCommand);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    parameters = new ParameterMap();
    parameters.set("jndi_name", "mail/MyMailSession");
    org.glassfish.resources.javamail.admin.cli.DeleteJavaMailResource deleteCommand = habitat.getService(org.glassfish.resources.javamail.admin.cli.DeleteJavaMailResource.class);
    assertTrue(deleteCommand != null);
    cr.getCommandInvocation("delete-javamail-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(deleteCommand);
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    boolean isDeleted = true;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof MailResource) {
            MailResource jr = (MailResource) resource;
            if (jr.getJndiName().equals("mail/MyMailSession")) {
                isDeleted = false;
                logger.fine("JavaMailResource config bean mail/MyMailSession is deleted.");
                break;
            }
        }
    }
    assertTrue(isDeleted);
    logger.fine("msg: " + context.getActionReport().getMessage());
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    Servers servers = habitat.getService(Servers.class);
    boolean isRefDeleted = true;
    for (Server server : servers.getServer()) {
        if (server.getName().equals(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME)) {
            for (ResourceRef ref : server.getResourceRef()) {
                if (ref.getRef().equals("mail/MyMailSession")) {
                    isRefDeleted = false;
                    break;
                }
            }
        }
    }
    assertTrue(isRefDeleted);
}
Also used : MailResource(org.glassfish.resources.javamail.config.MailResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ParameterMap(org.glassfish.api.admin.ParameterMap) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) MailResource(org.glassfish.resources.javamail.config.MailResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Aggregations

MailResource (org.glassfish.resources.javamail.config.MailResource)16 Resource (com.sun.enterprise.config.serverbeans.Resource)6 Resources (com.sun.enterprise.config.serverbeans.Resources)4 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)4 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)4 Test (org.junit.Test)4 PropertyVetoException (java.beans.PropertyVetoException)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)3 ResourceRef (com.sun.enterprise.config.serverbeans.ResourceRef)2 ActionReport (org.glassfish.api.ActionReport)2 Domain (com.sun.enterprise.config.serverbeans.Domain)1 Server (com.sun.enterprise.config.serverbeans.Server)1 Servers (com.sun.enterprise.config.serverbeans.Servers)1 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)1 CommandRunner (org.glassfish.api.admin.CommandRunner)1 ResourceStatus (org.glassfish.resourcebase.resources.api.ResourceStatus)1