public class WebClientsManager extends java.lang.Object implements WebClientAllocator, WebClientRegistrar, SessionListener
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
forwardedHostByDirectory
The forwarded host value provided by the directory settings, this value overrides
the corresponding value given by the "X-Forwarded-Host" HTTP header.
|
private java.lang.String |
forwardedProtoByDirectory
The forwarded protocol provided by the directory settings, this value overrides
the corresponding value given by the "X-Forwarded-Proto" HTTP header.
|
private int |
from
ports range started from this port number
|
private org.eclipse.jetty.client.HttpClient |
httpClient
Used to test if this web client is still alive and free its resources if it is not.
|
static java.lang.String |
HTTPS
https protocol
|
private static WebClientsManager |
instance
one instance per standard server
|
private static java.util.logging.Logger |
LOG
Logger
|
private static int |
MAX_RELEASE_TRIES
Maximal tries to release resources
|
private boolean |
portsRestricted
True value indicates that ports range is given
|
private java.lang.String |
prefix
Prefix is used to build mapping from ports to strings
|
private static int |
TESTS_PERIOD
interval between connection tests
|
private static int |
TIMEOUT
connect timeout
|
private int |
to
ports range bounded by this port number
|
private java.util.Map<java.lang.Integer,WebClientConfig> |
usedLocalPorts
Maps web client's port to its configuration
|
private java.util.Map<java.lang.String,WebClientConfig> |
webClientConfigs
Maps client's uuid to its configuration
|
private java.util.Map<java.lang.Integer,WebClientConfig> |
webClientSessions
Maps web client's node address to its configuration
|
Modifier | Constructor and Description |
---|---|
private |
WebClientsManager()
Creates a unique instance per a standard P2J server.
|
Modifier and Type | Method and Description |
---|---|
WebClientConfig |
allocateClient(java.lang.String[] requestParameters)
Allocates the dedicated port number from the restricted ports range for the new spawned
web client.
|
void |
freeClient(WebClientConfig config)
Releases the system resources for the web client.
|
void |
freeWebClientResources(java.lang.String webClientId)
Releases the web client resources managed by the server.
|
private int |
getFreeLocalPort()
Runs over the available ports range and gets a first free port number to use.
|
static WebClientsManager |
getInstance()
Returns the web clients manager or null if is is not setup properly.
|
private java.lang.String |
getPortName(int port)
Represents a well-defined mapping from ports numbers to their names.
|
void |
initialize(Session session)
This method is not implemented by WebClientsManager.
|
private void |
isStillAlive(WebClientConfig config,
org.eclipse.jetty.client.api.Response.CompleteListener listener)
Starts asynchronous connect test in order to free the unused port.
|
void |
registerWebClientSession(java.lang.String webClientId,
int peerNode)
Registers the P2J session between the server and the web client to release its system
resources on the server side in the case if the web client is failed unexpectedly.
|
void |
registerWebClientUuid(java.lang.String webClientId,
WebClientConfig config)
Sets the unique identifier for the target web client.
|
private void |
sleep(long timeout)
Causes the current thread to sleep for a given period of milliseconds
|
void |
terminate(Session session)
This method is called when the session is ending.
|
private int |
tryAcquirePort()
Try to acquire a first available port by doing a connection test.
|
private void |
tryReleasePort(WebClientConfig config,
long expectedCount)
Try to release port acquired by the target web client on triggered terminated session events,
but it might not free it if is still in use (client was restarted) or if the client is
'runaway' and the port is open at the OS level, without re-using it.
|
public static final java.lang.String HTTPS
private static final java.util.logging.Logger LOG
private static final int TIMEOUT
private static final int MAX_RELEASE_TRIES
private static final int TESTS_PERIOD
private static WebClientsManager instance
private final boolean portsRestricted
private final int from
private final int to
private final java.lang.String prefix
private final java.util.Map<java.lang.Integer,WebClientConfig> webClientSessions
private final java.util.Map<java.lang.String,WebClientConfig> webClientConfigs
private final java.util.Map<java.lang.Integer,WebClientConfig> usedLocalPorts
private final java.lang.String forwardedHostByDirectory
private final java.lang.String forwardedProtoByDirectory
private org.eclipse.jetty.client.HttpClient httpClient
private WebClientsManager() throws java.lang.Exception
java.lang.Exception
- if it is not setup properly.public static WebClientsManager getInstance()
At this time the code doesn't throw exceptions, it just logs them in the standard error stream.
public WebClientConfig allocateClient(java.lang.String[] requestParameters)
allocateClient
in interface WebClientAllocator
requestParameters
- Holds additional parameters if they are provided. It is expected that the first
requestParameters[0] holds the forwarded host of "X-Forwarded-Host" HTTP header
and requestParameters[1] holds the corresponding forwarded protocol that is
provided by "X-Forwarded-Proto" value.public void registerWebClientSession(java.lang.String webClientId, int peerNode)
registerWebClientSession
in interface WebClientRegistrar
webClientId
- The web client unique identifierpeerNode
- The remote node of the P2J session between the server and the web clientpublic void registerWebClientUuid(java.lang.String webClientId, WebClientConfig config)
webClientId
- The provided unique identifierconfig
- The web client configurationpublic void initialize(Session session)
initialize
in interface SessionListener
session
- The session that is starting.public void freeClient(WebClientConfig config)
freeClient
in interface WebClientAllocator
config
- The web client configurationpublic void freeWebClientResources(java.lang.String webClientId)
freeWebClientResources
in interface WebClientRegistrar
webClientId
- The web client unique identifierpublic void terminate(Session session)
terminate
in interface SessionListener
session
- The session that is ending.private void sleep(long timeout)
timeout
- A given period of millisecondsprivate void tryReleasePort(WebClientConfig config, long expectedCount)
config
- The web client configurationexpectedCount
- The expected sessions countprivate int getFreeLocalPort()
private int tryAcquirePort()
private java.lang.String getPortName(int port)
port
- The port numberprivate void isStillAlive(WebClientConfig config, org.eclipse.jetty.client.api.Response.CompleteListener listener)
config
- The tested web client configurationlistener
- Listener for the request and response completed event.