final ModelNode address = operation.require(OP_ADDR);
final String dsName = PathAddress.pathAddress(address).getLastElement().getValue();
final String jndiName = model.get(JNDINAME.getName()).asString();
final ServiceRegistry registry = context.getServiceRegistry(true);
final ServiceName dataSourceServiceName = AbstractDataSourceService.SERVICE_NAME_BASE.append(jndiName);
final ServiceController<?> dataSourceController = registry.getService(dataSourceServiceName);
if (dataSourceController != null) {
if (ServiceController.State.UP.equals(dataSourceController.getState())) {
dataSourceController.setMode(ServiceController.Mode.NEVER);
} else {
throw new OperationFailedException(new ModelNode().set(MESSAGES.serviceNotEnabled("Data-source", dsName)));
}
} else {
throw new OperationFailedException(new ModelNode().set(MESSAGES.serviceNotAvailable("Data-source", dsName)));
}
final ServiceName referenceServiceName = DataSourceReferenceFactoryService.SERVICE_NAME_BASE.append(dsName);
final ServiceController<?> referenceController = registry.getService(referenceServiceName);
if (referenceController != null ) {
context.removeService(referenceController);
}
final ServiceName binderServiceName = ContextNames.bindInfoFor(jndiName).getBinderServiceName();
final ServiceController<?> binderController = registry.getService(binderServiceName);
if (binderController != null ) {
context.removeService(binderController);
}
final ServiceName dataSourceConfigServiceName = DataSourceConfigService.SERVICE_NAME_BASE.append(dsName);
final ServiceController<?> dataSourceConfigController = registry.getService(dataSourceConfigServiceName);
final List<ServiceName> serviceNames = registry.getServiceNames();
final ServiceName xaDataSourceConfigServiceName = XADataSourceConfigService.SERVICE_NAME_BASE.append(dsName);
final ServiceController<?> xaDataSourceConfigController = registry.getService(xaDataSourceConfigServiceName);
for (ServiceName name : serviceNames) {
if (dataSourceConfigServiceName.append("connection-properties").isParentOf(name)) {
final ServiceController<?> connPropertyController = registry.getService(name);
if (connPropertyController != null) {
if (ServiceController.State.UP.equals(connPropertyController.getState())) {
connPropertyController.setMode(ServiceController.Mode.NEVER);
} else {
throw new OperationFailedException(new ModelNode().set(MESSAGES.serviceAlreadyStarted("Data-source.connectionProperty", name)));
}
} else {
throw new OperationFailedException(new ModelNode().set(MESSAGES.serviceNotAvailable("Data-source.connectionProperty", name)));
}
}
if (xaDataSourceConfigServiceName.append("xa-datasource-properties").isParentOf(name)) {
final ServiceController<?> xaConfigPropertyController = registry.getService(name);
if (xaConfigPropertyController != null) {
if (ServiceController.State.UP.equals(xaConfigPropertyController.getState())) {
xaConfigPropertyController.setMode(ServiceController.Mode.NEVER);
} else {