org.apache.camel.component.consul.ConsulRegistry Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.component.consul;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Registry;
import org.kiwiproject.consul.Consul;
import org.kiwiproject.consul.ConsulException;
import org.kiwiproject.consul.KeyValueClient;
import org.kiwiproject.consul.SessionClient;
import org.kiwiproject.consul.model.session.ImmutableSession;
import org.kiwiproject.consul.model.session.SessionCreatedResponse;
/**
* Apache Camel Plug-in for Consul Registry (Objects stored under kv/key as well as bookmarked under kv/[type]/key to
* avoid iteration over types)
*/
public class ConsulRegistry implements Registry {
private String hostname = "localhost";
private int port = 8500;
private Consul consul;
private KeyValueClient kvClient;
/* constructor with default port */
public ConsulRegistry(String hostname) {
this(hostname, 8500);
}
/* constructor (since spring.xml does not support builder pattern) */
public ConsulRegistry(String hostname, int port) {
this.hostname = hostname;
this.port = port;
this.consul = Consul.builder().withUrl("http://" + this.hostname + ":" + this.port).build();
}
/* builder pattern */
private ConsulRegistry(Builder builder) {
this.hostname = builder.hostname;
this.port = builder.port;
this.consul = Consul.builder().withUrl("http://" + this.hostname + ":" + this.port).build();
}
@Override
public Object lookupByName(String key) {
// Substitute $ character in key
key = key.replace('$', '/');
kvClient = consul.keyValueClient();
return kvClient.getValueAsString(key).map(result -> {
byte[] postDecodedValue = ConsulRegistryUtils.decodeBase64(result);
return ConsulRegistryUtils.deserialize(postDecodedValue);
}).orElse(null);
}
@Override
public T lookupByNameAndType(String name, Class type) {
Object object = lookupByName(name);
if (object == null) {
return null;
}
try {
return type.cast(object);
} catch (Exception e) {
String msg = "Found bean: " + name + " in Consul Registry: " + this + " of type: " + object.getClass().getName()
+ "expected type was: " + type;
throw new NoSuchBeanException(name, msg, e);
}
}
@Override
public Map findByTypeWithName(Class type) {
Map result = new HashMap<>();
// encode $ signs as they occur in subclass types
String keyPrefix = type.getName().replace('$', '/');
kvClient = consul.keyValueClient();
List keys;
try {
keys = kvClient.getKeys(keyPrefix);
} catch (ConsulException e) {
return result;
}
if (keys != null) {
Object obj;
for (String key : keys) {
// change bookmark back into actual key
key = key.substring(key.lastIndexOf('/') + 1);
obj = lookupByName(key.replace('$', '/'));
if (type.isInstance(obj)) {
result.put(key, type.cast(obj));
}
}
}
return result;
}
@Override
public Set findByType(Class type) {
String keyPrefix = type.getName().replace('$', '/');
Set result = new HashSet<>();
List keys;
try {
keys = kvClient.getKeys(keyPrefix);
} catch (ConsulException e) {
return result;
}
if (keys != null) {
Object obj;
for (String key : keys) {
// change bookmark back into actual key
key = key.substring(key.lastIndexOf('/') + 1);
obj = lookupByName(key.replace('$', '/'));
if (type.isInstance(obj)) {
result.add(type.cast(obj));
}
}
}
return result;
}
@Override
public void bind(String id, Class> type, Object bean) throws RuntimeCamelException {
put(id, bean);
}
@Override
public void bind(String id, Class> type, Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy