Skip to content

Commit 3a1b340

Browse files
fbrutonrcameronm
authored andcommitted
replace reflection with a factory
1 parent 5ddda8b commit 3a1b340

5 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/main/java/emissary/grpc/channel/ChannelManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,7 @@ public static ChannelManager ofSubClass(
112112

113113
public static ChannelManager ofSubClass(
114114
Class<? extends ChannelManager> clz, String host, int port, Configurator configG, ChannelCredentials credentials) {
115-
try {
116-
return clz
117-
.getDeclaredConstructor(String.class, int.class, Configurator.class, ChannelCredentials.class)
118-
.newInstance(host, port, configG, credentials);
119-
} catch (ReflectiveOperationException e) {
120-
throw new IllegalStateException("Unable to instantiate new ChannelManager: " + e.getMessage(), e);
121-
}
115+
return ChannelManagerRegistry.ofSubClass(clz, host, port, configG, credentials);
122116
}
123117

124118
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package emissary.grpc.channel;
2+
3+
import emissary.config.Configurator;
4+
5+
import io.grpc.ChannelCredentials;
6+
7+
@FunctionalInterface
8+
public interface ChannelManagerFactory {
9+
ChannelManager create(String host, int port, Configurator configG, ChannelCredentials credentials);
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package emissary.grpc.channel;
2+
3+
import emissary.config.Configurator;
4+
5+
import io.grpc.ChannelCredentials;
6+
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
10+
public class ChannelManagerRegistry {
11+
private ChannelManagerRegistry() {
12+
/* This utility class should not be instantiated */
13+
}
14+
15+
private static final Map<Class<? extends ChannelManager>, ChannelManagerFactory> REGISTRY = new ConcurrentHashMap<>();
16+
17+
public static void register(Class<? extends ChannelManager> clz, ChannelManagerFactory factory) {
18+
REGISTRY.put(clz, factory);
19+
}
20+
21+
public static ChannelManager ofSubClass(
22+
Class<? extends ChannelManager> clz, String host, int port, Configurator configG, ChannelCredentials credentials) {
23+
24+
ChannelManagerFactory factory = REGISTRY.get(clz);
25+
if (factory == null) {
26+
throw new IllegalArgumentException("No factory registered for class: " + clz.getName() +
27+
". Make sure the class is loaded so its static initialization block runs.");
28+
}
29+
return factory.create(host, port, configG, credentials);
30+
}
31+
}

src/main/java/emissary/grpc/channel/PooledChannelManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class PooledChannelManager extends ChannelManager implements PooledObject
4747
public static final String RETRIEVAL_ORDER = GRPC_CHANNEL_POOL_PREFIX + "RETRIEVAL_ORDER";
4848
public static final String TEST_BEFORE_BORROW = GRPC_CHANNEL_POOL_PREFIX + "TEST_BEFORE_BORROW";
4949

50+
static {
51+
ChannelManagerRegistry.register(PooledChannelManager.class, PooledChannelManager::new);
52+
}
53+
5054
private final ObjectPool<ManagedChannel> channelPool;
5155

5256
/**

src/main/java/emissary/grpc/channel/SingletonChannelManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
public class SingletonChannelManager extends ChannelManager {
1313
private final ManagedChannel channel;
1414

15+
static {
16+
ChannelManagerRegistry.register(SingletonChannelManager.class, SingletonChannelManager::new);
17+
}
18+
1519
/**
1620
* Constructs a new gRPC connection factory using the provided host, port, and configuration.
1721
*

0 commit comments

Comments
 (0)