Search in sources :

Example 1 with RestValidationMessage

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setHighPrioritySettings.

@ApiOperation(value = "Update high priority queue settings", notes = "")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/high-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setHighPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            ThreadPoolExecutor executor = (ThreadPoolExecutor) Common.timer.getExecutorService();
            int currentCorePoolSize = executor.getCorePoolSize();
            int currentMaxPoolSize = executor.getMaximumPoolSize();
            if ((model.getMaximumPoolSize() != null) && (model.getMaximumPoolSize() < BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, currentMaxPoolSize)) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    executor.setCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.HIGH_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = executor.getCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() != null) {
                    executor.setMaximumPoolSize(model.getMaximumPoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.HIGH_PRI_MAX_POOL_SIZE, model.getMaximumPoolSize());
                } else {
                    // Get the info for the user
                    int maximumPoolSize = executor.getMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = executor.getActiveCount();
                int largestPoolSize = executor.getLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set high priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RestValidationMessage

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setMediumPrioritySettings.

@ApiOperation(value = "Update medium priority queue settings", notes = "Only corePoolSize and maximumPoolSize are used")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/medium-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setMediumPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            int currentCorePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE);
            if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, model.getCorePoolSize() == null ? currentCorePoolSize : model.getCorePoolSize())) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    Common.backgroundProcessing.setMediumPriorityServiceCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.MED_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = Common.backgroundProcessing.getMediumPriorityServiceCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() == null) {
                    // Get the info for the user
                    int maximumPoolSize = Common.backgroundProcessing.getMediumPriorityServiceMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = Common.backgroundProcessing.getMediumPriorityServiceActiveCount();
                int largestPoolSize = Common.backgroundProcessing.getMediumPriorityServiceLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set medium priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with RestValidationMessage

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage in project ma-core-public by infiniteautomation.

the class AbstractVoModel method validate.

/*
	 * (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.model.AbstractRestModel#validate(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)
	 */
@Override
public boolean validate() {
    ProcessResult validation = new ProcessResult();
    this.data.validate(validation);
    if (validation.getHasMessages()) {
        // Add our messages to the list
        for (ProcessMessage message : validation.getMessages()) {
            if (message.getGenericMessage() != null) {
                this.messages.add(new RestValidationMessage(message.getGenericMessage(), RestMessageLevel.ERROR, ""));
            } else {
                this.messages.add(new RestValidationMessage(message.getContextualMessage(), RestMessageLevel.ERROR, message.getContextKey()));
            }
        }
        return false;
    } else {
        // Validated ok
        return true;
    }
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 4 with RestValidationMessage

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method validate.

/**
 * Helper for validation
 * @param model
 * @param currentCorePoolSize
 * @param currentMaximumPoolSize
 * @return
 */
protected boolean validate(ThreadPoolSettingsModel model, int currentCorePoolSize, int currentMaximumPoolSize) {
    boolean passed = true;
    model.setMessages(new ArrayList<RestValidationMessage>());
    if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < 1)) {
        model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanZero"), RestMessageLevel.ERROR, "corePoolSize"));
        passed = false;
    }
    // Compare both if they are being added
    if ((model.getMaximumPoolSize() != null) && (model.getCorePoolSize() != null) && (model.getMaximumPoolSize() < model.getCorePoolSize())) {
        model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.maxGreaterThanMin"), RestMessageLevel.ERROR, "maximumPoolSize"));
        passed = false;
    }
    // Compare the max pool size to the existing if we aren't changing the Core size
    if ((model.getCorePoolSize() == null) && (model.getMaximumPoolSize() != null)) {
        if (model.getMaximumPoolSize() < currentCorePoolSize) {
            model.setCorePoolSize(currentCorePoolSize);
            model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.maxGreaterThanMin"), RestMessageLevel.ERROR, "maximumPoolSize"));
            passed = false;
        }
    }
    // Compare the max pool size to the existing if we aren't changing the Core size
    if ((model.getMaximumPoolSize() == null) && (model.getCorePoolSize() != null)) {
        if (model.getCorePoolSize() > currentMaximumPoolSize) {
            model.setMaximumPoolSize(currentMaximumPoolSize);
            model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.minLessThanMax"), RestMessageLevel.ERROR, "corePoolSize"));
            passed = false;
        }
    }
    return passed;
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 5 with RestValidationMessage

use of com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage in project ma-modules-public by infiniteautomation.

the class BackgroundProcessingRestController method setLowPrioritySettings.

@ApiOperation(value = "Update low priority service settings", notes = "Only corePoolSize and maximumPoolSize are used")
@RequestMapping(method = RequestMethod.PUT, produces = { "application/json" }, value = "/low-priority-thread-pool-settings")
public ResponseEntity<ThreadPoolSettingsModel> setLowPrioritySettings(@ApiParam(value = "Settings", required = true, allowMultiple = false) @RequestBody ThreadPoolSettingsModel model, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<ThreadPoolSettingsModel> result = new RestProcessResult<ThreadPoolSettingsModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Validate the settings
            int currentCorePoolSize = SystemSettingsDao.getIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE);
            if ((model.getCorePoolSize() != null) && (model.getCorePoolSize() < BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN)) {
                // Test to ensure we aren't setting too low
                model.getMessages().add(new RestValidationMessage(new TranslatableMessage("validate.greaterThanOrEqualTo", BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN), RestMessageLevel.ERROR, "corePoolSize"));
                result.addRestMessage(this.getValidationFailedError());
            } else if (!validate(model, currentCorePoolSize, model.getCorePoolSize() == null ? currentCorePoolSize : model.getCorePoolSize())) {
                result.addRestMessage(this.getValidationFailedError());
            } else {
                if (model.getCorePoolSize() != null) {
                    Common.backgroundProcessing.setLowPriorityServiceCorePoolSize(model.getCorePoolSize());
                    SystemSettingsDao.instance.setIntValue(SystemSettingsDao.LOW_PRI_CORE_POOL_SIZE, model.getCorePoolSize());
                } else {
                    // Get the info for the user
                    int corePoolSize = Common.backgroundProcessing.getLowPriorityServiceCorePoolSize();
                    model.setCorePoolSize(corePoolSize);
                }
                if (model.getMaximumPoolSize() == null) {
                    // Get the info for the user
                    int maximumPoolSize = Common.backgroundProcessing.getLowPriorityServiceMaximumPoolSize();
                    model.setMaximumPoolSize(maximumPoolSize);
                }
                // Get the settings for the model
                int activeCount = Common.backgroundProcessing.getLowPriorityServiceActiveCount();
                int largestPoolSize = Common.backgroundProcessing.getLowPriorityServiceLargestPoolSize();
                model.setActiveCount(activeCount);
                model.setLargestPoolSize(largestPoolSize);
            }
            return result.createResponseEntity(model);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to set low priority thread pool settings.");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : RestValidationMessage(com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage) ThreadPoolSettingsModel(com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestValidationMessage (com.serotonin.m2m2.web.mvc.rest.v1.message.RestValidationMessage)6 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 User (com.serotonin.m2m2.vo.User)3 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)3 ThreadPoolSettingsModel (com.serotonin.m2m2.web.mvc.rest.v1.model.backgroundProcessing.ThreadPoolSettingsModel)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 ArrayList (java.util.ArrayList)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1