com.couchbase.client.core.io.IoContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-io Show documentation
Show all versions of core-io Show documentation
The official Couchbase JVM Core IO Library
/*
* Copyright (c) 2018 Couchbase, Inc.
*
* 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.couchbase.client.core.io;
import com.couchbase.client.core.CoreContext;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;
import java.util.Optional;
import static com.couchbase.client.core.logging.RedactableArgument.redactMeta;
import static com.couchbase.client.core.logging.RedactableArgument.redactSystem;
/**
* The {@link IoContext} is used to extend the core context with IO related metadata
* that is useful during event generation.
*
* @since 2.0.0
*/
public class IoContext extends CoreContext {
private final SocketAddress localSocket;
private final String localHostname;
private final int localPort;
private final SocketAddress remoteSocket;
private final Optional bucket;
/**
* Creates a new IO Context.
*
* @param ctx the core context as a parent.
* @param localSocket the local io socket.
* @param remoteSocket the remote io socket.
* @param bucket the bucket name, if it makes sense.
*/
public IoContext(final CoreContext ctx, final SocketAddress localSocket,
final SocketAddress remoteSocket, final Optional bucket) {
super(ctx.core(), ctx.id(), ctx.environment(), ctx.authenticator());
this.localSocket = localSocket;
this.remoteSocket = remoteSocket;
this.bucket = bucket;
if (localSocket instanceof InetSocketAddress) {
localHostname = ((InetSocketAddress) localSocket).getHostString();
localPort = ((InetSocketAddress) localSocket).getPort();
} else {
localHostname = null;
localPort = 0;
}
}
@Override
public void injectExportableParams(final Map input) {
super.injectExportableParams(input);
input.put("local", redactSystem(localSocket()));
input.put("remote", redactSystem(remoteSocket()));
bucket.ifPresent(s -> input.put("bucket", redactMeta(s)));
}
/**
* Returns the local socket.
*/
public SocketAddress localSocket() {
return localSocket;
}
/**
* Returns the remote socket.
*/
public SocketAddress remoteSocket() {
return remoteSocket;
}
/**
* Returns the local hostname, might be null if not available.
*/
public String localHostname() {
return localHostname;
}
/**
* Returns the local port, might be 0 if not available.
*/
public int localPort() {
return localPort;
}
/**
* Returns the bucket name if present.
*/
public Optional bucket() {
return bucket;
}
}