Java Code Examples for org.jboss.as.controller.OperationContext#completeStep()

The following examples show how to use org.jboss.as.controller.OperationContext#completeStep() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoggingResourceDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode operation) {
    final ModelNode model = Resource.Tools.readModel(context.readResource(PathAddress.EMPTY_ADDRESS));
    final String logDir = pathManager.getPathEntry(ServerEnvironment.SERVER_LOG_DIR).resolvePath();
    List<File> logFiles;
    try {
        logFiles = findFiles(logDir, model);
    } catch (IOException e) {
        logFiles = Collections.emptyList();
        LoggingLogger.ROOT_LOGGER.errorProcessingLogDirectory(logDir);
    }
    final SimpleDateFormat dateFormat = new SimpleDateFormat(LogFileResourceDefinition.ISO_8601_FORMAT);
    final ModelNode result = context.getResult().setEmptyList();
    for (File logFile : logFiles) {
        final ModelNode fileInfo = new ModelNode();
        fileInfo.get(FILE_NAME.getName()).set(logFile.getName());
        fileInfo.get(FILE_SIZE.getName()).set(logFile.length());
        fileInfo.get(LAST_MODIFIED_DATE.getName()).set(dateFormat.format(new Date(logFile.lastModified())));
        result.add(fileInfo);
    }
    context.completeStep(ResultHandler.NOOP_RESULT_HANDLER);
}
 
Example 2
Source File: SnapshotTakeHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    AuthorizationResult authorizationResult = context.authorize(operation);
    if (authorizationResult.getDecision() == AuthorizationResult.Decision.DENY) {
        throw ControllerLogger.ROOT_LOGGER.unauthorized(operation.get(OP).asString(), context.getCurrentAddress(), authorizationResult.getExplanation());
    }
    ModelNode commentNode = COMMENT.resolveModelAttribute(context, operation);
    ModelNode snapshotNode = SNAPSHOT_NAME.resolveModelAttribute(context, operation);
    String comment = commentNode.asStringOrNull();
    String snapshot = snapshotNode.asStringOrNull();
    try {
        String name = persister.snapshot(snapshot, comment);
        context.getResult().set(name);
    } catch (ConfigurationPersistenceException e) {
        throw new OperationFailedException(e);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 3
Source File: SnapshotListHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    AuthorizationResult authorizationResult = context.authorize(operation);
    if (authorizationResult.getDecision() == AuthorizationResult.Decision.DENY) {
        throw ControllerLogger.ROOT_LOGGER.unauthorized(operation.get(OP).asString(), context.getCurrentAddress(), authorizationResult.getExplanation());
    }

    try {
        SnapshotInfo info = persister.listSnapshots();
        ModelNode result = context.getResult();
        result.get(ModelDescriptionConstants.DIRECTORY).set(info.getSnapshotDirectory());
        result.get(ModelDescriptionConstants.NAMES).setEmptyList();
        for (String name : info.names()) {
            result.get(ModelDescriptionConstants.NAMES).add(name);
        }
    } catch (Exception e) {
        throw new OperationFailedException(e);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 4
Source File: SnapshotDeleteHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    AuthorizationResult authorizationResult = context.authorize(operation);
    if (authorizationResult.getDecision() == AuthorizationResult.Decision.DENY) {
        throw ControllerLogger.ROOT_LOGGER.unauthorized(operation.get(OP).asString(), context.getCurrentAddress(), authorizationResult.getExplanation());
    }

    String name = operation.require(ModelDescriptionConstants.NAME).asString();
    try {
        persister.deleteSnapshot(name);
    } catch (Exception e) {
        throw new OperationFailedException(e);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 5
Source File: InterfaceCriteriaWriteHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    nameValidator.validate(operation);
    final String attributeName = operation.require(NAME).asString();
    final ModelNode newValue = operation.hasDefined(VALUE) ? operation.get(VALUE) : new ModelNode();
    final ModelNode submodel = context.readResourceForUpdate(PathAddress.EMPTY_ADDRESS).getModel();
    final AttributeDefinition attributeDefinition = ATTRIBUTES.get(attributeName);
    if (attributeDefinition != null) {
        final ModelNode syntheticOp = new ModelNode();
        syntheticOp.get(attributeName).set(newValue);
        attributeDefinition.validateAndSet(syntheticOp, submodel);
    } else {
        throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.unknownAttribute(attributeName));
    }
    if (updateRuntime) {
        // Require a reload
        context.reloadRequired();
    }
    // Verify the model in a later step
    context.addStep(VERIFY_HANDLER, OperationContext.Stage.MODEL);
    OperationContext.RollbackHandler rollbackHandler = updateRuntime
            ? OperationContext.RollbackHandler.REVERT_RELOAD_REQUIRED_ROLLBACK_HANDLER
            : OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER;
    context.completeStep(rollbackHandler);
}
 
Example 6
Source File: ConfigurationPublishHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    AuthorizationResult authorizationResult = context.authorize(operation);
    if (authorizationResult.getDecision() == AuthorizationResult.Decision.DENY) {
        throw ControllerLogger.ROOT_LOGGER.unauthorized(operation.get(OP).asString(), context.getCurrentAddress(), authorizationResult.getExplanation());
    }

    String name = null;
    if(operation.hasDefined(LOCATION.getName())) {
        name = LOCATION.resolveModelAttribute(context, operation).asString();
    }
    try {
        context.getResult().set(persister.publish(name));
    } catch (ConfigurationPersistenceException e) {
        throw new OperationFailedException(e);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 7
Source File: LogFileResourceDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    final ModelNode model = context.getResult();
    final String name = context.getCurrentAddressValue();
    final String logDir = pathManager.getPathEntry(ServerEnvironment.SERVER_LOG_DIR).resolvePath();
    validateFile(context, logDir, name);
    final Path path = Paths.get(logDir, name);
    if (Files.notExists(path)) {
        throw LoggingLogger.ROOT_LOGGER.logFileNotFound(name, logDir);
    }
    try {
        updateModel(path, model);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    context.completeStep(ResultHandler.NOOP_RESULT_HANDLER);
}
 
Example 8
Source File: MigrateJsonOperation.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    List<ModelNode> ops = null;
    try {
        PathAddress currentAddr = context.getCurrentAddress();
        ops = JsonConfigConverter.convertJsonConfig(readConfig(operation), currentAddr);
    } catch (IOException ioe) {
        throw new OperationFailedException(ioe);
    }
     
    for (ModelNode op : ops) {
        PathAddress addr = PathAddress.pathAddress(op.get(ADDRESS));
        String opName = op.get(OP).asString();
        context.addStep(op, 
                        context.getRootResourceRegistration().getOperationHandler(addr, opName), 
                        OperationContext.Stage.MODEL);
    }
    
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 9
Source File: CamelRootResource.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
private void handleReadAttributeOperation(OperationContext context, ModelNode operation) {
    String name = operation.require(ModelDescriptionConstants.NAME).asString();
    if (ModelConstants.ENDPOINTS.equals(name)) {
        List<ModelNode> values = new ArrayList<>();
        for (URL aux : subsystemState.getRuntimeState().getEndpointURLs()) {
            values.add(new ModelNode(aux.toString()));
        }
        context.getResult().set(values);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 10
Source File: InterfaceCriteriaWriteHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode ignored) throws OperationFailedException {
    final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
    final ModelNode model = resource.getModel();
    for(final AttributeDefinition definition : InterfaceDefinition.ROOT_ATTRIBUTES) {
        final String attributeName = definition.getName();
        final boolean has = model.hasDefined(attributeName);
        if(! has && isRequired(definition, model)) {
            throw ControllerLogger.ROOT_LOGGER.required(attributeName);
        }
        if(has) {
            // Just ignore 'false'
            if(definition.getType() == ModelType.BOOLEAN && ! model.get(attributeName).asBoolean()) {
                continue;
            }
            if(! isAllowed(definition, model)) {
                // TODO probably move this into AttributeDefinition
                String[] alts = definition.getAlternatives();
                StringBuilder sb = null;
                if (alts != null) {
                    for (String alt : alts) {
                        if (model.hasDefined(alt)) {
                            if (sb == null) {
                                sb = new StringBuilder();
                            } else {
                                sb.append(", ");
                            }
                            sb.append(alt);
                        }
                    }
                }
                throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.invalidAttributeCombo(attributeName, sb));
            }
        }
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 11
Source File: LogFileResourceDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    final String fileName = context.getCurrentAddressValue();
    final String logDir = pathManager.getPathEntry(ServerEnvironment.SERVER_LOG_DIR).resolvePath();
    validateFile(context, logDir, fileName);
    // Validate the operation
    for (AttributeDefinition attribute : READ_LOG_FILE.getParameters()) {
        attribute.validateOperation(operation);
    }
    final int numberOfLines = LINES.resolveModelAttribute(context, operation).asInt();
    final int skip = SKIP.resolveModelAttribute(context, operation).asInt();
    final boolean tail = TAIL.resolveModelAttribute(context, operation).asBoolean();
    final ModelNode encodingModel = ENCODING.resolveModelAttribute(context, operation);
    final String encoding = (encodingModel.isDefined() ? encodingModel.asString() : null);
    final File path = new File(pathManager.resolveRelativePathEntry(fileName, ServerEnvironment.SERVER_LOG_DIR));

    // The file must exist
    if (!path.exists()) {
        throw LoggingLogger.ROOT_LOGGER.logFileNotFound(fileName, ServerEnvironment.SERVER_LOG_DIR);
    }

    // Read the contents of the log file
    try {
        final List<String> lines;
        if (numberOfLines == 0) {
            lines = Collections.emptyList();
        } else {
            lines = readLines(path, encoding, tail, skip, numberOfLines);
        }
        final ModelNode result = context.getResult().setEmptyList();
        for (String line : lines) {
            result.add(line);
        }
    } catch (IOException e) {
        throw LoggingLogger.ROOT_LOGGER.failedToReadLogFile(e, fileName);
    }
    context.completeStep(ResultHandler.NOOP_RESULT_HANDLER);
}
 
Example 12
Source File: ResolvePathHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    // Get the resource
    final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
    final ModelNode model = resource.getModel();

    // Validate the operation
    final ModelNode relativeToOnly = RELATIVE_TO_ONLY.validateOperation(operation);
    final boolean resolveRelativeToOnly = relativeToOnly.asBoolean(false);

    // Resolve the model values
    final ModelNode file = (parentAttribute != null ? parentAttribute.resolveModelAttribute(context, model) : model);
    final ModelNode relativeTo = relativeToAttribute.resolveModelAttribute(context, file);
    final ModelNode path = pathAttribute.resolveModelAttribute(context, file);

    // Resolve paths
    final String result;

    if (checkAbsolutePath
            && path.isDefined()
            && AbsolutePathService.isAbsoluteUnixOrWindowsPath(path.asString())) {
            result = pathManager.resolveRelativePathEntry(path.asString(), null);
    } else if (relativeTo.isDefined()) {
        // If resolving the full path and a path is defined
        if (!resolveRelativeToOnly && path.isDefined()) {
            result = pathManager.resolveRelativePathEntry(path.asString(), relativeTo.asString());
        } else {
            result = pathManager.getPathEntry(relativeTo.asString()).resolvePath();
        }
    } else if (path.isDefined()) {
        result = pathManager.resolveRelativePathEntry(path.asString(), null);
    } else {
        throw ControllerLogger.ROOT_LOGGER.noPathToResolve(relativeToAttribute.getName(), pathAttribute.getName(), model);
    }
    context.getResult().set(new ModelNode(result));
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 13
Source File: ThreadPoolMetricsHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
    final String attributeName = operation.require(ModelDescriptionConstants.NAME).asString();
    if (context.getRunningMode() == RunningMode.NORMAL) {
        ServiceController<?> serviceController = getService(context, operation);
        final Service<?> service = serviceController.getService();
        setResult(context, attributeName, service);
    }

    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 14
Source File: AbstractPlatformMBeanAttributeHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    String op = operation.require(OP).asString();
    if (READ_ATTRIBUTE_OPERATION.equals(op)) {
        readAttributeValidator.validate(operation);
        context.addStep(this::executeReadAttribute, OperationContext.Stage.RUNTIME);
    } else if (WRITE_ATTRIBUTE_OPERATION.equals(op)) {
        writeAttributeValidator.validate(operation);
        context.addStep(this::executeWriteAttribute, OperationContext.Stage.RUNTIME);
    }

    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 15
Source File: ThreadMXBeanResetPeakThreadCountHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    try {
        ManagementFactory.getThreadMXBean().resetPeakThreadCount();
    } catch (SecurityException e) {
        throw new OperationFailedException(e.toString());
    }

    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 16
Source File: MemoryPoolMXBeanResetPeakUsageHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {

    try {
        getMemoryPoolMXBean(operation).resetPeakUsage();
    } catch (SecurityException e) {
        throw new OperationFailedException(e.toString());
    }

    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 17
Source File: PatchAttributeReadHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void execute(final OperationContext context, final ModelNode operation, final InstalledIdentity installedIdentity) throws OperationFailedException {

    final ModelNode result = context.getResult();
    final Identity info = installedIdentity.getIdentity();
    try {
        handle(result, info);
    } catch (IOException e) {
        throw new OperationFailedException(PatchLogger.ROOT_LOGGER.failedToLoadInfo(info.getName()), e);
    }
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 18
Source File: ElementProviderAttributeReadHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void execute(final OperationContext context, final ModelNode operation, final InstalledIdentity installedIdentity) throws OperationFailedException {

    final PathAddress address = PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR));
    final PathElement element = address.getLastElement();
    final String name = element.getValue();

    PatchableTarget target = getProvider(name, installedIdentity);
    final ModelNode result = context.getResult();
    handle(result, target);
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 19
Source File: CoreManagementResourceRegistrationUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}
 
Example 20
Source File: ExtendWildCardRegistrationUnitTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
    context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
}