-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
PrepareCatalogMojo.java
1708 lines (1513 loc) · 70.6 KB
/
PrepareCatalogMojo.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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.maven.packaging;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import static org.apache.camel.maven.packaging.PackageHelper.loadText;
/**
* Prepares the camel catalog to include component, data format, and eip descriptors,
* and generates a report.
*/
@Mojo(name = "prepare-catalog", threadSafe = true)
public class PrepareCatalogMojo extends AbstractMojo {
public static final int BUFFER_SIZE = 128 * 1024;
private static final String[] EXCLUDE_DOC_FILES = {
"camel-core-osgi", "camel-core-xml",
"camel-http-common", "camel-jetty", "camel-jetty-common"
};
private static final Pattern LABEL_PATTERN = Pattern.compile("\\\"label\\\":\\s\\\"([\\w,]+)\\\"");
private static final int UNUSED_LABELS_WARN = 15;
/**
* The maven project.
*/
@Parameter(property = "project", required = true, readonly = true)
protected MavenProject project;
/**
* Whether to validate if the components, data formats, and languages are properly documented and have all the needed details.
*/
@Parameter(defaultValue = "true")
protected Boolean validate;
/**
* The output directory for components catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/components")
protected File componentsOutDir;
/**
* The output directory for dataformats catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/dataformats")
protected File dataFormatsOutDir;
/**
* The output directory for languages catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/languages")
protected File languagesOutDir;
/**
* The output directory for others catalog
*
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/others")
protected File othersOutDir;
/**
* The output directory for documents catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/docs")
protected File documentsOutDir;
/**
* The output directory for models catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/models")
protected File modelsOutDir;
/**
* The output directory for archetypes catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/archetypes")
protected File archetypesOutDir;
/**
* The output directory for XML schemas catalog
*/
@Parameter(defaultValue = "${project.build.directory}/classes/org/apache/camel/catalog/schemas")
protected File schemasOutDir;
/**
* The components directory where all the Apache Camel components are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../components")
protected File componentsDir;
/**
* The camel-core directory where camel-core components are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../core/camel-core")
protected File coreDir;
/**
* The directory where the camel-spring XML models are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../components/camel-spring")
protected File springDir;
/**
* The archetypes directory where all the Apache Camel Maven archetypes are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../archetypes")
protected File archetypesDir;
/**
* The directory where the camel-spring XML schema are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../components/camel-spring/target/schema")
protected File springSchemaDir;
/**
* The directory where the camel-blueprint XML schema are
*/
@Parameter(defaultValue = "${project.build.directory}/../../../components/camel-blueprint/target/schema")
protected File blueprintSchemaDir;
/**
* Maven ProjectHelper.
*/
@Component
private MavenProjectHelper projectHelper;
/**
* Execute goal.
*
* @throws org.apache.maven.plugin.MojoExecutionException execution of the main class or one of the
* threads it generated failed.
* @throws org.apache.maven.plugin.MojoFailureException something bad happened...
*/
public void execute() throws MojoExecutionException, MojoFailureException {
executeModel();
Set<String> components = executeComponents();
Set<String> dataformats = executeDataFormats();
Set<String> languages = executeLanguages();
Set<String> others = executeOthers();
executeDocuments(components, dataformats, languages, others);
executeArchetypes();
executeXmlSchemas();
}
protected void executeModel() throws MojoExecutionException, MojoFailureException {
getLog().info("================================================================================");
getLog().info("Copying all Camel model json descriptors");
// lets use sorted set/maps
Set<File> jsonFiles = new TreeSet<>();
Set<File> duplicateJsonFiles = new TreeSet<>();
Set<File> missingLabels = new TreeSet<>();
Set<File> missingJavaDoc = new TreeSet<>();
Map<String, Set<String>> usedLabels = new TreeMap<>();
// find all json files in camel-core
if (coreDir != null && coreDir.isDirectory()) {
File target = new File(coreDir, "target/classes/org/apache/camel/model");
PackageHelper.findJsonFiles(target, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
}
// find all json files in camel-spring
if (springDir != null && springDir.isDirectory()) {
File target = new File(springDir, "target/classes/org/apache/camel/spring");
PackageHelper.findJsonFiles(target, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
File target2 = new File(springDir, "target/classes/org/apache/camel/core/xml");
PackageHelper.findJsonFiles(target2, jsonFiles, new PackageHelper.CamelComponentsModelFilter());
}
getLog().info("Found " + jsonFiles.size() + " model json files");
// make sure to create out dir
modelsOutDir.mkdirs();
// we only want to warn for duplicates if its a clean build
boolean warnDups = modelsOutDir.list() == null || modelsOutDir.list().length == 0;
for (File file : jsonFiles) {
File to = new File(modelsOutDir, file.getName());
if (to.exists()) {
if (warnDups) {
duplicateJsonFiles.add(to);
getLog().warn("Duplicate model name detected: " + to);
} else if (file.lastModified() < to.lastModified()) {
getLog().debug("Skipping generated file: " + to);
continue;
} else {
getLog().warn("Stale file: " + to);
}
}
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
try {
// check if we have a label as we want the eip to include labels
String text = loadText(new FileInputStream(file));
// just do a basic label check
if (text.contains("\"label\": \"\"")) {
missingLabels.add(file);
} else {
String name = asComponentName(file);
Matcher matcher = LABEL_PATTERN.matcher(text);
// grab the label, and remember it in the used labels
if (matcher.find()) {
String label = matcher.group(1);
String[] labels = label.split(",");
for (String s : labels) {
usedLabels.computeIfAbsent(s, k -> new TreeSet<>()).add(name);
}
}
}
// check all the properties if they have description
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("properties", text, true);
for (Map<String, String> row : rows) {
String name = row.get("name");
// skip checking these as they have no documentation
if ("outputs".equals(name) || "transforms".equals(name)) {
continue;
}
String doc = row.get("description");
if (doc == null || doc.isEmpty()) {
missingJavaDoc.add(file);
break;
}
}
} catch (IOException e) {
// ignore
}
}
File all = new File(modelsOutDir, "../models.properties");
try {
FileOutputStream fos = new FileOutputStream(all, false);
String[] names = modelsOutDir.list();
List<String> models = new ArrayList<>();
// sort the names
for (String name : names) {
if (name.endsWith(".json")) {
// strip out .json from the name
String modelName = name.substring(0, name.length() - 5);
models.add(modelName);
}
}
Collections.sort(models);
for (String name : models) {
fos.write(name.getBytes());
fos.write("\n".getBytes());
}
fos.close();
} catch (IOException e) {
throw new MojoFailureException("Error writing to file " + all);
}
printModelsReport(jsonFiles, duplicateJsonFiles, missingLabels, usedLabels, missingJavaDoc);
}
// CHECKSTYLE:OFF
protected Set<String> executeComponents() throws MojoExecutionException, MojoFailureException {
getLog().info("Copying all Camel component json descriptors");
// lets use sorted set/maps
Set<File> jsonFiles = new TreeSet<>();
Set<File> duplicateJsonFiles = new TreeSet<>();
Set<File> componentFiles = new TreeSet<>();
Set<File> missingComponents = new TreeSet<>();
Map<String, Set<String>> usedComponentLabels = new TreeMap<>();
Set<String> usedOptionLabels = new TreeSet<>();
Set<String> unlabeledOptions = new TreeSet<>();
Set<File> missingFirstVersions = new TreeSet<>();
// find all json files in components and camel-core
if (componentsDir != null && componentsDir.isDirectory()) {
File[] components = componentsDir.listFiles();
if (components != null) {
for (File dir : components) {
if (dir.isDirectory() && !"target".equals(dir.getName())) {
File target = new File(dir, "target/classes");
// special for these as they are in sub dir
if ("camel-as2".equals(dir.getName())) {
target = new File(dir, "camel-as2-component/target/classes");
} else if ("camel-salesforce".equals(dir.getName())) {
target = new File(dir, "camel-salesforce-component/target/classes");
} else if ("camel-linkedin".equals(dir.getName())) {
target = new File(dir, "camel-linkedin-component/target/classes");
} else if ("camel-olingo2".equals(dir.getName())) {
target = new File(dir, "camel-olingo2-component/target/classes");
} else if ("camel-olingo4".equals(dir.getName())) {
target = new File(dir, "camel-olingo4-component/target/classes");
} else if ("camel-box".equals(dir.getName())) {
target = new File(dir, "camel-box-component/target/classes");
} else if ("camel-servicenow".equals(dir.getName())) {
target = new File(dir, "camel-servicenow-component/target/classes");
} else if ("camel-fhir".equals(dir.getName())) {
target = new File(dir, "camel-fhir-component/target/classes");
} else {
// this module must be active with a source folder
File src = new File(dir, "src");
boolean active = src.isDirectory() && src.exists();
if (!active) {
continue;
}
}
int before = componentFiles.size();
int before2 = jsonFiles.size();
findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
int after = componentFiles.size();
int after2 = jsonFiles.size();
if (before != after && before2 == after2) {
missingComponents.add(dir);
}
}
}
}
}
if (coreDir != null && coreDir.isDirectory()) {
File target = new File(coreDir, "target/classes");
int before = componentFiles.size();
int before2 = jsonFiles.size();
findComponentFilesRecursive(target, jsonFiles, componentFiles, new CamelComponentsFileFilter());
int after = componentFiles.size();
int after2 = jsonFiles.size();
if (before != after && before2 == after2) {
missingComponents.add(coreDir);
}
}
getLog().info("Found " + componentFiles.size() + " component.properties files");
getLog().info("Found " + jsonFiles.size() + " component json files");
// make sure to create out dir
componentsOutDir.mkdirs();
// we only want to warn for duplicates if its a clean build
boolean warnDups = componentsOutDir.list() == null || componentsOutDir.list().length == 0;
Set<String> alternativeSchemes = new HashSet<>();
for (File file : jsonFiles) {
File to = new File(componentsOutDir, file.getName());
if (to.exists()) {
if (warnDups) {
duplicateJsonFiles.add(to);
getLog().warn("Duplicate component name detected: " + to);
} else if (file.lastModified() < to.lastModified()) {
getLog().debug("Skipping generated file: " + to);
continue;
} else {
getLog().warn("Stale file: " + to);
}
}
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
// check if we have a component label as we want the components to include labels
try {
String text = loadText(new FileInputStream(file));
String name = asComponentName(file);
Matcher matcher = LABEL_PATTERN.matcher(text);
// grab the label, and remember it in the used labels
if (matcher.find()) {
String label = matcher.group(1);
String[] labels = label.split(",");
for (String s : labels) {
Set<String> components = usedComponentLabels.get(s);
if (components == null) {
components = new TreeSet<>();
usedComponentLabels.put(s, components);
}
components.add(name);
}
}
// check all the component options and grab the label(s) they use
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("componentProperties", text, true);
for (Map<String, String> row : rows) {
String label = row.get("label");
if (label != null && !label.isEmpty()) {
String[] parts = label.split(",");
for (String part : parts) {
usedOptionLabels.add(part);
}
}
}
// check all the endpoint options and grab the label(s) they use
int unused = 0;
rows = JSonSchemaHelper.parseJsonSchema("properties", text, true);
for (Map<String, String> row : rows) {
String label = row.get("label");
if (label != null && !label.isEmpty()) {
String[] parts = label.split(",");
for (String part : parts) {
usedOptionLabels.add(part);
}
} else {
unused++;
}
}
if (unused >= UNUSED_LABELS_WARN) {
unlabeledOptions.add(name);
}
// remember alternative schemes
rows = JSonSchemaHelper.parseJsonSchema("component", text, false);
for (Map<String, String> row : rows) {
String alternativeScheme = row.get("alternativeSchemes");
if (alternativeScheme != null && !alternativeScheme.isEmpty()) {
String[] parts = alternativeScheme.split(",");
for (int i = 1; i < parts.length; i++) {
// skip first as that is the regular scheme
String part = parts[i];
alternativeSchemes.add(part);
}
}
}
// detect missing first version
String firstVersion = null;
for (Map<String, String> row : rows) {
if (row.get("firstVersion") != null) {
firstVersion = row.get("firstVersion");
}
}
if (firstVersion == null) {
missingFirstVersions.add(file);
}
} catch (IOException e) {
// ignore
}
}
Set<String> componentNames = new LinkedHashSet<>();
File all = new File(componentsOutDir, "../components.properties");
try {
FileOutputStream fos = new FileOutputStream(all, false);
String[] names = componentsOutDir.list();
List<String> components = new ArrayList<>();
// sort the names
for (String name : names) {
if (name.endsWith(".json")) {
// strip out .json from the name
String componentName = name.substring(0, name.length() - 5);
components.add(componentName);
}
}
Collections.sort(components);
for (String name : components) {
fos.write(name.getBytes());
fos.write("\n".getBytes());
// remember component name
componentNames.add(name);
}
fos.close();
} catch (IOException e) {
throw new MojoFailureException("Error writing to file " + all);
}
printComponentsReport(jsonFiles, duplicateJsonFiles, missingComponents, usedComponentLabels, usedOptionLabels, unlabeledOptions, missingFirstVersions);
// filter out duplicate component names that are alternative scheme names
Set<String> answer = new LinkedHashSet<>();
for (String componentName : componentNames) {
if (!alternativeSchemes.contains(componentName)) {
answer.add(componentName);
}
}
return answer;
}
// CHECKSTYLE:ON
protected Set<String> executeDataFormats() throws MojoExecutionException, MojoFailureException {
getLog().info("Copying all Camel dataformat json descriptors");
// lets use sorted set/maps
Set<File> jsonFiles = new TreeSet<>();
Set<File> duplicateJsonFiles = new TreeSet<>();
Set<File> dataFormatFiles = new TreeSet<>();
Map<String, Set<String>> usedLabels = new TreeMap<>();
Set<File> missingFirstVersions = new TreeSet<>();
// find all data formats from the components directory
if (componentsDir != null && componentsDir.isDirectory()) {
File[] dataFormats = componentsDir.listFiles();
if (dataFormats != null) {
for (File dir : dataFormats) {
// special for this as the data format is in the sub dir
if (dir.isDirectory() && "camel-fhir".equals(dir.getName())) {
dir = new File(dir, "camel-fhir-component");
}
if (dir.isDirectory() && !"target".equals(dir.getName())) {
File target = new File(dir, "target/classes");
// this module must be active with a source folder
File src = new File(dir, "src");
boolean active = src.isDirectory() && src.exists();
if (active) {
findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter());
}
}
}
}
}
if (coreDir != null && coreDir.isDirectory()) {
File target = new File(coreDir, "target/classes");
findDataFormatFilesRecursive(target, jsonFiles, dataFormatFiles, new CamelDataFormatsFileFilter());
}
getLog().info("Found " + dataFormatFiles.size() + " dataformat.properties files");
getLog().info("Found " + jsonFiles.size() + " dataformat json files");
// make sure to create out dir
dataFormatsOutDir.mkdirs();
// we only want to warn for duplicates if its a clean build
boolean warnDups = dataFormatsOutDir.list() == null || dataFormatsOutDir.list().length == 0;
for (File file : jsonFiles) {
File to = new File(dataFormatsOutDir, file.getName());
if (to.exists()) {
if (warnDups) {
duplicateJsonFiles.add(to);
getLog().warn("Duplicate dataformat name detected: " + to);
} else if (file.lastModified() < to.lastModified()) {
getLog().debug("Skipping generated file: " + to);
continue;
} else {
getLog().warn("Stale file: " + to);
}
}
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
// check if we have a label as we want the data format to include labels
try {
String text = loadText(new FileInputStream(file));
String name = asComponentName(file);
Matcher matcher = LABEL_PATTERN.matcher(text);
// grab the label, and remember it in the used labels
if (matcher.find()) {
String label = matcher.group(1);
String[] labels = label.split(",");
for (String s : labels) {
Set<String> dataFormats = usedLabels.get(s);
if (dataFormats == null) {
dataFormats = new TreeSet<>();
usedLabels.put(s, dataFormats);
}
dataFormats.add(name);
}
}
// detect missing first version
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("dataformat", text, false);
String firstVersion = null;
for (Map<String, String> row : rows) {
if (row.get("firstVersion") != null) {
firstVersion = row.get("firstVersion");
}
}
if (firstVersion == null) {
missingFirstVersions.add(file);
}
} catch (IOException e) {
// ignore
}
}
Set<String> answer = new LinkedHashSet<>();
File all = new File(dataFormatsOutDir, "../dataformats.properties");
try {
FileOutputStream fos = new FileOutputStream(all, false);
String[] names = dataFormatsOutDir.list();
List<String> dataFormats = new ArrayList<>();
// sort the names
for (String name : names) {
if (name.endsWith(".json")) {
// strip out .json from the name
String dataFormatName = name.substring(0, name.length() - 5);
dataFormats.add(dataFormatName);
}
}
Collections.sort(dataFormats);
for (String name : dataFormats) {
fos.write(name.getBytes());
fos.write("\n".getBytes());
// remember dataformat name
answer.add(name);
}
fos.close();
} catch (IOException e) {
throw new MojoFailureException("Error writing to file " + all);
}
printDataFormatsReport(jsonFiles, duplicateJsonFiles, usedLabels, missingFirstVersions);
return answer;
}
protected Set<String> executeLanguages() throws MojoExecutionException, MojoFailureException {
getLog().info("Copying all Camel language json descriptors");
// lets use sorted set/maps
Set<File> jsonFiles = new TreeSet<>();
Set<File> duplicateJsonFiles = new TreeSet<>();
Set<File> languageFiles = new TreeSet<>();
Map<String, Set<String>> usedLabels = new TreeMap<>();
Set<File> missingFirstVersions = new TreeSet<>();
// find all languages from the components directory
if (componentsDir != null && componentsDir.isDirectory()) {
File[] languages = componentsDir.listFiles();
if (languages != null) {
for (File dir : languages) {
if (dir.isDirectory() && !"target".equals(dir.getName())) {
File target = new File(dir, "target/classes");
// this module must be active with a source folder
File src = new File(dir, "src");
boolean active = src.isDirectory() && src.exists();
if (active) {
findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
}
}
}
}
}
if (coreDir != null && coreDir.isDirectory()) {
File target = new File(coreDir, "target/classes");
findLanguageFilesRecursive(target, jsonFiles, languageFiles, new CamelLanguagesFileFilter());
}
getLog().info("Found " + languageFiles.size() + " language.properties files");
getLog().info("Found " + jsonFiles.size() + " language json files");
// make sure to create out dir
languagesOutDir.mkdirs();
// we only want to warn for duplicates if its a clean build
boolean warnDups = languagesOutDir.list() == null || languagesOutDir.list().length == 0;
for (File file : jsonFiles) {
File to = new File(languagesOutDir, file.getName());
if (to.exists()) {
if (warnDups) {
duplicateJsonFiles.add(to);
getLog().warn("Duplicate language name detected: " + to);
} else if (file.lastModified() < to.lastModified()) {
getLog().debug("Skipping generated file: " + to);
continue;
} else {
getLog().warn("Stale file: " + to);
}
}
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
// check if we have a label as we want the data format to include labels
try {
String text = loadText(new FileInputStream(file));
String name = asComponentName(file);
Matcher matcher = LABEL_PATTERN.matcher(text);
// grab the label, and remember it in the used labels
if (matcher.find()) {
String label = matcher.group(1);
String[] labels = label.split(",");
for (String s : labels) {
Set<String> languages = usedLabels.get(s);
if (languages == null) {
languages = new TreeSet<>();
usedLabels.put(s, languages);
}
languages.add(name);
}
}
// detect missing first version
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("language", text, false);
String firstVersion = null;
for (Map<String, String> row : rows) {
if (row.get("firstVersion") != null) {
firstVersion = row.get("firstVersion");
}
}
if (firstVersion == null) {
missingFirstVersions.add(file);
}
} catch (IOException e) {
// ignore
}
}
Set<String> answer = new LinkedHashSet<>();
File all = new File(languagesOutDir, "../languages.properties");
try {
FileOutputStream fos = new FileOutputStream(all, false);
String[] names = languagesOutDir.list();
List<String> languages = new ArrayList<>();
// sort the names
for (String name : names) {
if (name.endsWith(".json")) {
// strip out .json from the name
String languageName = name.substring(0, name.length() - 5);
languages.add(languageName);
}
}
Collections.sort(languages);
for (String name : languages) {
fos.write(name.getBytes());
fos.write("\n".getBytes());
// remember language name
answer.add(name);
}
fos.close();
} catch (IOException e) {
throw new MojoFailureException("Error writing to file " + all);
}
printLanguagesReport(jsonFiles, duplicateJsonFiles, usedLabels, missingFirstVersions);
return answer;
}
private Set<String> executeOthers() throws MojoFailureException {
getLog().info("Copying all Camel other json descriptors");
// lets use sorted set/maps
Set<File> jsonFiles = new TreeSet<>();
Set<File> duplicateJsonFiles = new TreeSet<>();
Set<File> otherFiles = new TreeSet<>();
Map<String, Set<String>> usedLabels = new TreeMap<>();
Set<File> missingFirstVersions = new TreeSet<>();
// find all others from the components directory
if (componentsDir != null && componentsDir.isDirectory()) {
File[] others = componentsDir.listFiles();
if (others != null) {
for (File dir : others) {
// skip these special cases
boolean special = "camel-core-osgi".equals(dir.getName())
|| "camel-core-xml".equals(dir.getName())
|| "camel-box".equals(dir.getName())
|| "camel-http-common".equals(dir.getName())
|| "camel-jetty-common".equals(dir.getName());
boolean special2 = "camel-as2".equals(dir.getName())
|| "camel-linkedin".equals(dir.getName())
|| "camel-olingo2".equals(dir.getName())
|| "camel-olingo4".equals(dir.getName())
|| "camel-servicenow".equals(dir.getName())
|| "camel-salesforce".equals(dir.getName())
|| "camel-fhir".equals(dir.getName());
if (special || special2) {
continue;
}
if (dir.isDirectory() && !"target".equals(dir.getName())) {
File target = new File(dir, "target/classes");
if (target.exists()) {
// this module must be active with a source folder
File src = new File(dir, "src");
boolean active = src.isDirectory() && src.exists();
if (active) {
findOtherFilesRecursive(target, jsonFiles, otherFiles, new CamelOthersFileFilter());
}
}
}
}
}
}
// nothing in camel-core
getLog().info("Found " + otherFiles.size() + " other.properties files");
getLog().info("Found " + jsonFiles.size() + " other json files");
// make sure to create out dir
othersOutDir.mkdirs();
// we only want to warn for duplicates if its a clean build
boolean warnDups = othersOutDir.list() == null || othersOutDir.list().length == 0;
for (File file : jsonFiles) {
File to = new File(othersOutDir, file.getName());
if (to.exists()) {
if (warnDups) {
duplicateJsonFiles.add(to);
getLog().warn("Duplicate other name detected: " + to);
} else if (file.lastModified() < to.lastModified()) {
getLog().debug("Skipping generated file: " + to);
continue;
} else {
getLog().warn("Stale file: " + to);
}
}
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
// check if we have a label as we want the other to include labels
try {
String text = loadText(new FileInputStream(file));
String name = asComponentName(file);
Matcher matcher = LABEL_PATTERN.matcher(text);
// grab the label, and remember it in the used labels
if (matcher.find()) {
String label = matcher.group(1);
String[] labels = label.split(",");
for (String s : labels) {
Set<String> others = usedLabels.get(s);
if (others == null) {
others = new TreeSet<>();
usedLabels.put(s, others);
}
others.add(name);
}
}
// detect missing first version
List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("other", text, false);
String firstVersion = null;
for (Map<String, String> row : rows) {
if (row.get("firstVersion") != null) {
firstVersion = row.get("firstVersion");
}
}
if (firstVersion == null) {
missingFirstVersions.add(file);
}
} catch (IOException e) {
// ignore
}
}
Set<String> answer = new LinkedHashSet<>();
File all = new File(othersOutDir, "../others.properties");
try {
FileOutputStream fos = new FileOutputStream(all, false);
String[] names = othersOutDir.list();
List<String> others = new ArrayList<>();
// sort the names
for (String name : names) {
if (name.endsWith(".json")) {
// strip out .json from the name
String otherName = name.substring(0, name.length() - 5);
others.add(otherName);
}
}
Collections.sort(others);
for (String name : others) {
fos.write(name.getBytes());
fos.write("\n".getBytes());
// remember other name
answer.add(name);
}
fos.close();
} catch (IOException e) {
throw new MojoFailureException("Error writing to file " + all);
}
printOthersReport(jsonFiles, duplicateJsonFiles, usedLabels, missingFirstVersions);
return answer;
}
protected void executeArchetypes() throws MojoExecutionException, MojoFailureException {
getLog().info("Copying Archetype Catalog");
// find the generate catalog
File file = new File(archetypesDir, "target/classes/archetype-catalog.xml");
// make sure to create out dir
archetypesOutDir.mkdirs();
if (file.exists() && file.isFile()) {
File to = new File(archetypesOutDir, file.getName());
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
}
}
protected void executeXmlSchemas() throws MojoExecutionException, MojoFailureException {
getLog().info("Copying Spring/Blueprint XML schemas");
schemasOutDir.mkdirs();
File file = new File(springSchemaDir, "camel-spring.xsd");
if (file.exists() && file.isFile()) {
File to = new File(schemasOutDir, file.getName());
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}
}
file = new File(blueprintSchemaDir, "camel-blueprint.xsd");
if (file.exists() && file.isFile()) {
File to = new File(schemasOutDir, file.getName());
try {
copyFile(file, to);
} catch (IOException e) {
throw new MojoFailureException("Cannot copy file from " + file + " -> " + to, e);
}