[[Application_Client_Migration]]
= Application Client Migration

ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
endif::[]

[[naming-client]]
== Naming Client

This migration example assumes a client application performs a remote
JNDI lookup using an  `InitialContext` backed by the
`org.wildfly.naming.client.WildFlyInitialContextFactory` class.

[[original-configuration-app-client]]
=== Original Configuration

An `InitialContext` backed by the
`org.wildfly.naming.client.WildFlyInitialContextFactory` class can be
created by specifying properties that contain the URL of the naming
provider to connect to along with appropriate user credentials:

[source,java,options="nowrap"]
----
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
properties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
properties.put(Context.SECURITY_PRINCIPAL, "bob");
properties.put(Context.SECURITY_CREDENTIALS, "secret");
InitialContext context = new InitialContext(properties);
Bar bar = (Bar) context.lookup("foo/bar");
...
----

[[migrated-configuration-app-client]]
=== Migrated Configuration

An `InitialContext` backed by the
`org.wildfly.naming.client.WildFlyInitialContextFactory` class can be
created by specifying a property that contains the URL of the naming
provider to connect to. The user credentials can be specified using a
WildFly client configuration file or programmatically.

[[configuration-file-approach]]
==== Configuration File Approach

A `wildfly-config.xml` file that contains the user credentials to use
when establishing a connection to the naming provider can be added to
the client application's `META-INF` directory:

*wildfly-config.xml*

[source,xml,options="nowrap"]
----
<configuration>
    <authentication-client xmlns="urn:elytron:1.0">
        <authentication-rules>
            <rule use-configuration="namingConfig">
                <match-host name="127.0.0.1"/>
            </rule>
        </authentication-rules>
        <authentication-configurations>
            <configuration name="namingConfig">
                <set-user-name name="bob"/>
                <credentials>
                    <clear-password password="secret"/>
                </credentials>
            </configuration>
        </authentication-configurations>
    </authentication-client>
</configuration>
----

An `InitialContext` can then be created as follows:

[source,java,options="nowrap"]
----
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
properties.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
InitialContext context = new InitialContext(properties);
Bar bar = (Bar) context.lookup("foo/bar");
...
----

[[application_client_programmatic-approach]]
==== Programmatic Approach

The user credentials to use when establishing a connection to the naming
provider can be specified directly in the client application's code:

[source,java,options="nowrap"]
----
// create your authentication configuration
AuthenticationConfiguration namingConfig = AuthenticationConfiguration.empty().useName("bob").usePassword("secret");

// create your authentication context
AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL.matchHost("127.0.0.1"), namingConfig);

// create a callable that creates and uses an InitialContext
Callable<Void> callable = () -> {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);
    Bar bar = (Bar) context.lookup("foo/bar");
    ...
    return null;
};

// use your authentication context to run your callable
context.runCallable(callable);
----

[[ejb-client]]
== EJB Client

This migration example assumes a client application is configured to
invoke an EJB deployed on a remote server using a
`jboss-ejb-client.properties` file.

[[original-configuration-app-client-1]]
=== Original Configuration

A `jboss-ejb-client.properties` file that contains the information
needed to connect to the remote server can be specified in a client
application's `META-INF` directory:

*jboss-ejb-client.properties*

....
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 8080
remote.connection.default.username=bob
remote.connection.default.password=secret
....

An EJB can then be looked up and a method can be invoked on it as
follows:

[source,java,options="nowrap"]
----
// create an InitialContext
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext context = new InitialContext(properties);

// look up an EJB and invoke one of its methods
RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
    "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
int sum = statelessRemoteCalculator.add(101, 202);
----

[[migrated-configuration-1]]
=== Migrated Configuration

The information needed to connect to the remote server can be specified
using a WildFly client configuration file or programmatically.

[[configuration-file-approach-1]]
==== Configuration File Approach

A `wildfly-config.xml` file that contains the information needed to
connect to the remote server can be added to the client application's
`META-INF` directory:

*wildfly-config.xml*

[source,xml,options="nowrap"]
----
<configuration>
    <authentication-client xmlns="urn:elytron:1.0">
        <authentication-rules>
            <rule use-configuration="ejbConfig">
                <match-host name="127.0.0.1"/>
            </rule>
        </authentication-rules>
        <authentication-configurations>
            <configuration name="ejbConfig">
                <set-user-name name="bob"/>
                <credentials>
                    <clear-password password="secret"/>
                </credentials>
            </configuration>
        </authentication-configurations>
    </authentication-client>
    <jboss-ejb-client xmlns="urn:jboss:wildfly-client-ejb:3.0">
        <connections>
            <connection uri="remote+http://127.0.0.1:8080" />
        </connections>
    </jboss-ejb-client>
</configuration>
----

An EJB can then be looked up and a method can be invoked on it as
follows:

[source,java,options="nowrap"]
----
// create an InitialContext
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
InitialContext context = new InitialContext(properties);

// look up an EJB and invoke one of its methods (same as before)
RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
    "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
int sum = statelessRemoteCalculator.add(101, 202);
----

[[programmatic-approach-1]]
==== Programmatic Approach

The information needed to connect to the remote server can be specified
directly in the client application's code:

[source,java,options="nowrap"]
----
// create your authentication configuration
AuthenticationConfiguration ejbConfig = AuthenticationConfiguration.empty().useName("bob").usePassword("secret");

// create your authentication context
AuthenticationContext context = AuthenticationContext.empty().with(MatchRule.ALL.matchHost("127.0.0.1"), ejbConfig);

// create a callable that invokes an EJB
Callable<Void> callable = () -> {

    // create an InitialContext
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
    InitialContext context = new InitialContext(properties);

    // look up an EJB and invoke one of its methods (same as before)
    RemoteCalculator statelessRemoteCalculator = (RemoteCalculator) context.lookup(
        "ejb:/ejb-remote-server-side//CalculatorBean!" + RemoteCalculator.class.getName());
    int sum = statelessRemoteCalculator.add(101, 202);
    ...
    return null;
};

// use your authentication context to run your callable
context.runCallable(callable);
----

NOTE: References in this document to Enterprise JavaBeans (EJB) refer to the Jakarta Enterprise Beans unless otherwise noted.
