-
Notifications
You must be signed in to change notification settings - Fork 8
/
EventProcessorConfig.java
560 lines (499 loc) · 21.5 KB
/
EventProcessorConfig.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright (C) 2018 2024 gregory higgins.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package com.fluxtion.compiler;
import com.fluxtion.compiler.builder.callback.*;
import com.fluxtion.compiler.builder.context.EventProcessorContextFactory;
import com.fluxtion.compiler.builder.context.InstanceSupplierFactory;
import com.fluxtion.compiler.builder.factory.*;
import com.fluxtion.compiler.builder.filter.EventHandlerFilterOverride;
import com.fluxtion.compiler.builder.input.SubscriptionManagerFactory;
import com.fluxtion.compiler.builder.output.SinkPublisherFactory;
import com.fluxtion.compiler.builder.time.ClockFactory;
import com.fluxtion.compiler.generation.serialiser.*;
import com.fluxtion.runtime.audit.Auditor;
import com.fluxtion.runtime.audit.EventLogControlEvent.LogLevel;
import com.fluxtion.runtime.audit.EventLogManager;
import com.fluxtion.runtime.dataflow.function.MergeProperty;
import com.fluxtion.runtime.partition.LambdaReflection;
import com.fluxtion.runtime.time.Clock;
import lombok.ToString;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;
import java.util.function.Function;
/**
* Configuration used by Fluxtion event stream compiler at generation time to
* control the processing logic of the {@link com.fluxtion.runtime.EventProcessor}
*
* @author Greg Higgins
*/
@ToString
public class EventProcessorConfig {
private final Set<Class<?>> interfaces = new HashSet<>();
private final Clock clock = Clock.DEFAULT_CLOCK;
private final Map<String, String> class2replace = new HashMap<>();
private final Map<Object, Integer> filterMap = new HashMap<>();
private final Map<Class<?>, Function<FieldContext, String>> classSerializerMap = new HashMap<>();
private String templateFile;
private List<Object> nodeList;
private HashMap<Object, String> publicNodes;
private HashMap<String, Auditor> auditorMap;
private NodeFactoryRegistration nodeFactoryRegistration;
private RootNodeConfig rootNodeConfig;
private boolean inlineEventHandling = false;
private boolean supportDirtyFiltering = true;
private boolean assignPrivateMembers = false;
private boolean instanceOfDispatch = true;
private DISPATCH_STRATEGY dispatchStrategy = DISPATCH_STRATEGY.INSTANCE_OF;
private List<String> compilerOptions = new ArrayList<>();
public EventProcessorConfig() {
clock();
this.nodeFactoryRegistration = new NodeFactoryRegistration(NodeFactoryConfig.required.getFactoryClasses());
classSerializerMap.put(String.class, BasicTypeSerializer::stringToSource);
classSerializerMap.put(Character.class, BasicTypeSerializer::charToSource);
classSerializerMap.put(char.class, BasicTypeSerializer::charToSource);
classSerializerMap.put(Long.class, BasicTypeSerializer::longToSource);
classSerializerMap.put(long.class, BasicTypeSerializer::longToSource);
classSerializerMap.put(int.class, BasicTypeSerializer::intToSource);
classSerializerMap.put(Integer.class, BasicTypeSerializer::intToSource);
classSerializerMap.put(Short.class, BasicTypeSerializer::shortToSource);
classSerializerMap.put(short.class, BasicTypeSerializer::shortToSource);
classSerializerMap.put(Byte.class, BasicTypeSerializer::byteToSource);
classSerializerMap.put(byte.class, BasicTypeSerializer::byteToSource);
classSerializerMap.put(Double.class, BasicTypeSerializer::doubleToSource);
classSerializerMap.put(double.class, BasicTypeSerializer::doubleToSource);
classSerializerMap.put(Float.class, BasicTypeSerializer::floatToSource);
classSerializerMap.put(float.class, BasicTypeSerializer::floatToSource);
classSerializerMap.put(Boolean.class, BasicTypeSerializer::booleanToSource);
classSerializerMap.put(boolean.class, BasicTypeSerializer::booleanToSource);
classSerializerMap.put(Map.class, CollectionSerializer::mapToSource);
classSerializerMap.put(List.class, CollectionSerializer::listToSource);
classSerializerMap.put(Set.class, CollectionSerializer::setToSource);
classSerializerMap.put(Duration.class, TimeSerializer::durationToSource);
classSerializerMap.put(Instant.class, TimeSerializer::instantToSource);
classSerializerMap.put(LocalDate.class, TimeSerializer::localDateToSource);
classSerializerMap.put(LocalTime.class, TimeSerializer::localTimeToSource);
classSerializerMap.put(LocalDateTime.class, TimeSerializer::localDateTimeToSource);
classSerializerMap.put(Period.class, TimeSerializer::periodToSource);
classSerializerMap.put(ZoneId.class, TimeSerializer::zoneIdToSource);
classSerializerMap.put(ZonedDateTime.class, TimeSerializer::zoneDateTimeToSource);
classSerializerMap.put(Date.class, TimeSerializer::dateToSource);
classSerializerMap.put(File.class, IoSerializer::fileToSource);
classSerializerMap.put(URI.class, IoSerializer::uriToSource);
classSerializerMap.put(URL.class, IoSerializer::urlToSource);
classSerializerMap.put(InetSocketAddress.class, IoSerializer::inetSocketAddressToSource);
classSerializerMap.put(SimpleDateFormat.class, FormatSerializer::simpleDataFormatToSource);
classSerializerMap.put(DateFormat.class, FormatSerializer::simpleDataFormatToSource);
classSerializerMap.put(DecimalFormat.class, FormatSerializer::decimalFormatToSource);
classSerializerMap.put(NumberFormat.class, FormatSerializer::decimalFormatToSource);
classSerializerMap.put(Class.class, MetaSerializer::classToSource);
classSerializerMap.put(MergeProperty.class, MetaSerializer::mergePropertyToSource);
classSerializerMap.put(LambdaReflection.MethodReferenceReflection.class, MetaSerializer::methodReferenceToSource);
}
/**
* Add a node to the SEP. The node will have private final scope, the
* variable name of the node will be generated from {@link NodeNameProducer}
* strategy.<p>
* Fluxtion will check if this node is already in the node set and will
* return the previously added node.
*
* @param <T> The type of the node to add to the SEP
* @param node the node instance to add
* @return The de-duplicated added node
*/
@SuppressWarnings("unchecked")
public <T> T addNode(T node) {
if (getNodeList() == null) {
setNodeList(new ArrayList<>());
}
if (!getNodeList().contains(node)) {
getNodeList().add(node);
return node;
}
return (T) getNodeList().get(getNodeList().indexOf(node));
}
public void addNode(Object node, Object... nodeList) {
addNode(node);
Arrays.asList(nodeList).forEach(this::addNode);
}
/**
* Add a node to the SEP. The node will have public final scope, the
* variable name of the node will be generated from {@link NodeNameProducer}
* strategy if the provided name is null.<p>
* Fluxtion will check if this node is already in the node set and will
* return the previously added node.
*
* @param <T> The type of the node to add to the SEP
* @param node the node instance to add
* @param name the variable name of the node
* @return The de-duplicated added node
*/
@SuppressWarnings("unchecked")
public <T> T addNode(T node, String name) {
addNode(node);
addPublicNode(node, name);
return (T) getNodeList().get(getNodeList().indexOf(node));
}
/**
* Add a node to the SEP. The node will have public final scope, the
* variable name of the node will be generated from {@link NodeNameProducer}
* strategy if the provided name is null.<p>
* Fluxtion will check if this node is already in the node set and will
* return the previously added node.
*
* @param <T> The type of the node to add to the SEP
* @param node the node instance to add
* @param name the variable name of the node
* @return The de-duplicated added node
*/
public <T> T addPublicNode(T node, String name) {
if (getPublicNodes() == null) {
setPublicNodes(new HashMap<>());
}
getPublicNodes().put(node, name);
return node;
}
// public void addNode(MethodReferenceReflection methodReference){
//
// }
/**
* Adds an {@link Auditor} to this SEP. The Auditor will have public final
* scope and can be accessed via the provided variable name.
*
* @param <T> The type of the Auditor
* @param listener Auditor instance
* @param name public name of Auditor
* @return the added Auditor
*/
public <T extends Auditor> T addAuditor(T listener, String name) {
if (getAuditorMap() == null) {
setAuditorMap(new HashMap<>());
}
getAuditorMap().put(name, listener);
return listener;
}
/**
* Maps a class name from one String to another in the generated output.
*
* @param originalFqn Class name to replace
* @param mappedFqn Class name replacement
*/
public void mapClass(String originalFqn, String mappedFqn) {
getClass2replace().put(originalFqn, mappedFqn);
}
/**
* adds a clock to the generated SEP.
*
* @return the clock in generated SEP
*/
public Clock clock() {
addAuditor(clock, "clock");
return clock;
}
/**
* Add an {@link EventLogManager} auditor to the generated SEP. Specify
* the level at which method tracing will take place.
*/
public void addEventAudit(LogLevel tracingLogLevel) {
if (tracingLogLevel != null) {
addAuditor(new EventLogManager().tracingOn(tracingLogLevel), EventLogManager.NODE_NAME);
}
}
/**
* Add an {@link EventLogManager} auditor to the generated SEP without method tracing
*/
public void addEventAudit() {
addAuditor(new EventLogManager().tracingOff(), EventLogManager.NODE_NAME);
}
public void addEventAudit(LogLevel tracingLogLevel, boolean printEventToString) {
addEventAudit(tracingLogLevel, printEventToString, true);
}
public void addEventAudit(LogLevel tracingLogLevel, boolean printEventToString, boolean printThreadName) {
addAuditor(
new EventLogManager()
.tracingOn(tracingLogLevel)
.printEventToString(printEventToString)
.printThreadName(printThreadName),
EventLogManager.NODE_NAME);
}
public void addInterfaceImplementation(Class<?> clazz) {
interfaces.add(clazz);
}
public Set<Class<?>> interfacesToImplement() {
return interfaces;
}
/**
* Users can override this method and add SEP description logic here. The
* buildConfig method will be called by the Fluxtion generator at build
* time.
*/
public void buildConfig() {
}
/**
* the name of the template file to use as an input
*/
public String getTemplateFile() {
return templateFile;
}
public void setTemplateFile(String templateFile) {
this.templateFile = templateFile;
}
/**
* the nodes included in this graph
*/
public List<Object> getNodeList() {
return nodeList;
}
public void setNodeList(List<Object> nodeList) {
this.nodeList = nodeList;
}
/**
* Variable names overrides for public nodes, these will be well known and
* addressable from outside the SEP.
*/
public HashMap<Object, String> getPublicNodes() {
return publicNodes;
}
public <T> T getNode(String name) {
Object[] obj = new Object[1];
publicNodes.entrySet().stream()
.filter(e -> e.getValue().equals(name))
.findFirst()
.ifPresent(e -> obj[0] = e.getKey());
return (T) obj[0];
}
public void setPublicNodes(HashMap<Object, String> publicNodes) {
this.publicNodes = publicNodes;
}
public HashMap<String, Auditor> getAuditorMap() {
return auditorMap;
}
public void setAuditorMap(HashMap<String, Auditor> auditorMap) {
this.auditorMap = auditorMap;
}
/**
* Node Factory configuration
*/
public NodeFactoryRegistration getNodeFactoryRegistration() {
return nodeFactoryRegistration;
}
public void setNodeFactoryRegistration(NodeFactoryRegistration nodeFactoryRegistration) {
//add defaults
nodeFactoryRegistration.factoryClassSet.addAll(NodeFactoryConfig.required.getFactoryClasses());
this.nodeFactoryRegistration = nodeFactoryRegistration;
}
/**
* Makes available in the graph an injectable instance that other nodes can inject see {@link com.fluxtion.runtime.annotations.builder.Inject}.
* The factoryName parameter must match the factoryName attribute in the inject annotation
* <pre>
* {@literal }@Inject(factoryName = "someUniqueName")
* public RoomSensor roomSensor2;
*
* </pre>
* If no inject annotations reference the instance it will not be added to the graph
*
* @param factoryName The unique name for this instance
* @param injectionType The type of injection
* @param injectableInstance The instance to inject
* @param <T> The concrete type of the injected instance
* @param <S> The type of the injected instance
* @return
*/
public <T, S extends T> EventProcessorConfig registerInjectable(String factoryName, Class<T> injectionType, S injectableInstance) {
nodeFactoryRegistration.factorySet.add(new SingletonNodeFactory<>(injectableInstance, injectionType, factoryName));
return this;
}
/**
* Makes available in the graph an injectable instance that other nodes can inject see {@link com.fluxtion.runtime.annotations.builder.Inject}.
* The factoryName parameter must match the factoryName attribute in the inject annotation
* <pre>
* {@literal }@Inject(factoryName = "someUniqueName")
* public RoomSensor roomSensor2;
*
* </pre>
* If no inject annotations reference the instance it will not be added to the graph
*
* @param factoryName The unique name for this instance
* @param injectableInstance The instance to inject
* @param <T> The concrete type of the injected instance and the type of the injected instance
* @return
*/
public <T> EventProcessorConfig registerInjectable(String factoryName, T injectableInstance) {
registerInjectable(factoryName, (Class<T>) injectableInstance.getClass(), injectableInstance);
return this;
}
public RootNodeConfig getRootNodeConfig() {
return rootNodeConfig;
}
public void setRootNodeConfig(RootNodeConfig rootNodeConfig) {
this.rootNodeConfig = rootNodeConfig;
}
/**
* overrides the filter integer id's for a set of instances
*/
public Map<Object, Integer> getFilterMap() {
return filterMap;
}
public void setFilterMap(Map<Object, Integer> filterMap) {
this.filterMap.clear();
this.filterMap.putAll(filterMap);
}
/**
* Overrides the filterId for any methods annotated with {@link com.fluxtion.runtime.annotations.OnEventHandler} in
* an instance or for an {@link com.fluxtion.runtime.node.EventHandlerNode}.
* <p>
* If a single {@link com.fluxtion.runtime.annotations.OnEventHandler} annotated method needs to be overridden then
* use {@link this#overrideOnEventHandlerFilterId(Object, Class, int)}
*
* @param eventHandler the event handler instance to override filterId
* @param newFilterId the new filterId
*/
public void overrideOnEventHandlerFilterId(Object eventHandler, int newFilterId) {
getFilterMap().put(eventHandler, newFilterId);
}
/**
* Overrides the filterId for a method annotated with {@link com.fluxtion.runtime.annotations.OnEventHandler} in
* an instance handling a particular event type.
*
* @param eventHandler the event handler instance to override filterId
* @param eventClass The event handler methods of this type to override filterId
* @param newFilterId the new filterId
*/
public void overrideOnEventHandlerFilterId(Object eventHandler, Class<?> eventClass, int newFilterId) {
getFilterMap().put(new EventHandlerFilterOverride(eventHandler, eventClass, newFilterId), newFilterId);
}
/**
* Register a custom serialiser that maps a field to source at generation time
*
* @param classToSerialize the class type to support custom serialisation
* @param serializationFunction The instance to source function
* @return current {@link EventProcessorConfig}
*/
@SuppressWarnings("unchecked")
public <T> EventProcessorConfig addClassSerializer(
Class<T> classToSerialize, Function<FieldContext<T>, String> serializationFunction) {
classSerializerMap.put(classToSerialize, (Function<FieldContext, String>) (Object) serializationFunction);
return this;
}
public Map<Class<?>, Function<FieldContext, String>> getClassSerializerMap() {
return classSerializerMap;
}
/**
* configures generated code to inline the event handling methods or not.
*/
public boolean isInlineEventHandling() {
return inlineEventHandling;
}
public void setInlineEventHandling(boolean inlineEventHandling) {
this.inlineEventHandling = inlineEventHandling;
}
/**
* configures generated code to support dirty filtering
*/
public boolean isSupportDirtyFiltering() {
return supportDirtyFiltering;
}
public void setSupportDirtyFiltering(boolean supportDirtyFiltering) {
this.supportDirtyFiltering = supportDirtyFiltering;
}
/**
* attempt to assign private member variables, some platforms will support
* access to non-public scoped members. e.g. reflection utilities in Java.
*/
public boolean isAssignPrivateMembers() {
return assignPrivateMembers;
}
public void setAssignPrivateMembers(boolean assignPrivateMembers) {
this.assignPrivateMembers = assignPrivateMembers;
}
/**
* Map an original fully qualified class name into a new value. Can be
* useful if generated code wants to remove all dependencies to Fluxtion
* classes and replaced with user classes.
*/
public Map<String, String> getClass2replace() {
return class2replace;
}
public boolean isInstanceOfDispatch() {
return instanceOfDispatch;
}
public void setInstanceOfDispatch(boolean instanceOfDispatch) {
this.instanceOfDispatch = instanceOfDispatch;
}
public DISPATCH_STRATEGY getDispatchStrategy() {
return dispatchStrategy;
}
public void setDispatchStrategy(DISPATCH_STRATEGY dispatchStrategy) {
Objects.requireNonNull(dispatchStrategy, "Dispatch strategy must be non null");
if (dispatchStrategy == DISPATCH_STRATEGY.PATTERN_MATCH) {
enablePreviewFeatures();
javaTargetRelease("21");
}
this.dispatchStrategy = dispatchStrategy;
}
public List<String> getCompilerOptions() {
return compilerOptions;
}
public void setCompilerOptions(List<String> compilerOptions) {
Objects.requireNonNull(compilerOptions);
this.compilerOptions = compilerOptions;
}
public EventProcessorConfig enablePreviewFeatures() {
compilerOptions.add("--enable-preview");
return this;
}
public EventProcessorConfig javaTargetRelease(String release) {
Objects.requireNonNull(release);
compilerOptions.add("--release");
compilerOptions.add(release);
return this;
}
public enum DISPATCH_STRATEGY {
CLASS_NAME,
INSTANCE_OF,
PATTERN_MATCH
}
enum NodeFactoryConfig {
required(
CallBackDispatcherFactory.class,
CallbackNodeFactory.class,
ClockFactory.class,
InstanceSupplierFactory.class,
DirtyStateMonitorFactory.class,
EventDispatcherFactory.class,
EventProcessorCallbackInternalFactory.class,
EventProcessorContextFactory.class,
NodeNameLookupFactory.class,
SubscriptionManagerFactory.class,
SinkPublisherFactory.class
);
private final HashSet<Class<? extends NodeFactory<?>>> defaultFactories = new HashSet<>();
NodeFactoryConfig(Class<? extends NodeFactory<?>>... factoryClasses) {
Arrays.asList(factoryClasses).forEach(defaultFactories::add);
}
public Set<Class<? extends NodeFactory<?>>> getFactoryClasses() {
return new HashSet<>(defaultFactories);
}
}
}