/
DefaultCamelContext.java
979 lines (869 loc) · 40.6 KB
/
DefaultCamelContext.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/*
* 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.impl;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.Expression;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.FailedToStartRouteException;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.Route;
import org.apache.camel.RouteTemplateContext;
import org.apache.camel.StartupStep;
import org.apache.camel.ValueHolder;
import org.apache.camel.api.management.JmxSystemPropertyKeys;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.impl.engine.DefaultExecutorServiceManager;
import org.apache.camel.impl.engine.RouteService;
import org.apache.camel.impl.engine.SimpleCamelContext;
import org.apache.camel.impl.engine.TransformerKey;
import org.apache.camel.impl.engine.ValidatorKey;
import org.apache.camel.impl.scan.AssignableToPackageScanFilter;
import org.apache.camel.impl.scan.InvertingPackageScanFilter;
import org.apache.camel.model.DataFormatDefinition;
import org.apache.camel.model.FaultToleranceConfigurationDefinition;
import org.apache.camel.model.HystrixConfigurationDefinition;
import org.apache.camel.model.Model;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ModelLifecycleStrategy;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.ProcessorDefinitionHelper;
import org.apache.camel.model.Resilience4jConfigurationDefinition;
import org.apache.camel.model.RouteConfigurationDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RouteDefinitionHelper;
import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.model.RouteTemplatesDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition;
import org.apache.camel.model.language.ExpressionDefinition;
import org.apache.camel.model.rest.RestDefinition;
import org.apache.camel.model.rest.RestsDefinition;
import org.apache.camel.model.transformer.TransformerDefinition;
import org.apache.camel.model.validator.ValidatorDefinition;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.DataType;
import org.apache.camel.spi.ExecutorServiceManager;
import org.apache.camel.spi.LocalBeanRepositoryAware;
import org.apache.camel.spi.ModelReifierFactory;
import org.apache.camel.spi.ModelToXMLDumper;
import org.apache.camel.spi.PackageScanClassResolver;
import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.Registry;
import org.apache.camel.spi.StartupStepRecorder;
import org.apache.camel.spi.Transformer;
import org.apache.camel.spi.UuidGenerator;
import org.apache.camel.spi.Validator;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.DefaultRegistry;
import org.apache.camel.support.LocalBeanRegistry;
import org.apache.camel.support.SimpleUuidGenerator;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents the context used to configure routes and the policies to use.
*/
public class DefaultCamelContext extends SimpleCamelContext implements ModelCamelContext {
protected static final ThreadLocal<OptionHolder> OPTIONS = ThreadLocal.withInitial(OptionHolder::new);
private static final Logger LOG = LoggerFactory.getLogger(DefaultCamelContext.class);
private static final UuidGenerator UUID = new SimpleUuidGenerator();
private Model model = new DefaultModel(this);
/**
* Creates the {@link ModelCamelContext} using {@link org.apache.camel.support.DefaultRegistry} as registry.
* <p/>
* Use one of the other constructors to force use an explicit registry.
*/
public DefaultCamelContext() {
this(true);
}
/**
* Creates the {@link CamelContext} using the given {@link BeanRepository} as first-choice repository, and the
* {@link org.apache.camel.support.SimpleRegistry} as fallback, via the {@link DefaultRegistry} implementation.
*
* @param repository the bean repository.
*/
public DefaultCamelContext(BeanRepository repository) {
this(new DefaultRegistry(repository));
}
/**
* Creates the {@link ModelCamelContext} using the given registry
*
* @param registry the registry
*/
public DefaultCamelContext(Registry registry) {
this();
setRegistry(registry);
}
public DefaultCamelContext(boolean init) {
super(init);
if (isDisableJmx()) {
disableJMX();
}
}
@Override
protected void doStop() throws Exception {
super.doStop();
OPTIONS.remove();
}
@Override
protected void doDumpRoutes() {
ModelToXMLDumper dumper = getModelToXMLDumper();
int size = getRouteDefinitions().size();
if (size > 0) {
LOG.info("Dumping {} routes as XML", size);
// for XML to output nicely all routes in one XML then lets put them into <routes>
RoutesDefinition def = new RoutesDefinition();
def.setRoutes(getRouteDefinitions());
try {
String xml = dumper.dumpModelAsXml(this, def, true, true);
// lets separate routes with empty line
xml = StringHelper.replaceFirst(xml, "xmlns=\"http://camel.apache.org/schema/spring\">",
"xmlns=\"http://camel.apache.org/schema/spring\">\n");
xml = StringHelper.replaceAll(xml, "</route>", "</route>\n");
LOG.info("\n\n{}\n", xml);
} catch (Exception e) {
LOG.warn("Error dumping routes to XML due to {}. This exception is ignored.", e.getMessage(), e);
}
}
size = getRestDefinitions().size();
if (size > 0) {
LOG.info("Dumping {} rests as XML", size);
// for XML to output nicely all routes in one XML then lets put them into <routes>
RestsDefinition def = new RestsDefinition();
def.setRests(getRestDefinitions());
try {
String xml = dumper.dumpModelAsXml(this, def, true, true);
// lets separate rests with empty line
xml = StringHelper.replaceFirst(xml, "xmlns=\"http://camel.apache.org/schema/spring\">",
"xmlns=\"http://camel.apache.org/schema/spring\">\n");
xml = StringHelper.replaceAll(xml, "</rest>", "</rest>\n");
LOG.info("\n\n{}\n", xml);
} catch (Exception e) {
LOG.warn("Error dumping rests to XML due to {}. This exception is ignored.", e.getMessage(), e);
}
}
size = getRouteTemplateDefinitions().size();
if (size > 0) {
LOG.info("Dumping {} route templates as XML", size);
// for XML to output nicely all routes in one XML then lets put them into <routes>
RouteTemplatesDefinition def = new RouteTemplatesDefinition();
def.setRouteTemplates(getRouteTemplateDefinitions());
try {
String xml = dumper.dumpModelAsXml(this, def, true, true);
// lets separate rests with empty line
xml = StringHelper.replaceFirst(xml, "xmlns=\"http://camel.apache.org/schema/spring\">",
"xmlns=\"http://camel.apache.org/schema/spring\">\n");
xml = StringHelper.replaceAll(xml, "</routeTemplate>", "</routeTemplate>\n");
LOG.info("\n\n{}\n", xml);
} catch (Exception e) {
LOG.warn("Error dumping route-templates to XML due to {}. This exception is ignored.", e.getMessage(), e);
}
}
}
public static void setNoStart(boolean b) {
getOptions().noStart = b;
}
public static boolean isNoStart() {
return getOptions().noStart;
}
public static void setDisableJmx(boolean b) {
getOptions().disableJmx = b;
}
public static boolean isDisableJmx() {
return getOptions().disableJmx;
}
@Override
public String getTestExcludeRoutes() {
return getExcludeRoutes();
}
public static String getExcludeRoutes() {
return getOptions().excludeRoutes;
}
public static void setExcludeRoutes(String s) {
getOptions().excludeRoutes = s;
}
public static void clearOptions() {
OPTIONS.set(new OptionHolder());
}
private static OptionHolder getOptions() {
return OPTIONS.get();
}
@Override
public void start() {
// for example from unit testing we want to start Camel later (manually)
if (isNoStart()) {
LOG.trace("Ignoring start() as NO_START is true");
return;
}
if (!isStarted() && !isStarting()) {
StopWatch watch = new StopWatch();
super.start();
LOG.debug("start() took {} millis", watch.taken());
} else {
// ignore as Camel is already started
LOG.trace("Ignoring start() as Camel is already started");
}
}
@Override
protected PackageScanClassResolver createPackageScanClassResolver() {
PackageScanClassResolver resolver = super.createPackageScanClassResolver();
String excluded = getExcludeRoutes();
if (ObjectHelper.isNotEmpty(excluded)) {
Set<Class<?>> excludedClasses = new HashSet<>();
for (String str : excluded.split(",")) {
excludedClasses.add(getClassResolver().resolveClass(str));
}
resolver.addFilter(new InvertingPackageScanFilter(new AssignableToPackageScanFilter(excludedClasses)));
}
return resolver;
}
@Override
public void disposeModel() {
LOG.debug("Disposing Model on CamelContext");
model = null;
}
@Override
public void addModelLifecycleStrategy(ModelLifecycleStrategy modelLifecycleStrategy) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addModelLifecycleStrategy(modelLifecycleStrategy);
}
@Override
public List<ModelLifecycleStrategy> getModelLifecycleStrategies() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getModelLifecycleStrategies();
}
@Override
public void addRouteConfiguration(RouteConfigurationDefinition routesConfiguration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteConfiguration(routesConfiguration);
}
@Override
public void addRouteConfigurations(List<RouteConfigurationDefinition> routesConfigurations) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteConfigurations(routesConfigurations);
}
@Override
public List<RouteConfigurationDefinition> getRouteConfigurationDefinitions() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteConfigurationDefinitions();
}
@Override
public List<RouteDefinition> getRouteDefinitions() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteDefinitions();
}
@Override
public RouteDefinition getRouteDefinition(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteDefinition(id);
}
@Override
public void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteDefinitions(routeDefinitions);
}
@Override
public void addRouteDefinition(RouteDefinition routeDefinition) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteDefinition(routeDefinition);
}
@Override
public void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteDefinitions(routeDefinitions);
}
@Override
public void removeRouteDefinition(RouteDefinition routeDefinition) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteDefinition(routeDefinition);
}
@Override
public List<RouteTemplateDefinition> getRouteTemplateDefinitions() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteTemplateDefinitions();
}
@Override
public RouteTemplateDefinition getRouteTemplateDefinition(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteTemplateDefinition(id);
}
@Override
public void addRouteTemplateDefinitions(Collection<RouteTemplateDefinition> routeTemplateDefinitions) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteTemplateDefinitions(routeTemplateDefinitions);
}
@Override
public void addRouteTemplateDefinition(RouteTemplateDefinition routeTemplateDefinition) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteTemplateDefinition(routeTemplateDefinition);
}
@Override
public void removeRouteTemplateDefinitions(Collection<RouteTemplateDefinition> routeTemplateDefinitions) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteTemplateDefinitions(routeTemplateDefinitions);
}
@Override
public void removeRouteTemplateDefinition(RouteTemplateDefinition routeTemplateDefinition) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteTemplateDefinition(routeTemplateDefinition);
}
@Override
public void removeRouteTemplateDefinitions(String pattern) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteTemplateDefinitions(pattern);
}
@Override
public void addRouteTemplateDefinitionConverter(String templateIdPattern, RouteTemplateDefinition.Converter converter) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRouteTemplateDefinitionConverter(templateIdPattern, converter);
}
@Override
public String addRouteFromTemplate(String routeId, String routeTemplateId, Map<String, Object> parameters)
throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.addRouteFromTemplate(routeId, routeTemplateId, parameters);
}
@Override
public String addRouteFromTemplate(String routeId, String routeTemplateId, RouteTemplateContext routeTemplateContext)
throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.addRouteFromTemplate(routeId, routeTemplateId, routeTemplateContext);
}
@Override
public void removeRouteTemplates(String pattern) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.removeRouteTemplateDefinitions(pattern);
}
@Override
public List<RestDefinition> getRestDefinitions() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRestDefinitions();
}
@Override
public void addRestDefinitions(Collection<RestDefinition> restDefinitions, boolean addToRoutes) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addRestDefinitions(restDefinitions, addToRoutes);
}
@Override
public void setDataFormats(Map<String, DataFormatDefinition> dataFormats) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setDataFormats(dataFormats);
}
@Override
public Map<String, DataFormatDefinition> getDataFormats() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getDataFormats();
}
@Override
public DataFormatDefinition resolveDataFormatDefinition(String name) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.resolveDataFormatDefinition(name);
}
@Override
public ProcessorDefinition<?> getProcessorDefinition(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getProcessorDefinition(id);
}
@Override
public <T extends ProcessorDefinition<T>> T getProcessorDefinition(String id, Class<T> type) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getProcessorDefinition(id, type);
}
@Override
public void setValidators(List<ValidatorDefinition> validators) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setValidators(validators);
}
@Override
public HystrixConfigurationDefinition getHystrixConfiguration(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getHystrixConfiguration(id);
}
@Override
public void setHystrixConfiguration(HystrixConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setHystrixConfiguration(configuration);
}
@Override
public void setHystrixConfigurations(List<HystrixConfigurationDefinition> configurations) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setHystrixConfigurations(configurations);
}
@Override
public void addHystrixConfiguration(String id, HystrixConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addHystrixConfiguration(id, configuration);
}
@Override
public Resilience4jConfigurationDefinition getResilience4jConfiguration(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getResilience4jConfiguration(id);
}
@Override
public void setResilience4jConfiguration(Resilience4jConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setResilience4jConfiguration(configuration);
}
@Override
public void setResilience4jConfigurations(List<Resilience4jConfigurationDefinition> configurations) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setResilience4jConfigurations(configurations);
}
@Override
public void addResilience4jConfiguration(String id, Resilience4jConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addResilience4jConfiguration(id, configuration);
}
@Override
public FaultToleranceConfigurationDefinition getFaultToleranceConfiguration(String id) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getFaultToleranceConfiguration(id);
}
@Override
public void setFaultToleranceConfiguration(FaultToleranceConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setFaultToleranceConfiguration(configuration);
}
@Override
public void setFaultToleranceConfigurations(List<FaultToleranceConfigurationDefinition> configurations) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setFaultToleranceConfigurations(configurations);
}
@Override
public void addFaultToleranceConfiguration(String id, FaultToleranceConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addFaultToleranceConfiguration(id, configuration);
}
@Override
public List<ValidatorDefinition> getValidators() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getValidators();
}
@Override
public void setTransformers(List<TransformerDefinition> transformers) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setTransformers(transformers);
}
@Override
public List<TransformerDefinition> getTransformers() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getTransformers();
}
@Override
public ServiceCallConfigurationDefinition getServiceCallConfiguration(String serviceName) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getServiceCallConfiguration(serviceName);
}
@Override
public void setServiceCallConfiguration(ServiceCallConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setServiceCallConfiguration(configuration);
}
@Override
public void setServiceCallConfigurations(List<ServiceCallConfigurationDefinition> configurations) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setServiceCallConfigurations(configurations);
}
@Override
public void addServiceCallConfiguration(String serviceName, ServiceCallConfigurationDefinition configuration) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.addServiceCallConfiguration(serviceName, configuration);
}
@Override
public void setRouteFilterPattern(String include, String exclude) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setRouteFilterPattern(include, exclude);
}
@Override
public void setRouteFilter(Function<RouteDefinition, Boolean> filter) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setRouteFilter(filter);
}
@Override
public Function<RouteDefinition, Boolean> getRouteFilter() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getRouteFilter();
}
@Override
public ModelReifierFactory getModelReifierFactory() {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getModelReifierFactory();
}
@Override
public void setModelReifierFactory(ModelReifierFactory modelReifierFactory) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.setModelReifierFactory(modelReifierFactory);
}
@Override
protected void bindDataFormats() throws Exception {
// eager lookup data formats and bind to registry so the dataformats can
// be looked up and used
if (model != null) {
for (Map.Entry<String, DataFormatDefinition> e : model.getDataFormats().entrySet()) {
String id = e.getKey();
DataFormatDefinition def = e.getValue();
LOG.debug("Creating Dataformat with id: {} and definition: {}", id, def);
DataFormat df = model.getModelReifierFactory().createDataFormat(this, def);
addService(df, true);
getRegistry().bind(id, df);
}
}
}
@Override
protected synchronized void shutdownRouteService(RouteService routeService) throws Exception {
if (model != null) {
RouteDefinition rd = model.getRouteDefinition(routeService.getId());
if (rd != null) {
model.getRouteDefinitions().remove(rd);
}
}
super.shutdownRouteService(routeService);
}
@Override
protected boolean isStreamCachingInUse() throws Exception {
boolean streamCachingInUse = super.isStreamCachingInUse();
if (!streamCachingInUse) {
for (RouteDefinition route : model.getRouteDefinitions()) {
Boolean routeCache = CamelContextHelper.parseBoolean(this, route.getStreamCache());
if (routeCache != null && routeCache) {
streamCachingInUse = true;
break;
}
}
}
return streamCachingInUse;
}
@Override
public void startRouteDefinitions() throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
List<RouteDefinition> routeDefinitions = model.getRouteDefinitions();
if (routeDefinitions != null) {
startRouteDefinitions(routeDefinitions);
}
}
public void startRouteDefinitions(List<RouteDefinition> routeDefinitions) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
// indicate we are staring the route using this thread so
// we are able to query this if needed
boolean alreadyStartingRoutes = isStartingRoutes();
if (!alreadyStartingRoutes) {
setStartingRoutes(true);
}
PropertiesComponent pc = getCamelContextReference().getPropertiesComponent();
// route templates supports binding beans that are local for the template only
// in this local mode then we need to check for side-effects (see further)
LocalBeanRepositoryAware localBeans = null;
if (getCamelContextReference().getRegistry() instanceof LocalBeanRepositoryAware) {
localBeans = (LocalBeanRepositoryAware) getCamelContextReference().getRegistry();
}
try {
RouteDefinitionHelper.forceAssignIds(getCamelContextReference(), routeDefinitions);
for (RouteDefinition routeDefinition : routeDefinitions) {
// assign ids to the routes and validate that the id's is all unique
String duplicate = RouteDefinitionHelper.validateUniqueIds(routeDefinition, routeDefinitions);
if (duplicate != null) {
throw new FailedToStartRouteException(
routeDefinition.getId(),
"duplicate id detected: " + duplicate + ". Please correct ids to be unique among all your routes.");
}
// if the route definition was created via a route template then we need to prepare its parameters when the route is being created and started
if (routeDefinition.isTemplate() != null && routeDefinition.isTemplate()
&& routeDefinition.getTemplateParameters() != null) {
// apply configurer if any present
if (routeDefinition.getRouteTemplateContext().getConfigurer() != null) {
routeDefinition.getRouteTemplateContext().getConfigurer()
.accept(routeDefinition.getRouteTemplateContext());
}
// copy parameters/bean repository to not cause side-effect
Map<String, Object> params = new HashMap<>(routeDefinition.getTemplateParameters());
LocalBeanRegistry bbr
= (LocalBeanRegistry) routeDefinition.getRouteTemplateContext().getLocalBeanRepository();
LocalBeanRegistry bbrCopy = new LocalBeanRegistry();
// make all bean in the bean repository use unique keys (need to add uuid counter)
// so when the route template is used again to create another route, then there is
// no side-effect from previously used values that Camel may use in its endpoint
// registry and elsewhere
if (bbr != null && !bbr.isEmpty()) {
for (Map.Entry<String, Object> param : params.entrySet()) {
Object value = param.getValue();
if (value instanceof String) {
String oldKey = (String) value;
boolean clash = bbr.keys().stream().anyMatch(k -> k.equals(oldKey));
if (clash) {
String newKey = oldKey + "-" + UUID.generateUuid();
LOG.debug(
"Route: {} re-assigning local-bean id: {} to: {} to ensure ids are globally unique",
routeDefinition.getId(), oldKey, newKey);
bbrCopy.put(newKey, bbr.remove(oldKey));
param.setValue(newKey);
}
}
}
// the remainder of the local beans must also have their ids made global unique
for (String oldKey : bbr.keySet()) {
String newKey = oldKey + "-" + UUID.generateUuid();
LOG.debug(
"Route: {} re-assigning local-bean id: {} to: {} to ensure ids are globally unique",
routeDefinition.getId(), oldKey, newKey);
bbrCopy.put(newKey, bbr.get(oldKey));
if (!params.containsKey(oldKey)) {
// if a bean was bound as local bean with a key and it was not defined as template parameter
// then store it as if it was a template parameter with same key=value which allows us
// to use this local bean in the route without any problem such as:
// to("bean:{{myBean}}")
// and myBean is the local bean id.
params.put(oldKey, newKey);
}
}
}
Properties prop = new Properties();
prop.putAll(params);
pc.setLocalProperties(prop);
// we need to shadow the bean registry on the CamelContext with the local beans from the route template context
if (localBeans != null && bbrCopy != null) {
localBeans.setLocalBeanRepository(bbrCopy);
}
// need to reset auto assigned ids, so there is no clash when creating routes
ProcessorDefinitionHelper.resetAllAutoAssignedNodeIds(routeDefinition);
}
// must ensure route is prepared, before we can start it
if (!routeDefinition.isPrepared()) {
RouteDefinitionHelper.prepareRoute(getCamelContextReference(), routeDefinition);
routeDefinition.markPrepared();
}
StartupStepRecorder recorder
= getCamelContextReference().adapt(ExtendedCamelContext.class).getStartupStepRecorder();
StartupStep step = recorder.beginStep(Route.class, routeDefinition.getRouteId(), "Create Route");
Route route = model.getModelReifierFactory().createRoute(this, routeDefinition);
recorder.endStep(step);
RouteService routeService = new RouteService(route);
startRouteService(routeService, true);
// clear local after the route is created via the reifier
pc.setLocalProperties(null);
if (localBeans != null) {
localBeans.setLocalBeanRepository(null);
}
}
} finally {
if (!alreadyStartingRoutes) {
setStartingRoutes(false);
}
pc.setLocalProperties(null);
if (localBeans != null) {
localBeans.setLocalBeanRepository(null);
}
}
}
@Override
protected ExecutorServiceManager createExecutorServiceManager() {
return new DefaultExecutorServiceManager(this);
}
@Override
public Processor createErrorHandler(Route route, Processor processor) throws Exception {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getModelReifierFactory().createErrorHandler(route, processor);
}
@Override
public Expression createExpression(ExpressionDefinition definition) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getModelReifierFactory().createExpression(this, definition);
}
@Override
public Predicate createPredicate(ExpressionDefinition definition) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
return model.getModelReifierFactory().createPredicate(this, definition);
}
@Override
public RouteDefinition adviceWith(RouteDefinition definition, AdviceWithRouteBuilder builder) throws Exception {
return AdviceWith.adviceWith(definition, this, builder);
}
@Override
public void registerValidator(ValidatorDefinition def) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.getValidators().add(def);
Validator validator = model.getModelReifierFactory().createValidator(this, def);
getValidatorRegistry().put(createValidatorKey(def), validator);
}
private static ValueHolder<String> createValidatorKey(ValidatorDefinition def) {
return new ValidatorKey(new DataType(def.getType()));
}
@Override
public void registerTransformer(TransformerDefinition def) {
if (model == null && isLightweight()) {
throw new IllegalStateException("Access to model not supported in lightweight mode");
}
model.getTransformers().add(def);
Transformer transformer = model.getModelReifierFactory().createTransformer(this, def);
getTransformerRegistry().put(createTransformerKey(def), transformer);
}
@Override
protected synchronized boolean removeRoute(String routeId, LoggingLevel loggingLevel) throws Exception {
boolean removed = super.removeRoute(routeId, loggingLevel);
if (removed) {
// must also remove the route definition
RouteDefinition def = getRouteDefinition(routeId);
if (def != null) {
removeRouteDefinition(def);
}
}
return removed;
}
private static ValueHolder<String> createTransformerKey(TransformerDefinition def) {
return ObjectHelper.isNotEmpty(def.getScheme())
? new TransformerKey(def.getScheme())
: new TransformerKey(new DataType(def.getFromType()), new DataType(def.getToType()));
}
protected static class OptionHolder {
public boolean noStart;
public boolean disableJmx = Boolean.getBoolean(JmxSystemPropertyKeys.DISABLED);
public String excludeRoutes;
}
}