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);
}
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);
}
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);
}
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());
}
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());
}
Aggregations