brave.internal.propagation.StringPropagationAdapter Maven / Gradle / Ivy
/*
* Copyright 2013-2020 The OpenZipkin Authors
*
* 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 brave.internal.propagation;
import brave.propagation.Propagation;
import brave.propagation.TraceContext.Extractor;
import brave.propagation.TraceContext.Injector;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This supports the deprecated {@link Propagation.Factory#create(KeyFactory)}.
*
* Here's how to integrate:
*
*
First, override {@link Propagation.Factory#get()} with your actual implementation. Then,
* dispatch {@link Propagation.Factory#create(KeyFactory)} to your implementation via {@link
* #create(Propagation, KeyFactory)}:
*
*
{@code
* @Override public Propagation get() {
* return new YourPropagation(this);
* }
*
* @Deprecated public Propagation create(KeyFactory keyFactory) {
* return StringPropagationAdapter.create(get(), keyFactory);
* }
* }
*
* This is internal, and will be removed in Brave 6. However, it won't be removed
* before Brave 6. If you wish to use this, use "maven-shade-plugin" with the following
* configuration, or something similar.
*
*
{@code
*
* false
*
*
* io.zipkin.brave:brave
*
*
*
*
* io.zipkin.brave:brave
*
* brave/internal/propagation/StringPropagationAdapter*.class
*
*
*
*
*
* brave.internal.propagation
* YOUR_PACKAGE.brave_internal
*
*
* }
*
* @since 5.12
*/
public final class StringPropagationAdapter implements Propagation {
public static Propagation create(Propagation delegate, KeyFactory keyFactory) {
if (delegate == null) throw new NullPointerException("delegate == null");
if (keyFactory == null) throw new NullPointerException("keyFactory == null");
if (keyFactory == KeyFactory.STRING) return (Propagation) delegate;
return new StringPropagationAdapter<>(delegate, keyFactory);
}
final Propagation delegate;
final KeyFactory keyFactory;
final Map map;
final List keysList;
StringPropagationAdapter(Propagation delegate, KeyFactory keyFactory) {
this.delegate = delegate;
this.keyFactory = keyFactory;
this.map = new LinkedHashMap<>();
this.keysList = toKeyList(delegate.keys(), keyFactory);
}
List toKeyList(List keyNames, KeyFactory keyFactory) {
int length = keyNames.size();
K[] keys = (K[]) new Object[length];
for (int i = 0; i < length; i++) {
String keyName = keyNames.get(i);
K key = keyFactory.create(keyName);
keys[i] = key;
map.put(keyName, key);
}
return Collections.unmodifiableList(Arrays.asList(keys));
}
@Override public List keys() {
return keysList;
}
@Override public Injector injector(Setter setter) {
// No check if Setter is a RemoteSetter because this instance cannot have String keys while
// RemoteSetter must have String keys
return delegate.injector(new SetterAdapter<>(setter, map));
}
@Override public Extractor extractor(Getter getter) {
// No check if Setter is a RemoteGetter because this instance cannot have String keys while
// RemoteGetter must have String keys
return delegate.extractor(new GetterAdapter<>(getter, map));
}
@Override public int hashCode() {
return delegate.hashCode();
}
@Override public String toString() {
return delegate.toString();
}
@Override public boolean equals(Object obj) {
if (obj instanceof StringPropagationAdapter) {
return delegate.equals(((StringPropagationAdapter>) obj).delegate);
} else if (obj instanceof Propagation) {
return delegate.equals(obj);
}
return false;
}
static final class GetterAdapter implements Getter {
final Getter getter;
final Map map;
GetterAdapter(Getter getter, Map map) {
if (getter == null) throw new NullPointerException("getter == null");
this.getter = getter;
this.map = map;
}
@Override public String get(R request, String keyName) {
K key = map.get(keyName);
if (key == null) return null;
return getter.get(request, key);
}
@Override public int hashCode() {
return getter.hashCode();
}
@Override public String toString() {
return getter.toString();
}
@Override public boolean equals(Object obj) {
if (obj instanceof GetterAdapter) {
return getter.equals(((GetterAdapter, ?>) obj).getter);
} else if (obj instanceof Getter) {
return getter.equals(obj);
}
return false;
}
}
static final class SetterAdapter implements Setter {
final Setter setter;
final Map map;
SetterAdapter(Setter setter, Map map) {
if (setter == null) throw new NullPointerException("setter == null");
this.setter = setter;
this.map = map;
}
@Override public void put(R request, String keyName, String value) {
K key = map.get(keyName);
if (key == null) return;
setter.put(request, key, value);
}
@Override public int hashCode() {
return setter.hashCode();
}
@Override public String toString() {
return setter.toString();
}
@Override public boolean equals(Object obj) {
if (obj instanceof SetterAdapter) {
return setter.equals(((SetterAdapter, ?>) obj).setter);
} else if (obj instanceof Setter) {
return setter.equals(obj);
}
return false;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy