Skip to content

Commit

Permalink
Refactoring to allow for external configuration via
Browse files Browse the repository at this point in the history
ManagedChannelBuilderConfigurer service instance (highest priority).
Update to 1.3.2
  • Loading branch information
scottslewis committed Aug 28, 2024
1 parent 3dbe346 commit 68c4a0e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.ecf.provider.etcd3/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ javac.source=17
javac.target=17

package-version = 1.0.2
Bundle-Version: 1.3.1.${tstamp}
Bundle-Version: 1.3.2.${tstamp}

-runfw: org.apache.felix.framework;version='[7.0.5,7.0.5]'
-runee: JavaSE-17
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.ecf.provider.etcd3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>org.eclipse.ecf.provider.etcd3</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
public class Etcd3ConfigConnectContext implements IConnectContext {

private final Etcd3DiscoveryContainerConfig config;

public Etcd3ConfigConnectContext(Etcd3DiscoveryContainerConfig config) {
this.config = config;
}

public Etcd3DiscoveryContainerConfig getConfig() {
return config;
}

@Override
public CallbackHandler getCallbackHandler() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
public class Etcd3DiscoveryContainer extends AbstractDiscoveryContainerAdapter {

private static final Logger logger = LoggerFactory.getLogger(Etcd3DiscoveryContainer.class);

class EtcdServiceInfoKey {
private final String sessId;
private final String serviceInfoId;
Expand Down Expand Up @@ -299,10 +299,10 @@ public void subscribe(FlowableEmitter<WatchRequest> emitter) throws Exception {

} catch (Exception e) {
URI uri = getEtcdConfig().getTargetLocation();
ContainerConnectException e1 = new ContainerConnectException(
"Cannot connect to Etcd3 server "+ uri, e);
ContainerConnectException e1 = new ContainerConnectException("Cannot connect to Etcd3 server " + uri,
e);
e1.setStackTrace(e.getStackTrace());
logEtcdError("connect","Etcd3 connection error", e);
logEtcdError("connect", "Etcd3 connection error", e);
throw e1;
}

Expand Down Expand Up @@ -373,7 +373,7 @@ private EtcdServiceInfoKey parseServiceInfoKey(String key) {

private void handlePutWatchEvent(KeyValue keyValue) {
String key = keyValue.getKey().toStringUtf8();
debug("handlePutWatchEvent","key="+key);
debug("handlePutWatchEvent", "key=" + key);
EtcdServiceInfoKey siKey = parseServiceInfoKey(key);
if (siKey != null) {
if (!siKey.matchSessionId(getSessionId())) {
Expand All @@ -398,7 +398,7 @@ private void handlePutWatchEvent(KeyValue keyValue) {

private void handleDeleteWatchEvent(KeyValue keyValue) {
String key = keyValue.getKey().toStringUtf8();
debug("handleDeleteWatchEvent","key="+key);
debug("handleDeleteWatchEvent", "key=" + key);
EtcdServiceInfoKey siKey = parseServiceInfoKey(key);
if (siKey != null) {
if (!siKey.matchSessionId(getSessionId())) {
Expand Down Expand Up @@ -471,11 +471,11 @@ private void fireServiceTypeDiscovered(IServiceTypeID serviceTypeID) {
}

private void debug(String methodName, String message) {
logger.debug("methodName="+methodName+",msg="+message);
logger.debug("methodName=" + methodName + ",msg=" + message);
}

private void logEtcdError(String method, String message, Throwable e) {
logger.error("methodName="+method+",msg="+message,e);
logger.error("methodName=" + method + ",msg=" + message, e);
}

public IServiceInfo getServiceInfo(IServiceID aServiceID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import org.eclipse.ecf.provider.etcd3.identity.Etcd3ServiceID;

import io.grpc.Channel;
import io.grpc.Grpc;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ChannelCredentials;

import org.osgi.util.tracker.ServiceTracker;

public class Etcd3DiscoveryContainerConfig extends DiscoveryContainerConfig {
Expand Down Expand Up @@ -70,7 +73,7 @@ public class Etcd3DiscoveryContainerConfig extends DiscoveryContainerConfig {
private String keyPrefix = ETCD_KEYPREFIX_DEFAULT;
private int keepAliveUpdateTime = ETCD_KEEPALIVE_UPDATE_TIME_DEFAULT.intValue();
private boolean usePlaintext = ETCD_USEPLAINTEXT_DEFAULT;
private boolean retry = ETCD_RETRY_DEFAULT;
private ChannelCredentials channelCredentials = null;

public static class Builder {

Expand Down Expand Up @@ -118,8 +121,11 @@ public Builder setUsePlaintext() {
return this;
}

public Builder setUseRetry() {
this.config.retry = true;
public Builder setChannelCredentials(ChannelCredentials credentials) {
if (credentials != null) {
this.config.usePlaintext = false;
}
this.config.channelCredentials = credentials;
return this;
}

Expand Down Expand Up @@ -168,14 +174,14 @@ public String getSessionId() {
return sessionId;
}

public void setTTL(long ttl) {
this.ttl = ttl;
}

public long getTTL() {
return this.ttl;
}

public void setTTL(long ttl) {
this.ttl = ttl;
}

public String getKeyPrefix() {
return this.keyPrefix;
}
Expand All @@ -184,6 +190,14 @@ public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}

public ChannelCredentials getChannelCredentials() {
return this.channelCredentials;
}

public boolean usePlaintext() {
return this.usePlaintext;
}

public String getSessionKey() {
return String.join("/", getKeyPrefix(), getSessionId());
}
Expand All @@ -196,17 +210,30 @@ public URI getTargetLocation() {
return getTargetID().getLocation();
}

protected ManagedChannelBuilder<?> createAndConfigureManagedChannelBuilder() {
Activator.getDefault();
ServiceTracker<ManagedChannelBuilderConfigurer, ManagedChannelBuilderConfigurer> st = new ServiceTracker<ManagedChannelBuilderConfigurer, ManagedChannelBuilderConfigurer>(
Activator.getContext(), ManagedChannelBuilderConfigurer.class, null);
st.open();
ManagedChannelBuilderConfigurer configurer = st.getService();
st.close();
return (configurer != null) ? configurer.createAndConfigureManagedChannelBuilder(this) : null;
}

@SuppressWarnings("rawtypes")
protected ManagedChannelBuilder createManagedChannelBuilder() {
URI uri = getTargetLocation();
ManagedChannelBuilder mcBuilder = ManagedChannelBuilder.forAddress(uri.getHost(), uri.getPort());
if (this.usePlaintext) {
mcBuilder.usePlaintext();
}
if (this.retry) {
mcBuilder.enableRetry();
ManagedChannelBuilder mcb = createAndConfigureManagedChannelBuilder();
if (mcb == null) {
URI uri = getTargetLocation();
ChannelCredentials channelCreds = getChannelCredentials();
mcb = (channelCreds == null) ? ManagedChannelBuilder.forAddress(uri.getHost(), uri.getPort())
: Grpc.newChannelBuilderForAddress(uri.getHost(), uri.getPort(), channelCreds);
if (usePlaintext()) {
mcb.usePlaintext();
}
mcb.enableRetry();
}
return mcBuilder;
return mcb;
}

public Channel createChannel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Etcd3ServiceInfo extends ServiceInfo {
public static final String BYTES_TYPE = "bytes"; //$NON-NLS-1$
public static final String STRING_TYPE = "string"; //$NON-NLS-1$
public static final String LIST_TYPE = "list"; //$NON-NLS-1$
public static final String SET_TYPE = "set"; //$NON-NLS-1$
public static final String SET_TYPE = "set"; //$NON-NLS-1$
public static final String DOUBLE_TYPE = "double";//$NON-NLS-1$
public static final String FLOAT_TYPE = "float";//$NON-NLS-1$
public static final String CHAR_TYPE = "char";//$NON-NLS-1$
Expand Down Expand Up @@ -126,15 +126,15 @@ else if (LIST_TYPE.equals(type)) {
List newList = new ArrayList();
Object obj = jsonProperty.get(VALUE_KEY);
JSONArray sarr = (JSONArray) jsonProperty.get(VALUE_KEY);
for(int j=0; j < sarr.length(); j++) {
for (int j = 0; j < sarr.length(); j++) {
newList.add(sarr.get(j));
}
sProps.setProperty(name, newList);
} else if (SET_TYPE.equals(type)) {
@SuppressWarnings("rawtypes")
Set s = new HashSet();
JSONArray sarr = (JSONArray) jsonProperty.get(VALUE_KEY);
for(int j=0; j < sarr.length(); j++) {
for (int j = 0; j < sarr.length(); j++) {
s.add(sarr.get(j));
}
sProps.setProperty(name, s);
Expand Down Expand Up @@ -246,20 +246,20 @@ public String serializeToJsonString() throws JSONException {
@SuppressWarnings("rawtypes")
List l = (List) value;
JSONArray array = new JSONArray();
for(int i=0; i < l.size(); i++) {
array.put(i,l.get(i));
for (int i = 0; i < l.size(); i++) {
array.put(i, l.get(i));
}
value = array;
} else if (SET_TYPE.equals(type)) {
@SuppressWarnings("rawtypes")
Set s = (Set) value;
JSONArray array = new JSONArray();
int i = 0;
for(Object o: s) {
array.put(i,o);
for (Object o : s) {
array.put(i, o);
i++;
}
value = array;
value = array;
} else if (CHAR_TYPE.equals(type)) {
value = Character.toString((char) value);
} else if (LONG_TYPE.equals(type)) {
Expand Down

0 comments on commit 68c4a0e

Please sign in to comment.