Package org.omegazero.net.common
Class NetworkApplicationBuilder
java.lang.Object
org.omegazero.net.common.NetworkApplicationBuilder
- Direct Known Subclasses:
NetClientManagerBuilder,NetServerBuilder
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn enum containing the transport type options of a network application. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected SSLContextprotected NetworkApplicationBuilder.TransportTypeprotected Function<SocketConnection, Consumer<Runnable>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddImplementationAlias(String name, String className) Adds an implementation name alias.abstract NetworkApplicationbuild()Creates theNetworkApplicationfrom the previously set parameters.encrypted(boolean encrypted) Sets whether to use encryption.static NetClientManagerBuildernewClientManager(String implementation) Creates a newNetClientManagerBuilderto create aNetClientManagerinstance.static NetServerBuilderCreates a newNetServerBuilderto create aNetServerinstance.protected voidThis method does parameter validation and preparations.Sets an implementation specific configuration parameter.sslContext(SSLContext sslContext) Sets theSSLContextto use for encryption.transportType(NetworkApplicationBuilder.TransportType transportType) Sets theNetworkApplicationBuilder.TransportTypeto use in this instance.workerCreator(Function<SocketConnection, Consumer<Runnable>> workerCreator) Sets a worker creator which creates a worker instance for each connection created.
-
Field Details
-
transportType
-
encrypted
protected boolean encrypted -
workerCreator
-
sslContext
-
-
Constructor Details
-
NetworkApplicationBuilder
public NetworkApplicationBuilder()
-
-
Method Details
-
build
Creates theNetworkApplicationfrom the previously set parameters.- Returns:
- The
NetworkApplication - Throws:
UnsupportedOperationException- If the specific configuration is not supported by the implementationIllegalArgumentException- If a configuration parameter combination is invalidIllegalStateException- 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
public NetworkApplicationBuilder transportType(NetworkApplicationBuilder.TransportType transportType) Sets theNetworkApplicationBuilder.TransportTypeto use in this instance.The default is
NetworkApplicationBuilder.TransportType.STREAM.- Parameters:
transportType- TheTransportType- Returns:
- This builder
- Throws:
NullPointerException- If transportType isnull
-
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
Sets theSSLContextto 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- TheSSLContext- Returns:
- This builder
-
set
Sets an implementation specific configuration parameter.- Parameters:
option- The name of the parametervalue- 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
Adds an implementation name alias.Usually, the methods
newServer(String)andnewClientManager(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:
makes the following calls equivalent:addImplementationAlias("example_server", "com.example.server.ServerBuilder");
The last call is allowed because when looking up an alias for servers, the given name suffixed withnewServer("com.example.server.ServerBuilder"); newServer("example_server"); newServer("example");"_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 nameclassName- The implementation builder class name
-
newServer
Creates a newNetServerBuilderto create aNetServerinstance.- Parameters:
implementation- The implementation name or class name of the builder- Returns:
- The new builder
- See Also:
-
newClientManager
Creates a newNetClientManagerBuilderto create aNetClientManagerinstance.- Parameters:
implementation- The implementation name or class name of the builder- Returns:
- The new builder
- See Also:
-