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

The following examples show how to use org.jboss.as.controller.OperationContext#getCurrentAddress() . 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: SocketBindingResourceDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
static void validateInterfaceReference(final OperationContext context, final ModelNode binding) throws OperationFailedException {

        ModelNode interfaceNode = binding.get(INTERFACE.getName());
        if (interfaceNode.getType() == ModelType.STRING) { // ignore UNDEFINED and EXPRESSION
            String interfaceName = interfaceNode.asString();
            PathAddress operationAddress = context.getCurrentAddress();
            //This can be used on both the host and the server, the socket binding group will be a
            //sibling to the interface in the model
            PathAddress interfaceAddress = PathAddress.EMPTY_ADDRESS;
            for (PathElement element : operationAddress) {
                if (element.getKey().equals(ModelDescriptionConstants.SOCKET_BINDING_GROUP)) {
                    break;
                }
                interfaceAddress = interfaceAddress.append(element);
            }
            interfaceAddress = interfaceAddress.append(ModelDescriptionConstants.INTERFACE, interfaceName);
            try {
                context.readResourceFromRoot(interfaceAddress, false);
            } catch (RuntimeException e) {
                throw ControllerLogger.ROOT_LOGGER.nonexistentInterface(interfaceName, INTERFACE.getName());
            }
        }

    }
 
Example 2
Source File: ReadAttributeHandler.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 {
    if (filteredData == null) {
        doExecuteInternal(context, operation);
    } else {
        try {
            doExecuteInternal(context, operation);
        } catch (UnauthorizedException ue) {
            if (context.hasResult()) {
                context.getResult().set(new ModelNode());
            }
            // Report the failure to the filter and complete normally
            PathAddress pa = context.getCurrentAddress();
            filteredData.addReadRestrictedAttribute(pa, operation.get(NAME).asString());
            context.getResult().set(new ModelNode());
        }
    }
}
 
Example 3
Source File: ModifiableRealmDecorator.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Try to obtain a {@link ModifiableSecurityRealm} based on the given {@link OperationContext}.
 *
 * @param context the current context
 * @return the current security realm
 * @throws OperationFailedException if any error occurs obtaining the reference to the security realm.
 */
static ModifiableSecurityRealm getModifiableSecurityRealm(OperationContext context) throws OperationFailedException {
    ServiceRegistry serviceRegistry = context.getServiceRegistry(true);
    PathAddress currentAddress = context.getCurrentAddress();
    RuntimeCapability<Void> runtimeCapability = MODIFIABLE_SECURITY_REALM_RUNTIME_CAPABILITY.fromBaseCapability(currentAddress.getLastElement().getValue());
    ServiceName realmName = runtimeCapability.getCapabilityServiceName();
    ServiceController<ModifiableSecurityRealm> serviceController = getRequiredService(serviceRegistry, realmName, ModifiableSecurityRealm.class);
    if ( serviceController.getState() != ServiceController.State.UP ){
        try {
            serviceController.awaitValue(500, TimeUnit.MILLISECONDS);
        } catch (IllegalStateException | InterruptedException | TimeoutException e) {
            throw ROOT_LOGGER.requiredServiceNotUp(serviceController.getName(), serviceController.getState());
        }
    }
    return serviceController.getValue();
}
 
Example 4
Source File: HandlerUtil.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
static void checkNoOtherHandlerWithTheSameName(OperationContext context) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    final PathAddress parentAddress = address.subAddress(0, address.size() - 1);
    final Resource resource = context.readResourceFromRoot(parentAddress);

    final PathElement element = address.getLastElement();
    final String handlerType = element.getKey();
    final String handlerName = element.getValue();

    for (String otherHandler: HANDLER_TYPES) {
        if (handlerType.equals(otherHandler)) {
            // we need to check other handler types for the same name
            continue;
        }
        final PathElement check = PathElement.pathElement(otherHandler, handlerName);
        if (resource.hasChild(check)) {
            throw DomainManagementLogger.ROOT_LOGGER.handlerAlreadyExists(check.getValue(), parentAddress.append(check));
        }
    }
}
 
Example 5
Source File: DeploymentScannerAdd.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
static void performRuntime(final OperationContext context, ModelNode operation, ModelNode model,
                            final ScheduledExecutorService executorService,
                            final FileSystemDeploymentService bootTimeScanner) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    final String path = DeploymentScannerDefinition.PATH.resolveModelAttribute(context, model).asString();
    final Boolean enabled = SCAN_ENABLED.resolveModelAttribute(context, model).asBoolean();
    final Integer interval = SCAN_INTERVAL.resolveModelAttribute(context, model).asInt();
    final String relativeTo = operation.hasDefined(CommonAttributes.RELATIVE_TO) ? RELATIVE_TO.resolveModelAttribute(context, model).asString() : null;
    final Boolean autoDeployZip = AUTO_DEPLOY_ZIPPED.resolveModelAttribute(context, model).asBoolean();
    final Boolean autoDeployExp = AUTO_DEPLOY_EXPLODED.resolveModelAttribute(context, model).asBoolean();
    final Boolean autoDeployXml = AUTO_DEPLOY_XML.resolveModelAttribute(context, model).asBoolean();
    final Long deploymentTimeout = DEPLOYMENT_TIMEOUT.resolveModelAttribute(context, model).asLong();
    final Boolean rollback = RUNTIME_FAILURE_CAUSES_ROLLBACK.resolveModelAttribute(context, model).asBoolean();
    DeploymentScannerService.addService(context, address, relativeTo, path, interval, TimeUnit.MILLISECONDS,
            autoDeployZip, autoDeployExp, autoDeployXml, enabled, deploymentTimeout, rollback, bootTimeScanner, executorService);

}
 
Example 6
Source File: HttpConnectorAdd.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 {
    final PathAddress address = context.getCurrentAddress();
    final String connectorName = address.getLastElement().getValue();

    PathAddress parentAddress = address.getParent();
    Resource parent = context.readResourceFromRoot(parentAddress, false);
    Resource resource = context.readResourceFromRoot(address, false);
    ModelNode resourceRef = resource.getModel().get(CommonAttributes.CONNECTOR_REF);
    boolean listenerAlreadyExists = false;

    for(Resource.ResourceEntry child: parent.getChildren(CommonAttributes.HTTP_CONNECTOR)) {
        if(!connectorName.equals(child.getName())) {
            Resource childResource = context.readResourceFromRoot(PathAddress.pathAddress(parentAddress, child.getPathElement()), false);
            if(childResource.getModel().get(CommonAttributes.CONNECTOR_REF).equals(resourceRef)) {
                listenerAlreadyExists = true;
                break;
            }
        }
    }

    if(listenerAlreadyExists) {
        throw ControllerLogger.ROOT_LOGGER.alreadyDefinedAttribute(CommonAttributes.HTTP_CONNECTOR, resourceRef.asString(), CommonAttributes.CONNECTOR_REF);
    }
}
 
Example 7
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 8
Source File: DomainModelControllerService.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected OperationStepHandler createExtraValidationStepHandler() {
    return new OperationStepHandler() {
        @Override
        public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
            if (!context.isBooting()) {
                PathAddress addr = context.getCurrentAddress();
                if (addr.size() > 0 && addr.getLastElement().getKey().equals(SUBSYSTEM)) {
                    //For subsystem adds in domain mode we need to check that the new subsystem does not break the rule
                    //that when profile includes are used, we don't allow overriding subsystems.
                    DomainModelIncludesValidator.addValidationStep(context, operation);
                }
            }
        }
    };
}
 
Example 9
Source File: ManagedDMRContentTypeAddHandler.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 {
    PathAddress address = context.getCurrentAddress();
    byte[] hash = HASH.validateOperation(operation).asBytesOrNull();

    if (hash != null && fileRepository != null) {
        // Ensure the local repo has the files
        fileRepository.getDeploymentFiles(new ContentReference(address.toCLIStyleString(), hash));
    }

    // Create and add the specialized resource type we use for this resource tree
    ManagedDMRContentTypeResource resource = new ManagedDMRContentTypeResource(address, childType, hash, contentRepository);
    context.addResource(PathAddress.EMPTY_ADDRESS, resource);
}
 
Example 10
Source File: ModifiableKeyStoreDecorator.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Try to obtain a {@link KeyStore} based on the given {@link OperationContext}.
 *
 * @param context the current context
 * @return the unmodifiable KeyStore
 * @throws OperationFailedException if any error occurs while obtaining.
 */
static KeyStore getKeyStore(OperationContext context) throws OperationFailedException {
    ServiceRegistry serviceRegistry = context.getServiceRegistry(true);
    PathAddress currentAddress = context.getCurrentAddress();
    RuntimeCapability<Void> runtimeCapability = KEY_STORE_RUNTIME_CAPABILITY.fromBaseCapability(currentAddress.getLastElement().getValue());
    ServiceName serviceName = runtimeCapability.getCapabilityServiceName();
    ServiceController<KeyStore> serviceController = getRequiredService(serviceRegistry, serviceName, KeyStore.class);

    return Assert.assertNotNull(serviceController.getValue());
}
 
Example 11
Source File: OutboundSocketBindingWriteHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected boolean requiresRuntime(OperationContext context) {
    if (context.getProcessType().isServer()) {
        return super.requiresRuntime(context);
    }
    //Check if we are a host's socket binding and install the service if we are
    PathAddress pathAddress = context.getCurrentAddress();
    return pathAddress.size() > 0 && pathAddress.getElement(0).getKey().equals(HOST);
}
 
Example 12
Source File: StaticDiscoveryWriteAttributeHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void finishModelStage(OperationContext context, ModelNode operation, String attributeName, ModelNode newValue,
        ModelNode oldValue, Resource model) throws OperationFailedException {
    super.finishModelStage(context, operation, attributeName, newValue, oldValue, model);

    final PathAddress operationAddress = context.getCurrentAddress();
    final PathAddress discoveryOptionsAddress = operationAddress.getParent();
    final ModelNode discoveryOptions = context.readResourceFromRoot(discoveryOptionsAddress, false).getModel();

    // Get the current list of discovery options and set new value for attribute
    final ModelNode currentListClone = discoveryOptions.get(ModelDescriptionConstants.OPTIONS).clone();
    final String name = context.getCurrentAddressValue();
    for (ModelNode element : currentListClone.asList()) {
        final Property prop = element.asProperty();
        final String discoveryOptionType = prop.getName();
        final String discoveryOptionName = prop.getValue().get(ModelDescriptionConstants.NAME).asString();
        if ( discoveryOptionName.equals(name) && discoveryOptionType.equals(ModelDescriptionConstants.STATIC_DISCOVERY)) {
            final ModelNode node = element.get(ModelDescriptionConstants.STATIC_DISCOVERY);
            node.get(attributeName).set(newValue);
            break;
        }
    }

    final ModelNode writeOp = Util.getWriteAttributeOperation(discoveryOptionsAddress, ModelDescriptionConstants.OPTIONS, currentListClone);
    final OperationStepHandler writeHandler = context.getRootResourceRegistration().getSubModel(discoveryOptionsAddress).getOperationHandler(PathAddress.EMPTY_ADDRESS, WRITE_ATTRIBUTE_OPERATION);
    context.addStep(writeOp, writeHandler, OperationContext.Stage.MODEL, true);
}
 
Example 13
Source File: StoppedServerResource.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void readResourceServerConfig(OperationContext context) {
    final PathAddress address = context.getCurrentAddress();
    final String hostName = address.getElement(0).getValue();
    final PathElement element = address.getLastElement();
    final String serverName = element.getValue();

    final ModelNode addr = new ModelNode();
    addr.add(HOST, hostName);
    addr.add(SERVER_CONFIG, serverName);
    context.readResourceFromRoot(PathAddress.pathAddress(addr), false);
}
 
Example 14
Source File: LocalDestinationOutboundSocketBindingAddHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected boolean requiresRuntime(OperationContext context) {
    if (context.getProcessType().isServer()) {
        return super.requiresRuntime(context);
    }
    //Check if we are a host's socket binding and install the service if we are
    PathAddress pathAddress = context.getCurrentAddress();
    return pathAddress.size() > 0 && pathAddress.getElement(0).getKey().equals(HOST);
}
 
Example 15
Source File: RemoteDestinationOutboundSocketBindingAddHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected boolean requiresRuntime(OperationContext context) {
    if (context.getProcessType().isServer()) {
        return super.requiresRuntime(context);
    }
    //Check if we are a host's socket binding and install the service if we are
    PathAddress pathAddress = context.getCurrentAddress();
    return pathAddress.size() > 0 && pathAddress.getElement(0).getKey().equals(HOST);
}
 
Example 16
Source File: DomainDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) {
    super.performRuntime(context, operation, model);
    if (context.isResourceServiceRestartAllowed()) {
        final PathAddress address = context.getCurrentAddress();
        final String name = address.getLastElement().getValue();
        context.removeService(serviceName(name, address).append(INITIAL));
    }
}
 
Example 17
Source File: CachingRealmDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void executeRuntimeStep(final OperationContext context, final ModelNode operation) throws OperationFailedException {
    ServiceRegistry serviceRegistry = context.getServiceRegistry(true);
    PathAddress currentAddress = context.getCurrentAddress();
    RuntimeCapability<Void> runtimeCapability = SECURITY_REALM_RUNTIME_CAPABILITY.fromBaseCapability(currentAddress.getLastElement().getValue());
    ServiceName realmName = runtimeCapability.getCapabilityServiceName();
    ServiceController<SecurityRealm> serviceController = getRequiredService(serviceRegistry, realmName, SecurityRealm.class);
    CachingSecurityRealm securityRealm = CachingSecurityRealm.class.cast(serviceController.getValue());
    securityRealm.removeAllFromCache();
}
 
Example 18
Source File: ReadChildrenNamesHandler.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 {

    final PathAddress address = context.getCurrentAddress();
    final String childType = CHILD_TYPE.resolveModelAttribute(context, operation).asString();
    final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS, false);
    ImmutableManagementResourceRegistration registry = context.getResourceRegistration();
    Map<String, Set<String>> childAddresses = GlobalOperationHandlers.getChildAddresses(context, address, registry, resource, childType);
    Set<String> childNames = childAddresses.get(childType);
    if (childNames == null) {
        throw new OperationFailedException(ControllerLogger.ROOT_LOGGER.unknownChildType(childType));
    }
    final boolean singletons = INCLUDE_SINGLETONS.resolveModelAttribute(context, operation).asBoolean(false);
    if (singletons && isSingletonResource(registry, childType)) {
        Set<PathElement> childTypes = registry.getChildAddresses(PathAddress.EMPTY_ADDRESS);
        for (PathElement child : childTypes) {
            if (childType.equals(child.getKey())) {
                childNames.add(child.getValue());
            }
        }
    }
    // Sort the result
    childNames = new TreeSet<String>(childNames);
    ModelNode result = context.getResult();
    result.setEmptyList();
    PathAddress childAddress = address.append(PathElement.pathElement(childType));
    ModelNode op = Util.createEmptyOperation(READ_RESOURCE_OPERATION, childAddress);
    op.get(OPERATION_HEADERS).set(operation.get(OPERATION_HEADERS));
    ModelNode opAddr = op.get(OP_ADDR);
    ModelNode childProperty = opAddr.require(address.size());
    Set<Action.ActionEffect> actionEffects = EnumSet.of(Action.ActionEffect.ADDRESS);
    FilteredData fd = null;
    for (String childName : childNames) {
        childProperty.set(childType, new ModelNode(childName));
        if (context.authorize(op, actionEffects).getDecision() == AuthorizationResult.Decision.PERMIT) {
            result.add(childName);
        } else {
            if (fd == null) {
                fd = new FilteredData(address);
            }
            fd.addAccessRestrictedResource(childAddress);
        }
    }

    if (fd != null) {
        context.getResponseHeaders().get(ACCESS_CONTROL).set(fd.toModelNode());
    }
}
 
Example 19
Source File: ReadResourceDescriptionHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
void doExecute(OperationContext context, ModelNode operation, FilteredData filteredData, boolean ignoreMissingResource) throws OperationFailedException {
    final PathAddress address = context.getCurrentAddress();
    ReadResourceDescriptionAccessControlContext accessControlContext = getAccessControlContext() == null ? new ReadResourceDescriptionAccessControlContext(address, null) : getAccessControlContext();
    doExecute(context, operation, accessControlContext);
}
 
Example 20
Source File: ReadResourceDescriptionHandler.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 {
            ModelNode result = new ModelNode();
            boolean customDefaultCheck = operation.get(OP).asString().equals(GlobalOperationHandlers.CHECK_DEFAULT_RESOURCE_ACCESS);
            ResourceAuthorization authResp = context.authorizeResource(true, customDefaultCheck);
            if (authResp == null || authResp.getResourceResult(ActionEffect.ADDRESS).getDecision() == Decision.DENY) {
                if (!defaultSetting || authResp == null) {
                    //We are not allowed to see the resource, so we don't set the accessControlResult, meaning that the ReadResourceAssemblyHandler will ignore it for this address
                } else {
                    result.get(ActionEffect.ADDRESS.toString()).set(false);
                }
            } else {
//                if (!defaultSetting) {
//                    result.get(ADDRESS).set(operation.get(OP_ADDR));
//                }
                addResourceAuthorizationResults(result, authResp);

                ModelNode attributes = new ModelNode();
                attributes.setEmptyObject();

                if (result.get(READ).asBoolean()) {
                    if (nodeDescription.hasDefined(ATTRIBUTES)) {
                        for (Property attrProp : nodeDescription.require(ATTRIBUTES).asPropertyList()) {
                            ModelNode attributeResult = new ModelNode();
                            Storage storage = Storage.valueOf(attrProp.getValue().get(STORAGE).asString().toUpperCase(Locale.ENGLISH));
                            addAttributeAuthorizationResults(attributeResult, attrProp.getName(), authResp, storage == Storage.RUNTIME);
                            if (attributeResult.isDefined()) {
                                attributes.get(attrProp.getName()).set(attributeResult);
                            }
                        }
                    }
                    result.get(ATTRIBUTES).set(attributes);

                    if (operations != null) {
                        ModelNode ops = new ModelNode();
                        ops.setEmptyObject();
                        PathAddress currentAddress = context.getCurrentAddress();
                        for (Map.Entry<String, ModelNode> entry : operations.entrySet()) {

                            ModelNode operationToCheck = Util.createOperation(entry.getKey(), currentAddress);

                            ModelNode operationResult = new ModelNode();

                            addOperationAuthorizationResult(context, operationResult, operationToCheck, entry.getKey());

                            ops.get(entry.getKey()).set(operationResult);
                        }
                        result.get(ModelDescriptionConstants.OPERATIONS).set(ops);
                    }
                }
            }
            accessControlResult.set(result);
        }