/
ComponentConfig.java
1165 lines (1031 loc) · 44.6 KB
/
ComponentConfig.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.ofbiz.base.component;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.ofbiz.base.container.ContainerConfig;
import org.apache.ofbiz.base.container.ContainerConfig.Configuration;
import org.apache.ofbiz.base.container.ContainerException;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.Assert;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.KeyStoreUtil;
import org.apache.ofbiz.base.util.UtilURL;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* An object that models the <code><ofbiz-component></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public final class ComponentConfig {
public static final String module = ComponentConfig.class.getName();
public static final String OFBIZ_COMPONENT_XML_FILENAME = "ofbiz-component.xml";
// This map is not a UtilCache instance because there is no strategy or implementation for reloading components.
private static final ComponentConfigCache componentConfigCache = new ComponentConfigCache();
public static Boolean componentExists(String componentName) {
Assert.notEmpty("componentName", componentName);
return componentConfigCache.fromGlobalName(componentName) != null;
}
/**
* Constructs a component predicate checking if it corresponds to a specific name.
*
* @param cname the name of the component to match which can be {@code null}
* which means "any" component
* @return a component predicate for matching a specific component name.
*/
private static Predicate<ComponentConfig> matchingComponentName(String cname) {
return cc -> cname == null || cname.equals(cc.getComponentName());
}
/**
* Provides the list of all the classpath information available in components.
*
* @return a list of classpath information
*/
public static List<ClasspathInfo> getAllClasspathInfos() {
return components()
.flatMap(cc -> cc.getClasspathInfos().stream())
.collect(Collectors.toList());
}
public static Collection<ComponentConfig> getAllComponents() {
return componentConfigCache.values();
}
public static Stream<ComponentConfig> components() {
return componentConfigCache.values().stream();
}
/**
* Provides the list of all the container configuration elements available in components.
*
* @return a list of container configuration elements
*/
public static List<ContainerConfig.Configuration> getAllConfigurations() {
return components()
.flatMap(cc -> cc.getConfigurations().stream())
.collect(Collectors.toList());
}
public static List<EntityResourceInfo> getAllEntityResourceInfos(String type) {
return getAllEntityResourceInfos(type, null);
}
/**
* Provides the list of all the entity resource information matching a type.
*
* @param type the service resource type to match
* @param name the name of the component to match
* @return a list of entity resource information
*/
public static List<EntityResourceInfo> getAllEntityResourceInfos(String type, String name) {
return components()
.filter(matchingComponentName(name))
.flatMap(cc -> cc.getEntityResourceInfos().stream())
.filter(eri -> UtilValidate.isEmpty(type) || type.equals(eri.type))
.collect(Collectors.toList());
}
/**
* Provides the list of all the keystore information available in components.
*
* @return a list of keystore information
*/
public static List<KeystoreInfo> getAllKeystoreInfos() {
return components()
.flatMap(cc -> cc.getKeystoreInfos().stream())
.collect(Collectors.toList());
}
/**
* Provides the list of all the service resource information matching a type.
*
* @param type the service resource type to match
* @return a list of service resource information
*/
public static List<ServiceResourceInfo> getAllServiceResourceInfos(String type) {
return components()
.flatMap(cc -> cc.getServiceResourceInfos().stream())
.filter(sri -> UtilValidate.isEmpty(type) || type.equals(sri.type))
.collect(Collectors.toList());
}
/**
* Provides the list of all the test-suite information matching a component name.
*
* @param name the name of the component to match where {@code null} means "any"
* @return a list of test-suite information
*/
public static List<TestSuiteInfo> getAllTestSuiteInfos(String name) {
return components()
.filter(matchingComponentName(name))
.flatMap(cc -> cc.getTestSuiteInfos().stream())
.collect(Collectors.toList());
}
/**
* Provides the list of all the web-app information in components
*
* @return a list of web-app information
*/
public static List<WebappInfo> getAllWebappResourceInfos() {
return components()
.flatMap(cc -> cc.getWebappInfos().stream())
.collect(Collectors.toList());
}
public static ComponentConfig getComponentConfig(String globalName) throws ComponentException {
// TODO: we need to look up the rootLocation from the container config, or this will blow up
return getComponentConfig(globalName, null);
}
public static ComponentConfig getComponentConfig(String globalName, String rootLocation) throws ComponentException {
ComponentConfig componentConfig;
if (globalName != null && !globalName.isEmpty()) {
componentConfig = componentConfigCache.fromGlobalName(globalName);
if (componentConfig != null) {
return componentConfig;
}
}
if (rootLocation != null && !rootLocation.isEmpty()) {
componentConfig = componentConfigCache.fromRootLocation(rootLocation);
if (componentConfig != null) {
return componentConfig;
}
}
if (rootLocation == null) {
// Do we really need to do this?
throw new ComponentException("No component found named : " + globalName);
}
componentConfig = new ComponentConfig(globalName, rootLocation);
if (componentConfig.enabled()) {
componentConfigCache.put(componentConfig);
}
return componentConfig;
}
public static String getFullLocation(String componentName, String resourceLoaderName, String location) throws ComponentException {
ComponentConfig cc = getComponentConfig(componentName, null);
return cc.getFullLocation(resourceLoaderName, location);
}
/**
* Provides the first key-store matching a name from a specific component.
*
* @param componentName the name of the component to match which can be {@code null}
* @param keystoreName the name of the key-store to match which can be {@code null}
* @return the first key-store matching both {@code componentName} and {@code keystoreName}.
*/
public static KeystoreInfo getKeystoreInfo(String componentName, String keystoreName) {
return components()
.filter(cc -> componentName != null && componentName.equals(cc.getComponentName()))
.flatMap(cc -> cc.getKeystoreInfos().stream())
.filter(ks -> keystoreName != null && keystoreName.equals(ks.getName()))
.findFirst()
.orElse(null);
}
public static String getRootLocation(String componentName) throws ComponentException {
ComponentConfig cc = getComponentConfig(componentName);
return cc.rootLocation().toString();
}
public static InputStream getStream(String componentName, String resourceLoaderName, String location) throws ComponentException {
ComponentConfig cc = getComponentConfig(componentName);
return cc.getStream(resourceLoaderName, location);
}
public static URL getURL(String componentName, String resourceLoaderName, String location) throws ComponentException {
ComponentConfig cc = getComponentConfig(componentName);
return cc.getURL(resourceLoaderName, location);
}
public static WebappInfo getWebAppInfo(String serverName, String contextRoot) {
if (serverName == null || contextRoot == null) {
return null;
}
ComponentConfig.WebappInfo info = null;
for (ComponentConfig cc : getAllComponents()) {
for (WebappInfo wInfo : cc.getWebappInfos()) {
if (serverName.equals(wInfo.server) && contextRoot.equals(wInfo.getContextRoot())) {
info = wInfo;
}
}
}
return info;
}
// ========== ComponentConfig instance ==========
private final String globalName;
private final Path rootLocation;
private final String componentName;
private final boolean enabled;
private final Map<String, ResourceLoaderInfo> resourceLoaderInfos;
private final List<ClasspathInfo> classpathInfos;
private final List<DependsOnInfo> dependsOnInfos;
private final List<EntityResourceInfo> entityResourceInfos;
private final List<ServiceResourceInfo> serviceResourceInfos;
private final List<TestSuiteInfo> testSuiteInfos;
private final List<KeystoreInfo> keystoreInfos;
private final List<WebappInfo> webappInfos;
private final List<ContainerConfig.Configuration> configurations;
/**
* Instantiates a component configuration from a {@link ComponentConfig.Builder builder} object.
*
* This allows instantiating component configuration without an XML entity object,
* which is useful for example when writing unit tests.
*
* @param b the component configuration builder
*/
private ComponentConfig(Builder b) {
this.globalName = b.globalName;
String rootLocation = (b.rootLocation == null) ? "" : b.rootLocation;
this.rootLocation = Paths.get(rootLocation.replace('\\', '/')).normalize().toAbsolutePath();
this.componentName = b.componentName;
this.enabled = b.enabled;
this.resourceLoaderInfos = b.resourceLoaderInfos;
this.classpathInfos = (b.classpathInfos == null) ? Collections.emptyList() : b.classpathInfos;
this.dependsOnInfos = (b.dependsOnInfos == null) ? Collections.emptyList() : b.dependsOnInfos;
this.entityResourceInfos = b.entityResourceInfos;
this.serviceResourceInfos = b.serviceResourceInfos;
this.testSuiteInfos = b.testSuiteInfos;
this.keystoreInfos = b.keystoreInfos;
this.webappInfos = b.webappInfos;
this.configurations = b.configurations;
}
/**
* Builder for component configuration.
*/
public static final class Builder {
private String globalName;
private String rootLocation;
private String componentName;
private boolean enabled = true;
private Map<String, ResourceLoaderInfo> resourceLoaderInfos;
private List<ClasspathInfo> classpathInfos;
private List<DependsOnInfo> dependsOnInfos;
private List<EntityResourceInfo> entityResourceInfos;
private List<ServiceResourceInfo> serviceResourceInfos;
private List<TestSuiteInfo> testSuiteInfos;
private List<KeystoreInfo> keystoreInfos;
private List<WebappInfo> webappInfos;
private List<ContainerConfig.Configuration> configurations;
public Builder globalName(String name) {
this.globalName = name;
return this;
}
public Builder rootLocation(String rootLocation) {
this.rootLocation = rootLocation;
return this;
}
public Builder componentName(String componentName) {
this.componentName = componentName;
return this;
}
public Builder enabled(boolean enabled) {
this.enabled = enabled;
return this;
}
public Builder resourceLoaderInfos(Map<String, ResourceLoaderInfo> resourceLoaderInfos) {
this.resourceLoaderInfos = resourceLoaderInfos;
return this;
}
public Builder classpathInfos(List<ClasspathInfo> classpathInfos) {
this.classpathInfos = classpathInfos;
return this;
}
public Builder dependsOnInfos(List<DependsOnInfo> dependsOnInfos) {
this.dependsOnInfos = dependsOnInfos;
return this;
}
public Builder entityResourceInfos(List<EntityResourceInfo> entityResourceInfos) {
this.entityResourceInfos = entityResourceInfos;
return this;
}
public Builder serviceResourceInfos(List<ServiceResourceInfo> serviceResourceInfos) {
this.serviceResourceInfos = serviceResourceInfos;
return this;
}
public Builder testSuiteInfos(List<TestSuiteInfo> testSuiteInfos) {
this.testSuiteInfos = testSuiteInfos;
return this;
}
public Builder keystoreInfos(List<KeystoreInfo> keystoreInfos) {
this.keystoreInfos = keystoreInfos;
return this;
}
public Builder webappInfos(List<WebappInfo> webappInfos) {
this.webappInfos = webappInfos;
return this;
}
public Builder configurations(List<ContainerConfig.Configuration> configurations) {
this.configurations = configurations;
return this;
}
public ComponentConfig create() {
return new ComponentConfig(this);
}
}
/**
* Instantiates a component config from a component name and a root location.
*
* @param globalName the global name of the component which can be {@code null}
* @param rootLocation the root location of the component
* @throws ComponentException when component directory does not exist or if the
* {@code ofbiz-component.xml} file of that component is not properly defined
* @throws NullPointerException when {@code rootLocation} is {@code null}
*/
private ComponentConfig(String globalName, String rootLocation) throws ComponentException {
this.rootLocation = Paths.get(rootLocation.replace('\\', '/')).normalize().toAbsolutePath();
if (Files.notExists(this.rootLocation)) {
throw new ComponentException("The component root location does not exist: " + rootLocation);
} else if (!Files.isDirectory(this.rootLocation)) {
throw new ComponentException("The component root location is not a directory: " + rootLocation);
}
String xmlFilename = this.rootLocation.resolve(OFBIZ_COMPONENT_XML_FILENAME).toString();
URL xmlUrl = UtilURL.fromFilename(xmlFilename);
if (xmlUrl == null) {
throw new ComponentException("Could not find the " + OFBIZ_COMPONENT_XML_FILENAME
+ " configuration file in the component root location: " + rootLocation);
}
Element componentElement = null;
try {
Document ofbizComponentDocument = UtilXml.readXmlDocument(xmlUrl, true);
componentElement = ofbizComponentDocument.getDocumentElement();
} catch (Exception e) {
throw new ComponentException("Error reading the component config file: " + xmlUrl, e);
}
componentName = componentElement.getAttribute("name");
enabled = "true".equalsIgnoreCase(componentElement.getAttribute("enabled"));
this.globalName = UtilValidate.isEmpty(globalName) ? componentName : globalName;
dependsOnInfos = collectElements(componentElement, "depends-on", DependsOnInfo::new);
classpathInfos = collectElements(componentElement, "classpath", ClasspathInfo::new);
entityResourceInfos = collectElements(componentElement, "entity-resource", EntityResourceInfo::new);
serviceResourceInfos = collectElements(componentElement, "service-resource", ServiceResourceInfo::new);
testSuiteInfos = collectElements(componentElement, "test-suite", TestSuiteInfo::new);
keystoreInfos = collectElements(componentElement, "keystore", KeystoreInfo::new);
webappInfos = collectElements(componentElement, "webapp", WebappInfo::new);
resourceLoaderInfos = UtilXml.childElementList(componentElement, "resource-loader").stream()
.map(ResourceLoaderInfo::new)
.collect(Collectors.collectingAndThen(
Collectors.toMap(rli -> rli.name, rli -> rli),
Collections::unmodifiableMap));
try {
Collection<Configuration> configurations = ContainerConfig.getConfigurations(xmlUrl);
this.configurations = Collections.unmodifiableList(new ArrayList<>(configurations));
} catch (ContainerException ce) {
throw new ComponentException("Error reading container configurations for component: " + this.globalName, ce);
}
if (Debug.verboseOn()) {
Debug.logVerbose("Read component config : [" + rootLocation + "]", module);
}
}
/**
* Constructs an immutable list of objects from the the childs of an XML element.
*
* @param ofbizComponentElement the XML element containing the childs
* @param elemName the name of the child elements to collect
* @param mapper the constructor use to map child elements to objects
* @return an immutable list of objects corresponding to {@code mapper}
*/
private <T> List<T> collectElements(Element ofbizComponentElement, String elemName,
BiFunction<ComponentConfig, Element, T> mapper) {
return UtilXml.childElementList(ofbizComponentElement, elemName).stream()
.flatMap(element -> {
try {
return Stream.of(mapper.apply(this, element));
} catch(IllegalArgumentException e) {
Debug.log(e.getMessage());
return Stream.empty();
}
})
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
}
public boolean enabled() {
return this.enabled;
}
/**
* Provides the list of class path information.
*
* @return an immutable list containing the class path information.
*/
public List<ClasspathInfo> getClasspathInfos() {
return this.classpathInfos;
}
public String getComponentName() {
return this.componentName;
}
public List<ContainerConfig.Configuration> getConfigurations() {
return this.configurations;
}
public List<EntityResourceInfo> getEntityResourceInfos() {
return this.entityResourceInfos;
}
public String getFullLocation(String resourceLoaderName, String location) throws ComponentException {
ResourceLoaderInfo resourceLoaderInfo = resourceLoaderInfos.get(resourceLoaderName);
if (resourceLoaderInfo == null) {
throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName);
}
StringBuilder buf = new StringBuilder();
// pre-pend component root location if this is a type component resource-loader
if ("component".equals(resourceLoaderInfo.type)) {
buf.append(rootLocation);
buf.append('/');
}
if (UtilValidate.isNotEmpty(resourceLoaderInfo.prependEnv)) {
String propValue = System.getProperty(resourceLoaderInfo.prependEnv);
if (propValue == null) {
String errMsg = "The Java environment (-Dxxx=yyy) variable with name " + resourceLoaderInfo.prependEnv + " is not set, cannot load resource.";
Debug.logError(errMsg, module);
throw new IllegalArgumentException(errMsg);
}
buf.append(propValue);
}
if (UtilValidate.isNotEmpty(resourceLoaderInfo.prefix)) {
buf.append(resourceLoaderInfo.prefix);
}
buf.append(location);
return buf.toString();
}
public String getGlobalName() {
return this.globalName;
}
/**
* Provides the list of dependent components.
*
* @return an immutable list containing the dependency information.
*/
public List<DependsOnInfo> getDependsOn() {
return this.dependsOnInfos;
}
public List<KeystoreInfo> getKeystoreInfos() {
return this.keystoreInfos;
}
public Map<String, ResourceLoaderInfo> getResourceLoaderInfos() {
return this.resourceLoaderInfos;
}
/**
* Provides the root location of the component definition.
*
* @return a normalized absolute path
*/
public Path rootLocation() {
return rootLocation;
}
public List<ServiceResourceInfo> getServiceResourceInfos() {
return this.serviceResourceInfos;
}
public InputStream getStream(String resourceLoaderName, String location) throws ComponentException {
URL url = getURL(resourceLoaderName, location);
try {
return url.openStream();
} catch (java.io.IOException e) {
throw new ComponentException("Error opening resource at location [" + url.toExternalForm() + "]", e);
}
}
public List<TestSuiteInfo> getTestSuiteInfos() {
return this.testSuiteInfos;
}
public URL getURL(String resourceLoaderName, String location) throws ComponentException {
ResourceLoaderInfo resourceLoaderInfo = resourceLoaderInfos.get(resourceLoaderName);
if (resourceLoaderInfo == null) {
throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName);
}
if ("component".equals(resourceLoaderInfo.type) || "file".equals(resourceLoaderInfo.type)) {
String fullLocation = getFullLocation(resourceLoaderName, location);
URL fileUrl = UtilURL.fromFilename(fullLocation);
if (fileUrl == null) {
throw new ComponentException("File Resource not found: " + fullLocation);
}
return fileUrl;
} else if ("classpath".equals(resourceLoaderInfo.type)) {
String fullLocation = getFullLocation(resourceLoaderName, location);
URL url = UtilURL.fromResource(fullLocation);
if (url == null) {
throw new ComponentException("Classpath Resource not found: " + fullLocation);
}
return url;
} else if ("url".equals(resourceLoaderInfo.type)) {
String fullLocation = getFullLocation(resourceLoaderName, location);
URL url = null;
try {
url = FlexibleLocation.resolveLocation(location);
} catch (java.net.MalformedURLException e) {
throw new ComponentException("Error with malformed URL while trying to load URL resource at location [" + fullLocation + "]", e);
}
if (url == null) {
throw new ComponentException("URL Resource not found: " + fullLocation);
}
return url;
} else {
throw new ComponentException("The resource-loader type is not recognized: " + resourceLoaderInfo.type);
}
}
public List<WebappInfo> getWebappInfos() {
return this.webappInfos;
}
public boolean isFileResource(ResourceInfo resourceInfo) throws ComponentException {
return isFileResourceLoader(resourceInfo.loader);
}
public boolean isFileResourceLoader(String resourceLoaderName) throws ComponentException {
ResourceLoaderInfo resourceLoaderInfo = resourceLoaderInfos.get(resourceLoaderName);
if (resourceLoaderInfo == null) {
throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName);
}
return "file".equals(resourceLoaderInfo.type) || "component".equals(resourceLoaderInfo.type);
}
/**
* An object that models the <code><classpath></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class ClasspathInfo {
private final ComponentConfig componentConfig;
private final Type type;
private final Path location;
private ClasspathInfo(ComponentConfig componentConfig, Element element) {
String loc = element.getAttribute("location").replace('\\', '/');
Path location = Paths.get(loc.startsWith("/") ? loc.substring(1) : loc).normalize();
Path fullLocation = componentConfig.rootLocation().resolve(location);
Type type = Type.of(element.getAttribute("type"));
switch (type) {
case DIR:
if (!Files.isDirectory(fullLocation)) {
String msg = String.format("Classpath location '%s' is not a valid directory", location);
throw new IllegalArgumentException(msg);
}
break;
case JAR:
if (Files.notExists(fullLocation)) {
String msg = String.format("Classpath location '%s' does not exist", location);
throw new IllegalArgumentException(msg);
}
break;
}
this.componentConfig = componentConfig;
this.type = type;
this.location = location;
}
public ComponentConfig componentConfig() {
return componentConfig;
}
public Type type() {
return type;
}
public Path location() {
return componentConfig.rootLocation().resolve(location);
}
public static enum Type {
DIR, JAR;
private static Type of(String type) {
if ("jar".equals(type) || "dir".equals(type)) {
return valueOf(type.toUpperCase());
} else {
throw new IllegalArgumentException("Classpath type '" + type + "' is not supported; '");
}
}
}
}
// ComponentConfig instances need to be looked up by their global name and root location,
// so this class encapsulates the Maps and synchronization code required to do that.
private static final class ComponentConfigCache {
// Key is the global name.
private final Map<String, ComponentConfig> componentConfigs = new LinkedHashMap<>();
// Root location mapped to global name.
private final Map<String, String> componentLocations = new HashMap<>();
private synchronized ComponentConfig fromGlobalName(String globalName) {
return componentConfigs.get(globalName);
}
private synchronized ComponentConfig fromRootLocation(String rootLocation) {
String globalName = componentLocations.get(rootLocation);
if (globalName == null) {
return null;
}
return componentConfigs.get(globalName);
}
private synchronized ComponentConfig put(ComponentConfig config) {
String globalName = config.getGlobalName();
String fileLocation = config.rootLocation().toString();
componentLocations.put(fileLocation, globalName);
return componentConfigs.put(globalName, config);
}
private synchronized Collection<ComponentConfig> values() {
return Collections.unmodifiableList(new ArrayList<>(componentConfigs.values()));
}
}
/**
* An object that models the <code><entity-resource></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class EntityResourceInfo extends ResourceInfo {
public final String type;
public final String readerName;
private EntityResourceInfo(ComponentConfig componentConfig, Element element) {
super(componentConfig, element);
this.type = element.getAttribute("type");
this.readerName = element.getAttribute("reader-name");
}
}
public static final class DependsOnInfo extends ResourceInfo {
public final String componentName;
private DependsOnInfo(ComponentConfig componentConfig, Element element) {
super(componentConfig, element);
this.componentName = element.getAttribute("component-name");
}
}
/**
* An object that models the <code><keystore></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class KeystoreInfo extends ResourceInfo {
private final String name;
private final String type;
private final String password;
private final boolean isCertStore;
private final boolean isTrustStore;
private KeystoreInfo(ComponentConfig componentConfig, Element element) {
super(componentConfig, element);
this.name = element.getAttribute("name");
this.type = element.getAttribute("type");
this.password = element.getAttribute("password");
this.isCertStore = "true".equalsIgnoreCase(element.getAttribute("is-certstore"));
this.isTrustStore = "true".equalsIgnoreCase(element.getAttribute("is-truststore"));
}
public KeyStore getKeyStore() {
ComponentResourceHandler rh = this.createResourceHandler();
try {
return KeyStoreUtil.getStore(rh.getURL(), this.getPassword(), this.getType());
} catch (Exception e) {
Debug.logWarning(e, module);
}
return null;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String getType() {
return type;
}
public boolean isCertStore() {
return isCertStore;
}
public boolean isTrustStore() {
return isTrustStore;
}
}
public static abstract class ResourceInfo {
private final ComponentConfig componentConfig;
private final String loader;
private final String location;
protected ResourceInfo(ComponentConfig componentConfig, Element element) {
this.componentConfig = componentConfig;
this.loader = element.getAttribute("loader");
this.location = element.getAttribute("location");
}
public ComponentResourceHandler createResourceHandler() {
return new ComponentResourceHandler(componentConfig.getGlobalName(), loader, location);
}
public ComponentConfig getComponentConfig() {
return componentConfig;
}
public String getLocation() {
return location;
}
}
/**
* An object that models the <code><resource-loader></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class ResourceLoaderInfo {
public final String name;
public final String type;
public final String prependEnv;
public final String prefix;
private ResourceLoaderInfo(Element element) {
this.name = element.getAttribute("name");
this.type = element.getAttribute("type");
this.prependEnv = element.getAttribute("prepend-env");
this.prefix = element.getAttribute("prefix");
}
}
/**
* An object that models the <code><service-resource></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class ServiceResourceInfo extends ResourceInfo {
public final String type;
private ServiceResourceInfo(ComponentConfig componentConfig, Element element) {
super(componentConfig, element);
this.type = element.getAttribute("type");
}
}
/**
* An object that models the <code><test-suite></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class TestSuiteInfo extends ResourceInfo {
public TestSuiteInfo(ComponentConfig componentConfig, Element element) {
super(componentConfig, element);
}
}
/**
* An object that models the <code><webapp></code> element.
*
* @see <code>ofbiz-component.xsd</code>
*
*/
public static final class WebappInfo {
// FIXME: These fields should be private - since we have accessors - but
// client code accesses the fields directly.
public final ComponentConfig componentConfig;
public final List<String> virtualHosts;
public final Map<String, String> initParameters;
public final String name;
public final String title;
public final String description;
public final String menuName;
public final String server;
public final String mountPoint;
public final String contextRoot;
public final String location;
public final String[] basePermission;
public final String position;
public final boolean privileged;
// CatalinaContainer modifies this field.
private volatile boolean appBarDisplay;
private final String accessPermission;
private final boolean useAutologinCookie;
/**
* Instantiates a webapp information from a {@link WebappInfo.Builder builder} object.
*
* This allows instantiating webapp information without an XML entity object,
* which is useful for example when writing unit tests.
*
* @param b the webapp information builder
*/
private WebappInfo(Builder b) {
this.componentConfig = b.componentConfig;
this.virtualHosts = b.virtualHosts;
this.initParameters = b.initParameters;
this.name = b.name;
this.title = b.title;
this.description = b.description;
this.menuName = b.menuName;
this.server = b.server;
this.mountPoint = b.mountPoint;
this.contextRoot = b.contextRoot;
this.location = b.location;
this.basePermission = b.basePermissions;
this.position = b.position;
this.privileged = b.privileged;
this.appBarDisplay = b.appBarDisplay;
this.accessPermission = b.accessPermission;
this.useAutologinCookie = b.useAutologinCookie;
}
/**
* Builder for webapp information.
*/
public static class Builder {
private ComponentConfig componentConfig;
private List<String> virtualHosts;
private Map<String, String> initParameters;
private String name;
private String title;
private String description;
private String menuName;
private String server;
private String mountPoint = "";
private String contextRoot;
private String location;
private String[] basePermissions;
private String position;
private boolean privileged = false;
private boolean appBarDisplay = true;
private String accessPermission;
private boolean useAutologinCookie;
public Builder componentConfig(ComponentConfig componentConfig) {
this.componentConfig = componentConfig;
return this;
}
public Builder virtualHosts(List<String> virtualHosts) {
this.virtualHosts = virtualHosts;
return this;
}
public Builder initParameters(Map<String, String> initParameters) {
this.initParameters = initParameters;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder title(String title) {
this.title = title;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder menuName(String menuName) {
this.menuName = menuName;
return this;
}
public Builder server(String server) {
this.server = server;
return this;
}
public Builder mountPoint(String mountPoint) {
this.mountPoint = mountPoint;
return this;
}
public Builder contextRoot(String contextRoot) {
this.contextRoot = contextRoot;
return this;
}
public Builder location(String location) {
this.location = location;
return this;
}
public Builder basePermissions(String[] basePermissions) {
this.basePermissions = basePermissions;
return this;
}
public Builder position(String position) {
this.position = position;