Search in sources :

Example 26 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class DeleteCustomResourceTest 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 CustomResource) {
                    CustomResource r = (CustomResource) resource;
                    if (r.getJndiName().equals("sample_custom_resource")) {
                        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) CustomResource(org.glassfish.resources.config.CustomResource) CustomResource(org.glassfish.resources.config.CustomResource) Resource(com.sun.enterprise.config.serverbeans.Resource) ParameterMap(org.glassfish.api.admin.ParameterMap) After(org.junit.After)

Example 27 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class DeleteConnectorResource method execute.

/**
 * Executes the command with the command parameters passed as Properties
 * where the keys are the parameter names and the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    if (jndiName == null) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.noJndiName", "No JNDI name defined for connector resource."));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    // ensure we already have this resource
    Resource r = ConnectorsUtil.getResourceByName(domain.getResources(), ConnectorResource.class, jndiName);
    if (r == null) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.notfound", "A connector resource named {0} does not exist.", jndiName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if ("system-all-req".equals(r.getObjectType())) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.notAllowed", "The {0} resource cannot be deleted as it is required to be configured in the system.", 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.connector.resource.resource-ref.exist", "connector-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.connector.resource.no.resource-ref", "connector-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.connector.resource.multiple.resource-refs", "connector 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 connector-resource
        if (ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                ConnectorResource resource = (ConnectorResource) ConnectorsUtil.getResourceByName(domain.getResources(), ConnectorResource.class, jndiName);
                return param.getResources().remove(resource);
            }
        }, domain.getResources()) == null) {
            report.setMessage(localStrings.getLocalString("delete.connector.resource.fail", "Connector resource {0} delete failed ", jndiName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    } catch (TransactionFailure tfe) {
        report.setMessage(localStrings.getLocalString("delete.connector.resource.fail", "Connector resource {0} delete failed ", jndiName) + " " + tfe.getLocalizedMessage());
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(tfe);
    }
    report.setMessage(localStrings.getLocalString("delete.connector.resource.success", "Connector resource {0} deleted successfully", jndiName));
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) ConnectorResource(org.glassfish.connectors.config.ConnectorResource) Resource(com.sun.enterprise.config.serverbeans.Resource) Resources(com.sun.enterprise.config.serverbeans.Resources) ActionReport(org.glassfish.api.ActionReport) ConnectorResource(org.glassfish.connectors.config.ConnectorResource)

Example 28 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class CreateCustomResourceTest method testExecuteFailDuplicateResource.

/**
 * Test of execute method, of class CreateCustomResource.
 * asadmin create-custom-resource --restype=topic --factoryclass=javax.naming.spi.ObjectFactory
 * dupRes
 * asadmin create-custom-resource --restype=topic --factoryclass=javax.naming.spi.ObjectFactory
 * dupRes
 */
@Test
public void testExecuteFailDuplicateResource() {
    parameters.set("restype", "topic");
    parameters.set("factoryclass", "javax.naming.spi.ObjectFactory");
    parameters.set("jndi_name", "dupRes");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("create-custom-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    // Check the exit code is SUCCESS
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    // Check that the resource was created
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof CustomResource) {
            CustomResource jr = (CustomResource) resource;
            if (jr.getJndiName().equals("dupRes")) {
                isCreated = true;
                logger.fine("Custom Resource config bean dupRes is created.");
                break;
            }
        }
    }
    assertTrue(isCreated);
    // Try to create a duplicate resource dupRes. Get a new instance of the command.
    org.glassfish.resources.admin.cli.CreateCustomResource command2 = habitat.getService(org.glassfish.resources.admin.cli.CreateCustomResource.class);
    cr.getCommandInvocation("create-custom-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command2);
    // Check the exit code is FAILURE
    assertEquals(ActionReport.ExitCode.FAILURE, context.getActionReport().getActionExitCode());
    // Check that the 2nd resource was NOT created
    int numDupRes = 0;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof CustomResource) {
            CustomResource jr = (CustomResource) resource;
            if (jr.getJndiName().equals("dupRes")) {
                numDupRes = numDupRes + 1;
            }
        }
    }
    assertEquals(1, numDupRes);
    logger.fine("msg: " + context.getActionReport().getMessage());
}
Also used : CustomResource(org.glassfish.resources.config.CustomResource) CustomResource(org.glassfish.resources.config.CustomResource) Resource(com.sun.enterprise.config.serverbeans.Resource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 29 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class CreateCustomResourceTest method testExecuteWithOptionalValuesSet.

/**
 * Test of execute method, of class CreateCustomResource.
 * asadmin create-custom-resource --restype=topic --factoryclass=javax.naming.spi.ObjectFactory
 * --enabled=false --description=Administered Object sample_custom_resource
 */
@Test
public void testExecuteWithOptionalValuesSet() {
    parameters.set("restype", "topic");
    parameters.set("factoryclass", "javax.naming.spi.ObjectFactory");
    parameters.set("enabled", "false");
    parameters.set("description", "Administered Object");
    parameters.set("jndi_name", "sample_custom_resource");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("create-custom-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    // Check the exit code is SUCCESS
    assertEquals(ActionReport.ExitCode.SUCCESS, context.getActionReport().getActionExitCode());
    // Check that the resource was created
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof CustomResource) {
            CustomResource r = (CustomResource) resource;
            if (r.getJndiName().equals("sample_custom_resource")) {
                assertEquals("topic", r.getResType());
                assertEquals("javax.naming.spi.ObjectFactory", r.getFactoryClass());
                // expect enabled for the resource to be true as resource-ref's enabled
                // would be set to false
                assertEquals("true", r.getEnabled());
                assertEquals("Administered Object", r.getDescription());
                isCreated = true;
                logger.fine("Custom Resource config bean sample_custom_resource is created.");
                break;
            }
        }
    }
    assertTrue(isCreated);
    logger.fine("msg: " + context.getActionReport().getMessage());
}
Also used : CustomResource(org.glassfish.resources.config.CustomResource) CustomResource(org.glassfish.resources.config.CustomResource) Resource(com.sun.enterprise.config.serverbeans.Resource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 30 with Resource

use of com.sun.enterprise.config.serverbeans.Resource in project Payara by payara.

the class ConnectorService method loadResourcesAndItsRar.

/*    public boolean checkAndLoadResource(Object resource, Object pool, String resourceType, String resourceName,
                                        String raName)
            throws ConnectorRuntimeException {
        String resname = ConnectorAdminServiceUtils.getOriginalResourceName(resourceName);
        if(_logger.isLoggable(Level.FINE)) {
            _logger.fine("ConnectorService :: checkAndLoadResource resolved to load " + resname);
        }

        DeferredResourceConfig defResConfig = getResourcesUtil().getDeferredResourceConfig(resource, pool, resourceType, raName);
        //DeferredResourceConfig defResConfig = resUtil.getDeferredResourceConfig(resname);
        return loadResourcesAndItsRar(defResConfig);
    }
*/
public boolean loadResourcesAndItsRar(DeferredResourceConfig defResConfig) {
    if (defResConfig != null) {
        try {
            loadDeferredResources(defResConfig.getResourceAdapterConfig());
            final String rarName = defResConfig.getRarName();
            loadDeferredResourceAdapter(rarName);
            final Resource[] resToLoad = defResConfig.getResourcesToLoad();
            AccessController.doPrivileged(new PrivilegedAction() {

                public Object run() {
                    try {
                        loadDeferredResources(resToLoad);
                    } catch (Exception ex) {
                        Object[] params = new Object[] { rarName, ex };
                        _logger.log(Level.SEVERE, "failed.to.load.deferred.resources", params);
                    }
                    return null;
                }
            });
        } catch (Exception ex) {
            Object[] params = new Object[] { defResConfig.getRarName(), ex };
            _logger.log(Level.SEVERE, "failed.to.load.deferred.ra", params);
            return false;
        }
        return true;
    }
    return false;
}
Also used : PrivilegedAction(java.security.PrivilegedAction) Resource(com.sun.enterprise.config.serverbeans.Resource) URISyntaxException(java.net.URISyntaxException) ConnectorRuntimeException(com.sun.appserv.connectors.internal.api.ConnectorRuntimeException)

Aggregations

Resource (com.sun.enterprise.config.serverbeans.Resource)44 Resources (com.sun.enterprise.config.serverbeans.Resources)14 Test (org.junit.Test)14 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)13 ParameterMap (org.glassfish.api.admin.ParameterMap)10 BindableResource (com.sun.enterprise.config.serverbeans.BindableResource)9 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)9 Domain (com.sun.enterprise.config.serverbeans.Domain)6 ResourceRef (com.sun.enterprise.config.serverbeans.ResourceRef)6 JdbcResource (org.glassfish.jdbc.config.JdbcResource)6 MailResource (org.glassfish.resources.javamail.config.MailResource)6 ResourceException (javax.resource.ResourceException)5 AdminObjectResource (org.glassfish.connectors.config.AdminObjectResource)5 ConnectorResource (org.glassfish.connectors.config.ConnectorResource)5 Resource (org.glassfish.resources.api.Resource)5 CustomResource (org.glassfish.resources.config.CustomResource)5 SingleConfigCode (org.jvnet.hk2.config.SingleConfigCode)5 ResourcePool (com.sun.enterprise.config.serverbeans.ResourcePool)4 PropsFileActionReporter (com.sun.enterprise.v3.common.PropsFileActionReporter)4 AdminCommandContextImpl (org.glassfish.api.admin.AdminCommandContextImpl)4