Class NetworkApplicationBuilder

java.lang.Object
org.omegazero.net.common.NetworkApplicationBuilder
Direct Known Subclasses:
NetClientManagerBuilder, NetServerBuilder

public abstract class NetworkApplicationBuilder extends Object
A builder class for creating NetworkApplications.

Builders for server and client manager instances are created using newServer(String) and newClientManager(String).

Since:
2.1.0
See Also:
  • Field Details

  • Constructor Details

    • NetworkApplicationBuilder

      public NetworkApplicationBuilder()
  • Method Details

    • build

      public abstract NetworkApplication build()
      Creates the NetworkApplication from the previously set parameters.
      Returns:
      The NetworkApplication
      Throws:
      UnsupportedOperationException - If the specific configuration is not supported by the implementation
      IllegalArgumentException - If a configuration parameter combination is invalid
      IllegalStateException - If a required parameter was not set
      Implementation Requirements:
      Implementations of this method must call prepareBuild() first.
    • prepareBuild

      protected void prepareBuild()
      This method does parameter validation and preparations.
    • transportType

      Parameters:
      transportType - The TransportType
      Returns:
      This builder
      Throws:
      NullPointerException - If transportType is null
    • encrypted

      public NetworkApplicationBuilder encrypted(boolean encrypted)
      Sets whether to use encryption. Specific implementation or types may require additional parameters to enable encryption.

      The default is false.

      Parameters:
      encrypted - Whether encryption is enabled
      Returns:
      This builder
    • workerCreator

      public NetworkApplicationBuilder workerCreator(Function<SocketConnection,Consumer<Runnable>> workerCreator)
      Sets a worker creator which creates a worker instance for each connection created.

      The default is null (no workers).

      Parameters:
      workerCreator - The function
      Returns:
      This builder
    • sslContext

      public NetworkApplicationBuilder sslContext(SSLContext sslContext)
      Sets the SSLContext to use for encryption.

      This method implicitly enables encryption if the given parameter is not null, and disables it otherwise.

      The default is null. Setting this parameter may be required by the implementation if encryption is enabled.

      Parameters:
      sslContext - The SSLContext
      Returns:
      This builder
    • set

      public NetworkApplicationBuilder set(String option, Object value)
      Sets an implementation specific configuration parameter.
      Parameters:
      option - The name of the parameter
      value - The value
      Throws:
      IllegalArgumentException - If the given parameter name is not valid
      Since:
      2.2.1
      Implementation Note:
      By default, this method always throws an IllegalArgumentException
    • addImplementationAlias

      public static void addImplementationAlias(String name, String className)
      Adds an implementation name alias.

      Usually, the methods newServer(String) and newClientManager(String) require a class name for the implementation parameter. Adding a name alias allows a shorter name to be passed instead.

      For example, adding an alias as follows:

       
       addImplementationAlias("example_server", "com.example.server.ServerBuilder");
       
       
      makes the following calls equivalent:
       
       newServer("com.example.server.ServerBuilder");
       newServer("example_server");
       newServer("example");
       
       
      The last call is allowed because when looking up an alias for servers, the given name suffixed with "_server" (or "_client" for client managers) is also tried.

      By default, two aliases are registered:

      • "nio_server" - "org.omegazero.net.nio.server.NioServerBuilder"
      • "nio_client" - "org.omegazero.net.nio.client.NioClientManagerBuilder"
      Parameters:
      name - The short name
      className - The implementation builder class name
    • newServer

      public static NetServerBuilder newServer(String implementation)
      Creates a new NetServerBuilder to create a NetServer instance.
      Parameters:
      implementation - The implementation name or class name of the builder
      Returns:
      The new builder
      See Also:
    • newClientManager

      public static NetClientManagerBuilder newClientManager(String implementation)
      Creates a new NetClientManagerBuilder to create a NetClientManager instance.
      Parameters:
      implementation - The implementation name or class name of the builder
      Returns:
      The new builder
      See Also: