<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright The WildFly Authors
  ~ SPDX-License-Identifier: Apache-2.0
  -->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:jboss:domain:jca:2.0"
           xmlns="urn:jboss:domain:jca:2.0"
           xmlns:threads="urn:jboss:domain:threads:1.1"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           version="2.0">

    <xs:import namespace="urn:jboss:domain:threads:1.1" schemaLocation="jboss-as-threads_1_1.xsd"/>

    <xs:element name="subsystem" type="subsystemType"/>

    <xs:complexType name="subsystemType">
        <xs:sequence>
            <xs:element name="archive-validation"
                type="archive-validationType" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>
                        Toggle archive validation for the deployment
                        units. If it's not present it's considered true
                        with default attributes.
                    </xs:documentation>
                </xs:annotation>
            </xs:element>

            <xs:element name="bean-validation"
                type="bean-validationType" minOccurs="0">
                <xs:annotation>
                    <xs:documentation>
                        Toggle bean validation (JSR-303) for the
                        deployment units. If it's not present it's
                        considered true
                    </xs:documentation>
                </xs:annotation>
            </xs:element>

            <xs:element name="default-workmanager" type="workmanagerType" minOccurs="1">
                <xs:annotation>
                    <xs:documentation>
                        The default work manager and its thread pools
                    </xs:documentation>
                </xs:annotation>
            </xs:element>

            <xs:element name="workmanager" type="workmanagerType" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>
                        A custom work manager definition and its thread pools
                    </xs:documentation>
                </xs:annotation>
            </xs:element>

            <xs:element name="distributed-workmanager" type="distributedWorkmanagerType" minOccurs="0"
                        maxOccurs="unbounded">
              <xs:annotation>
                <xs:documentation>
                  A distributed work manager definition and its thread pools
                </xs:documentation>
              </xs:annotation>
            </xs:element>

            <xs:element name="bootstrap-contexts" type="bootstrap-contextsType" minOccurs="0">
              <xs:annotation>
                <xs:documentation>
                  Definition of custom bootstrap contexts
                </xs:documentation>
              </xs:annotation>
            </xs:element>

            <xs:element name="cached-connection-manager" type="cached-connection-managerType" minOccurs="0" maxOccurs="1"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="archive-validationType">
        <xs:attribute name="enabled" type="xs:boolean" default="true" use="optional">
            <xs:annotation>
                <xs:documentation>
                    Specify whether archive validation is enabled.  Default: true
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="fail-on-error" type="xs:boolean" default="true" use="optional">
            <xs:annotation>
                <xs:documentation>
                    Should an archive validation error report fail the deployment. Default: true
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="fail-on-warn" type="xs:boolean" default="false" use="optional">
            <xs:annotation>
                <xs:documentation>
                    Should an archive validation warning report fail the deployment. Default: false
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="bean-validationType">
        <xs:attribute name="enabled" type="xs:boolean" use="required">
            <xs:annotation>
                <xs:documentation>
                    Specify whether bean validation is enabled.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="workmanagerType">
        <xs:all>
            <xs:element name="short-running-threads" type="thread-pool" maxOccurs="1" minOccurs="1"></xs:element>
            <xs:element name="long-running-threads" type="thread-pool" maxOccurs="1" minOccurs="0"></xs:element>
        </xs:all>
        <xs:attribute name="name" type="xs:token" use="optional">
            <xs:annotation>
                <xs:documentation>
                   Specifies the name of the work manager. Note, that custom work managers need
                   to have a name defined.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>

  <xs:complexType name="distributedWorkmanagerType">
          <xs:all>
              <xs:element name="short-running-threads" type="thread-pool" maxOccurs="1" minOccurs="1"></xs:element>
              <xs:element name="long-running-threads" type="thread-pool" maxOccurs="1" minOccurs="0"></xs:element>
              <xs:element name="policy" type="policyType" maxOccurs="1" minOccurs="0" />
              <xs:element name="selector" type="selectorType" maxOccurs="1" minOccurs="0" />
              <xs:element name="transport" type="transportType" maxOccurs="1" minOccurs="1" />

          </xs:all>
          <xs:attribute name="name" type="xs:token" use="required">
              <xs:annotation>
                  <xs:documentation>
                     Specifies the name of the work manager. Note, that custom work managers need
                     to have a name defined.
                  </xs:documentation>
              </xs:annotation>
          </xs:attribute>
      </xs:complexType>

    <xs:complexType name="bootstrap-contextsType">
        <xs:sequence>
            <xs:element name="bootstrap-context" type="bootstrap-contextType" maxOccurs="unbounded" minOccurs="1"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="bootstrap-contextType">
        <xs:attribute name="name" type="xs:token" use="required">
            <xs:annotation>
                <xs:documentation>
                   Specifies the name of the bootstrap context.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="workmanager" type="xs:token" use="required">
            <xs:annotation>
                <xs:documentation>
                   Specifies the name of the work manager to use for this context.
                </xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>

    <xs:complexType name="cached-connection-managerType">
        <xs:attribute name="debug" type="xs:boolean" use="optional"></xs:attribute>
        <xs:attribute name="error" type="xs:boolean" use="optional"></xs:attribute>
        <xs:attribute name="ignore-unknown-connections" type="xs:boolean" use="optional"></xs:attribute>
    </xs:complexType>

     <xs:complexType name="thread-pool">
        <xs:annotation>
            <xs:documentation>
            <![CDATA[
                See threads:blocking-bounded-queue-thread-pool.
            ]]>
            </xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="core-threads" type="threads:countType" minOccurs="0"/>
            <xs:element name="queue-length" type="threads:countType"/>
            <xs:element name="max-threads" type="threads:countType"/>
            <xs:element name="keepalive-time" type="threads:time" minOccurs="0"/>
            <xs:element name="thread-factory" type="threads:ref" minOccurs="0"/>
        </xs:all>
        <xs:attribute name="allow-core-timeout" use="optional" type="xs:boolean"/>
    </xs:complexType>

    <xs:complexType name="policyType">
        <xs:sequence>
          <xs:element name="option" type="optionType" maxOccurs="unbounded" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="name" use="required">
                <xs:annotation>
                    <xs:documentation>
                        When to distribute the work instance
                        Supported policies
                        - NEVER
                        Never distribute the Work instance to another node.
                        - ALWAYS
                        Always distribute the Work instance to another node.
                        - WATERMARK
                        Distribute the Work instance to another node based on how many free worker threads the current
                        node has available.

                        Default is WATERMARK with a watermark of 0
                    </xs:documentation>
                </xs:annotation>
                <xs:simpleType>
                    <xs:restriction base="xs:token">
                        <xs:enumeration value="NEVER"/>
                        <xs:enumeration value="ALWAYS"/>
                        <xs:enumeration value="WATERMARK"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>


      </xs:complexType>
      <xs:complexType name="selectorType">
          <xs:sequence>
                  <xs:element name="option" type="optionType" maxOccurs="unbounded" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute name="name" use="required">
                <xs:annotation>
                    <xs:documentation>
                        To which work manager instance should the Work instance be distributed to
                        Supported selectors
                        - FIRST_AVAILABLE
                            Select the first available node in the list
                        - PING_TIME
                            Select the node with the lowest ping time
                        - MAX_FREE_THREADS
                            Select the node with highest number of free worker threads

                        Default is PING_TIME
                    </xs:documentation>
                </xs:annotation>
                <xs:simpleType>
                    <xs:restriction base="xs:token">
                        <xs:enumeration value="FIRST_AVAILABLE"/>
                        <xs:enumeration value="PING_TIME"/>
                        <xs:enumeration value="MAX_FREE_THREADS"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
        <xs:complexType name="transportType">
              <xs:attribute name="jgroups-stack" type="xs:token" use="optional">
                  <xs:annotation>
                      <xs:documentation>
                        Define which JGroups stack will be used to distribute Work instances in the cluster.

                        Default is udp
                      </xs:documentation>
                  </xs:annotation>
              </xs:attribute>
              <xs:attribute name="jgroups-cluster" type="xs:token" use="optional">
                <xs:annotation>
                  <xs:documentation>
                    Define the JGroups channel name that will be used to distribute Work instances in the cluster.

                    Default is jca
                  </xs:documentation>
                </xs:annotation>
              </xs:attribute>
              <xs:attribute name="request-timeout" type="xs:nonNegativeInteger" use="optional">
                <xs:annotation>
                  <xs:documentation>
                    Define the request timeout for Work distribution in the cluster. It is expressed in milliseconds.

                    Default is 10000
                  </xs:documentation>
                </xs:annotation>
              </xs:attribute>
          </xs:complexType>

        <xs:complexType name="optionType">
              <xs:attribute name="name" use="required">
                  <xs:annotation>
                      <xs:documentation>
                          Name of the option to be set
                      </xs:documentation>
                  </xs:annotation>
              </xs:attribute>
          </xs:complexType>
</xs:schema>
