-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
KameletMain.java
823 lines (735 loc) · 35.2 KB
/
KameletMain.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
/*
* 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.main;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import org.w3c.dom.Document;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.ManagementStatisticsLevel;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.dsl.support.SourceLoader;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.DefaultCompileStrategy;
import org.apache.camel.main.download.AutoConfigureDownloadListener;
import org.apache.camel.main.download.BasePackageScanDownloadListener;
import org.apache.camel.main.download.CamelCustomClassLoader;
import org.apache.camel.main.download.CircuitBreakerDownloader;
import org.apache.camel.main.download.CommandLineDependencyDownloader;
import org.apache.camel.main.download.DependencyDownloaderClassLoader;
import org.apache.camel.main.download.DependencyDownloaderClassResolver;
import org.apache.camel.main.download.DependencyDownloaderComponentResolver;
import org.apache.camel.main.download.DependencyDownloaderDataFormatResolver;
import org.apache.camel.main.download.DependencyDownloaderKamelet;
import org.apache.camel.main.download.DependencyDownloaderLanguageResolver;
import org.apache.camel.main.download.DependencyDownloaderPropertiesFunctionResolver;
import org.apache.camel.main.download.DependencyDownloaderPropertyBindingListener;
import org.apache.camel.main.download.DependencyDownloaderResourceLoader;
import org.apache.camel.main.download.DependencyDownloaderRoutesLoader;
import org.apache.camel.main.download.DependencyDownloaderStrategy;
import org.apache.camel.main.download.DependencyDownloaderTransformerResolver;
import org.apache.camel.main.download.DependencyDownloaderUriFactoryResolver;
import org.apache.camel.main.download.DownloadListener;
import org.apache.camel.main.download.DownloadModelineParser;
import org.apache.camel.main.download.KameletAutowiredLifecycleStrategy;
import org.apache.camel.main.download.KameletMainInjector;
import org.apache.camel.main.download.KnownDependenciesResolver;
import org.apache.camel.main.download.KnownReposResolver;
import org.apache.camel.main.download.MavenDependencyDownloader;
import org.apache.camel.main.download.PackageNameSourceLoader;
import org.apache.camel.main.download.PromptPropertyPlaceholderSource;
import org.apache.camel.main.download.StubBeanRepository;
import org.apache.camel.main.download.TypeConverterLoaderDownloadListener;
import org.apache.camel.main.injection.AnnotationDependencyInjection;
import org.apache.camel.main.reload.OpenApiGeneratorReloadStrategy;
import org.apache.camel.main.util.ClipboardReloadStrategy;
import org.apache.camel.main.util.ExtraClassesClassLoader;
import org.apache.camel.main.util.ExtraFilesClassLoader;
import org.apache.camel.main.xml.blueprint.BlueprintXmlBeansHandler;
import org.apache.camel.main.xml.spring.SpringXmlBeansHandler;
import org.apache.camel.reifier.ProcessorReifier;
import org.apache.camel.spi.ClassResolver;
import org.apache.camel.spi.CliConnector;
import org.apache.camel.spi.CliConnectorFactory;
import org.apache.camel.spi.CompileStrategy;
import org.apache.camel.spi.ComponentResolver;
import org.apache.camel.spi.DataFormatResolver;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.FactoryFinderResolver;
import org.apache.camel.spi.LanguageResolver;
import org.apache.camel.spi.LifecycleStrategy;
import org.apache.camel.spi.PeriodTaskScheduler;
import org.apache.camel.spi.Registry;
import org.apache.camel.spi.ResourceLoader;
import org.apache.camel.spi.RoutesLoader;
import org.apache.camel.spi.TransformerResolver;
import org.apache.camel.spi.UriFactoryResolver;
import org.apache.camel.startup.jfr.FlightRecorderStartupStepRecorder;
import org.apache.camel.support.DefaultContextReloadStrategy;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.RouteOnDemandReloadStrategy;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.startup.BacklogStartupStepRecorder;
import org.apache.camel.tooling.maven.MavenGav;
/**
* A Main class for booting up Camel with Kamelet in standalone mode.
*/
public class KameletMain extends MainCommandLineSupport {
public static final String DEFAULT_KAMELETS_LOCATION = "classpath:/kamelets,github:apache:camel-kamelets/kamelets";
protected final MainRegistry registry = new MainRegistry();
private boolean download = true;
private String repos;
private boolean fresh;
private boolean verbose;
private String mavenSettings;
private String mavenSettingsSecurity;
boolean mavenCentralEnabled = true;
boolean mavenApacheSnapshotEnabled = true;
private String stubPattern;
private boolean silent;
private DownloadListener downloadListener;
private DependencyDownloaderClassLoader classLoader;
private final SpringXmlBeansHandler springXmlBeansHandler = new SpringXmlBeansHandler();
private final BlueprintXmlBeansHandler blueprintXmlBeansHandler = new BlueprintXmlBeansHandler();
public KameletMain() {
configureInitialProperties(DEFAULT_KAMELETS_LOCATION);
}
public KameletMain(String overrides) {
Objects.requireNonNull(overrides);
String locations = overrides + "," + DEFAULT_KAMELETS_LOCATION;
configureInitialProperties(locations);
}
public static void main(String... args) throws Exception {
KameletMain main = new KameletMain();
int code = main.run(args);
if (code != 0) {
System.exit(code);
}
// normal exit
}
/**
* Binds the given <code>name</code> to the <code>bean</code> object, so that it can be looked up inside the
* CamelContext this command line tool runs with.
*
* @param name the used name through which we do bind
* @param bean the object to bind
*/
public void bind(String name, Object bean) {
registry.bind(name, bean);
}
/**
* Using the given <code>name</code> does lookup for the bean being already bound using the
* {@link #bind(String, Object)} method.
*
* @see Registry#lookupByName(String)
*/
public Object lookup(String name) {
return registry.lookupByName(name);
}
/**
* Using the given <code>name</code> and <code>type</code> does lookup for the bean being already bound using the
* {@link #bind(String, Object)} method.
*
* @see Registry#lookupByNameAndType(String, Class)
*/
public <T> T lookup(String name, Class<T> type) {
return registry.lookupByNameAndType(name, type);
}
/**
* Using the given <code>type</code> does lookup for the bean being already bound using the
* {@link #bind(String, Object)} method.
*
* @see Registry#findByTypeWithName(Class)
*/
public <T> Map<String, T> lookupByType(Class<T> type) {
return registry.findByTypeWithName(type);
}
public boolean isDownload() {
return download;
}
/**
* Whether to allow automatic downloaded JAR dependencies, over the internet, that Kamelets requires. This is by
* default enabled.
*/
public void setDownload(boolean download) {
this.download = download;
}
public String getRepos() {
return repos;
}
/**
* Additional maven repositories for download on-demand (Use commas to separate multiple repositories).
*/
public void setRepos(String repos) {
this.repos = repos;
}
public boolean isFresh() {
return fresh;
}
/**
* Make sure we use fresh (i.e. non-cached) resources.
*/
public void setFresh(boolean fresh) {
this.fresh = fresh;
}
/**
* Whether to use stub endpoints instead of creating the actual endpoints. This allows to simulate using real
* components but run without them on the classpath.
*
* @param stubPattern endpoint pattern (Use * for all).
*/
public void setStubPattern(String stubPattern) {
this.stubPattern = stubPattern;
}
public String getStubPattern() {
return stubPattern;
}
public boolean isSilent() {
return silent;
}
/**
* Whether to run in silent mode (used during export or resolving dependencies)
*/
public void setSilent(boolean silent) {
this.silent = silent;
}
/**
* Optionally set the location of Maven settings.xml if it's different than {@code ~/.m2/settings.xml}. If set to
* {@code false}, no default settings file will be used at all.
*/
public void setMavenSettings(String mavenSettings) {
this.mavenSettings = mavenSettings;
}
public String getMavenSettings() {
return mavenSettings;
}
/**
* Optionally set the location of Maven settings-security.xml if it's different than
* {@code ~/.m2/settings-security.xml}.
*/
public void setMavenSettingsSecurity(String mavenSettingsSecurity) {
this.mavenSettingsSecurity = mavenSettingsSecurity;
}
public String getMavenSettingsSecurity() {
return mavenSettingsSecurity;
}
public DownloadListener getDownloadListener() {
return downloadListener;
}
public boolean isMavenCentralEnabled() {
return mavenCentralEnabled;
}
/**
* Whether downloading JARs from Maven Central repository is enabled
*/
public void setMavenCentralEnabled(boolean mavenCentralEnabled) {
this.mavenCentralEnabled = mavenCentralEnabled;
}
public boolean isMavenApacheSnapshotEnabled() {
return mavenApacheSnapshotEnabled;
}
/**
* Whether downloading JARs from ASF Maven Snapshot repository is enabled
*/
public void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled) {
this.mavenApacheSnapshotEnabled = mavenApacheSnapshotEnabled;
}
/**
* Sets a custom download listener
*/
public void setDownloadListener(DownloadListener downloadListener) {
this.downloadListener = downloadListener;
}
// Implementation methods
// -------------------------------------------------------------------------
@Override
public void showOptionsHeader() {
System.out.println("Apache Camel (KameletMain) takes the following options");
System.out.println();
}
@Override
protected void addInitialOptions() {
addOption(new Option("h", "help", "Displays the help screen") {
protected void doProcess(String arg, LinkedList<String> remainingArgs) {
showOptions();
completed();
}
});
addOption(new ParameterOption(
"download", "download", "Whether to allow automatic downloaded JAR dependencies, over the internet.",
"download") {
@Override
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
if (arg.equals("-download")) {
setDownload("true".equalsIgnoreCase(parameter));
}
}
});
addOption(new ParameterOption(
"repos", "repositories", "Additional maven repositories for download on-demand.",
"repos") {
@Override
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
if (arg.equals("-repos")) {
setRepos(parameter);
}
}
});
}
@Override
protected void doInit() throws Exception {
super.doInit();
initCamelContext();
}
@Override
protected void doStart() throws Exception {
super.doStart();
if (getCamelContext() != null) {
try {
// if we were vetoed started then mark as completed
getCamelContext().start();
} finally {
if (getCamelContext().isVetoStarted()) {
completed();
}
}
}
}
@Override
protected void doStop() throws Exception {
super.doStop();
if (getCamelContext() != null) {
getCamelContext().stop();
}
springXmlBeansHandler.stop();
blueprintXmlBeansHandler.stop();
}
@Override
protected ProducerTemplate findOrCreateCamelTemplate() {
if (getCamelContext() != null) {
return getCamelContext().createProducerTemplate();
} else {
return null;
}
}
@Override
protected CamelContext createCamelContext() {
this.verbose = "true".equals(getInitialProperties().get("camel.jbang.verbose"));
// do not build/init camel context yet
DefaultCamelContext answer = new DefaultCamelContext(false);
// setup backlog recorder from very start
answer.getCamelContextExtension().setStartupStepRecorder(new BacklogStartupStepRecorder());
boolean prompt = "true".equals(getInitialProperties().get("camel.jbang.prompt"));
if (prompt) {
answer.getPropertiesComponent().addPropertiesSource(new PromptPropertyPlaceholderSource());
}
ClassLoader dynamicCL = createApplicationContextClassLoader(answer);
answer.setApplicationContextClassLoader(dynamicCL);
PluginHelper.getPackageScanClassResolver(answer).addClassLoader(dynamicCL);
PluginHelper.getPackageScanResourceResolver(answer).addClassLoader(dynamicCL);
KnownReposResolver knownRepos = new KnownReposResolver();
knownRepos.loadKnownDependencies();
MavenDependencyDownloader downloader = new MavenDependencyDownloader();
downloader.setDownload(download);
downloader.setKnownReposResolver(knownRepos);
downloader.setClassLoader(dynamicCL);
downloader.setCamelContext(answer);
downloader.setVerbose(verbose);
downloader.setRepos(repos);
downloader.setFresh(fresh);
downloader.setMavenSettings(mavenSettings);
downloader.setMavenSettingsSecurity(mavenSettingsSecurity);
downloader.setMavenCentralEnabled(mavenCentralEnabled);
downloader.setMavenApacheSnapshotEnabled(mavenApacheSnapshotEnabled);
if (downloadListener != null) {
downloader.addDownloadListener(downloadListener);
}
downloader.addDownloadListener(new AutoConfigureDownloadListener());
downloader.addArtifactDownloadListener(new TypeConverterLoaderDownloadListener());
downloader.addArtifactDownloadListener(new BasePackageScanDownloadListener());
// register as extension
try {
answer.addService(downloader);
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeException(e);
}
// in case we use circuit breakers
CircuitBreakerDownloader.registerDownloadReifiers();
if (silent || "*".equals(stubPattern)) {
// turn off auto-wiring when running in silent mode (or stub = *)
mainConfigurationProperties.setAutowiredEnabled(false);
// and turn off fail fast as we stub components
mainConfigurationProperties.setAutoConfigurationFailFast(false);
}
String info = startupInfo();
if (info != null) {
LOG.info(info);
}
answer.getCamelContextExtension().setRegistry(registry);
if (silent || "*".equals(stubPattern)) {
registry.addBeanRepository(new StubBeanRepository(stubPattern));
}
// load camel component and custom health-checks
answer.setLoadHealthChecks(true);
// annotation based dependency injection for camel/spring/quarkus annotations in DSLs and Java beans
AnnotationDependencyInjection.initAnnotationBasedDependencyInjection(answer);
if (!silent) {
// silent should not include cli-connector
// setup cli-connector if not already done
if (answer.hasService(CliConnector.class) == null) {
CliConnectorFactory ccf = answer.getCamelContextExtension().getContextPlugin(CliConnectorFactory.class);
if (ccf != null && ccf.isEnabled()) {
CliConnector connector = ccf.createConnector();
try {
answer.addService(connector, true);
// force start cli connector early as otherwise it will be deferred until context is started
// but, we want status available during startup phase
ServiceHelper.startService(connector);
} catch (Exception e) {
LOG.warn("Cannot start camel-cli-connector due: {}. This integration cannot be managed by Camel CLI.",
e.getMessage());
}
}
}
}
// embed HTTP server if port is specified
Object port = getInitialProperties().get("camel.jbang.platform-http.port");
if (port != null) {
configure().httpServer().withEnabled(true);
configure().httpServer().withPort(Integer.parseInt(port.toString()));
}
boolean console = "true".equals(getInitialProperties().get("camel.jbang.console"));
if (console) {
configure().httpServer().withEnabled(true);
configure().httpServer().withInfoEnabled(true); // also enable info if console is enabled
configure().httpServer().withDevConsoleEnabled(true);
}
// always enable developer console as it is needed by camel-cli-connector
configure().withDevConsoleEnabled(true);
// and enable a bunch of other stuff that gives more details for developers
configure().withCamelEventsTimestampEnabled(true);
configure().withLoadHealthChecks(true);
configure().withModeline(true);
configure().withLoadStatisticsEnabled(true);
configure().withMessageHistory(true);
configure().withInflightRepositoryBrowseEnabled(true);
configure().withEndpointRuntimeStatisticsEnabled(true);
configure().withJmxManagementStatisticsLevel(ManagementStatisticsLevel.Extended);
configure().withShutdownLogInflightExchangesOnTimeout(false);
configure().withShutdownTimeout(10);
configure().withStartupRecorder("backlog");
boolean tracing = "true".equals(getInitialProperties().get("camel.jbang.backlogTracing"));
if (tracing) {
configure().tracerConfig().withEnabled(true);
}
boolean infoConsole = "true".equals(getInitialProperties().get("camel.jbang.info"));
if (infoConsole) {
configure().httpServer().withEnabled(true);
configure().httpServer().withInfoEnabled(true);
}
boolean health = "true".equals(getInitialProperties().get("camel.jbang.health"));
if (health) {
configure().health().withEnabled(true);
configure().httpServer().withEnabled(true);
configure().httpServer().withHealthCheckEnabled(true);
}
boolean metrics = "true".equals(getInitialProperties().get("camel.jbang.metrics"));
if (metrics) {
configure().metrics()
.witheEnableRouteEventNotifier(true)
.withEnableMessageHistory(true)
.withEnableExchangeEventNotifier(true)
.withEnableRoutePolicy(true).withEnabled(true);
configure().httpServer().withEnabled(true);
configure().httpServer().withMetricsEnabled(true);
}
boolean ignoreLoading = "true".equals(getInitialProperties().get("camel.jbang.ignoreLoadingError"));
if (ignoreLoading) {
configure().withRoutesCollectorIgnoreLoadingError(true);
}
// if transforming DSL then disable processors as we just want to work on the model (not runtime processors)
boolean transform = "true".equals(getInitialProperties().get("camel.jbang.transform"));
if (transform) {
// we just want to transform, so disable all processors
answer.getGlobalOptions().put(ProcessorReifier.DISABLE_ALL_PROCESSORS, "true");
blueprintXmlBeansHandler.setTransform(true);
}
if (silent) {
// silent should not include http server
configure().httpServer().withEnabled(false);
}
// need to setup jfr early
Object jfr = getInitialProperties().get("camel.jbang.jfr");
Object jfrProfile = getInitialProperties().get("camel.jbang.jfr-profile");
if ("jfr".equals(jfr) || jfrProfile != null) {
FlightRecorderStartupStepRecorder recorder = new FlightRecorderStartupStepRecorder();
recorder.setRecording(true);
if (jfrProfile != null) {
recorder.setRecordingProfile(jfrProfile.toString());
}
answer.getCamelContextExtension().setStartupStepRecorder(recorder);
}
// special for source compilation to a specific package based on Maven GAV
String gav = getInitialProperties().getProperty("camel.jbang.gav");
if (gav != null) {
MavenGav g = MavenGav.parseGav(gav);
if (g.getGroupId() != null && g.getArtifactId() != null) {
// plugin a custom source loader with package name based on GAV
String defaultPackageName = g.getGroupId().replace('-', '.') + "." + g.getArtifactId().replace('-', '.');
SourceLoader sl = new PackageNameSourceLoader(defaultPackageName);
answer.getRegistry().bind("PackageNameSourceLoader", sl);
}
}
// source-dir
String sourceDir = getInitialProperties().getProperty("camel.jbang.sourceDir");
try {
// dependencies from CLI
Object dependencies = getInitialProperties().get("camel.jbang.dependencies");
if (dependencies != null) {
answer.addService(new CommandLineDependencyDownloader(answer, dependencies.toString()));
}
KnownDependenciesResolver knownDeps = new KnownDependenciesResolver(answer);
knownDeps.loadKnownDependencies();
DependencyDownloaderPropertyBindingListener listener
= new DependencyDownloaderPropertyBindingListener(answer, knownDeps);
answer.getCamelContextExtension().getRegistry()
.bind(DependencyDownloaderPropertyBindingListener.class.getSimpleName(), listener);
answer.getCamelContextExtension().getRegistry().bind(DependencyDownloaderStrategy.class.getSimpleName(),
new DependencyDownloaderStrategy(answer));
// download class-resolver
ClassResolver classResolver = new DependencyDownloaderClassResolver(answer, knownDeps, silent);
answer.setClassResolver(classResolver);
// re-create factory finder with download class-resolver
FactoryFinderResolver ffr = PluginHelper.getFactoryFinderResolver(answer);
FactoryFinder ff = ffr.resolveBootstrapFactoryFinder(classResolver);
answer.getCamelContextExtension().setBootstrapFactoryFinder(ff);
ff = ffr.resolveDefaultFactoryFinder(classResolver);
answer.getCamelContextExtension().setDefaultFactoryFinder(ff);
answer.getCamelContextExtension().addContextPlugin(ComponentResolver.class,
new DependencyDownloaderComponentResolver(answer, stubPattern, silent));
answer.getCamelContextExtension().addContextPlugin(DataFormatResolver.class,
new DependencyDownloaderDataFormatResolver(answer, stubPattern, silent));
answer.getCamelContextExtension().addContextPlugin(LanguageResolver.class,
new DependencyDownloaderLanguageResolver(answer, stubPattern, silent));
answer.getCamelContextExtension().addContextPlugin(TransformerResolver.class,
new DependencyDownloaderTransformerResolver(answer, stubPattern, silent));
answer.getCamelContextExtension().addContextPlugin(UriFactoryResolver.class,
new DependencyDownloaderUriFactoryResolver(answer));
answer.getCamelContextExtension().addContextPlugin(ResourceLoader.class,
new DependencyDownloaderResourceLoader(answer, sourceDir));
answer.setInjector(new KameletMainInjector(answer.getInjector(), stubPattern, silent));
Object kameletsVersion = getInitialProperties().get("camel.jbang.kameletsVersion");
if (kameletsVersion != null) {
answer.addService(new DependencyDownloaderKamelet(answer, kameletsVersion.toString()));
} else {
answer.addService(new DependencyDownloaderKamelet(answer));
}
answer.getCamelContextExtension().getRegistry().bind(DownloadModelineParser.class.getSimpleName(),
new DownloadModelineParser(answer));
// reloader
if (sourceDir != null) {
if (console || health) {
// allow to upload source via http when HTTP console enabled
configure().httpServer().withEnabled(true);
configure().httpServer().withUploadEnabled(true);
configure().httpServer().withUploadSourceDir(sourceDir);
}
RouteOnDemandReloadStrategy reloader = new RouteOnDemandReloadStrategy(sourceDir, true);
reloader.setPattern("*");
answer.addService(reloader);
// add source-dir as location for loading kamelets (if not already included)
String loc = this.initialProperties.getProperty("camel.component.kamelet.location");
String target = "file:" + sourceDir + ",";
if (!loc.contains(target)) {
loc = target + loc;
addInitialProperty("camel.component.kamelet.location", loc);
}
} else {
answer.addService(new DefaultContextReloadStrategy());
}
// special for reloading enabled on clipboard
String reloadDir = getInitialProperties().getProperty("camel.main.routesIncludePattern");
if (reloadDir != null && reloadDir.startsWith("file:.camel-jbang/generated-clipboard")) {
String name = reloadDir.substring(5);
File file = new File(name);
ClipboardReloadStrategy reloader = new ClipboardReloadStrategy(file);
answer.addService(reloader);
PeriodTaskScheduler scheduler = PluginHelper.getPeriodTaskScheduler(answer);
scheduler.schedulePeriodTask(reloader, 2000);
}
// reload with openapi
String openapi = getInitialProperties().getProperty("camel.jbang.open-api");
String reload = getInitialProperties().getProperty("camel.main.routesReloadDirectory");
if (openapi != null && (reload != null || sourceDir != null)) {
// add open-api reloader that generate output to .camel-jbang/generated-openapi.yaml
File file = Paths.get(openapi).toFile();
OpenApiGeneratorReloadStrategy rs = new OpenApiGeneratorReloadStrategy(file);
answer.addService(rs);
}
} catch (Exception e) {
throw RuntimeCamelException.wrapRuntimeException(e);
}
return answer;
}
@Override
protected void configurePropertiesService(CamelContext camelContext) throws Exception {
super.configurePropertiesService(camelContext);
org.apache.camel.component.properties.PropertiesComponent pc
= (org.apache.camel.component.properties.PropertiesComponent) camelContext.getPropertiesComponent();
pc.setPropertiesFunctionResolver(new DependencyDownloaderPropertiesFunctionResolver(camelContext));
}
@Override
protected void autoconfigure(CamelContext camelContext) throws Exception {
ClassLoader cl = createApplicationContextClassLoader(camelContext);
// create classloader that may include additional JARs
camelContext.setApplicationContextClassLoader(cl);
// auto configure camel afterwards
super.autoconfigure(camelContext);
}
@Override
protected LifecycleStrategy createLifecycleStrategy(CamelContext camelContext) {
return new KameletAutowiredLifecycleStrategy(camelContext, stubPattern, silent);
}
protected ClassLoader createApplicationContextClassLoader(CamelContext camelContext) {
if (classLoader == null) {
// jars need to be added to dependency downloader classloader
List<String> jars = new ArrayList<>();
List<String> classes = new ArrayList<>();
// create class loader (that are download capable) only once
// any additional files to add to classpath
ClassLoader parentCL = KameletMain.class.getClassLoader();
String cpFiles = getInitialProperties().getProperty("camel.jbang.classpathFiles");
if (cpFiles != null) {
String[] arr = cpFiles.split(",");
List<String> files = new ArrayList<>();
for (String s : arr) {
if (s.endsWith(".jar")) {
jars.add(s);
} else if (s.endsWith(".class")) {
classes.add(s);
} else {
files.add(s);
}
}
if (!classes.isEmpty()) {
parentCL = new ExtraClassesClassLoader(parentCL, classes);
LOG.info("Additional classes added to classpath: {}", String.join(", ", classes));
}
if (!files.isEmpty()) {
parentCL = new ExtraFilesClassLoader(parentCL, files);
LOG.info("Additional files added to classpath: {}", String.join(", ", files));
}
}
parentCL = new CamelCustomClassLoader(parentCL, camelContext);
DependencyDownloaderClassLoader cl = new DependencyDownloaderClassLoader(parentCL);
if (!jars.isEmpty()) {
for (String jar : jars) {
File f = new File(jar).getAbsoluteFile();
if (f.isFile() && f.exists()) {
cl.addFile(f);
}
}
LOG.info("Additional jars added to classpath: {}", String.join(", ", jars));
}
classLoader = cl;
}
return classLoader;
}
@Override
protected void configureRoutesLoader(CamelContext camelContext) {
ExtendedCamelContext ecc = camelContext.getCamelContextExtension();
// need to configure compile work dir as its used from routes loader when it discovered code to dynamic compile
String dir = getInitialProperties().getProperty("camel.jbang.compileWorkDir");
if (dir != null) {
CompileStrategy cs = camelContext.getCamelContextExtension().getContextPlugin(CompileStrategy.class);
if (cs == null) {
cs = new DefaultCompileStrategy();
ecc.addContextPlugin(CompileStrategy.class, cs);
}
cs.setWorkDir(dir);
}
DependencyDownloaderRoutesLoader routesLoader;
Object kameletsVersion = getInitialProperties().get("camel.jbang.kameletsVersion");
if (kameletsVersion != null) {
routesLoader = new DependencyDownloaderRoutesLoader(camelContext, kameletsVersion.toString());
} else {
routesLoader = new DependencyDownloaderRoutesLoader(camelContext);
}
routesLoader.setIgnoreLoadingError(this.mainConfigurationProperties.isRoutesCollectorIgnoreLoadingError());
// use resolvers that can auto downloaded
ecc.addContextPlugin(RoutesLoader.class, routesLoader);
}
/**
* Sets initial properties that are specific to camel-kamelet-main
*/
protected void configureInitialProperties(String location) {
addInitialProperty("camel.component.kamelet.location", location);
addInitialProperty("camel.component.rest-api.consumerComponentName", "platform-http");
addInitialProperty("camel.component.rest.consumerComponentName", "platform-http");
addInitialProperty("camel.component.rest.producerComponentName", "vertx-http");
addInitialProperty("came.main.jmxUpdateRouteEnabled", "true");
}
protected String startupInfo() {
StringBuilder sb = new StringBuilder();
sb.append("Using Java ").append(System.getProperty("java.version"));
String pid = getPid();
if (pid != null) {
sb.append(" with PID ").append(pid);
}
sb.append(". Started by ").append(System.getProperty("user.name"));
sb.append(" in ").append(System.getProperty("user.dir"));
return sb.toString();
}
@Override
protected void preProcessCamelRegistry(CamelContext camelContext, MainConfigurationProperties config) {
final Map<String, Document> springXmls = new TreeMap<>();
final Map<String, Document> blueprintXmls = new TreeMap<>();
Map<String, Document> xmlDocs = registry.findByTypeWithName(Document.class);
if (xmlDocs != null) {
xmlDocs.forEach((id, doc) -> {
if (id.startsWith("camel-xml-io-dsl-spring-xml:")) {
springXmls.put(id, doc);
} else if (id.startsWith("camel-xml-io-dsl-blueprint-xml:")) {
blueprintXmls.put(id, doc);
}
});
}
if (!springXmls.isEmpty()) {
// camel-kamelet-main has access to Spring libraries, so we can grab XML documents representing
// actual Spring Beans and read them using Spring's BeanFactory to populate Camel registry
springXmlBeansHandler.processSpringBeans(camelContext, config, springXmls);
}
if (!blueprintXmls.isEmpty()) {
blueprintXmlBeansHandler.processBlueprintBeans(camelContext, config, blueprintXmls);
}
}
@Override
protected void postProcessCamelRegistry(CamelContext camelContext, MainConfigurationProperties config) {
springXmlBeansHandler.createAndRegisterBeans(camelContext);
blueprintXmlBeansHandler.createAndRegisterBeans(camelContext);
}
private static String getPid() {
return String.valueOf(ProcessHandle.current().pid());
}
}