1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.channel;
18
19 import io.netty5.util.internal.StringUtil;
20
21 import java.lang.reflect.Constructor;
22
23 import static java.util.Objects.requireNonNull;
24
25
26
27
28
29 public final class ReflectiveServerChannelFactory<T extends ServerChannel> implements ServerChannelFactory<T> {
30
31 private final Constructor<? extends T> constructor;
32
33 public ReflectiveServerChannelFactory(Class<? extends T> clazz) {
34 requireNonNull(clazz, "clazz");
35 try {
36 this.constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
37 } catch (NoSuchMethodException e) {
38 throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) +
39 " does not have a public constructor that takes an EventLoop and EventLoopGroup instance", e);
40 }
41 }
42
43 @Override
44 public T newChannel(EventLoop eventLoop, EventLoopGroup childEventLoopGroup) {
45 try {
46 return constructor
47 .newInstance(eventLoop, childEventLoopGroup);
48 } catch (Throwable t) {
49 throw new ChannelException(
50 "Unable to create ServerChannel from class " + constructor.getDeclaringClass(), t);
51 }
52 }
53
54 @Override
55 public String toString() {
56 return StringUtil.simpleClassName(ReflectiveServerChannelFactory.class) +
57 '(' + StringUtil.simpleClassName(constructor.getDeclaringClass()) + ".class)";
58 }
59 }