Search in sources :

Example 21 with JdbcResource

use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.

the class JDBCResourceManager method delete.

public ResourceStatus delete(final Resources resources, final String jndiName, final String target) throws Exception {
    if (jndiName == null) {
        String msg = localStrings.getLocalString("jdbc.resource.noJndiName", "No JNDI name defined for JDBC resource.");
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    // ensure we already have this resource
    if (ConnectorsUtil.getResourceByName(resources, JdbcResource.class, jndiName) == null) {
        String msg = localStrings.getLocalString("delete.jdbc.resource.notfound", "A JDBC resource named {0} does not exist.", jndiName);
        return new ResourceStatus(ResourceStatus.FAILURE, msg);
    }
    if (environment.isDas()) {
        if ("domain".equals(target)) {
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 0) {
                String msg = localStrings.getLocalString("delete.jdbc.resource.resource-ref.exist", "jdbc-resource [ {0} ] is referenced in an" + "instance/cluster target, Use delete-resource-ref on appropriate target", jndiName);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
        } else {
            if (!resourceUtil.isResourceRefInTarget(jndiName, target)) {
                String msg = localStrings.getLocalString("delete.jdbc.resource.no.resource-ref", "jdbc-resource [ {0} ] is not referenced in target [ {1} ]", jndiName, target);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
            if (resourceUtil.getTargetsReferringResourceRef(jndiName).size() > 1) {
                String msg = localStrings.getLocalString("delete.jdbc.resource.multiple.resource-refs", "jdbc resource [ {0} ] is referenced in multiple " + "instance/cluster targets, Use delete-resource-ref on appropriate target", jndiName);
                return new ResourceStatus(ResourceStatus.FAILURE, msg);
            }
        }
    }
    try {
        JdbcResource jdbcResource = (JdbcResource) ConnectorsUtil.getResourceByName(resources, JdbcResource.class, jndiName);
        if (ResourceConstants.SYSTEM_ALL_REQ.equals(jdbcResource.getObjectType())) {
            String msg = localStrings.getLocalString("delete.jdbc.resource.system-all-req.object-type", "The jdbc resource [ {0} ] cannot be deleted as it is required to be configured in the system.", jndiName);
            return new ResourceStatus(ResourceStatus.FAILURE, msg);
        }
        // delete resource-ref
        resourceUtil.deleteResourceRef(jndiName, target);
        // delete jdbc-resource
        if (ConfigSupport.apply(new SingleConfigCode<Resources>() {

            public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
                JdbcResource resource = (JdbcResource) ConnectorsUtil.getResourceByName(resources, JdbcResource.class, jndiName);
                return param.getResources().remove(resource);
            }
        }, resources) == null) {
            String msg = localStrings.getLocalString("jdbc.resource.deletionFailed", "JDBC resource {0} delete failed ", jndiName);
            return new ResourceStatus(ResourceStatus.FAILURE, msg);
        }
    } catch (TransactionFailure tfe) {
        String msg = localStrings.getLocalString("jdbc.resource.deletionFailed", "JDBC resource {0} delete failed ", jndiName);
        ResourceStatus status = new ResourceStatus(ResourceStatus.FAILURE, msg);
        status.setException(tfe);
        return status;
    }
    String msg = localStrings.getLocalString("jdbc.resource.deleteSuccess", "JDBC resource {0} deleted successfully", jndiName);
    return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) JdbcResource(org.glassfish.jdbc.config.JdbcResource) SingleConfigCode(org.jvnet.hk2.config.SingleConfigCode) ResourceStatus(org.glassfish.resourcebase.resources.api.ResourceStatus) Resources(com.sun.enterprise.config.serverbeans.Resources)

Example 22 with JdbcResource

use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.

the class ListJdbcResources 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();
    try {
        Collection<JdbcResource> jdbcResources = domain.getResources().getResources(JdbcResource.class);
        List<Map<String, String>> resourcesList = new ArrayList<Map<String, String>>();
        List<DefaultResourceProxy> drps = habitat.getAllServices(DefaultResourceProxy.class);
        for (JdbcResource jdbcResource : jdbcResources) {
            String jndiName = jdbcResource.getJndiName();
            if (bindableResourcesHelper.resourceExists(jndiName, target)) {
                ActionReport.MessagePart part = report.getTopMessagePart().addChild();
                part.setMessage(jndiName);
                Map<String, String> resourceNameMap = new HashMap<String, String>();
                String logicalName = DefaultResourceProxy.Util.getLogicalName(drps, jndiName);
                if (logicalName != null) {
                    resourceNameMap.put("logical-jndi-name", logicalName);
                }
                resourceNameMap.put("name", jndiName);
                resourcesList.add(resourceNameMap);
            }
        }
        Properties extraProperties = new Properties();
        extraProperties.put("jdbcResources", resourcesList);
        report.setExtraProperties(extraProperties);
    } catch (Exception e) {
        report.setMessage(localStrings.getLocalString("list.jdbc.resources.failed", "List JDBC resources failed"));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) DefaultResourceProxy(org.glassfish.api.naming.DefaultResourceProxy) ActionReport(org.glassfish.api.ActionReport)

Example 23 with JdbcResource

use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.

the class DeleteJdbcResourceTest method testExecuteFailInvalidTarget.

/**
 * Test of execute method, of class DeleteJdbcResource.
 * delete-jdbc-resource --target invalid jdbc/foo
 */
@Test
@Ignore
public // the modified command does not fail when an invalid target is specified
void testExecuteFailInvalidTarget() {
    // Set operand
    parameters.add("target", "invalid");
    parameters.add("DEFAULT", "jdbc/foo");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("delete-jdbc-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(deleteCommand);
    // Check that the resource was NOT deleted
    boolean isDeleted = true;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof JdbcResource) {
            JdbcResource jr = (JdbcResource) resource;
            if (jr.getJndiName().equals("jdbc/foo")) {
                isDeleted = false;
                logger.fine("JdbcResource config bean jdbc/foo is not deleted.");
                continue;
            }
        }
    }
    // Need bug fix in DeleteJdbcResource before uncommenting assertion
    // assertFalse(isDeleted);
    // Check the exit code is FAILURE
    assertEquals(ActionReport.ExitCode.FAILURE, context.getActionReport().getActionExitCode());
    // Check the error message
    // Need bug fix in DeleteJdbcResource before uncommenting assertion
    // assertEquals(" Invalid target: invalid", context.getActionReport().getMessage());
    logger.fine("msg: " + context.getActionReport().getMessage());
    // Check that the resource ref was NOT deleted
    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("jdbc/foo")) {
                    isRefDeleted = false;
                    continue;
                }
            }
        }
    }
    assertFalse(isRefDeleted);
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) Ignore(org.junit.Ignore) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 24 with JdbcResource

use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.

the class CreateJdbcResourceTest method testExecuteSuccessDefaultValues.

/**
 * Test of execute method, of class CreateJdbcResource.
 * asadmin create-jdbc-resource --connectionpoolid DerbyPool jdbc/alldefaults
 */
@Test
public void testExecuteSuccessDefaultValues() {
    // Only pass the required option and operand
    parameters = new ParameterMap();
    parameters.set("connectionpoolid", "DerbyPool");
    parameters.set("DEFAULT", "jdbc/alldefaults");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("create-jdbc-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 JdbcResource) {
            JdbcResource jr = (JdbcResource) resource;
            if (jr.getJndiName().equals("jdbc/alldefaults")) {
                assertEquals("DerbyPool", jr.getPoolName());
                assertEquals("true", jr.getEnabled());
                assertNull(jr.getDescription());
                isCreated = true;
                logger.fine("JdbcResource config bean jdbc/alldefaults is created.");
                continue;
            }
        }
    }
    assertTrue(isCreated);
    logger.fine("msg: " + context.getActionReport().getMessage());
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) ParameterMap(org.glassfish.api.admin.ParameterMap) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Example 25 with JdbcResource

use of org.glassfish.jdbc.config.JdbcResource in project Payara by payara.

the class CreateJdbcResourceTest method testExecuteFailInvalidConnPoolId.

/**
 * Test of execute method, of class CreateJdbcResource when specified
 * connectionpoolid does not exist.
 * asadmin create-jdbc-resource --connectionpoolid xxxxxx --enabled=true
 *         --description "my resource" jdbc/nopool
 */
@Test
public void testExecuteFailInvalidConnPoolId() {
    // Set invalid connectionpoolid
    parameters.set("connectionpoolid", "xxxxxx");
    parameters.set("DEFAULT", "jdbc/nopool");
    // Call CommandRunnerImpl.doCommand(..) to execute the command
    cr.getCommandInvocation("create-jdbc-resource", context.getActionReport(), adminSubject()).parameters(parameters).execute(command);
    // Check the exit code is Failure
    assertEquals(ActionReport.ExitCode.FAILURE, context.getActionReport().getActionExitCode());
    // Check that the resource was NOT created
    boolean isCreated = false;
    for (Resource resource : resources.getResources()) {
        if (resource instanceof JdbcResource) {
            JdbcResource jr = (JdbcResource) resource;
            if (jr.getJndiName().equals("jdbc/nopool")) {
                isCreated = true;
                logger.fine("JdbcResource config bean jdbc/nopool is created.");
            }
        }
    }
    assertFalse(isCreated);
    // Check the error message
    assertEquals("Attribute value (pool-name = xxxxxx) is not found in list of jdbc connection pools.", context.getActionReport().getMessage());
}
Also used : JdbcResource(org.glassfish.jdbc.config.JdbcResource) JdbcResource(org.glassfish.jdbc.config.JdbcResource) Test(org.junit.Test) ConfigApiTest(org.glassfish.tests.utils.ConfigApiTest)

Aggregations

JdbcResource (org.glassfish.jdbc.config.JdbcResource)35 Test (org.junit.Test)12 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)9 ConfigApiTest (org.glassfish.tests.utils.ConfigApiTest)9 JdbcConnectionPool (org.glassfish.jdbc.config.JdbcConnectionPool)7 Resource (com.sun.enterprise.config.serverbeans.Resource)6 Resources (com.sun.enterprise.config.serverbeans.Resources)6 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)6 Domain (com.sun.enterprise.config.serverbeans.Domain)4 PoolInfo (org.glassfish.resourcebase.resources.api.PoolInfo)4 PropertyVetoException (java.beans.PropertyVetoException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 DataSourceDefinitionDescriptor (com.sun.enterprise.deployment.DataSourceDefinitionDescriptor)2 NamingException (javax.naming.NamingException)2 ResourceException (javax.resource.ResourceException)2 XAResource (javax.transaction.xa.XAResource)2 ConnectorRuntime (com.sun.appserv.connectors.internal.api.ConnectorRuntime)1