Example #1
0
  @Override
  protected void performRuntime(
      OperationContext context,
      ModelNode operation,
      ModelNode model,
      ServiceVerificationHandler verificationHandler,
      List<ServiceController<?>> newControllers)
      throws OperationFailedException {
    final PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));
    final PathAddress parent = address.subAddress(0, address.size() - 1);
    String name = address.getLastElement().getValue();
    String bindingRef =
        ListenerResourceDefinition.SOCKET_BINDING.resolveModelAttribute(context, model).asString();
    String workerName =
        ListenerResourceDefinition.WORKER.resolveModelAttribute(context, model).asString();
    String bufferPoolName =
        ListenerResourceDefinition.BUFFER_POOL.resolveModelAttribute(context, model).asString();
    boolean enabled =
        ListenerResourceDefinition.ENABLED.resolveModelAttribute(context, model).asBoolean();
    OptionMap listenerOptions =
        OptionList.resolveOptions(context, model, ListenerResourceDefinition.LISTENER_OPTIONS);
    OptionMap socketOptions =
        OptionList.resolveOptions(context, model, ListenerResourceDefinition.SOCKET_OPTIONS);
    String serverName = parent.getLastElement().getValue();
    final ServiceName listenerServiceName = UndertowService.listenerName(name);
    final ListenerService<? extends ListenerService> service =
        createService(name, serverName, context, model, listenerOptions, socketOptions);
    final ServiceBuilder<? extends ListenerService> serviceBuilder =
        context.getServiceTarget().addService(listenerServiceName, service);
    serviceBuilder
        .addDependency(IOServices.WORKER.append(workerName), XnioWorker.class, service.getWorker())
        .addDependency(
            SocketBinding.JBOSS_BINDING_NAME.append(bindingRef),
            SocketBinding.class,
            service.getBinding())
        .addDependency(
            IOServices.BUFFER_POOL.append(bufferPoolName), Pool.class, service.getBufferPool())
        .addDependency(
            UndertowService.SERVER.append(serverName), Server.class, service.getServerService());

    configureAdditionalDependencies(context, serviceBuilder, model, service);
    serviceBuilder.setInitialMode(
        enabled ? ServiceController.Mode.ACTIVE : ServiceController.Mode.NEVER);

    serviceBuilder.addListener(verificationHandler);
    final ServiceController<? extends ListenerService> serviceController = serviceBuilder.install();
    if (newControllers != null) {
      newControllers.add(serviceController);
    }
  }
  public static OptionMap populate(final ExpressionResolver resolver, final ModelNode model)
      throws OperationFailedException {
    OptionMap.Builder builder =
        OptionMap.builder()
            .set(Options.TCP_NODELAY, Boolean.TRUE)
            .set(Options.REUSE_ADDRESSES, true)
            .addAll(OptionList.resolveOptions(resolver, model, RemotingEndpointResource.OPTIONS));

    return builder.getMap();
  }
/** @author <a href="mailto:[email protected]">Tomaz Cerar</a> (c) 2013 Red Hat Inc. */
abstract class ListenerResourceDefinition extends PersistentResourceDefinition {
  protected static final SimpleAttributeDefinition SOCKET_BINDING =
      new SimpleAttributeDefinitionBuilder(Constants.SOCKET_BINDING, ModelType.STRING)
          .setAllowNull(false)
          .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES)
          .setValidator(new StringLengthValidator(1))
          .build();
  protected static final SimpleAttributeDefinition WORKER =
      new SimpleAttributeDefinitionBuilder(Constants.WORKER, ModelType.STRING)
          .setAllowNull(true)
          .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES)
          .setValidator(new StringLengthValidator(1))
          .setDefaultValue(new ModelNode("default"))
          .build();
  protected static final SimpleAttributeDefinition BUFFER_POOL =
      new SimpleAttributeDefinitionBuilder(Constants.BUFFER_POOL, ModelType.STRING)
          .setAllowNull(true)
          .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES)
          .setValidator(new StringLengthValidator(1))
          .setDefaultValue(new ModelNode("default"))
          .build();
  protected static final SimpleAttributeDefinition ENABLED =
      new SimpleAttributeDefinitionBuilder(Constants.ENABLED, ModelType.BOOLEAN)
          .setAllowNull(true)
          .setFlags(AttributeAccess.Flag.RESTART_ALL_SERVICES)
          .setDefaultValue(new ModelNode(true))
          .setAllowExpression(true)
          .build();

  static final List<OptionAttributeDefinition> OPTIONS =
      OptionList.builder()
          .addOption(
              UndertowOptions.MAX_HEADER_SIZE,
              "max-header-size",
              new ModelNode(UndertowOptions.DEFAULT_MAX_HEADER_SIZE))
          .addOption(
              UndertowOptions.MAX_ENTITY_SIZE,
              Constants.MAX_POST_SIZE,
              new ModelNode(UndertowOptions.DEFAULT_MAX_ENTITY_SIZE))
          .addOption(
              UndertowOptions.BUFFER_PIPELINED_DATA, "buffer-pipelined-data", new ModelNode(true))
          .addOption(UndertowOptions.MAX_PARAMETERS, "max-parameters", new ModelNode(1000))
          .addOption(UndertowOptions.MAX_HEADERS, "max-headers", new ModelNode(200))
          .addOption(UndertowOptions.MAX_COOKIES, "max-cookies", new ModelNode(200))
          .addOption(
              UndertowOptions.ALLOW_ENCODED_SLASH, "allow-encoded-slash", new ModelNode(false))
          .addOption(UndertowOptions.DECODE_URL, "decode-url", new ModelNode(true))
          .addOption(UndertowOptions.URL_CHARSET, "url-charset", new ModelNode("UTF-8"))
          .build();

  protected static final Collection ATTRIBUTES;

  static {
    ATTRIBUTES =
        new LinkedHashSet<AttributeDefinition>(
            Arrays.asList(SOCKET_BINDING, WORKER, BUFFER_POOL, ENABLED));
    ATTRIBUTES.addAll(OPTIONS);
  }

  public ListenerResourceDefinition(PathElement pathElement) {
    super(pathElement, UndertowExtension.getResolver(Constants.LISTENER));
  }

  public Collection<AttributeDefinition> getAttributes() {
    //noinspection unchecked
    return ATTRIBUTES;
  }

  @Override
  public void registerOperations(ManagementResourceRegistration resourceRegistration) {
    super.registerOperations(resourceRegistration);
    super.registerAddOperation(
        resourceRegistration, getAddHandler(), OperationEntry.Flag.RESTART_NONE);
    super.registerRemoveOperation(
        resourceRegistration,
        new ListenerRemoveHandler(getAddHandler()),
        OperationEntry.Flag.RESTART_NONE);
  }

  protected abstract ListenerAdd getAddHandler();
}