<?xml version="1.0" encoding="UTF-8"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://oss.oracle.com/licenses/CDDL+GPL-1.1
    or LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<!DOCTYPE book [
<!ENTITY % ents SYSTEM "docbook.ent">
%ents;
]>
<section version="5.0"
         xml:id="deployment-jaxb-on-jpms"
         xml:lang="en" xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:ns5="http://www.w3.org/1999/xhtml"
         xmlns:ns3="http://www.w3.org/2000/svg"
         xmlns:ns="http://docbook.org/ns/docbook"
         xmlns:m="http://www.w3.org/1998/Math/MathML">
    <title>Using JAXB on JPMS</title>

    <para>JavaSE 9 features <link xmlns:xlink="http://openjdk.java.net/projects/jigsaw/spec/">JSR 376 Java Platform Module System</link>.
        Starting from 2.4.0 JAXB supports JPMS and can be loaded and used from module path.
        There are only a few things to be aware of.
    </para>

    <section xml:id="Jaxb_pojo_openness">
        <title>JAXB classes openness</title>
        <para>
        JAXB does reflectively access private members of the class, so client application if loaded from module path
            needs to "open" packages containing jaxb classes to JAXB. There are alternative JAXB implementations,
            which will have different module names, JAXB requires pojo classes to be open only to API module.
        <example><title>JPMS module descriptor opening JAXB pojo classes to JAXB API</title>

            <programlisting language="java"><![CDATA[
                //JPMS module descriptor
                module com.example.jaxbclasses {

                //jaxb-api module name
                requires java.xml.bind;

                //open jaxb pojo package to make accessing private members possible for JAXB.
                opens com.example.jaxbclasses.pojos to java.xml.bind;

             ]]></programlisting>
        </example>
        JAXB API will delegate openness to implementation module after resolving it with service discovery mechanism.
        </para>
    </section>

    <section xml:id="Jaxb_api_upgrade">
        <title>Upgrading JavaSE modules on Java 9 and 10.</title>
        <para>
            Prior to Java 11, JavaSE contains JAXB bundled inside JDK. Module <literal>java.xml.bind</literal>
            contains JAXB API and runtime.

            <itemizedlist>
                <listitem>
                    <para>If bundled JavaSE JAXB is used, there is no need
                        to provide standalone JAXB jars to java runtime.</para>
                </listitem>
                <listitem>
                    <para>If standalone JAXB is used, JavaSE bundled java.xml.bind module should be replaced with
                    JAXB API.</para>
                </listitem>

            </itemizedlist>

             Similar to
            <link xmlns:xlink="https://docs.oracle.com/javase/8/docs/technotes/guides/standards/">endorsed mechanism
            </link> prior to Java 9, starting from 9 there is an "upgrade module" mechanism which can replace content of JDK module.
            JavaSE bundled <literal>java.xml.bind</literal> module contains both API and Runtime classes and
            should be replaced with JAXB API module, which has the same <literal>java.xml.bind</literal> module name.

            <example><title>Upgrading JavaSE java.xml.bind module with JAXB API</title>

                <programlisting language="cli"><![CDATA[
                    # Using JAXB standalone jars
                    # Replace JavaSE bundled java.xml.bind with standalone API jar
                    user@host: java com.example.jaxb.Main -cp jaxbclient.jar --upgrade-module-path path/to/jaxb-api.jar
                    --module-path path/to/jaxb-runtime.jar --add-modules com.sun.xml.bind

                    #Same as above with client on module path
                    user@host: java -m com.example.jaxbclasses/com.example.jaxb.Main --upgrade-module-path path/to/jaxb-api.jar
                    --module-path path/to/jaxb-runtime.jar:jaxbclient.jar --add-modules com.sun.xml.bind

             ]]></programlisting>
            </example>
            <para>--upgrade-module-path will replace JavaSE java.xml.bind (runtime and API) with jaxb-api.jar contents.</para>
            <para>Since java.xml.bind module
                <link xmlns:xlink="http://openjdk.java.net/jeps/320">is removed starting from Java 11, there is no need
                    to upgrade this module on 11 and later.</link></para>

            <example><title>Using bundled JavaSE JAXB</title>

                <programlisting language="cli"><![CDATA[

                    # Using JAXB bundled in JDK
                    # No need for standalone jars here
                    user@host: java com.example.jaxb.Main -cp jaxbclient.jar

                    user@host: java -m com.example.jaxbclasses/com.example.jaxb.Main
                    --module-path jaxbclient.jar

             ]]></programlisting>
            </example>

        </para>
    </section>
</section>