use of org.jvnet.hk2.config in project Payara by payara.
the class SetMonitoringLevel method execute.
@Override
public void execute(AdminCommandContext context) {
final ActionReport actionReport = context.getActionReport();
Config config = targetUtil.getConfig(target);
if (config != null) {
monitoringService = config.getMonitoringService();
} else {
actionReport.setMessage("Cound not find target: " + target);
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
if (moduleNames != null && moduleMonitoringLevels != null) {
String modifiedModuleNames = moduleNames.replace(":", ",");
String modifiedModuleMonitoringLevels = moduleMonitoringLevels.replace(":", ",");
List<String> moduleNameList = Arrays.asList(modifiedModuleNames.split(","));
List<String> moduleLevelList = Arrays.asList(modifiedModuleMonitoringLevels.split(","));
if (moduleNameList.size() == moduleLevelList.size()) {
for (int i = 0; i < moduleLevelList.size(); i++) {
boolean isValidMoudle = false;
List<String> validModuleList = new ArrayList<>(Arrays.asList(Constants.validModuleNames));
String moduleName = moduleNameList.get(i).trim().toLowerCase();
for (String module : validModuleList) {
if (module.trim().equalsIgnoreCase(moduleName)) {
String moduleLevel = moduleLevelList.get(i).trim().toUpperCase();
try {
ConfigSupport.apply(new SingleConfigCode<MonitoringService>() {
@Override
public Object run(final MonitoringService monitoringServiceProxy) throws PropertyVetoException, TransactionFailure {
monitoringServiceProxy.setMonitoringLevel(moduleName, moduleLevel);
return monitoringServiceProxy;
}
}, monitoringService);
} catch (TransactionFailure ex) {
logger.log(Level.WARNING, "Failed to execute the command set-monitoring-level: {0}", ex.getCause().getMessage());
actionReport.setMessage(ex.getCause().getMessage());
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
isValidMoudle = true;
break;
}
}
if (!isValidMoudle) {
actionReport.setMessage(moduleNameList.get(i) + " isn't a valid Module name");
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
} else {
actionReport.setMessage("Number of Module and Level entered doesn't match");
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
}
actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
use of org.jvnet.hk2.config in project Payara by payara.
the class ReferenceConstrainTest method serverConfigRefInvalid.
@Test
public void serverConfigRefInvalid() throws TransactionFailure {
Server server = habitat.getService(Server.class, "server");
assertNotNull(server);
ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(server);
Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
Map<String, String> configChanges = new HashMap<String, String>();
configChanges.put("config-ref", "server-config-nonexist");
changes.put(serverConfig, configChanges);
try {
ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
cs.apply(changes);
fail("Can not reach this point");
} catch (TransactionFailure tf) {
ConstraintViolationException cv = findConstrViolation(tf);
assertNotNull(cv);
}
}
use of org.jvnet.hk2.config in project Payara by payara.
the class UpdateResourceRef method execute.
/**
* Execution method for updating the configuration of a ResourceRef. Will be
* replicated if the target is a cluster.
*
* @param context context for the command.
*/
@Override
public void execute(AdminCommandContext context) {
final ActionReport report = context.getActionReport();
final Logger logger = context.getLogger();
// Make a list of all ResourceRefs that need to change
List<ResourceRef> resourceRefsToChange = new ArrayList<>();
// Add the ResourceRef from a named server if the target is a server
Server server = domain.getServerNamed(target);
// if the target is a server
if (server != null) {
ResourceRef serverResourceRef = server.getResourceRef(name);
// if the ResourceRef doesn't exist
if (serverResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(serverResourceRef);
}
// Add the ResourceRef from a named config if the target is a config
Config config = domain.getConfigNamed(target);
// if the target is a config
if (config != null) {
ResourceRef configResourceRef = config.getResourceRef(name);
// if the ResourceRef doesn't exist
if (configResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(configResourceRef);
}
// Add the ResourceRefs from a named cluster if the target is a cluster
Cluster cluster = domain.getClusterNamed(target);
// if the target is a cluster
if (cluster != null) {
ResourceRef clusterResourceRef = cluster.getResourceRef(name);
// if the ResourceRef doesn't exist
if (clusterResourceRef == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(clusterResourceRef);
for (Server instance : cluster.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the cluster contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
if (dg != null) {
ResourceRef ref = dg.getResourceRef(name);
if (ref == null) {
report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
return;
}
resourceRefsToChange.add(ref);
for (Server instance : dg.getInstances()) {
ResourceRef instanceResourceRef = instance.getResourceRef(name);
// if the server in the dg contains the ResourceRef
if (instanceResourceRef != null) {
resourceRefsToChange.add(instanceResourceRef);
}
}
}
// Apply the configuration to the listed ResourceRefs
try {
ConfigSupport.apply(new ConfigCode() {
@Override
public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
for (ConfigBeanProxy proxy : params) {
if (proxy instanceof ResourceRef) {
ResourceRef resourceRefProxy = (ResourceRef) proxy;
if (enabled != null) {
resourceRefProxy.setEnabled(enabled.toString());
}
}
}
report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
return true;
}
}, resourceRefsToChange.toArray(new ResourceRef[] {}));
} catch (TransactionFailure ex) {
report.failure(logger, ex.getLocalizedMessage());
}
}
use of org.jvnet.hk2.config in project Payara by payara.
the class DeleteConfigCommand method execute.
@Override
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
report.setActionExitCode(SUCCESS);
// Do not delete default-config
if (destConfig.equals("default-config")) {
report.setMessage(LOCAL_STRINGS.getLocalString("Config.defaultConfig", "The default configuration template " + "named default-config cannot be deleted."));
report.setActionExitCode(FAILURE);
return;
}
// Get the config from the domain. Does the config exist?
// if not return
Config config = domain.getConfigNamed(destConfig);
if (config == null) {
report.setMessage(LOCAL_STRINGS.getLocalString("Config.noSuchConfig", "Config {0} does not exist.", destConfig));
report.setActionExitCode(FAILURE);
return;
}
// Check if the config in use by some other
// ReferenceContainer -- if so just return...
List<ReferenceContainer> referenceContainers = domain.getReferenceContainersOf(config);
if (!referenceContainers.isEmpty()) {
StringBuilder namesOfContainers = new StringBuilder();
for (ReferenceContainer referenceContainer : referenceContainers) {
namesOfContainers.append(referenceContainer.getReference()).append(',');
}
report.setMessage(LOCAL_STRINGS.getLocalString("Config.inUseConfig", "Config {0} is in use " + "and must be referenced by no server instances or clusters", destConfig, namesOfContainers));
report.setActionExitCode(FAILURE);
return;
}
try {
ConfigSupport.apply(new SingleConfigCode<Configs>() {
@Override
public Object run(Configs configs) throws PropertyVetoException, TransactionFailure {
configs.getConfig().remove(config);
return null;
}
}, configs);
} catch (TransactionFailure ex) {
report.setMessage(LOCAL_STRINGS.getLocalString("Config.deleteConfigFailed", "Unable to remove config {0} ", config) + " " + ex.getLocalizedMessage());
report.setActionExitCode(FAILURE);
report.setFailureCause(ex);
}
}
use of org.jvnet.hk2.config in project Payara by payara.
the class ResourceAdapterConfigManager method create.
public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target) throws Exception {
setParams(attributes);
ResourceStatus validationStatus = isValid(resources);
if (validationStatus.getStatus() == ResourceStatus.FAILURE) {
return validationStatus;
}
try {
ConfigSupport.apply(new SingleConfigCode<Resources>() {
public Object run(Resources param) throws PropertyVetoException, TransactionFailure {
ResourceAdapterConfig newResource = createConfigBean(param, properties);
param.getResources().add(newResource);
return newResource;
}
}, resources);
} catch (TransactionFailure tfe) {
Logger.getLogger(ResourceAdapterConfigManager.class.getName()).log(Level.SEVERE, "TransactionFailure: create-resource-adapter-config", tfe);
String msg = localStrings.getLocalString("create.resource.adapter.config.fail", "Unable to create resource adapter config", raName) + " " + tfe.getLocalizedMessage();
return new ResourceStatus(ResourceStatus.FAILURE, msg);
}
String msg = localStrings.getLocalString("create.resource.adapter.config.success", "Resource adapter config {0} created successfully", raName);
return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
Aggregations