forked from payara/Payara
/
DOLUtils.java
1019 lines (920 loc) · 42.6 KB
/
DOLUtils.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
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.deployment.util;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.deploy.shared.ModuleType;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.container.Sniffer;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.deployment.DeploymentContext;
import org.glassfish.api.deployment.archive.ArchiveHandler;
import org.glassfish.api.deployment.archive.ArchiveType;
import org.glassfish.api.deployment.archive.ReadableArchive;
import org.glassfish.deployment.common.DeploymentContextImpl;
import org.glassfish.deployment.common.DeploymentProperties;
import org.glassfish.deployment.common.DeploymentUtils;
import org.glassfish.deployment.common.Descriptor;
import org.glassfish.deployment.common.ModuleDescriptor;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.classmodel.reflect.Types;
import org.glassfish.internal.api.Globals;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;
import org.glassfish.internal.deployment.ExtendedDeploymentContext;
import org.glassfish.internal.deployment.SnifferManager;
import org.glassfish.loader.util.ASClassLoaderUtil;
import org.xml.sax.SAXParseException;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.ConnectorDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.deployment.archivist.Archivist;
import com.sun.enterprise.deployment.archivist.ArchivistFactory;
import com.sun.enterprise.deployment.deploy.shared.Util;
import com.sun.enterprise.deployment.io.ConfigurationDeploymentDescriptorFile;
import com.sun.enterprise.deployment.io.ConfigurationDeploymentDescriptorFileFor;
import com.sun.enterprise.deployment.io.DescriptorConstants;
import com.sun.enterprise.deployment.node.XMLElement;
import com.sun.enterprise.deployment.xml.TagNames;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.util.Set;
import java.util.regex.Pattern;
import org.glassfish.logging.annotation.LogMessageInfo;
import org.glassfish.logging.annotation.LoggerInfo;
import org.glassfish.logging.annotation.LogMessagesResourceBundle;
/**
* Utility class for convenience methods for deployment
*
* @author Jerome Dochez
* @version
*/
public class DOLUtils {
public final static String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
public final static String SCHEMA_LOCATION_TAG = "xsi:schemaLocation";
private static LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(DOLUtils.class);
@LogMessagesResourceBundle
private static final String SHARED_LOGMESSAGE_RESOURCE = "org.glassfish.deployment.LogMessages";
// Reserve this range [AS-DEPLOYMENT-00001, AS-DEPLOYMENT-02000]
// for message ids used in this deployment dol module
@LoggerInfo(subsystem = "DEPLOYMENT", description="Deployment logger for dol module", publish=true)
private static final String DEPLOYMENT_LOGGER = "javax.enterprise.system.tools.deployment.dol";
public static final Logger deplLogger =
Logger.getLogger(DEPLOYMENT_LOGGER, SHARED_LOGMESSAGE_RESOURCE);
@LogMessageInfo(message = "Ignore {0} in archive {1}, as WLS counterpart runtime xml {2} is present in the same archive.", level="WARNING")
private static final String COUNTERPART_CONFIGDD_EXISTS = "AS-DEPLOYMENT-00001";
@LogMessageInfo(message = "Exception caught: {0}.", level="WARNING")
private static final String EXCEPTION_CAUGHT = "AS-DEPLOYMENT-00002";
@LogMessageInfo(message = "{0} module [{1}] contains characteristics of other module type: {2}.", level="WARNING")
private static final String INCOMPATIBLE_TYPE = "AS-DEPLOYMENT-00003";
@LogMessageInfo(message = "Unsupported deployment descriptors element {0} value {1}.", level="WARNING")
public static final String INVALID_DESC_MAPPING = "AS-DEPLOYMENT-00015";
@LogMessageInfo(message = "DOLUtils: converting EJB to web bundle id {0}.", level="FINER")
private static final String CONVERT_EJB_TO_WEB_ID = "AS-DEPLOYMENT-00017";
/** The system property to control the precedence between GF DD
// and WLS DD when they are both present. When this property is
// set to true, GF DD will have higher precedence over WLS DD. */
private static final String GFDD_OVER_WLSDD = "gfdd.over.wlsdd";
/** The system property to control whether we should just ignore
// WLS DD. When this property is set to true, WLS DD will be ignored.*/
private static final String IGNORE_WLSDD = "ignore.wlsdd";
private static final String ID_SEPARATOR = "_";
private static final Set<String> SYSTEM_PACKAGES_ =
ImmutableSet.of("com.sun", "org.glassfish", "org.apache.jasper", "fish.payara", "com.ibm.jbatch",
"org.hibernate.validator", "org.jboss.weld", "com.ctc.wstx",
"java", "javax");
private static final Set<String> SYSTEM_PACKAGES = ImmutableSet.copyOf(Collections2.transform(SYSTEM_PACKAGES_,
new Function<String, String>() {
@Override
public String apply(String input) {
return input + ".";
}
}));
/** no need to creates new DOLUtils */
private DOLUtils() {
}
/**
* @return a logger to use in the DOL implementation classes
*/
public static synchronized Logger getDefaultLogger() {
return deplLogger;
}
/**
* Returns true if both objects are equal to {@code null} or {@code a.equals(b)}
* @param a
* @param b
* @return
* @see Object#equals(Object)
*/
public static boolean equals(Object a, Object b) {
return ((a == null && b == null) ||
(a != null && a.equals(b)));
}
/**
*
* @param bundleDesc
* @param archive
* @return an empty list if bundleDesc is null
* @throws Exception
*/
public static List<URI> getLibraryJarURIs(BundleDescriptor bundleDesc, ReadableArchive archive) throws Exception {
if (bundleDesc == null) {
return Collections.emptyList();
}
ModuleDescriptor moduleDesc = ((BundleDescriptor)bundleDesc).getModuleDescriptor();
Application app = ((BundleDescriptor)moduleDesc.getDescriptor()).getApplication();
return getLibraryJarURIs(app, archive);
}
/**
* Returns true if entry not excluded from scanning or specifically included in app
* @param app
* @param entryName
* @return
*/
public static boolean isScanningAllowed(Application app, String entryName) {
boolean included = !FluentIterable.from(app.getScanningExclusions()).anyMatch(new MatchingPredicate(entryName));
return included |= FluentIterable.from(app.getScanningInclusions()).anyMatch(new MatchingPredicate(entryName));
}
private static class MatchingPredicate implements Predicate<Pattern> {
private final String entryName;
public MatchingPredicate(String entryName) {
this.entryName = entryName;
}
@Override
public boolean apply(Pattern input) {
return input.matcher(entryName).matches();
}
}
/**
*
* @param app
* @param archive
* @return an empty list if the archive does not have a parent
* @throws Exception
*/
public static List<URI> getLibraryJarURIs(Application app, ReadableArchive archive) throws Exception {
List<URL> libraryURLs = new ArrayList<URL>();
List<URI> libraryURIs = new ArrayList<URI>();
// add libraries referenced through manifest
libraryURLs.addAll(DeploymentUtils.getManifestLibraries(archive));
ReadableArchive parentArchive = archive.getParentArchive();
if (parentArchive == null) {
return Collections.emptyList();
}
File appRoot = new File(parentArchive.getURI());
// add libraries jars inside application lib directory
libraryURLs.addAll(ASClassLoaderUtil.getAppLibDirLibrariesAsList(
appRoot, app.getLibraryDirectory(), null));
for (URL url : libraryURLs) {
libraryURIs.add(Util.toURI(url));
}
return libraryURIs;
}
/**
* Gets the associated descriptor with the context
* @param context
* @return null if there is no associated application
*/
public static BundleDescriptor getCurrentBundleForContext( DeploymentContext context) {
ExtendedDeploymentContext ctx = (ExtendedDeploymentContext)context;
Application application = context.getModuleMetaData(Application.class);
if (application == null) return null; // this can happen for non-JavaEE type deployment. e.g., issue 15869
if (ctx.getParentContext() == null) {
if (application.isVirtual()) {
// standalone module
return application.getStandaloneBundleDescriptor();
} else {
// top level
return application;
}
} else {
// a sub module of ear
return application.getModuleByUri(ctx.getModuleUri());
}
}
/**
* Returns true if there is a resource connection definition of the type with the application
* @param habitat
* @param type
* @param thisApp
* @return
*/
public static boolean isRAConnectionFactory(ServiceLocator habitat,
String type, Application thisApp) {
// first check if this is a connection factory defined in a resource
// adapter in this application
if (isRAConnectionFactory(type, thisApp)) {
return true;
}
// then check if this is a connection factory defined in a standalone
// resource adapter
Applications applications = habitat.getService(Applications.class);
if (applications != null) {
List<com.sun.enterprise.config.serverbeans.Application> raApps = applications.getApplicationsWithSnifferType(com.sun.enterprise.config.serverbeans.ServerTags.CONNECTOR, true);
ApplicationRegistry appRegistry = habitat.getService(ApplicationRegistry.class);
for (com.sun.enterprise.config.serverbeans.Application raApp : raApps) {
ApplicationInfo appInfo = appRegistry.get(raApp.getName());
if (appInfo == null)
continue;
if (isRAConnectionFactory(type, appInfo.getMetaData(Application.class))) {
return true;
}
}
}
return false;
}
/**
* Returns true if there is a resource connection definition of the type with the app
* @param type
* @param app
* @return
*/
private static boolean isRAConnectionFactory(String type, Application app) {
if (app == null) {
return false;
}
for (ConnectorDescriptor cd : app.getBundleDescriptors(ConnectorDescriptor.class)) {
if (cd.getConnectionDefinitionByCFType(type) != null) {
return true;
}
}
return false;
}
public static ArchiveType earType() {
return getModuleType(ModuleType.EAR.toString());
}
public static ArchiveType ejbType() {
return getModuleType(ModuleType.EJB.toString());
}
public static ArchiveType carType() {
return getModuleType(ModuleType.CAR.toString());
}
public static ArchiveType warType() {
return getModuleType(ModuleType.WAR.toString());
}
public static ArchiveType rarType() {
return getModuleType(ModuleType.RAR.toString());
}
/**
* Utility method to retrieve a {@link ArchiveType} from a stringified module type.
* Since {@link ArchiveType} is an extensible abstraction and implementations are plugged in via HK2 service
* registry, this method returns null if HK2 service registry is not setup.
*
* If null is passed to this method, it returns null instead of returning an arbitrary ArchiveType or throwing
* an exception.
*
* @param moduleType String equivalent of the module type being looked up. null is allowed.
* @return the corresponding ArchiveType, null if no such module type exists or HK2 Service registry is not set up
*/
public static ArchiveType getModuleType(String moduleType) {
if (moduleType == null) {
return null;
}
final ServiceLocator services = Globals.getDefaultHabitat();
ArchiveType result = null;
// This method is called without HK2 being setup when dol unit tests are run, so protect against NPE.
if(services != null) {
result = services.getService(ArchiveType.class, moduleType);
}
return result;
}
/** returns true if GF DD should have higher precedence over
* WLS DD when both present in the same archive
* @return */
public static boolean isGFDDOverWLSDD() {
return Boolean.valueOf(System.getProperty(GFDD_OVER_WLSDD));
}
/** returns true if we should ignore WLS DD in the archive
* @return */
public static boolean isIgnoreWLSDD() {
return Boolean.valueOf(System.getProperty(IGNORE_WLSDD));
}
/** process the list of the configuration files, and return the sorted
// configuration file with precedence from high to low
// this list does not take consideration of what runtime files are
// present in the current archive */
private static List<ConfigurationDeploymentDescriptorFile> sortConfigurationDDFiles(List<ConfigurationDeploymentDescriptorFile> ddFiles, ArchiveType archiveType, ReadableArchive archive) {
ConfigurationDeploymentDescriptorFile wlsConfDD = null;
ConfigurationDeploymentDescriptorFile payaraConfDD = null;
ConfigurationDeploymentDescriptorFile gfConfDD = null;
ConfigurationDeploymentDescriptorFile sunConfDD = null;
for (ConfigurationDeploymentDescriptorFile ddFile : ddFiles) {
ddFile.setArchiveType(archiveType);
String ddPath = ddFile.getDeploymentDescriptorPath();
if (ddPath.contains(DescriptorConstants.WLS)) {
wlsConfDD = ddFile;
} else if (ddPath.contains(DescriptorConstants.PAYARA_PREFIX)){
payaraConfDD = ddFile;
} else if (ddPath.contains(DescriptorConstants.GF_PREFIX)) {
gfConfDD = ddFile;
} else if (ddPath.contains(DescriptorConstants.S1AS_PREFIX)) {
sunConfDD = ddFile;
}
}
List<ConfigurationDeploymentDescriptorFile> sortedConfDDFiles = new ArrayList<ConfigurationDeploymentDescriptorFile>();
// if there is external runtime alternate deployment descriptor
// specified, just use that
File runtimeAltDDFile = archive.getArchiveMetaData(
DeploymentProperties.RUNTIME_ALT_DD, File.class);
if (runtimeAltDDFile != null && runtimeAltDDFile.exists() && runtimeAltDDFile.isFile()) {
String runtimeAltDDPath = runtimeAltDDFile.getPath();
validateRuntimeAltDDPath(runtimeAltDDPath);
if (runtimeAltDDPath.contains(DescriptorConstants.PAYARA_PREFIX) && payaraConfDD != null) {
sortedConfDDFiles.add(payaraConfDD);
return sortedConfDDFiles;
} else if (runtimeAltDDPath.contains(DescriptorConstants.GF_PREFIX) && gfConfDD != null) {
sortedConfDDFiles.add(gfConfDD);
return sortedConfDDFiles;
}
}
// sort the deployment descriptor files by precedence order
// when they are present in the same archive
if (Boolean.valueOf(System.getProperty(GFDD_OVER_WLSDD))) {
// if this property set, it means we need to make GF deployment
// descriptors higher precedence then weblogic
if (payaraConfDD != null){
sortedConfDDFiles.add(payaraConfDD);
}
if (gfConfDD != null) {
sortedConfDDFiles.add(gfConfDD);
}
if (wlsConfDD != null) {
sortedConfDDFiles.add(wlsConfDD);
}
} else if (Boolean.valueOf(System.getProperty(IGNORE_WLSDD))) {
// if this property set, it means we need to ignore
// WLS deployment descriptors
if (gfConfDD != null) {
sortedConfDDFiles.add(gfConfDD);
}
if (payaraConfDD != null){
sortedConfDDFiles.add(payaraConfDD);
}
} else {
// the default will be WLS DD has higher precedence
if (wlsConfDD != null) {
sortedConfDDFiles.add(wlsConfDD);
}
if (payaraConfDD != null){
sortedConfDDFiles.add(payaraConfDD);
}
if (gfConfDD != null) {
sortedConfDDFiles.add(gfConfDD);
}
}
if (sunConfDD != null) {
sortedConfDDFiles.add(sunConfDD);
}
return sortedConfDDFiles;
}
/**
* If the path does not contain "glassfish-" this method throws an IllegalArgumentException
* @param runtimeAltDDPath
*/
public static void validateRuntimeAltDDPath(String runtimeAltDDPath) {
if (!runtimeAltDDPath.contains(DescriptorConstants.GF_PREFIX) && !runtimeAltDDPath.contains(DescriptorConstants.PAYARA_PREFIX)) {
String msg = localStrings.getLocalString(
"enterprise.deployment.util.unsupportedruntimealtdd", "Unsupported external runtime alternate deployment descriptor [{0}].", new Object[] {runtimeAltDDPath});
throw new IllegalArgumentException(msg);
}
}
/** process the list of the configuration files, and return the sorted
* configuration file with precedence from high to low
* this list takes consideration of what runtime files are
* present in the current archive
* @param ddFiles
* @param archive
* @param archiveType
* @return
* @throws java.io.IOException */
public static List<ConfigurationDeploymentDescriptorFile> processConfigurationDDFiles(List<ConfigurationDeploymentDescriptorFile> ddFiles, ReadableArchive archive, ArchiveType archiveType) throws IOException {
File runtimeAltDDFile = archive.getArchiveMetaData(DeploymentProperties.RUNTIME_ALT_DD, File.class);
if (runtimeAltDDFile != null && runtimeAltDDFile.exists() && runtimeAltDDFile.isFile()) {
// if there are external runtime alternate deployment descriptor
// specified, the config DD files are already processed
return sortConfigurationDDFiles(ddFiles, archiveType, archive);
}
List<ConfigurationDeploymentDescriptorFile> processedConfDDFiles = new ArrayList<ConfigurationDeploymentDescriptorFile>();
for (ConfigurationDeploymentDescriptorFile ddFile : sortConfigurationDDFiles(ddFiles, archiveType, archive)) {
if (archive.exists(ddFile.getDeploymentDescriptorPath())) {
processedConfDDFiles.add(ddFile);
}
}
return processedConfDDFiles;
}
/** read alternative runtime descriptor if there is an alternative runtime
* DD packaged inside the archive
* @param appArchive
* @param embeddedArchive
* @param archivist
* @param descriptor
* @param altDDPath
* @throws java.io.IOException
* @throws org.xml.sax.SAXParseException */
public static void readAlternativeRuntimeDescriptor(ReadableArchive appArchive, ReadableArchive embeddedArchive, Archivist archivist, BundleDescriptor descriptor, String altDDPath) throws IOException, SAXParseException {
String altRuntimeDDPath = null;
ConfigurationDeploymentDescriptorFile confDD = null;
@SuppressWarnings("unchecked")
List<ConfigurationDeploymentDescriptorFile> archivistConfDDFiles = archivist.getConfigurationDDFiles();
for (ConfigurationDeploymentDescriptorFile ddFile : sortConfigurationDDFiles(archivistConfDDFiles, archivist.getModuleType(), embeddedArchive)) {
String ddPath = ddFile.getDeploymentDescriptorPath();
if (ddPath.contains(DescriptorConstants.WLS) && appArchive.exists(DescriptorConstants.WLS + altDDPath)) {
// TODO: need to revisit this for WLS alt-dd pattern
confDD = ddFile;
altRuntimeDDPath = DescriptorConstants.WLS + altDDPath;
} else if (ddPath.contains(DescriptorConstants.PAYARA_PREFIX) && appArchive.exists(DescriptorConstants.PAYARA_PREFIX + altDDPath)) {
confDD = ddFile;
altRuntimeDDPath = DescriptorConstants.PAYARA_PREFIX + altDDPath;
} else if (ddPath.contains(DescriptorConstants.GF_PREFIX) && appArchive.exists(DescriptorConstants.GF_PREFIX + altDDPath)) {
confDD = ddFile;
altRuntimeDDPath = DescriptorConstants.GF_PREFIX + altDDPath;
} else if (ddPath.contains(DescriptorConstants.S1AS_PREFIX) && appArchive.exists(DescriptorConstants.S1AS_PREFIX + altDDPath)){
confDD = ddFile;
altRuntimeDDPath = DescriptorConstants.S1AS_PREFIX + altDDPath;
}
}
if (confDD != null && altRuntimeDDPath != null) {
// found an alternative runtime DD file
InputStream is = appArchive.getEntry(altRuntimeDDPath);
confDD.setXMLValidation(
archivist.getRuntimeXMLValidation());
confDD.setXMLValidationLevel(
archivist.getRuntimeXMLValidationLevel());
if (appArchive.getURI()!=null) {
confDD.setErrorReportingString(
appArchive.getURI().getSchemeSpecificPart());
}
confDD.read(descriptor, is);
is.close();
archivist.postRuntimeDDsRead(descriptor, embeddedArchive);
} else {
archivist.readRuntimeDeploymentDescriptor(embeddedArchive,descriptor);
}
}
/**
* Read the runtime deployment descriptors (can contained in one or
* many file) set the corresponding information in the passed descriptor.
* By default, the runtime deployment descriptors are all contained in
* the xml file characterized with the path returned by
*
* @param confDDFiles the sorted configuration files for this archive
* @param archive the archive
* @param descriptor the initialized deployment descriptor
* @param main the main archivist
* @param warnIfMultipleDDs whether to log warnings if both the GlassFish and the legacy Sun descriptors are present
*/
public static void readRuntimeDeploymentDescriptor(List<ConfigurationDeploymentDescriptorFile> confDDFiles, ReadableArchive archive, RootDeploymentDescriptor descriptor, Archivist main, final boolean warnIfMultipleDDs) throws IOException, SAXParseException {
if (confDDFiles == null || confDDFiles.isEmpty()) {
return;
}
ConfigurationDeploymentDescriptorFile confDD = confDDFiles.get(0);
InputStream is = null;
try {
File runtimeAltDDFile = archive.getArchiveMetaData(
DeploymentProperties.RUNTIME_ALT_DD, File.class);
if (runtimeAltDDFile != null && runtimeAltDDFile.exists() && runtimeAltDDFile.isFile()) {
is = new FileInputStream(runtimeAltDDFile);
} else {
is = archive.getEntry(confDD.getDeploymentDescriptorPath());
}
if (is == null) {
return;
}
for (int i = 1; i < confDDFiles.size(); i++) {
if (warnIfMultipleDDs) {
deplLogger.log(Level.WARNING,
COUNTERPART_CONFIGDD_EXISTS,
new Object[] {
confDDFiles.get(i).getDeploymentDescriptorPath(),
archive.getURI().getSchemeSpecificPart(),
confDD.getDeploymentDescriptorPath()});
}
}
confDD.setErrorReportingString(archive.getURI().getSchemeSpecificPart());
if (confDD.isValidating()) {
confDD.setXMLValidation(main.getRuntimeXMLValidation());
confDD.setXMLValidationLevel(main.getRuntimeXMLValidationLevel());
} else {
confDD.setXMLValidation(false);
}
confDD.read(descriptor, is);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
}
}
}
}
/**
* Sets the class for processing an archive on a subarchive
* <p>
* If there an exception occurs in this method it will be caught and logged as a warning
* @param habitat
* @param archive
* @param md
* @param app
* @param subArchivist
*/
public static void setExtensionArchivistForSubArchivist(ServiceLocator habitat, ReadableArchive archive, ModuleDescriptor md, Application app, Archivist subArchivist) {
try {
Collection<Sniffer> sniffers = getSniffersForModule(habitat, archive, md, app);
ArchivistFactory archivistFactory = habitat.getService(ArchivistFactory.class);
subArchivist.setExtensionArchivists(archivistFactory.getExtensionsArchivists(sniffers, subArchivist.getModuleType()));
} catch (Exception e) {
deplLogger.log(Level.WARNING,
EXCEPTION_CAUGHT,
new Object[] { e.getMessage(), e });
}
}
/** get sniffer list for sub modules of an ear application */
private static Collection<Sniffer> getSniffersForModule(ServiceLocator habitat, ReadableArchive archive, ModuleDescriptor md, Application app) throws Exception {
ArchiveHandler handler = habitat.getService(ArchiveHandler.class, md.getModuleType().toString());
SnifferManager snifferManager = habitat.getService(SnifferManager.class);
List<URI> classPathURIs = handler.getClassPathURIs(archive);
classPathURIs.addAll(getLibraryJarURIs(app, archive));
Types types = archive.getParentArchive().getExtraData(Types.class);
DeployCommandParameters parameters = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.COMMAND_PARAMS, DeployCommandParameters.class);
Properties appProps = archive.getParentArchive().getArchiveMetaData(DeploymentProperties.APP_PROPS, Properties.class);
ExtendedDeploymentContext context = new DeploymentContextImpl(null, archive, parameters, habitat.<ServerEnvironment>getService(ServerEnvironment.class));
if (appProps != null) {
context.getAppProps().putAll(appProps);
}
context.setArchiveHandler(handler);
context.addTransientAppMetaData(Types.class.getName(), types);
Collection<Sniffer> sniffers = snifferManager.getSniffers(context, classPathURIs, types);
context.postDeployClean(true);
String type = getTypeFromModuleType(md.getModuleType());
Sniffer mainSniffer = null;
for (Sniffer sniffer : sniffers) {
if (sniffer.getModuleType().equals(type)) {
mainSniffer = sniffer;
}
}
// if the sub module does not show characteristics of certain module
// type, we should still use the application.xml defined module type
// to add the appropriate sniffer
if (mainSniffer == null) {
mainSniffer = snifferManager.getSniffer(type);
sniffers.add(mainSniffer);
}
String [] incompatibleTypes = mainSniffer.getIncompatibleSnifferTypes();
List<String> allIncompatTypes = addAdditionalIncompatTypes(mainSniffer, incompatibleTypes);
List<Sniffer> sniffersToRemove = new ArrayList<Sniffer>();
for (Sniffer sniffer : sniffers) {
for (String incompatType : allIncompatTypes) {
if (sniffer.getModuleType().equals(incompatType)) {
deplLogger.log(Level.WARNING,
INCOMPATIBLE_TYPE,
new Object[] { type,
md.getArchiveUri(),
incompatType });
sniffersToRemove.add(sniffer);
}
}
}
sniffers.removeAll(sniffersToRemove);
// store the module sniffer information so we don't need to
// recalculate them later
Hashtable sniffersTable = archive.getParentArchive().getExtraData(Hashtable.class);
if (sniffersTable == null) {
sniffersTable = new Hashtable<String, Collection<Sniffer>>();
archive.getParentArchive().setExtraData(Hashtable.class, sniffersTable);
}
sniffersTable.put(md.getArchiveUri(), sniffers);
return sniffers;
}
private static String getTypeFromModuleType(ArchiveType moduleType) {
if (moduleType.equals(DOLUtils.warType())) {
return "web";
} else if (moduleType.equals(DOLUtils.ejbType())) {
return "ejb";
} else if (moduleType.equals(DOLUtils.carType())) {
return "appclient";
} else if (moduleType.equals(DOLUtils.rarType())) {
return "connector";
}
return null;
}
// this is to add additional incompatible sniffers at ear level where
// we have information to determine what is the main sniffer
private static List<String> addAdditionalIncompatTypes(Sniffer mainSniffer, String[] incompatTypes) {
List<String> allIncompatTypes = new ArrayList<String>();
for (String incompatType : incompatTypes) {
allIncompatTypes.add(incompatType);
}
if (mainSniffer.getModuleType().equals("appclient")) {
allIncompatTypes.add("ejb");
} else if (mainSniffer.getModuleType().equals("ejb")) {
allIncompatTypes.add("appclient");
}
return allIncompatTypes;
}
/**
* Gets all classes for handling the XML configuration
* @param habitat habitat to search for classes
* @param containerType they type of container they must work on
* @return
*/
public static List<ConfigurationDeploymentDescriptorFile> getConfigurationDeploymentDescriptorFiles(ServiceLocator habitat, String containerType) {
List<ConfigurationDeploymentDescriptorFile> confDDFiles = new ArrayList<ConfigurationDeploymentDescriptorFile>();
for (ServiceHandle<?> serviceHandle : habitat.getAllServiceHandles(ConfigurationDeploymentDescriptorFileFor.class)) {
ActiveDescriptor<?> descriptor = serviceHandle.getActiveDescriptor();
String indexedType = descriptor.getMetadata().get(ConfigurationDeploymentDescriptorFileFor.DESCRIPTOR_FOR).get(0);
if(indexedType.equals(containerType)) {
ConfigurationDeploymentDescriptorFile confDD = (ConfigurationDeploymentDescriptorFile) serviceHandle.getService();
confDDFiles.add(confDD);
}
}
return confDDFiles;
}
/**
* receives notification of the value for a particular tag
*
* @param element the xml element
* @param value it's associated value
* @param o
* @return
*/
public static boolean setElementValue(XMLElement element,
String value,
Object o) {
if (SCHEMA_LOCATION_TAG.equals(element.getCompleteName())) {
// we need to keep all the non j2ee/javaee schemaLocation tags
StringTokenizer st = new StringTokenizer(value);
StringBuilder sb = new StringBuilder();
while (st.hasMoreElements()) {
String namespace = (String) st.nextElement();
String schema;
if (st.hasMoreElements()) {
schema = (String) st.nextElement();
} else {
schema = namespace;
namespace = TagNames.JAVAEE_NAMESPACE;
}
if (namespace.equals(TagNames.J2EE_NAMESPACE))
continue;
if (namespace.equals(TagNames.JAVAEE_NAMESPACE))
continue;
if (namespace.equals(W3C_XML_SCHEMA))
continue;
sb.append(namespace);
sb.append(" ");
sb.append(schema);
}
String clientSchemaLocation = sb.toString();
if (clientSchemaLocation!=null && clientSchemaLocation.length()!=0) {
if (o instanceof RootDeploymentDescriptor) {
((RootDeploymentDescriptor) o).setSchemaLocation(clientSchemaLocation);
}
}
return true;
} else if (element.getQName().equals(TagNames.METADATA_COMPLETE)) {
if (o instanceof BundleDescriptor) {
((BundleDescriptor) o).setFullAttribute(value);
}
return true;
}
return false;
}
/**
* Returns a list of the proprietary schema namespaces
* @return
*/
public static List<String> getProprietarySchemaNamespaces() {
ArrayList<String> ns = new ArrayList<String>();
ns.add(DescriptorConstants.WLS_SCHEMA_NAMESPACE_BEA);
ns.add(DescriptorConstants.WLS_SCHEMA_NAMESPACE_ORACLE);
return ns;
}
/**
* Returns a list of the proprietary dtd system IDs
* @return
*/
public static List<String> getProprietaryDTDStart() {
ArrayList<String> ns = new ArrayList<String>();
ns.add(DescriptorConstants.WLS_DTD_SYSTEM_ID_BEA);
return ns;
}
/**
*
* @param env
* @return
*/
public static Application getApplicationFromEnv(JndiNameEnvironment env) {
Application app = null;
if (env instanceof EjbDescriptor) {
// EJB component
EjbDescriptor ejbEnv = (EjbDescriptor) env;
app = ejbEnv.getApplication();
} else if (env instanceof EjbBundleDescriptor) {
EjbBundleDescriptor ejbBundle = (EjbBundleDescriptor) env;
app = ejbBundle.getApplication();
} else if (env instanceof WebBundleDescriptor) {
WebBundleDescriptor webEnv = (WebBundleDescriptor) env;
app = webEnv.getApplication();
} else if (env instanceof ApplicationClientDescriptor) {
ApplicationClientDescriptor appEnv = (ApplicationClientDescriptor) env;
app = appEnv.getApplication();
} else if (env instanceof ManagedBeanDescriptor) {
ManagedBeanDescriptor mb = (ManagedBeanDescriptor) env;
app = mb.getBundle().getApplication();
} else if (env instanceof Application) {
app = ((Application) env);
} else {
throw new IllegalArgumentException("IllegalJndiNameEnvironment : env");
}
return app;
}
/**
*
* @param env
* @return
*/
public static String getApplicationName(JndiNameEnvironment env) {
String appName = "";
Application app = getApplicationFromEnv(env);
if (app != null) {
appName = app.getAppName();
} else {
throw new IllegalArgumentException("IllegalJndiNameEnvironment : env");
}
return appName;
}
public static String getModuleName(JndiNameEnvironment env) {
String moduleName = null;
if (env instanceof EjbDescriptor) {
// EJB component
EjbDescriptor ejbEnv = (EjbDescriptor) env;
EjbBundleDescriptor ejbBundle = ejbEnv.getEjbBundleDescriptor();
moduleName = ejbBundle.getModuleDescriptor().getModuleName();
} else if (env instanceof EjbBundleDescriptor) {
EjbBundleDescriptor ejbBundle = (EjbBundleDescriptor) env;
moduleName = ejbBundle.getModuleDescriptor().getModuleName();
} else if (env instanceof WebBundleDescriptor) {
WebBundleDescriptor webEnv = (WebBundleDescriptor) env;
moduleName = webEnv.getModuleName();
} else if (env instanceof ApplicationClientDescriptor) {
ApplicationClientDescriptor appEnv = (ApplicationClientDescriptor) env;
moduleName = appEnv.getModuleName();
} else if (env instanceof ManagedBeanDescriptor) {
ManagedBeanDescriptor mb = (ManagedBeanDescriptor) env;
moduleName = mb.getBundle().getModuleName();
} else {
throw new IllegalArgumentException("IllegalJndiNameEnvironment : env");
}
return moduleName;
}
/**
* Returns true if the environment or its parent is a {@link WebBundleDescriptor}, false otherwise
* @param env
* @return
*/
public static boolean getTreatComponentAsModule(JndiNameEnvironment env) {
boolean treatComponentAsModule = false;
if (env instanceof WebBundleDescriptor) {
treatComponentAsModule = true;
} else {
if (env instanceof EjbDescriptor) {
EjbDescriptor ejbDesc = (EjbDescriptor) env;
EjbBundleDescriptor ejbBundle = ejbDesc.getEjbBundleDescriptor();
if (ejbBundle.getModuleDescriptor().getDescriptor() instanceof WebBundleDescriptor) {
treatComponentAsModule = true;
}
}
}
return treatComponentAsModule;
}
/**
* Generate a unique id name for each J2EE component.
* @param env
* @return
*/
public static String getComponentEnvId(JndiNameEnvironment env) {
String id = null;
if (env instanceof EjbDescriptor) {
// EJB component
EjbDescriptor ejbEnv = (EjbDescriptor) env;
// Make jndi name flat so it won't result in the creation of
// a bunch of sub-contexts.
String flattedJndiName = ejbEnv.getJndiName().replace('/', '.');
EjbBundleDescriptor ejbBundle = ejbEnv.getEjbBundleDescriptor();
Descriptor d = ejbBundle.getModuleDescriptor().getDescriptor();
// if this EJB is in a war file, use the same component ID
// as the web bundle, because they share the same JNDI namespace
if (d instanceof WebBundleDescriptor) {
// copy of code below
WebBundleDescriptor webEnv = (WebBundleDescriptor) d;
id = webEnv.getApplication().getName() + ID_SEPARATOR
+ webEnv.getContextRoot();
if (deplLogger.isLoggable(Level.FINER)) {
deplLogger.log(Level.FINER, CONVERT_EJB_TO_WEB_ID, id);
}
} else {
id = ejbEnv.getApplication().getName() + ID_SEPARATOR
+ ejbBundle.getModuleDescriptor().getArchiveUri() + ID_SEPARATOR
+ ejbEnv.getName() + ID_SEPARATOR + flattedJndiName
+ ejbEnv.getUniqueId();
}
} else if (env instanceof WebBundleDescriptor) {
WebBundleDescriptor webEnv = (WebBundleDescriptor) env;
id = webEnv.getApplication().getName() + ID_SEPARATOR
+ webEnv.getContextRoot();
} else if (env instanceof ApplicationClientDescriptor) {
ApplicationClientDescriptor appEnv = (ApplicationClientDescriptor) env;
id = "client" + ID_SEPARATOR + appEnv.getName() + ID_SEPARATOR
+ appEnv.getMainClassName();
} else if (env instanceof ManagedBeanDescriptor) {
id = ((ManagedBeanDescriptor) env).getGlobalJndiName();
} else if (env instanceof EjbBundleDescriptor) {
EjbBundleDescriptor ejbBundle = (EjbBundleDescriptor) env;
id = "__ejbBundle__" + ID_SEPARATOR
+ ejbBundle.getApplication().getName() + ID_SEPARATOR
+ ejbBundle.getModuleName();
}