com.microsoft.azure.storage.OperationContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-storage Show documentation
Show all versions of azure-storage Show documentation
SDK for Microsoft Azure Storage Clients
/**
* Copyright Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.azure.storage;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.slf4j.Logger;
/**
* Represents the current logical operation. A logical operation may have a one-to-many relationship with
* multiple individual physical requests.
*/
public final class OperationContext {
/**
* Name of the {@link org.slf4j.Logger} that will be created by default if logging is enabled and a
* {@link org.slf4j.Logger} has not been specified.
*/
public static final String defaultLoggerName = Logger.ROOT_LOGGER_NAME;
/**
* Indicates whether the client library should produce log entries by default. The default can be overridden to
* enable logging for an individual operation context instance by using {@link #setLoggingEnabled}.
*/
private static boolean enableLoggingByDefault = false;
/**
* Indicates whether the client library should use a proxy by default. The default can be overridden to
* enable proxy for an individual operation context instance by using {@link #setProxy}.
*/
private static Proxy proxyDefault;
/**
* Represents a proxy to be used when making a request.
*/
private Proxy proxy;
/**
* Represents the operation latency, in milliseconds, from the client's perspective. This may include any potential
* retries.
*/
private long clientTimeInMs;
/**
* The UUID representing the client side trace ID.
*/
private String clientRequestID;
/**
* Indicates whether to enable logging for a given operation context.
*/
private Boolean enableLogging;
/**
* The {@link org.slf4j.Logger} object associated with this operation.
*/
private org.slf4j.Logger logger;
/**
* Represents request results, in the form of an ArrayList
object that contains the
* {@link RequestResult} objects, for each physical request that is made.
*/
private final ArrayList requestResults;
/**
* Represents additional headers on the request, for example, for proxy or logging information.
*/
private HashMap userHeaders;
/**
* Represents an event that is triggered before sending a request.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see SendingRequestEvent
*/
private static StorageEventMultiCaster> globalSendingRequestEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a response is received from the storage service while processing a
* request.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see ResponseReceivedEvent
*/
private static StorageEventMultiCaster> globalResponseReceivedEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a response received from the service is fully processed.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see RequestCompletedEvent
*/
private static StorageEventMultiCaster> globalRequestCompletedEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a request is retried.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see RetryingEvent
*/
private static StorageEventMultiCaster> globalRetryingEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered before sending a request.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see SendingRequestEvent
*/
private StorageEventMultiCaster> sendingRequestEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a response is received from the storage service while processing a
* request.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see ResponseReceivedEvent
*/
private StorageEventMultiCaster> responseReceivedEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a response received from the service is fully processed.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see RequestCompletedEvent
*/
private StorageEventMultiCaster> requestCompletedEventHandler = new StorageEventMultiCaster>();
/**
* Represents an event that is triggered when a response is received from the storage service while processing a
* request.
*
* @see StorageEvent
* @see StorageEventMultiCaster
* @see RetryingEvent
*/
private StorageEventMultiCaster> retryingEventHandler = new StorageEventMultiCaster>();
/**
* Creates an instance of the OperationContext
class.
*/
public OperationContext() {
this.clientRequestID = UUID.randomUUID().toString();
this.requestResults = new ArrayList();
}
/**
* Gets the client side trace ID.
*
* @return A String which represents the client request ID.
*/
public String getClientRequestID() {
return this.clientRequestID;
}
/**
* Gets the operation latency, in milliseconds, from the client's perspective. This may include any potential
* retries.
*
* @return A long
which contains the client latency time in milliseconds.
*/
public long getClientTimeInMs() {
return this.clientTimeInMs;
}
/**
* Gets the last request result encountered for the operation.
*
* @return A {@link RequestResult} object which represents the last request result.
*/
public synchronized RequestResult getLastResult() {
if (this.requestResults == null || this.requestResults.size() == 0) {
return null;
}
else {
return this.requestResults.get(this.requestResults.size() - 1);
}
}
/**
* Gets the {@link org.slf4j.Logger} associated with this operation. If the {@link org.slf4j.Logger} object
* associated with this operation is null, this will return the class's default {@link org.slf4j.Logger} object.
*
* @return The {@link org.slf4j.Logger} object associated with this operation,
* or the class's default {@link org.slf4j.Logger} if null.
*/
public org.slf4j.Logger getLogger() {
if (this.logger == null) {
setDefaultLoggerSynchronized();
}
return this.logger;
}
/**
* Gets a proxy which will be used when making a request. Default is null
. To set a proxy to use by
* default, use {@link #setDefaultProxy}
*
* @return A {@link java.net.Proxy} to use when making a request.
*/
public Proxy getProxy() {
return this.proxy;
}
/**
* Gets any additional headers for the request, for example, for proxy or logging information.
*
* @return A java.util.HashMap
which contains the the user headers for the request.
*/
public HashMap getUserHeaders() {
return this.userHeaders;
}
/**
* Sets the default logger.
*
* This is in a separate method so that it's synchronized, just in case multiple threads are
* sharing the OperationContext
.
*/
private synchronized void setDefaultLoggerSynchronized() {
if (this.logger == null) {
this.logger = org.slf4j.LoggerFactory.getLogger(OperationContext.defaultLoggerName);
}
}
/**
* Returns the set of request results that the current operation has created.
*
* @return An ArrayList
object that contains {@link RequestResult} objects that represent
* the request results created by the current operation.
*/
public ArrayList getRequestResults() {
return this.requestResults;
}
/**
* Reserved for internal use. Appends a {@link RequestResult} object to the internal collection in a synchronized
* manner.
*
* @param requestResult
* A {@link RequestResult} to append.
*/
public synchronized void appendRequestResult(RequestResult requestResult) {
this.requestResults.add(requestResult);
}
/**
* Gets a global event multi-caster that is triggered before sending a request. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the globalSendingRequestEventHandler
.
*/
public static StorageEventMultiCaster> getGlobalSendingRequestEventHandler() {
return OperationContext.globalSendingRequestEventHandler;
}
/**
* Gets a global event multi-caster that is triggered when a response is received. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the globalResponseReceivedEventHandler
.
*/
public static StorageEventMultiCaster> getGlobalResponseReceivedEventHandler() {
return OperationContext.globalResponseReceivedEventHandler;
}
/**
* Gets a global event multi-caster that is triggered when a request is completed. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the globalRequestCompletedEventHandler
.
*/
public static StorageEventMultiCaster> getGlobalRequestCompletedEventHandler() {
return OperationContext.globalRequestCompletedEventHandler;
}
/**
* Gets a global event multi-caster that is triggered when a request is retried. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the globalRetryingEventHandler
.
*/
public static StorageEventMultiCaster> getGlobalRetryingEventHandler() {
return OperationContext.globalRetryingEventHandler;
}
/**
* Gets an event multi-caster that is triggered before sending a request. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the sendingRequestEventHandler
.
*/
public StorageEventMultiCaster> getSendingRequestEventHandler() {
return this.sendingRequestEventHandler;
}
/**
* Gets an event multi-caster that is triggered when a response is received. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the responseReceivedEventHandler
.
*/
public StorageEventMultiCaster> getResponseReceivedEventHandler() {
return this.responseReceivedEventHandler;
}
/**
* Gets an event multi-caster that is triggered when a request is completed. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the requestCompletedEventHandler
.
*/
public StorageEventMultiCaster> getRequestCompletedEventHandler() {
return this.requestCompletedEventHandler;
}
/**
* Gets an event multi-caster that is triggered when a request is retried. It allows event listeners to be
* dynamically added and removed.
*
* @return A {@link StorageEventMultiCaster} object for the retryingEventHandler
.
*/
public StorageEventMultiCaster> getRetryingEventHandler() {
return this.retryingEventHandler;
}
/**
* Reserved for internal use. Initializes the OperationContext
in order to begin processing a
* new operation. All operation specific information is erased.
*/
public void initialize() {
this.setClientTimeInMs(0);
this.requestResults.clear();
}
/**
* Indicates whether log entries will be produced for this request.
*
* @return true
if logging is enabled; otherwise false
.
*/
public boolean isLoggingEnabled() {
if (this.enableLogging == null) {
return enableLoggingByDefault;
}
return this.enableLogging;
}
/**
* Sets the client side request ID.
*
* @param clientRequestID
* A String
which contains the client request ID to set.
*/
public void setClientRequestID(final String clientRequestID) {
this.clientRequestID = clientRequestID;
}
/**
* Reserved for internal use. Represents the operation latency, in milliseconds, from the client's perspective. This
* may include any potential retries.
*
* @param clientTimeInMs
* A long
which contains the client operation latency in milliseconds.
*/
public void setClientTimeInMs(final long clientTimeInMs) {
this.clientTimeInMs = clientTimeInMs;
}
/**
* Sets the {@link org.slf4j.Logger} for this operation.
*
* @param logger
* The {@link org.slf4j.Logger}
to use for this operation.
*/
public void setLogger(final org.slf4j.Logger logger) {
this.logger = logger;
}
/**
* Sets a proxy which will be used when making a request. Default is null
. To set a proxy to use by
* default, use {@link #setDefaultProxy}
*
* @param proxy
* A {@link java.net.Proxy} to use when making a request.
*/
public void setProxy(Proxy proxy) {
this.proxy = proxy;
}
/**
* Sets any additional headers for the request, for example, for proxy or logging information.
*
* @param userHeaders
* A java.util.HashMap
which contains any additional headers to set.
*/
public void setUserHeaders(final HashMap userHeaders) {
this.userHeaders = userHeaders;
}
/**
* Specifies whether log entries will be produced for this request.
*
* @param loggingEnabled
* true
to enable logging; otherwise false
to disable logging.
*/
public void setLoggingEnabled(boolean loggingEnabled) {
this.enableLogging = loggingEnabled;
}
/**
* Sets a global event multi-caster that is triggered before sending a request.
*
* @param globalSendingRequestEventHandler
* The {@link StorageEventMultiCaster} object to set for the globalSendingRequestEventHandler
.
*/
public static void setGlobalSendingRequestEventHandler(
final StorageEventMultiCaster> globalSendingRequestEventHandler) {
OperationContext.globalSendingRequestEventHandler = globalSendingRequestEventHandler;
}
/**
* Sets a global event multi-caster that is triggered when a response is received.
*
* @param globalResponseReceivedEventHandler
* The {@link StorageEventMultiCaster} object to set for the globalResponseReceivedEventHandler
.
*/
public static void setGlobalResponseReceivedEventHandler(
final StorageEventMultiCaster> globalResponseReceivedEventHandler) {
OperationContext.globalResponseReceivedEventHandler = globalResponseReceivedEventHandler;
}
/**
* Sets a global event multi-caster that is triggered when a request is completed.
*
* @param globalRequestCompletedEventHandler
* The {@link StorageEventMultiCaster} object to set for the globalRequestCompletedEventHandler
.
*/
public static void setGlobalRequestCompletedEventHandler(
final StorageEventMultiCaster> globalRequestCompletedEventHandler) {
OperationContext.globalRequestCompletedEventHandler = globalRequestCompletedEventHandler;
}
/**
* Sets a global event multi-caster that is triggered when a request is retried.
*
* @param globalRetryingEventHandler
* The {@link StorageEventMultiCaster} object to set for the globalRetryingEventHandler
.
*/
public static void setGlobalRetryingEventHandler(
final StorageEventMultiCaster> globalRetryingEventHandler) {
OperationContext.globalRetryingEventHandler = globalRetryingEventHandler;
}
/**
* Sets an event multi-caster that is triggered before sending a request.
*
* @param sendingRequestEventHandler
* The {@link StorageEventMultiCaster} object to set for the sendingRequestEventHandler
.
*/
public void setSendingRequestEventHandler(
final StorageEventMultiCaster> sendingRequestEventHandler) {
this.sendingRequestEventHandler = sendingRequestEventHandler;
}
/**
* Sets an event multi-caster that is triggered when a response is received.
*
* @param responseReceivedEventHandler
* The {@link StorageEventMultiCaster} object to set for the responseReceivedEventHandler
.
*/
public void setResponseReceivedEventHandler(
final StorageEventMultiCaster> responseReceivedEventHandler) {
this.responseReceivedEventHandler = responseReceivedEventHandler;
}
/**
* Sets an event multi-caster that is triggered when a request is completed.
*
* @param requestCompletedEventHandler
* The {@link StorageEventMultiCaster} object to set for the requestCompletedEventHandler
.
*/
public void setRequestCompletedEventHandler(
final StorageEventMultiCaster> requestCompletedEventHandler) {
this.requestCompletedEventHandler = requestCompletedEventHandler;
}
/**
* Sets an event multi-caster that is triggered when a request is retried.
*
* @param retryingEventHandler
* The {@link StorageEventMultiCaster} object to set for the retryingEventHandler
.
*/
public void setRetryingEventHandler(
final StorageEventMultiCaster> retryingEventHandler) {
this.retryingEventHandler = retryingEventHandler;
}
/**
* Indicates whether the client library should produce log entries by default. The default can be overridden
* to enable logging for an individual operation context instance by using {@link #setLoggingEnabled}.
*
* @return
* true
if logging is enabled by default; otherwise false
.
*/
public static boolean isLoggingEnabledByDefault() {
return enableLoggingByDefault;
}
/**
* Specifies whether the client library should produce log entries by default. The default can be overridden
* to turn on logging for an individual operation context instance by using {@link #setLoggingEnabled}.
*
* @param enableLoggingByDefault
* true
if logging should be enabled by default; otherwise false
if logging should
* be disabled by default.
*/
public static void setLoggingEnabledByDefault(boolean enableLoggingByDefault) {
OperationContext.enableLoggingByDefault = enableLoggingByDefault;
}
/**
* Gets the default proxy used by the client library if enabled. The default can be overridden
* to enable a proxy for an individual operation context instance by using {@link #setProxy}.
*
* @return The default {@link java.net.Proxy} if set; otherwise null
*/
public static Proxy getDefaultProxy() {
return OperationContext.proxyDefault;
}
/**
* Specifies the proxy the client library should use by default. The default can be overridden
* to turn on a proxy for an individual operation context instance by using {@link #setProxy}.
*
* @param defaultProxy
* The {@link java.net.Proxy} to use by default, or null
to not use a proxy.
*/
public static void setDefaultProxy(Proxy defaultProxy) {
OperationContext.proxyDefault = defaultProxy;
}
}