forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseConnectionBuilder.java
211 lines (185 loc) · 6.43 KB
/
BaseConnectionBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package org.freedesktop.dbus.connections.impl;
import org.freedesktop.dbus.connections.AbstractConnection;
import org.freedesktop.dbus.connections.BusAddress;
import org.freedesktop.dbus.connections.IDisconnectCallback;
import org.freedesktop.dbus.connections.ReceivingService;
import org.freedesktop.dbus.connections.config.ReceivingServiceConfig;
import org.freedesktop.dbus.connections.config.ReceivingServiceConfigBuilder;
import org.freedesktop.dbus.connections.config.TransportConfig;
import org.freedesktop.dbus.connections.config.TransportConfigBuilder;
import org.freedesktop.dbus.connections.transports.TransportBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.messages.Message;
import org.freedesktop.dbus.messages.Message.Endian;
import java.nio.ByteOrder;
/**
* Base class for connection builders containing commonly used options.
*
* @author hypfvieh
* @since 4.2.0 - 2022-07-13
*
* @param <R> concrete type of connection builder
*/
public abstract class BaseConnectionBuilder<R extends BaseConnectionBuilder<R, C>, C extends AbstractConnection> {
private final Class<R> returnType;
private boolean weakReference = false;
private byte endianess = getSystemEndianness();
private IDisconnectCallback disconnectCallback;
private final ReceivingServiceConfigBuilder<R> rsConfigBuilder;
private final TransportConfigBuilder<?, R> transportConfigBuilder;
protected BaseConnectionBuilder(Class<R> _returnType, BusAddress _address) {
returnType = _returnType;
rsConfigBuilder = new ReceivingServiceConfigBuilder<>(() -> self());
transportConfigBuilder = new TransportConfigBuilder<>(() -> self());
transportConfigBuilder.withBusAddress(_address);
}
/**
* Return ourselves.
* @return concrete version of this
*/
R self() {
return returnType.cast(this);
}
/**
* Creates the configuration to use for {@link ReceivingService}.
*
* @return config
*/
protected ReceivingServiceConfig buildThreadConfig() {
return rsConfigBuilder.build();
}
/**
* Creates the configuration to use for {@link TransportBuilder}.
*
* @return config
*/
protected TransportConfig buildTransportConfig() {
return transportConfigBuilder.build();
}
protected boolean isWeakReference() {
return weakReference;
}
protected byte getEndianess() {
return endianess;
}
protected IDisconnectCallback getDisconnectCallback() {
return disconnectCallback;
}
/**
* Returns the builder to configure the receiving thread pools.
* @return builder
*/
public ReceivingServiceConfigBuilder<R> receivingThreadConfig() {
return rsConfigBuilder;
}
/**
* Returns the builder to configure the used transport.
* @return builder
*/
public TransportConfigBuilder<?, R> transportConfig() {
return transportConfigBuilder;
}
/**
* Set the size of the thread-pool used to handle signals from the bus.
* Caution: Using thread-pool size > 1 may cause signals to be handled out-of-order
* <p>
* Default: 1
*
* @param _threads int >= 1
* @return this
* @deprecated use receivingThreadConfig().withSignalThreadCount(_threads)
*/
@Deprecated(since = "4.2.0", forRemoval = true)
public R withSignalThreadCount(int _threads) {
receivingThreadConfig().withSignalThreadCount(_threads);
return self();
}
/**
* Set the size of the thread-pool used to handle error messages received on the bus.
* <p>
* Default: 1
*
* @param _threads int >= 1
* @return this
* @deprecated use receivingThreadConfig().withErrorHandlerThreadCount(_threads)
*/
@Deprecated(since = "4.2.0", forRemoval = true)
public R withErrorHandlerThreadCount(int _threads) {
receivingThreadConfig().withErrorHandlerThreadCount(_threads);
return self();
}
/**
* Set the size of the thread-pool used to handle methods calls previously sent to the bus.
* The thread pool size has to be > 1 to handle recursive calls.
* <p>
* Default: 4
*
* @param _threads int >= 1
* @return this
* @deprecated use receivingThreadConfig().withMethodCallThreadCount(_threads)
*/
@Deprecated(since = "4.2.0", forRemoval = true)
public R withMethodCallThreadCount(int _threads) {
receivingThreadConfig().withMethodCallThreadCount(_threads);
return self();
}
/**
* Set the size of the thread-pool used to handle method return values received on the bus.
* <p>
* Default: 1
*
* @param _threads int >= 1
* @return this
* @deprecated use receivingThreadConfig().withMethodReturnThreadCount(_threads)
*/
@Deprecated(since = "4.2.0", forRemoval = true)
public R withMethodReturnThreadCount(int _threads) {
receivingThreadConfig().withMethodReturnThreadCount(_threads);
return self();
}
/**
* Set the endianness for the connection
* Default is based on system endianness.
*
* @param _endianess {@link Endian#BIG} or {@value Endian#LITTLE}
* @return this
*/
public R withEndianess(byte _endianess) {
if (_endianess == Endian.BIG || _endianess == Endian.LITTLE) {
endianess = _endianess;
}
return self();
}
/**
* Enable/Disable weak references on connection.
* Default is false.
*
* @param _weakRef true to enable
* @return this
*/
public R withWeakReferences(boolean _weakRef) {
weakReference = _weakRef;
return self();
}
/**
* Set the given disconnect callback to the created connection.
*
* @param _disconnectCallback callback
* @return this
*/
public R withDisconnectCallback(IDisconnectCallback _disconnectCallback) {
disconnectCallback = _disconnectCallback;
return self();
}
public abstract C build() throws DBusException;
/**
* Get the default system endianness.
*
* @return LITTLE or BIG
*/
public static byte getSystemEndianness() {
return ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)
? Message.Endian.BIG
: Message.Endian.LITTLE;
}
}